Giter VIP home page Giter VIP logo

Comments (1)

nmdguerreiro avatar nmdguerreiro commented on July 24, 2024

We have a fork of the code that supports query params but that hasn't been integrated into this repo:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <haywire.h>
#include "route_compare_method.h"

typedef struct hw_route_token_st {
    hw_string string;
    int start;
} hw_route_token;

inline void update_route_token(hw_route_token* result, hw_string* value, int length, int start) {
    result->string.value = value;
    result->string.length = length;
    result->start = start;
}

void hw_route_next_token(hw_string* url, int start, hw_route_token* result) {
    // when url is /
    if (url->value[start] == '/' && url->length == 1){
        update_route_token(result, url->value + start, 1, start);
    }
    else if (url->value[start] == '?') {
        update_route_token(result, NULL, 0, -1);
    }
    else {
        while (start < url->length && url->value[start] == '/') {
            start++;
        }

        int end = start;

        while (end < url->length && url->value[end] != '/' && url->value[end] != '?') {
            end++;
        }

        if (end != start) {
            update_route_token(result, url->value + start, end - start - 1, start);
        }
        else {
            update_route_token(result, NULL, 0, -1);
        }
    }
}

int hw_route_compare_method(hw_string* url, char* route)
{
    int equal = 0;
    int match = 0;

    // TODO route should probably be a hw_string* too
    hw_string hw_route;
    hw_route.value = route;
    hw_route.length = strlen(route);

    hw_route_token route_token;
    hw_route_token request_token;

    hw_route_next_token(url, 0, &request_token);
    hw_route_next_token(&hw_route, 0, &route_token);

    while (route_token.string.value != NULL && request_token.string.value != NULL)
    {
        if (route_token.string.value[0] == '*') {
            // wildcard support: any route fragment marked with '*' matches the corresponding url fragment
            equal = 1;
        }
        else
        {
            match = hw_strcmp(&route_token.string, &request_token.string);
            if (!match)
            {
                equal = 1;
            }
            else
            {
                equal = 0;
                break;
            }
        }

        hw_route_next_token(url, request_token.start + request_token.string.length + 1, &request_token);
        hw_route_next_token(&hw_route, route_token.start + route_token.string.length + 1, &route_token);
    }

    if (!equal)
    {
        match = hw_strcmp(url, &hw_route);
        if (!match)
        {
            equal = 1;
        }
    }

    if ((route_token.string.value == NULL && request_token.string.value != NULL) || (route_token.string.value != NULL && request_token.string.value == NULL))
    {
        equal = 0;
    }

    return equal;
}

from haywire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.