0

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).

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 )
     {
         error(405);
         return false;
     }

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

    if ( strchr( request, '\"') != NULL )
    {
        error(400);
        return false;
    }



    if (strcmp( http, "HTTP/1.1") != 0)
    {
        error(505);
        return false;
    }

    abs_path = request;

    int b;
    for ( b = 0; b < strlen(scndsp); b++ )
    {
        if ( scndsp[b] == '?' )    
        {
            break;
        }
    }

    if ( strchr( scndsp, '=') != NULL)
    {
        while (scndsp[b] != ' ' )
        {
            query[b] = scndsp[b];
            b++;
        }
    }
    else
    {
        query[0] = '\0';
    }
    return true;

}

Check50:

:) 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

Thanks a lot,

1

three problems:

char* buffer = malloc(sizeof(line));

It is quite probable that we are not reserving enough memory, better to do:

char* buffer = malloc(strlen(line) + 1);

the use of strtok is incorrect, the pointers returned by strtok are as follows:

 firstsp  "GET"
 scndsp   "/cat.html"
 thirdsp  "HTTP/1.1\r\n"
 ver      "HTTP/1.1\r\n"

We only need three tokens, keep in mind that there are only two blanks, the third token should be "\r\n". Finally a more serious error, careful with the names of variables, you are defining a variable with the same name as a function:

char request[ strlen(scndsp) + 1 ];

bool request(char** message, size_t* length);

The last one is a function already written in parse by the staff, so that when you write request you are surely calling that function, with undesirable results in the program.

You must log in to answer this question.

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