0

I think these checks are related to indexes but I can't figure out where I have went wrong.

:) 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
:) Requesting hello.php?name=Alice returns 200, text/html, and correct output
:) Requesting /test redirects to /test/

:( Requesting /test/ outputs /test/index.html \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:( Requesting directory containing index.php outputs index.php \ expected output, not standard error of "======= Backtrace: =========\n======= M..."

:) Requesting two files in a row (cat.html then cat.jpg) succeeds https://sandbox.cs50.net/checks/6a7b17061cfe4ae1a19ea8f7f3617c55

INDEXES

char* indexes(const char* path)
{
    char* temp_path = malloc(sizeof(path));
    memcpy(temp_path, path, strlen(path) + 1);

    char* php_index = "/index.php";
    char* html_index = "/index.html";

    char* php_path = malloc(sizeof(path) + sizeof(php_index) + 1);
    char* html_path = malloc(sizeof(path) + sizeof(html_index) + 1);

    memcpy(php_path, temp_path, strlen(path) + 1);
    memcpy(html_path, temp_path, strlen(path) + 1);

    strcat(php_path, php_index);
    strcat(html_path, html_index);

    if(access(php_path, F_OK) != -1)
    {
        return php_path;
    }
    else if(access(html_path, F_OK) != -1)
    {
        return html_path;
    }
    else
    {
        return NULL;
    }
}

LOAD

bool load(FILE* file, BYTE** content, size_t* length)
{
    fseek(file, 0, SEEK_END);
    *length = ftell(file);
    rewind(file);

    char* fstore = malloc(*length);

    fread(fstore, *length, 1, file);
    *content = fstore;

    return true;
}

LOOKUP

const char* lookup(const char* path)
{
    char extension[6];

    char* css = ".css";
    char* html = ".html";
    char* gif = ".gif";
    char* xicon = ".ico";
    char* jpeg = ".jpg";
    char* java = ".js";
    char* php = ".php";
    char* png = ".png";

    char* location = strrchr(path, '.');
    int i = 0;

    while(location[i] != '\0')
    {
        extension[i] = location[i];
        i++;
    }

    printf("extension - %s\nLocation - %s\n", extension, location);

    if(strcasecmp(css, extension) == 0)
    {
        return "text/css";
    }
    else if(strcasecmp(html, extension) == 0)
    {
        return "text/html";
    }
    else if(strcasecmp(gif, extension) == 0)
    {
        return "image/gif";
    }
    else if(strcasecmp(xicon, extension) == 0)
    {
        return "image/x-icon";
    }
    else if(strcasecmp(jpeg, extension) == 0)
    {
        return "image/jpeg";
    }
    else if(strcasecmp(java, extension) == 0)
    {
        return "text/javascript";
    }
    else if(strcasecmp(php, extension) == 0)
    {
        return "text/x-php";
    }
    else if(strcasecmp(png, extension))
    {
        return "image/png";
    }
    else
    {
        return NULL;
    }
}

PARSE

bool parse(const char* line, char* abs_path, char* query)
{

    char line_copy[LimitRequestLine + 1];
    strcpy(line_copy, line);                       // Copy line for manipulation
    char* method = strtok(line_copy, " ");         // Parse at first space to get method
    char* req_target = strtok(NULL, " ");            // Parse at second space to get abs_path
    char* version = strtok(NULL, "\r");            // Parse at /r to get version
    int compare;

    printf("line_copy = %s\nMethod = %s\nreq_target = %s\nVersion = %s\n", line_copy, method, req_target, version);

    compare = strcmp(method, "GET");               // Check method is 'GET'. If not return error.

    if(compare != 0)
    {
        error(405);
        return false;
    }

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

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

    compare = strcmp(version, "HTTP/1.1");

    if(compare != 0)
    {
        error(505);
        return false;
    }
    if(strchr(req_target, '?') != NULL)
    {
        char* temp = strtok(req_target, "?");
        strcpy(abs_path, temp);

        temp = strtok(NULL, "");
        if(temp != NULL)
        {
            strcpy(query, temp);
        }
    }
    else
    {
        strcpy(abs_path, req_target);
        query = "";
    }
    return true;
}
1

sizeof bad. char* temp_path = malloc(sizeof(path)); path is a pointer. sizeof a pointer is 4 or 8 depending on architecture. Looks like you want the length of the strings, which is a different function. Et. al.

| improve this answer | |
  • Thanks! I used (strlen(path) + 1). It still isn't passing check50 though. – Ryan McCauley Sep 13 '16 at 12:52
  • Good start. Any other "sizeof" in your indexes function? – DinoCoderSaurus Sep 13 '16 at 12:54
  • Ah yes I did it quite a few times. I corrected them all now. I am getting 4 errors now instead of the two but they are different. Any idea what these are indicating? :( Requesting hello.php returns 200, text/html, and correct output :( Requesting hello.php?name=Alice returns 200, text/html, and correct output :( Requesting /test/ outputs /test/index.html :( Requesting directory containing index.php outputs index.php – Ryan McCauley Sep 13 '16 at 16:01
  • Curious. I didn't look at load because the errors pointed directly to indexes. The php tests should never have passed because php files are sent to load as pipes, not files, and you can't use ftell on a pipe (or fseek as far as I know). Info here. – DinoCoderSaurus Sep 13 '16 at 18:17
  • That makes sense I had been running it was giving me seg faults with only PHP files. I did a bit of reading on pipes. Just so I understand, in a pipe the communication flows through one way so I can do either fread or just getchar but can't move the file pointer around with functions like ftell and fseek? And the function that calls load - interpret - is used to send the php file through an interpreter and then send the interpreted data back through a one-way communication(pipe)? Thanks again for your help! – Ryan McCauley Sep 13 '16 at 22:34

You must log in to answer this question.

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