0
    I am getting these checks
    :( HTTP/1.0 returns error code 505
       \ expected output, not an exit code of 0
    :( Method of 'abcGET' returns error code 405
       \ expected output, not an exit code of 0
    :( Method of 'GETabc' returns error code 405
       \ expected output, not an exit code of 0
    :( request-target without starting '/' returns error code 501
       \ expected output, not an exit code of 0
    :( request-target of abc/hello.php returns error code 501
       \ expected output, not an exit code of 0
    :( Requesting cat.exe returns error code 501
       \ expected output, not an exit code of 0
    :( Requesting non-existant file returns error code 404
       \ expected output, not an exit code of 0
    :( Requesting request-target with " returns error code 400
       \ expected output, not an exit code of 0
    :( Two spaces after GET returns error code
       \ expected output, not an exit code of 0
    :( A space within the request target returns error code
       \ expected output, not an exit code of 0
    :( Two spaces before HTTP/1.1 returns error code
       \ expected output, not an exit code of 0

My code for parse function is:

bool parse(const char* line, char* abs_path, char* query)
{
    // TODO

   /* Request-line consists of method SP request-target SP HTTP-version CRLF*/
    //The request target consists of the absolute path and the query in this form : absolute-path [ "?" query ]
    //We will use strtok to store tokens in different strings
    //Referenced from stackoverflow and tutorialspoint.com
    //Copy the line into  a new string
    char *l = NULL;
    strcpy(l,line);

    //Token to store method
    char* method = strtok(l, " ");

    //Token to store request target
    char *request = strtok(NULL," ");

    //Store the HTTP version in a token
    char* http = strtok(NULL," ");

   //Store the absolute path in a token
   char* abs_path1 = strtok(request,"?");

   //Store the query after ? in a token
   char* query1 = strtok(NULL,"?");
   if(query1 == NULL)
     query1 = "";

   strcpy(query,query1);
   strcpy(abs_path,abs_path1);

    //Check if method is get
    if(strcmp(method,"GET") != 0)
        {
            error(405);
            return false;
        }

    //Check if request target consists a "
    if(strchr(query, '"'))
        {
            error(400);
            return false;
        }    

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

    if(strstr(request,"/") != 0)
        {
            error(404);
            return false;
        }

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

This message from check50 \ expected output, not an exit code of 0 is a seg fault. parse seg faults here strcpy(l,line);. For all intents and purposes, NULL is immutable, it is a defined constant. You have not allocated memory to l. Instead of char *l = NULL; you need something like char *l = malloc(????).

There may be other problems in this function, but I, like check50, stopped reading at the seg fault.

3
  • Can I use strdup in the place of strcpy? – Anirudh Thatipelli Jul 10 '16 at 3:34
  • Search in SE or your favorite search engine for c strdup v strcpy. Then you decide. – DinoCoderSaurus Jul 10 '16 at 11:43
  • Thanks, after using strdup, I was able to solve my problem. – Anirudh Thatipelli Jul 10 '16 at 13:50

You must log in to answer this question.

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