0

I've been trying to debug this code for days and can't find the problem. As far as I can tell, GDB tells me my abs_path is correct and NULL terminated properly, but still I get:

:( Requesting cat.exe returns error code 501
    \ expected output, but not "HTTP/1.1 500 Internal Server Error\r\nC..."

I'm really not sure where to check next - oddly enough when I check myself using curl I get error 404 - which should make sense because cat.exe is not actually in ./public.

Any help would be greatly appreciated.

Thanks!

Here is my parse() function:

bool parse(const char* line, char* abs_path, char* query)
{
    // Request line has format of: method SP request-target SP HTTP-version CRLF

    // first check ther are 2 spaces
    if (charno( line, ' ') != 2)
    {
        error(400); 
        return false; 
    }


    // find '\0' char - then go back two spaces and check
    char* endofstring = strchr( line, 0); 
    if ( *( endofstring -2 ) != '\r' || *( endofstring -1 ) != '\n'  )
    {
        error(400);
        return false; 
    }

    // read line, look for first space, look for second space
    char* first_space = strchr(line, 32); // 
    char* second_space = strchr( (first_space +1), 32); // 

    int dif = second_space - first_space; 


    // initialize buffer
    char requestpath_buffer[LimitRequestLine + 1]; // Zeroed out


    // copy request path to buffer 
    strncpy(requestpath_buffer, (first_space +1), dif); 
    // manually add null terminator
    requestpath_buffer[ (dif - 1)  ] = '\0';  // double check this




    // requestpath_buffer = (const char) requestpath_buffer; 
    // cast as const char for str functions; 
    // const char requestpath_buffer = (const char) requestpath_buffer1;

    // Check formating of requestpath_buffer

    // no spaces allowed in request_path - this part is redundant
    if ( strchr(requestpath_buffer, ' ' ) != NULL) 
    {
        error(400);
        return false; 
    }

    // ensure abs path starts with '/'
    else if (requestpath_buffer[0] != '/')
    {
        error(501);
        return false; 
    }
     // no " allowed in request line"    
    else if (strchr(requestpath_buffer, 34) != NULL)
    {
        error(400);
        return false; 
    }

    // Query parsing
    // check for '?' = 63
    char* qmark_ptr = strchr(requestpath_buffer, 63); 



    // Check formating of querry
    if (charno(&requestpath_buffer[0], '?' ) >= 2 ) // ensure no more than 1 '?'
    {
        error(404);
        return false; 
    }
    else if (qmark_ptr == NULL) // ensures only 1
    {
        query = "";
    }    
    else if (qmark_ptr != NULL) 
    {
        query = (qmark_ptr ); // place at'?' until '\0' 
        // mark null terminator here
    }
    else
    {
        printf( "Unexpected error in request path \n");
        return false; 
    }


    // Check to make sure the method is GET at begining
    if (line != strstr(line, "GET ") ) 
    {
        error(405);
        return false; 
    }

    // Verify Http version 1.1 
    //
    char* last_sp_ptr = strrchr(line, ' ');  

    // makes sure that last part is the version
    if ( (last_sp_ptr + 1) != strstr(line, "HTTP/1.1") )
    {
        error(505);
        return false; 
    }

    // store abs_path

    if (qmark_ptr != NULL)
    {
        int dif2 =  qmark_ptr - requestpath_buffer;  // error is here

        strncpy(abs_path, requestpath_buffer, dif2 );
        *(abs_path + strlen(requestpath_buffer) -2) = '\0';



    }
    else
    {   
        strncpy(abs_path, requestpath_buffer, (strlen(requestpath_buffer) + 1 )); 

        // *(abs_path + strlen(requestpath_buffer)  ) = '\0';
        //strcat(abs_path, "");

    }

    printf("ABSOLUTE PATH IS \"%s\" \n", abs_path ); 
    printf("QUERY IS \"%s\" \n", query ); 



   return true;   

}
  • charno() is just a small function I wrote to return the number of a specific characters in a string.
2

As you say, your curl request (properly) gives a 404 because you do not have a cat.exe file. There probably is a cat.exe file on the check50 server. You could copy cat.html to cat.exe. Then issue the curl request and make sure you get a 501. If not, the problem is likely in lookup function. If you do, well, keep at it with gdb.

2
  • That was it! Thanks so much.
    – adge
    Aug 2 '16 at 23:58
  • To anyone else having this problem - I just added a blank file into ./public and named it cat.exe and then could find the problem with GDB in lookup, which was never triggered by using curl (in my case I was wrongly expecting a return value of 1 instead of 0 from strcasecmp for an exact string match)
    – adge
    Aug 3 '16 at 0:00

You must log in to answer this question.

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