0

The code does not passing the tests with the Requesting cat.exe returns error code 501 Requesting non-existant file returns error code 404

Cannot find why , its even never goes to the lookup function

const char* lookup(const char* path)
{
     //get the point position 
    char *point = strrchr(path, '.');

    //copy the string from point to the point


    //checking extentions 
    if (strcasecmp(point , ".gif") == 0 ) {
        return "image/gif";
    } else if (strcasecmp(point , ".css") == 0) {
        return "text/css";
    } else if (strcasecmp(point , ".html") == 0) {
        return "text/html";
    } else if (strcasecmp(point , ".ico") == 0) {
        return "image/x-icon";
    } else if (strcasecmp(point , ".jpg") == 0) {
        return "image/jpeg";
    } else if (strcasecmp(point , ".js") == 0) {
        return "text/javascript";
    } else if (strcasecmp(point , ".php") == 0) {
        return "text/x-php";
    } else if (strcasecmp(point , ".png") == 0) {
        return "image/png";
    } else {
 // TODO
    return NULL;

    }
}

bool parse(const char* line, char* abs_path, char* query)
{
   char * linecopy = malloc(sizeof(char*));
   char * method = malloc(sizeof(char*));

   linecopy = strdup (line);
   method = strdup(strsep (&linecopy , " "));

    if (strcasecmp(method, "GET") != 0) 
    {
        error(405);

        return false;
    }
    char * request_line = malloc(sizeof(char*));
    request_line = strdup(strsep (&linecopy , " "));


    if (strncmp (&request_line[0] , "/",1 ) != 0) 
    {
        error(501);

        return false;
    }

    if (strchr(request_line ,'"')) 
    {

        error(400);
        return false;
    }
    char * http_version  = malloc(sizeof(char*));
    http_version = strdup(strsep (&linecopy , "\r\n"));



    if(strcasecmp(http_version, "HTTP/1.1") != 0)
    {
        error(505);
        return false;
    }

    char * temp_query = malloc(sizeof(char*));
    if (strrchr(request_line , '?') !=NULL ) 
    {
    temp_query = strdup(strrchr(request_line , '?')+1);
    query = strdup(temp_query);
    }
   // 

    char * temp_abs_path = malloc(sizeof(char*));

    if (query != NULL && strrchr(request_line , '?')) 
    {

        strncpy(temp_abs_path, request_line, strlen(request_line)-strlen(query)-1);
    } else {
        temp_abs_path  = strdup(request_line);
    }

    abs_path = strdup(temp_abs_path);

    free(temp_abs_path);
    free(temp_query);
    free(http_version);
    free(method);
    free(request_line);

    return false;
}
1

Your code is awesome! I found three lines that you might want to change in your parse() function:

Numbering based on the code you provided. For example:

const char* lookup(const char* path)                // Line #  1

http_version = strdup(strsep (&linecopy , "\r\n")); // Line # 65

You might want to change the following:

Line 79:

query = strdup(temp_query);

to something like

strcpy(query, temp_query);

Very similarly, line 93:

abs_path = strdup(temp_abs_path);

to:

strcpy(abs_path, temp_abs_path);

And third, line 101:

return false;

to:

return true;

EXPLANATION:

strdup() returns the address of duplicate string. If you make your pointer equal to that address, you overwrite your pointer. It is given to parse() and address of the strings abs_path and query. You need to edit those addresses so whoever calls parse() gets those strings changed even after parse() returns. You can edit those addresses with strcpy().

parse() is being evaluated in a condition in the main() function. The program will not proceed unless parse() succeeded represented by returning true.

1
  • It works! Tnx for the answer , I spend hours to find - What is wrong !!! The main problem was free() the memory it fail every time that's why I change strcpy everywhere to the strdup() , and receive a new problems ) Nov 11 '16 at 23:39

You must log in to answer this question.

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