The two sad faces are the following: :( Requesting hello.php?name=Alice returns 200, text/html, and correct output and :( Requesting /test/ outputs /test/index.html

Here's the code:

bool load(FILE* file, BYTE** content, size_t* length)
    int bytesRead = 0;

    //create a buffer to hold an individual byte
    char* buffer = malloc(sizeof(char));

    //ensure that there was memory available
    if(buffer == NULL)
    return false;

    //read through file byte by byte, increasing the allocated memory 
    //until the entire file is read
    for(int newByte = fgetc(file); newByte != EOF; newByte = fgetc(file))
        buffer[bytesRead] = (char)newByte;
        buffer = realloc(buffer, (1+bytesRead) * sizeof(char));

    *length = bytesRead;
    *content = buffer;
    return true;

Here's the part of parse that handles query:

//handle query
cursor = strstr(requestTarget, "?");
if(cursor != NULL)
    if(*(cursor + 1) != '\0')
        query = cursor + 1;
    else query[0] = '\0';
    strncpy(abs_path, requestTarget, cursor - requestTarget);
    abs_path[cursor - requestTarget] = '\0';
    query[0] = '\0';
    strcpy(abs_path, requestTarget);
return true;
  • Those two failures would point to parse and indexes respectively. Aug 28, 2016 at 18:29
  • Added query code Sep 2, 2016 at 18:15

1 Answer 1


Based on the fact that you only posted code that relates to query, you solved your indexes problem. Good on ya.

I'll try to be the "human gdb" for the query code. But really, command line gdb is much more reliable :) Find tips here.

I have to assume cursor is != NULL here if(cursor != NULL), because the test that failed was hello.php?name=Alice. It's obvious that if there is a '?' and no query, your program works correctly, since the hello.php? test passes. So I further assume that if(*(cursor + 1) != '\0') is true. Then what happens? query = cursor + 1; Scope. Pointers. cursor is a local variable. It will be "destroyed" when you return from the function. Therefore, back in main, query will be whatever was in query's memory when parse was called. Just curious, did you try requesting "/hello.php?name=Alice" in a browser or curl? What result did you get?

You need to "build" query with some string function, much the way you "build" abs_path.

Full disclosure: I didn't look at the load function, since it is not indicated by these errors. And frankly, if the first cat.jpg passes and the last, cat.html followed by cat.jpg passes, that's a good sign.

  • The indexes problem was a typo that I kept missing. The answer from DinoCoderSaurus led me to creating another pointer, one past cursor, and using strcpy(query, cursor2) in the query loop. Thanks guys! All smilies now :) Sep 6, 2016 at 18:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .