0

Most of the conditions on check 50 server1 return the correct error code but as soon as I break up line to check for quotation mark in the request line for example it gives the 505 error code. I can get it to find the quotation marks if I move that condition to somewhere else but then HTTP version error code doesn't work. I also tried null terminating the RT variable by iterating over it like I did with the abspath variable but that just caused the program to return exit code 0 rather than any error code at all. It also says it doesn't get a 404 error but I don't know where I'm supposed to put that in. It doesn't get a 501 error when asking for an EXE even though I provided for that in an else statement in look up I don't know where else to put it.

bool parse(const char* line, char* abs_path, char* query)
{
    //make sure method request is get
  const char* get = "GET /";
  if (strncmp(get, line, 4) != 0)
   {
    error(405);
    return false;
   }
   //make sure request Target begins with slash
  if (strncmp(get, line, 5) != 0)
   {
    error(501);
    return false;
   }
    //separate requests Target
   char* rt  = strchr (line, ' ');
//get absolute path path
   char* abspath = strchr (rt, '/');
   for (int i= 0; abspath[i - 1] != '\0'; i++)
   {
       //terminate string at first space
   if (abspath[i] == ' ' || abspath[i] == '?' )
  abspath[i] = '\0';
   }
  abs_path = abspath;

   // get query
   if (strchr (rt, '?') == NULL)
   query[0] = '\0';

  else
  {
   char* que = strchr (rt, '?');
   for (int i= 0; que[i - 1] != '\0'; i++)
   {
       //get rid of the ?
   que[i] = que[i + 1];
   //terminate string at first space
   if (que[i] == ' ')
   que[i] = '\0';
   }
 query = que;
  }
  //check for HTTP 1.1
   char* rst = strchr (line, ' ');
   //char *http = ' HTTP/1.1';

    if (strstr(rst, "HTTP/1.1") == NULL)
   {
           error(505);
    return false;
   }
   //check for the other thing
      char* http = "HTTP/1.1\r\n";
         if (strstr(rst, http) == NULL)
   {
           error(400);
    return false;
   }
   //check for quotation marks in request
     if (strchr( rt, '"') != NULL)
   {
    error(400);
    return false;
   }
   return true;
}
4
  • Your error handling with http version checking is not correct. right after "HTTP/1.1" comes the CRLF character or "\r\n". So you should check for the http version error with not just the string "HTTP/1.1", but "HTTP/1.1\r\n" and return false and error 505. You then check for the http version with CRLF but you respond with error 400. Just keep the one with the "HTTP/1.1\r\n" and respond with error 505 and return false.
    – yousafe007
    Jun 7 '16 at 23:05
  • Okay I will do that. . doesn't really make a difference though.
    – Evan
    Jun 8 '16 at 1:28
  • it does. That's what I had been doing wrong the whole time. Although it may or may not fix every problem, but you'll get closer to solving it.
    – yousafe007
    Jun 8 '16 at 9:04
  • Well I'm not sure how since I still have the same output from check 50.
    – Evan
    Jun 8 '16 at 17:35
0

Have you tested your server with a browser or a curl request? It looks like it will seg fault at this line abspath[i] = '\0'; if any valid GET request is sent. abspath is a pointer into rt which is a pointer into line which is a const char* which, as such, is immutable. It cannot be changed.

But wait, there's more. If server reaches this line abs_path = abspath; it will return the wrong result to main. Why? Because abs_path is a pointer, and you have just changed where it points to, not what it points to. query too.

There may be other problems as well. gdb is a great tool for troubleshooting and debugging problems with server.

As an aside, there should be no special handling for .exe in lookup function. It should just fall through and return NULL.

You must log in to answer this question.

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