0

Like the tittle says everything works perfectly when i use but check comes up with this. any help would be appreciated

Check50

:) server.c exists
:) server compiles
:( Requesting cat.jpg returns 200, image/jpeg, and correct image
:( Requesting cat.html returns 200, text/html, and correct file
:( Requesting cat2.HTML returns 200, text/html, and correct file
:( Requesting cat3.HtMl returns 200, text/html, and correct file
:( Requesting cat.gif returns 200, image/gif, and correct file
:( Requesting favicon.ico returns 200, image/x-icon, and correct file
:( Requesting test.css returns 200, text/css, and correct file
:( Requesting test.js returns 200, text/javascript, and correct file
:) Requesting hello.php returns 200, text/html, and correct output
:( Requesting hello.php? returns 200, text/html, and correct output
   \ expected output, not an exit code of 0
:) Requesting hello.php?name=Alice returns 200, text/html, and correct output
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
:( Requesting directory containing index.php outputs index.php
   \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."
:( Requesting two files in a row (cat.html then cat.jpg) succeeds
https://sandbox.cs50.net/checks/d0ce46c0ae8b43ae9202d9ba25044f1b

My testing

GET / HTTP/1.1
HTTP/1.1 200 OK
GET / HTTP/1.1
HTTP/1.1 200 OK
GET /cat.html HTTP/1.1
HTTP/1.1 200 OK
GET /cat.jpg HTTP/1.1
HTTP/1.1 200 OK
GET /cat.jpg HTTP/1.1
HTTP/1.1 200 OK
GET /favicon.ico HTTP/1.1
HTTP/1.1 200 OK
GET /hello.html HTTP/1.1
HTTP/1.1 200 OK
GET /hello.php HTTP/1.1
HTTP/1.1 200 OK
GET /hello.html HTTP/1.1
HTTP/1.1 200 OK
GET /hello.php?name=Dan HTTP/1.1
HTTP/1.1 200 OK
GET /test/ HTTP/1.1
HTTP/1.1 200 OK
GET /test/index.html HTTP/1.1
HTTP/1.1 200 OK

Parse

bool parse(const char* line, char* abs_path, char* query)
{
    // Boolean to check for query
    bool isQuery = false;

    // Store method in
    char method[LimitRequestLine];

    // To iterate over line
    int i = 0, j = 0;

    // Iterate over method
    while (line[i] != ' ')
    {
        method[j] = line[i];
        i++;
        j++;
    }

    // Move past whitespace charactor
    i++;

    // Check that method is GET
    if(strcmp(method, "GET") != 0)
    {
        error(405);
        return false;
    }

    // Temporary storage
    char chr;

    // For char* position
    j = 0;

    // Get absolute path, and query if there is one
    while(line[i] != ' ')
    {
        chr = line[i];

        // If there is a Query
        if(chr == '?')
        {
            // Null terminate path
            abs_path[j] = '\0';

            // Move past ?
            i++;

            // Reset counter
            j = 0;

            while(line[i] != ' ')
            {
                chr = line[i];

                query[j] = chr;
                i++;
                j++;

                isQuery = true;
            }

            // Null terminate
            query[j] = '\0';

            break;
        }

        if(chr == '"')
        {
            error(400);
            return false;
        }

        // Add char to abs_path
        abs_path[j] = chr;
        i++;
        j++;
    }

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

    // Check for query, if not null terminate query and path
    if(!isQuery)
    {
    // Null terminate path
    abs_path[j] = '\0';

    // Null terminate Query
    query[0] = '\0';
    }

    // Move past whitespace charactor
    i++;

    if(abs_path[0] != '/')
    {
        error(501);
        return false;
    }


    // Hold HTTP version
    char HTTP_ver[LimitRequestLine];

    // Reset counter
    j = 0;

    // Get HTTP version
    while(line[i] != '\r')
    {
        HTTP_ver[j] = line[i];
        i++;
        j++;
    }

    // Null terminate
    HTTP_ver[j] = '\0';

    // Check if correct version
    if(strcmp(HTTP_ver, "HTTP/1.1") != 0)
    {
       error(505);
       return false;
    }


    // Must be valid
    return true;
}

Lookup

const char* lookup(const char* path)
{
    //temporary strorage to hold type
    char type[10];

    int j = 0;

    // Find dot in string
    for(int i = 0, len = strlen(path); i < len; i ++)
    {
        if(path[i] == '.')
        {
            // Get type
            i++;

            while(i < len)
            {
                type[j] = tolower(path[i]);

                i++;
                j++;
            }

            type[j] = '\0';
        }
    }

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

Load

bool load(FILE* file, BYTE** content, size_t* length)
{
    // Variable to hold data
    char* data = malloc(sizeof(BYTE) + 1);
    if(data == NULL)
    {
        return false;
    }

    int size = 1, i = 0;

    // Iterate through file
    for(int c = fgetc(file); !feof(file); c = fgetc(file))
    {

        data = realloc(data, size * sizeof(BYTE));
        if(data == NULL)
        {
            free(data);
            return false;
        }

        data[i] = (char) c;

        size++;
        i++;
    }

    // Be sure content is pointing in the right place
    *content = data;

    *length = size;

    return true;
}

indexes

char* indexes(const char* path)
{
    int p_len = strlen(path);

    char* indexPHP = malloc(p_len + 11);

    strcpy(indexPHP, path);
    strcat(indexPHP, "index.php/");

    char* indexHTML = malloc(p_len + 12);

    strcpy(indexHTML, path);
    strcat(indexHTML, "index.html/");

    if(access(indexPHP, F_OK) == 0)
    {
        return indexPHP;
    }

    if(access(indexHTML, F_OK) == 0)
    {
        return indexHTML;
    }

    // Else
    return NULL;
}
1

Browser is very forgiving. If it wasn't, the internet would come to a screeching halt. There's lots of bad/invalid/buggy code out there. check50, on the other hand, is very precise.

One thing to know about check50/server2: It expects the return value of length (from load) to match the byte count (length) of the file exactly. You can find the length of a file with the ls -al command. For instance, ls -al htllo.html gives -rw--w---- 1 ubuntu ubuntu 316 Feb 1 2016 hello.html, where 316 is number of bytes in the file. What the computed value of length be for hello.html?

You could use gdb and curl to quickly find that answer. In one terminal, run gdb:

$ gdb ./server
(gdb) break load
(gdb) run ./public

In a second terminal, request hello.html with curl like so curl -i http://localhost:8080/hello.html. Back in the first terminal, gdb/server should be waiting at the load breakpoint. Now you can inspect the computed length before control returns to main.

This \ expected output, not an exit code of 0 is a seg fault. In order to see how your server responds to that request, you have to use a curl command (curl -i http://localhost:8080/hello.php?) because browsers (at least Chrome, Firefox, IE) strip a trailing '?' from a request line. Once you reproduce the error, gdb will help you find the cause.

There is a problem in indexes. By adding the trailing '/' to index.php|html, it is essentially looking for a directory, so neither access call will be true (unless of course the tested directory name exists). To accurately duplicate the indexes tests in check50, the files index.php and/or index.html must exist. To reproduce check50 results, you can copy hello.html to either/both files (eg cp hello.html index.html).

1
  • You're a G man, 10 minutes and clears all checks. thanks a lot! Dec 17 '16 at 16:09

You must log in to answer this question.

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