0

I have been working on this pset for a long time. When I run check50 now, there are three remaining errors: :( Requesting hello.php? returns 200, text/html, and correct output \ expected output, not an exit code of 0 :( Requesting /test/ outputs /test/index.html :( Requesting directory containing index.php outputs index.php \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."

All errors relate to requests for php files but I am not sure what function is giving rise to the issue. I think it could be my parse or my indexes functions (see below). Any help on where the errors may lie would be greatly appreciated. My indexes function is as follows:

char* indexes(const char* path)
{
char* html = "/index.html";
char* php = "/index.php";
char* html_file = malloc(strlen(html) + strlen(path) + 1);
strcpy(html_file, path);
strcat(html_file, html);
char* php_file = malloc(strlen(php) + strlen(path) + 1);
strcpy(php_file, path);
strcat(php_file, php);

if(access(html_file, F_OK) == 1)
{
    return html_file;
}
   else if(access(php_file, F_OK) == 1)
    {
        return php_file;
   }
    return NULL;

And my parse function is:

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

    //check that method is GET. 
//First create a usable copy of line
char req_line[strlen(line)];
//make copy of line and store in req_line
strcpy(req_line, line);

//parse the line to the first space to get method 
char* method = strtok(req_line, " "); 

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

    //we extract the request target which should run to the next space
   char* req_tgt = strtok(NULL, " ");

    // check that request target begins with /
    if (req_tgt[0] != '/')
{
    error(501); 
    return false;
}

//check whether request target contains a " and if it does return an error and false
if (strchr(req_tgt, '"') != NULL)
{
    error(400);
    return false;
}

//make sure that the correct version of HTTP is being used 
char* http_version = strtok(NULL, " ");

if(strcmp(http_version, "HTTP/1.1\r\n") != 0)
{
    error(505);
    return false;
}

//check whether there is a ? in req_tgt
const char* quest_mark = strchr (req_tgt, '?');
//const char* second_space = strrchr(req_tgt, ' ');
//if there is none then store abs_path and query
if (quest_mark == NULL) 
{
char* temp = strtok (req_tgt, " ");
strcpy(abs_path, temp);
strcat(abs_path, "\0");
strcpy (query, "");
strcat(query, "\0");
printf ("absolut path when no ? or ? but no query is.......%s\n", abs_path);
printf ("query when no ? or ? but no query is .......%s\n", query);
}

else if (strcmp(quest_mark + 1, " ") == 0)
{
 char* temp = strtok (req_tgt, "?");
strcpy(abs_path, temp);
strcat(abs_path, "\0");
strcpy (query, "");
strcat(query, "\0");
printf ("absolut path when no ? or ? but no query is.......%s\n", abs_path);
printf ("query when no ? or ? but no query is .......%s\n", query);   
}

else 
{
    char* temp = strtok (req_tgt, "?");
    strcpy(abs_path, temp);
    strcat(abs_path, "\0");
    char* query_temp = strtok (NULL, " ");
    strcpy(query, query_temp);
    strcat(query, "\0");
    printf ("absolut path when  ?  is.......%s\n", abs_path);
    printf ("query when ? ois .......%s\n", query);
}
return true;

}

1 Answer 1

0

I didn't follow the code all the way through, because this declaration in parse char req_line[strlen(line)]; is probably the root of your problems. Let's say line is our old friend "GET /cat.jpg HTTP/1.1\r\n". Then strlen(line) is 23. Which means it needs 24 bytes, the extra one allocated for the null terminator. req_line has reserved 23 bytes of memory.There is no byte allocated for the terminator. And, what happens if something else has been allocated at that address, like method maybe. Now when you build method, it stomps on the null terminator that strcpy copied in. Heed this warning from man strcpy (emphasis added):

the destination string dest must be large enough to receive the copy. Beware of buffer overruns! (See BUGS.)

10
  • Thanks very much for your help. I made a change to the length of req_line , its now [strlen(line) + 1]. However the same three errors are still showing up with check50. Any other ideas of things to look at would be greatly appreciated.
    – Breninio
    Commented Apr 13, 2016 at 22:26
  • Good time to practice with gdb. Request /html.php? since you know that fails check. See where that takes you. Some observations: Your printfs here if (quest_mark == NULL) and here else if (strcmp(quest_mark + 1, " ") == 0) are the same. How do you know which branch you are seeing? Is else if (strcmp(quest_mark + 1, " ") == 0) ever true? Commented Apr 14, 2016 at 12:16
  • PS recommend you use a curl request to test your code for /hello.php?. I haven't discovered why yet, but when using a browser ff (V45.0.2), Opera (v 36.0) and Chrome (v 50.0....) the incoming message does not include the trailing ? Commented Apr 14, 2016 at 13:48
  • Thanks for your comments. I have been using gdb but couldn't think how to test for /html.php?. Actually now as I am writing this I think I have copped it, just press "say hello" button on html page without entering anything in the box. I will try that. Thanks again. I will also try curl as you suggest. Is if (strcmp(quest_mark + 1, " ") == 0) not true n the case we are looking at i.e. a space rather than a query after the ?
    – Breninio
    Commented Apr 14, 2016 at 14:08
  • That didnt work. When I click "say hello" with no name entered, this requests /html.php?name= . How do i request /html.php?
    – Breninio
    Commented Apr 14, 2016 at 15:58

You must log in to answer this question.

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