Skip to main content
2 of 2
added 1358 characters in body

Sorry I was busy the past few days and did not provide a satisfactory answer to my own question after I solved it following Kareem's comment.

Here's my code (it's probably not the best way to do it). The problem comes in the "no query" portion of my code.

bool parse(const char* line, char* abs_path, char* query)
{
    // find spaces
    char* firstspace = strchr(line, ' ');
    char* lastspace = strrchr(line, ' ');

    // get lengths of method and request
    size_t length_line = strlen(line);
    size_t length_method = length_line - strlen(firstspace);
    size_t length_request = length_line - strlen(lastspace) - length_method - 1;
    
    // extract method and request target
    char method[length_method + 1];
    char request[length_request + 1];
    strncpy(method, line, length_method);
    method[length_method] = '\0';
    strncpy(request, firstspace + 1, length_request);
    request[length_request] = '\0';
    
    // init variables
    size_t path_length;
    size_t query_length;
    
    // query exists     

    // no query
    else if (!strchr(request, '?'))
    {
        query = "";
        abs_path = request;
    }
    
    // more code

}

I should have used strcpy(abs_path, request) instead of abs_path = request.