0

Me again. I've basically sorted out most of the issues now. I can load cat.html normally in the browser and indexes definitely works. From testing, I believe that my parse function is still fine.

Check50:

:) server.c exists
:) server compiles
:( Requesting cat.jpg returns 200, image/jpeg, and correct image
   \ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting cat.html returns 200, text/html, and correct file
   \ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting cat2.HTML returns 200, text/html, and correct file
   \ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting cat3.HtMl returns 200, text/html, and correct file
   \ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting cat.gif returns 200, image/gif, and correct file
   \ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting favicon.ico returns 200, image/x-icon, and correct file
   \ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting test.css returns 200, text/css, and correct file
   \ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting test.js returns 200, text/javascript, and correct file
   \ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting hello.php returns 200, text/html, and correct output
    \ expected output, not an exit code of 0
:( Requesting hello.php? returns 200, text/html, and correct output
    \ expected output, not an exit code of 0
:( Requesting hello.php?name=Alice returns 200, text/html, and correct output
   \ expected output, not an exit code of 0
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
   \ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting directory containing index.php outputs index.php
   \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."
:( Requesting two files in a row (cat.html then cat.jpg) succeeds
   \ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."

As can be seen, apparently my program doesn't exit cleanly and instead outputs this everytime.

LOOKUP CODE REMOVED FOR ACADEMIC HONESTY PURPOSES:

SOLUTION:

Thanks to DinoCoderSaurus and others who offered a lot of help, I realised where I went wrong in my lookup function. I got confused between the return value of strcasecmp and strcasestr and so I wasn't getting the proper output. I altered the condition so that it checks whether it's not equal to NULL and it works fine now.

MINOR ISSUE:

This is relating to my parse function. For some reason, this check50 test fails when one section of code is placed in one location, but passes when it is placed in another location.

bool parse(const char* line, char* abs_path, char* query)
{
//no errors in parse.
int length = strlen(line);

int space_count = 0;

for (int i = 0; i < length; i++)
{
    if (line[i] == (char) 32)
    {
        space_count++;
    }
}

if (space_count != 2)
{
    error(400);
    return false;
}

char* method = malloc(5 * sizeof(char));
strncpy(method, line, 4);

if (strstr(method, "GET ") == NULL)
{
    error(405);
    return false;
}

free(method);

if (line[4] != '/')
{
    error(501);
    return false;
}

char* question_mark = strchr(line, '?');
char* slash = strchr(line, '/');
char* HTTP = strstr(line, "HTTP/1.1\r\n");

//if another version of HTTP is used, the server can't handle it. 
//HTTP check works here.
if (HTTP == NULL)
{
    error(505);
    return false;
}

char* abs_path_copy = malloc(5000 * sizeof(char));
char* query_copy = malloc(5000 * sizeof(char));

if (question_mark != NULL)
{
    strncpy(abs_path_copy, slash, question_mark - slash);
    for (int i = question_mark - slash; i < strlen(abs_path + 1); i++)
    {
        abs_path_copy[i] = '\0';
    }

    if (question_mark + 1 == NULL)
    {
        strcpy(query_copy, "");
    }
    else
    {
        strncpy(query_copy, question_mark + 1, (HTTP - question_mark - 2));
        query_copy[HTTP - 2 - question_mark] = '\0';

    }
}
else
{
    strncpy(abs_path_copy, slash, HTTP - slash - 1);
    for (int i = HTTP - slash - 1; i < strlen(abs_path_copy + 1); i++)
    {
        abs_path_copy[i] = '\0';
    }
}

strcpy(abs_path, abs_path_copy);
strcpy(query, query_copy);

char* quot_mark = strchr(line, '\"');

if (quot_mark != NULL)
{
    error(400);
    return false;
}

//HTTP check doesn't work here.

return true;
}

The two positions are marked with HTTP check comments. Can anyone explain why my program behaves like this?

2

The "weird" results you report

For some reason, if I alter the order in which the file extensions are searched, cat.html suddenly doesn't load.

is the "unpredictable results" that happen when there are memory allocation problems and buffer overruns.

There shouldn't be an need for allocating in lookup(). You should simply be returning the string literal, as described in the spec. Why is returning an allocated pointer a problem? The call from main is const char* type = lookup(path);. When you allocate the local variable mime in lookup it gets a (new!) memory address. Then you (think) you put stuff in mime and return it to main. But since mime is local, that memory is freed, that stuff is gone, when you return to the caller.

Why did I say "you (think) you put stuff in mime"? Because this if (strcasestr(path, ".php") == 0) (and all the rest like it) will always be false. The only time that statement will be true is, well, I don't know a scenario where it would be true.

The function signature of strcasestr is:

char *strstr(const char *haystack, const char *needle);

It returns a char pointer (string), not an integer.

It might be best to stick to the functions mentioned in the instructions.

Odds are you’ll find functions like strcasecmp, strcpy, and/or strrchr of help!

Keep flexing those gdb muscles.

| improve this answer | |
  • This is great advice. More generally, don't overthink the lookup() function! It can be really simple. My implementation of lookup() is only 20 lines long, including two blank lines, three comments and six lines that are just curly braces. That's just a function declaration plus eight statements! In fact, looking at it now I can see one line I don't need at all, even for legibility. So this one can be simplified a lot. (Hint: try storing file extensions and mime types in a couple of matching arrays!) – hotwebmatter Aug 19 '16 at 19:58
  • Thank you @MattObert for your help with this. – Naveen Sivasankar Aug 20 '16 at 3:02
  • @DinoCoderSaurus thank you for all your help. – Naveen Sivasankar Aug 20 '16 at 3:02

You must log in to answer this question.

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