0

I think my parse and lookup are working fine, but check50 is not happy. Maybe anyone has an idea. here my functions:

const char* lookup(const char* path)
{
    // if there is a path, compare and give correct output if matched

        char* s = strrchr(path, '.');

        if(strcmp(s, "CSS")== 0)
        {
            return "text/css";
        }
        else if(strcmp(s, "HTML")== 0)
        {
            return "text/html";
        }
        else if(strcmp(s, "GIF") == 0)
        {
            return "image/gif";
        }
        else if(strcmp(s, "ICO")== 0)
        {
            return "image/x-icon";
        }
        else if(strcmp(s, "JPG")== 0)
        {
            return "image/jpeg";
        }
        else if(strcmp(s, "JS")== 0)
        {
            return "text/javescript";
        }
        else if(strcmp(s, "PHP")== 0)
        {
            return "text/x-php";
        }
        else if(strcmp(s, "PNG")== 0)
        {
            return "image/png";
        }

    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 method is not get, respond with error
    if(strncmp(line, "GET", 4) !=0)
    {
        error(405);
        return false;
    }

    //if requested target doesn't begin with /, respond with error
    if(line[4] != '/')
    {
        error(501);
        return false;
    }

    //get requested tagert
    char* haystack = strchr(line, '/');
    char* needle = strchr(haystack, ' ');
    //after the requested target
    char* HTTP_version = needle;

    //check for extra targets
    char* errchk = strchr(HTTP_version + 1, ' ');
    if(errchk != NULL)
    {
        error(505);
        return false;
    }

    char target[needle - haystack];
    strncpy(target, haystack, needle-haystack);
    target[needle- haystack] = '\0';

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

    //parse absolut path
    haystack = target;
    needle = strchr(haystack, '?');

    //check for query
    if(needle == NULL)
    {
        strcpy(abs_path, haystack);
        query[0] = '\0';
    }

    else
    {
        //if so, remove query
        char path[needle-haystack + 1];
        strncpy(path, haystack, needle- haystack);
        path[needle-haystack +1] = '\0';

        strcpy(abs_path,path);

        //parse query
        strcpy(query, needle +1);
    }

    //check for http version
    needle = strstr(HTTP_version, "HTTP/1.1");

    if(needle == NULL)
    {
        error(505);
        return false;
    }

        return true;
}

and the check 50 output:

:) server.c exists
:) server compiles
:( HTTP/1.0 returns error code 505
   \ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:) Method of 'abcGET' returns error code 405
:) Method of 'GETabc' returns error code 405
:( request-target without starting '/' returns error code 501
   \ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( request-target of abc/hello.php returns error code 501
   \ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( Requesting cat.exe returns error code 501
   \ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( Requesting non-existant file returns error code 404
   \ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( Requesting request-target with " returns error code 400
   \ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:) Two spaces after GET returns error code
:) A space within the request target returns error code
:) Two spaces before HTTP/1.1 returns error code
1
char* s = strrchr(path, '.');

as I understand strrchr returns a pointer to point '.', hence the string s would be something like ".css" for example, on the other hand strcmp, compares two string, to return zero, both chains must be identical, including uppercase or lowercase characters, you might want to take a look at the function strcasecmp

EDIT

if(strncmp(line, "GET", 4) !=0)

You will find the same problems this statement will always return a 405 error, be sure you understand the functions that handle strings, and care with pointers, a good option is to use gdb to check that our variables really are as expected.

| improve this answer | |
  • hi thanks for your answer. I used strcasecmp before. I changed it because I thought maye´be thats the problem... But didn´t change a thing! Maybe I need to take a closer look at uppercase and lowercase characters... – user10882 Aug 18 '16 at 17:12

You must log in to answer this question.

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