0

this is some pain in the neck every single test is passed except :( Requesting cat.exe returns error code 501 can you please help me my brain is a war zone right now here is my code

const char* lookup(const char* path)
{
    char* type=strrchr(path,'.');
    if((tolower(type[1])=='c')&&(tolower(type[2])=='s')&&(tolower(type[3])=='s')&&(type[4]=='\0'))
    {
        return "text/css";
    }
    else if((tolower(type[1])=='h')&&(tolower(type[2])=='t')&&(tolower(type[3])=='m')&&(tolower(type[4])=='l')&&(type[5]=='\0'))
    {
        return "text/html";
    }
    else if((tolower(type[1])=='g')&&(tolower(type[2])=='i')&&(tolower(type[3])=='f')&&(type[4]=='\0'))
    {
        return "image/gif";
    }
    else if((tolower(type[1])=='i')&&(tolower(type[2])=='c')&&(tolower(type[3])=='o')&&(type[4]=='\0'))
    {
        return "image/x-icon";
    }
    else if((tolower(type[1])=='j')&&(tolower(type[2])=='p')&&(tolower(type[3])=='g')&&(type[4]=='\0'))
    {
        return "image/jpeg";
    }
    else if((tolower(type[1])=='j')&&(tolower(type[2])=='s')&&(type[3]=='\0'))
    {
        return "text/javascript";
    }
    else if((tolower(type[1])=='p')&&(tolower(type[2])=='h')&&(tolower(type[3])=='p')&&(type[4]=='\0'))
    {
        return "text/php";
    }
    else if((tolower(type[1])=='p')&&(tolower(type[2])=='n')&&(tolower(type[3])=='g')&&(type[4]=='\0'))
    {
        return "image/png";
    }
    else
    {
        return NULL;
    }
}

and here is my parse function

bool parse(const char* line, char* abs_path, char* query)
{
    if(strncmp(line,"GET ",4)==0)
    {
        if(line[4]=='/')
        {
            if(strchr(line,'\"')==NULL)
            {`

                `char* string1=strchr(line,'.');
                string1=strchr(string1,' ');

                if(string1!=NULL&&(!strcmp(string1," HTTP/1.1\r\n")))
                {
                    strcpy(abs_path,line);
                    return abs_path;
                }
                else
                {
                    error(505);
                }
            }
            else
            {
                error(400);
            }
        }
        else
        {
            error(501);
        }
    }
    else
    {
        error(405);
    }
    return false;
}
0

That check50 test fails because of this strcpy(abs_path,line);. Recall from the specification:

Per 3.1.1 of https://www.rfc-editor.org/rfc/rfc7230, a request-line is defined as

method SP request-target SP HTTP-version CRLF

wherein SP represents a single space ( ) and CRLF represents \r\n. None of method, request-target, and HTTP-version,meanwhile, may contain SP.

Per 5.3 of the same RFC, request-target, meanwhile, can take several forms, the only one of which your server needs to support is

absolute-path [ "?" query ]

Let's say line is a well-formed request-line. It would be something like GET /cat.html HTTP/1.1\r\n.

So the result of this strcpy(abs_path,line); is that abs_path will be the same as line. It should be /cat.html. You are going to have to use some string functions to get only the part you need.

This return abs_path; is also a problem. parse returns a bool, so the function should only return true; or return false; .

The structure of parse is going to make it very difficult for you to troubleshoot and debug. It is difficult to follow the program flow because of all the nesting. Consider taking a different approach. Because this function tests for a lot of error conditions, something like this

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

might make the code easier to read and follow. By testing for the error condition (!= 0) and returning out of the function, you don't have to worry about an else condition. You can simply get on with your next test.

gdb could help you work through this pset. Find some tips here. (Or try the new debug50!)

You might want to read up on strcasecmp to see how it might help you streamline lookup.

You must log in to answer this question.

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