0

I've been checking similar questions about the problem I'm having but couldn't solve it myself. Help would be really appreciated.

After implementing lookup and parse and running check50 I'm receiving the following errors:

:( Requesting cat.exe returns error code 501
   \ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:( Requesting non-existant file returns error code 404
   \ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."

This is my code:

const char* lookup(const char* path)

{

    //create variable that will point the extension
    int pathLength = strlen(path);
    char* extension = malloc(pathLength);
    extension = strchr(path, '.');

    if(extension != NULL)
    {
        if((strcasecmp(extension, ".css") || strcasecmp(extension, ".CSS")) == 0)
        {
            const char* mime = "text/css";
            return mime;
        }
        if((strcasecmp(extension, ".html") || strcasecmp(extension, ".HTML")) == 0)
        {
            const char* mime = "text/html";
            return mime;
        }
        if((strcasecmp(extension, ".gif") || strcasecmp(extension, ".GIF")) == 0)
        {
            const char* mime = "image/gif";
            return mime;
        }
        if((strcasecmp(extension, ".ico") || strcasecmp(extension, ".ICO")) == 0)
        {
            const char* mime = "image/x-icon";
            return mime;
        }
        if((strcasecmp(extension, ".jpg") || strcasecmp(extension, ".JPG")) == 0)
        {
            const char* mime = "image/jpeg";
            return mime;
        }
        if((strcasecmp(extension, ".js") || strcasecmp(extension, ".JS")) == 0)
        {
            const char* mime = "text/javascript";
            return mime;
        }
        if((strcasecmp(extension, ".phm") || strcasecmp(extension, ".PHM")) == 0)
        {
            const char* mime = "text/x-php";
            return mime;
        }
        if((strcasecmp(extension, ".png") || strcasecmp(extension, ".PNG")) == 0)
        {
            const char* mime = "image/png";
            return mime;
        }
    }
    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)
{

    //check if method is GET
    if (strncmp(line, "GET ", 4) != 0)
    {
        error(405);
        return false;
    }

    //check if request-target starts with /
    if (line[4] != '/')
    {
        error(501);
        return false;
    }

    //get starting of request target
    char* haystack = strchr(line, '/');
    char* needle = strchr(haystack, ' ');

    // used to check what comes after the request-target
    char* http_version = needle;

    //check that there are not extra spaces in request target
    char rtarget[needle - haystack];
    strncpy(rtarget, haystack, needle - haystack);
    rtarget[needle - haystack] = '\0'; 

    char* errchk = strchr(rtarget, '\"');
    if (errchk != NULL)
    {
        error(400);
        return false;
    }   

    //check http version
    if (strncmp(http_version, "HTTP/1.1 ", 9) != 0)
    {
        error(505);
        return false;
    }

    // parse absolute path
    haystack = rtarget;
    needle = strchr(haystack, '?');

    // check if there's a query
    if (needle == NULL)
    {
        strcpy(abs_path, haystack);
        query[0] = '\0';
        return true;
    }
    else
    {
        char path[needle - haystack + 1];
        strncpy(path, haystack, needle - haystack);
        path[needle - haystack + 1] = '\0'; 

        strcpy(abs_path, path);
        strcpy(query, needle + 1);
        return true;
    }


}

Any hint on why am I getting this errors?

Thanks a lot in advance.

1

strcasecmp is meant for comparing string without caring about capitalization. Comparing twice, with different capitalizations is not really useful here.

The reason why you are getting the 505 HTTP Version Not Supported error is because http_version starts with a whitespace.

Indeed, when doing

char* needle = strchr(haystack, ' ');
the ' ' char is included in the needle.

When comparing " HTTP/1.1" with "HTTP/1.1", you are getting, as expected, a true value.

| improve this answer | |
  • thanks!!! It works now – Mateo Aug 27 '16 at 22:23

You must log in to answer this question.

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