0

Please I have been trying different ways of soling this problem. Non seems to be working. I tried check50 and got these errors:

:) 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
   \ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( request-target of abc/hello.php returns error code 501
   \ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( 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

Please what am I doing wrong? Thank you.

bool parse(const char* line, char* abs_path, char* query)
{
    // TODO
    int line_len = strlen(line);
    char absol_path[LimitRequestLine + 1] = {0};
    char query_path[LimitRequestLine + 1] = {0};
    int x = 0;
    for(int m = 0; m < line_len; m++)
    {
        if(line[m] == ' ')
        x++;
    }
    if (x != 2 && strstr(line, "\r\n") == NULL)
    {
        error(400);
        return false;
    }
    char getf[4]= {0};
    char versionf[9] = {0};
    int i = 0, j = 0, k = 0, l = 0, n = 0;

    while (line[i] != '\0')
    {
       getf[j++] = line[i];
       i++;
       if (line[i] == ' ')
       i++;
       if (line[i] == '/')
       {
           while(line[i] != ' ')
           {
                absol_path[k++] = line[i];
                i++;
                 if(line[i] == '?')
                {
                    while(line[i] != ' ')
                    {
                        if(line[i] == '?')
                        {
                          i++;
                              if(line[i] == ' ')
                               query_path[0] = '\0';
                        }
                        else
                        {
                        query_path[n++] = line[i];
                        i++; 
                        }
                    }
                }
           }

            if(line[i] == ' ')
            {
                while (line[i] != '\0')
                {
                    if(line[i] == ' ')
                    i++;
                    else
                    {
                    versionf[l++] = line[i];
                    i++;
                    }
                }    
            }

       }
   }

   if (strcmp(getf, "GET") != 0)
    {
      error(405);
      return false;
    } 
    if (absol_path[0] != '/')
    {
      error(501);
      return false;
    } 
    if (strchr(absol_path, '"') != NULL)
    {
      error(400);
      return false;
    } 
    if (strchr(query_path, '"') != NULL)
    {
      error(400);
      return false;
    } 
    if (strcmp(versionf, "HTTP/1.1") != 0)
    {
      error(505);
      return false;
    }

    strcpy(abs_path, absol_path);
    strcpy(query, query_path);
    return true;
}
1

A quick way to format your code is: after you paste it, select it and click the {} in the format bar.

This sequence:

if (line[i] == ' ')
   i++;
   if (line[i] == '/')

assumes the first space is followed by a '/'. What happens if it is not? Apparently getf will not be built correctly, thus the first two errors that test for that condition. (It will/should eventually find a '/' somewhere in the middle of method).

Review the definition of a request line in the spec:

method SP request-target SP HTTP-version CRLF
wherein SP represents a single space ( ) and CRLF represents \r\n.

That is going to make this test if (strcmp(versionf, "HTTP/1.1") != 0) always evaluate to true. Why? The "second-space loop" where versionf is built processes line to the null-terminator, which includes the CRLF.

You will likely find debug50 and/or gdb very helpful for troubleshooting and debugging server.

2
  • Hi, thanks for you help. I really helped a lot. I improved the code by removing { if (line[i] == '/')} and enclosed the rest of the while loop in the first 'if' statement. However, I now have two errors left. Any idea what they are pointing to? :( 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
    – excel
    Nov 21 '16 at 19:01
  • 1
    Hi @DinoCoderSaurus, never mind, I had to read through your comment several times before I fully get what u were pointing to. So thanks, SOLVED IT! Thanks to you!
    – excel
    Nov 21 '16 at 19:47

You must log in to answer this question.

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