Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Here is my code for the parse function for PSET6. I am not sure why I get the 501 fail. (I'm not told to do the one for 404).

~/workspace/pset6/ $ check50 2015.fall.pset6.server1 server.c
:) server.c exists
:) server compiles
:) HTTP/1.0 returns error code 505
:) Method of 'abcGET' returns error code 405
:) Method of 'GETabc' returns error code 405
:) request-target without starting '/' returns error code 501
:) request-target of abc/hello.php returns error code 501
:( Requesting cat.exe returns error code 501
\ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:( Requesting non-existant file returns error code 404
\ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:) Requesting request-target with " returns error code 400
:) Two spaces after GET returns error code
:) A space within the request target returns error code
:) Two spaces before HTTP/1.1 returns error code

What I have tried:

bool parse(const char* line, char* abs_path, char* query)
    char* buffer = malloc(sizeof(line));
    strcpy( buffer, line);
    char* firstsp = strtok( buffer, " ");
    char* scndsp = strtok( NULL, " "); 
    char* thirdsp = strtok( NULL, " "); 
    char* ver = strtok( thirdsp, "\\");
    char method[ strlen(firstsp) + 1 ];
    char request[ strlen(scndsp) + 1 ];
    char http[ strlen(ver) + 1 ];
    strcpy( method, firstsp);
    strcpy( request, scndsp);
    strcpy( http, ver);
     if ( strcmp(method, "GET") != 0 )
         return false;

     if ( request[0] != '/')
         return false;
    if ( strchr( request, '\"') != NULL )
        return false;
    if (strcmp( http, "HTTP/1.1") != 0)
        return false;
    abs_path = request;
    int b;
    for ( b = 0; b < strlen(scndsp); b++ )
        if ( scndsp[b] == '?' )    
    if ( strchr( scndsp, '=') != NULL)
        while (scndsp[b] != ' ' )
            query[b] = scndsp[b];
        query[0] = '\0';
    return true;
Updated 11-Jan-17 22:01pm
Afzaal Ahmad Zeeshan 11-Jan-17 16:50pm    
Your code has that part in it, it says that it requires the request to start with a "/". Otherwise, it will throw that error.

But it is unclear, 501 errors typically mean that there is something wrong (in the context of HTTP communication). What exactly is this all about?
Member 12919791 11-Jan-17 20:15pm    
Exact instructions link:

1 solution

There are two a big problems with your code.

char* buffer = malloc(sizeof(line));
strcpy( buffer, line);
line is of type char* and sizeof(line) will be therefore the size of a pointer (4 or 8 bytes). What you probably want is
char* buffer = malloc(strlen(line) + 1);

The strtok() calls return NULL on subsequent calls when the previous token was not found (parsing stopped at the terminating NULL character).

So your scndsp and thirdsp pointer might be NULL with malformed request strings. But you are using these pointers as arguments to strtok() (for ver), strlen(), and strcpy(). All these functions did not check the arguments for being not NULL but perform the operation using the address zero.

If that happens, you are coyping random characters to the request and http strings (the bytes contained at address zero until a NULL byte).

So you have to check for NULL pointers first and return corresponding error codes.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900