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

code for lookup and parse-

/**
 * Returns MIME type for supported extensions, else NULL.
 */
const char* lookup(const char* path)
{
    char* ext = strstr(path,".");

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

/**
 * Parses a request-line, storing its absolute-path at abs_path 
 * and its query string at query, both of which are assumed
 * to be at least of length LimitRequestLine + 1.
 */
bool parse(const char* line, char* abs_path, char* query)
{
    if (strstr(line,"HTTP/1.1") == NULL)
    {
        error(505);
        return false;
    }

    int blnk_count = 0;
    int x = 0;
    while (line[x] != '\0')
    {
        if (line[x] == '"')
        {
            error(400);
            return false;
        }
        if (line[x] == ' ')
        {
            blnk_count++;
        }
        x++;
    }
    if (blnk_count != 2)
    {
        error(400);
        return false;
    }
    if (strncmp(line,"GET ",4) != 0)
    {
        error(405);
        return false;
    }
    if (strncmp(line,"GET /",5) != 0)
    {
        error(501);
        return false;
    }
    char* ptr1 = strchr(line,'/');
    char* ptr2 = strchr(line,'?');

    if (ptr2 == NULL)
    {
        ptr2 = strrchr(line,' ');
    }

    char path[ptr2 - ptr1 + 1];
    int n = ptr2 - ptr1;

    for (int i=0;i<n;i++)
    {
        path[i]=*ptr1;
        ptr1++;
    }
    path[n]='\0';

    strcpy(abs_path,path);
    free(path);

    if (*ptr1 == '?')
    {
        if (*(ptr1+1) ==' ')
        {
            query = "\0";
        }
        else
        {
            ptr1 = ptr1+1;
            ptr2 = (strchr(line,' '));
            char temp_query[ptr2 - ptr1 + 1];
            int m = ptr2 - ptr1;

            for (int i=0;i<n;i++)
            {
                temp_query[i] = *ptr1;
                ptr1++;
            }
            temp_query[m]='\0';
            strcpy(query,temp_query);
            free(temp_query);
        }
    }
    else if(*ptr1 == ' ')
    {
        query = "\0";
    }

    return true;
}

1 Answer 1

1

First suggest you send a request to server from browser or curl. You will discover that server seg faults. (That's what this \ expected output, not an exit code of 0 feedback from check50 indicates).

Next, in Week4 > Section > Dynamic Memory Allocation review the "Three Golden Rules" of memory allocation, around 8:45.

(1) Every block of memory that you malloc() must subsequently be free() d. (2) Only memory that you malloc() should be free() d. (3) Do not free() a block of memory more than once.

Then review your program for "violations" of the second rule. I see two places.

2
  • I gone through that section and my code again and i found that only places i used free(), was free(path) and free (temp_query), both of these variable strings are created by me and i freed them after their use. I am not able to find out where my code violated the second (or any of the) rule(s). Can you please pin point the places which are causing the error.
    – Vinz.R
    Aug 8, 2016 at 4:01
  • Thanks to you I finally solved it. The problem really was memory allocation. I assigned all the working variables (for eg. int blnk_count,x, and char path, temp_mem) to static memory which cannot be freed using any means (or at least free()). What I did is I malloced every variable (even the ones which are used in for loops). As a result the function can free them before returning any values. I can not imagine me solving this blunder if you wouldn't have helped. But still can anyone give some more info on why this happened because of static memory instead of dynamic alloc.
    – Vinz.R
    Aug 8, 2016 at 5:23

You must log in to answer this question.

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