0

I spend a lot of time on this pset, and still got some errors which I can not figure out, so please help if you may. Here are errors cs50 checker sends me:
:) 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
\ expected output, not an exit code of 0
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
:( Requesting directory containing index.php outputs index.php
\ expected output, not an exit code of 0
:) Requesting two files in a row (cat.html then cat.jpg) succeeds

here is load:

bool load(FILE* file, BYTE** content, size_t* length)
{
*length = 0;
char *buffer = malloc(1);

for (unsigned int c = fgetc(file); c != EOF; c = fgetc(file))
{
    buffer[*length] = c;
    *length = *length + 1;
    buffer = realloc(buffer, *length + 1);
}

*content = &buffer[0];
return true;
}

here is lookup:

const char* lookup(const char* path)
{
const char ch = '.';
char* rest = strrchr(path, ch);

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

Parse:

bool parse(const char* line, char* abs_path, char* query)
{
//parses line and puts words into array
char buff[strlen(line)];
strcpy(buff, line);
int i = 0;
char *p = strtok (buff, " ");
char *parsed[3];

while (p != NULL)
{
    parsed[i++] = p;
    p = strtok (NULL, " ");
}
//checks for the method to be GET
if (strcmp(parsed[0], "GET") != 0)
{
    error(405);
    return false;
}
//checks for the HTTP/1.1
if (strcmp(parsed[2], "HTTP/1.1\r\n") != 0)
{
    error(505);
    return false;
}
char *request_target = parsed[1];
if (request_target[0] != '/')
{
    error(501);
    return false;
}
if (strchr(request_target, '"') != NULL)
{
    error(400);
    return false;
}

strcpy(abs_path, request_target);
query = strchr(abs_path, '?');
return true;
}

and indexes:

char* indexes(const char* path)
{
const char *php = "/index.php";
const char *html = "/index.html";

if (strstr(path, php) != NULL || strstr(path, html) != NULL)
{
    char *string = malloc(strlen(path) + 1);
    strcpy(string, path);
    return string;
}
else
    return NULL;
}

appriciate your help!

0

First problem, indexes, responsible for these errors:

:( Requesting /test/ outputs /test/index.html
:( Requesting directory containing index.php outputs index.php

From the specification:

given a /path/to/a/directory, returns /path/to/a/directory/index.php if index.php actually exists therein,

This does not mean "does the given path contain index.php". It means given the path, is there a file named index.php in that path (directory). You might want to investigate the access function.

The next problem is load (version before post edited), responsible for these errors:

( 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
\ expected output, not an exit code of 0

(BTW, this \ expected output, not an exit code of 0 indicates a seg fault).

From the spec:

Odds are you’re unfamiliar with popen. That function opens a "pipe" to a process (php-cgi in our case), which provides us with a FILE pointer via which we can read that process’s standard output (as though it were an actual file).

Notice it says "we can read ... as though it were an actual file". One cannot, however, rewind a pipe. (or ftell or fseek or any number of other "file" functions). The answer to this post describes it far better than I ever could. load generally requires realloc.

Addendum

  1. Problem in parse. It might be time to break out debug50 (there's starter info here) or gdb. This

    strcpy(abs_path, request_target); query = strchr(abs_path, '?');
    is problematic. From the definition of request-target in the spec:

absolute-path [ "?" query ]

The ["?" query ] means that phrase is optional. And it also indicates that "?" is not part of absolute-path nor query. What should query be if there is no '?' ? What result do you get when you request "hello.php?name=Alice" with browser or curl? (NB if you want to test the result of "hellp.php?", you need to use a curl request. All browsers that I've tested (ff, chrome) drop the trailing "?").

  1. Unsolicited advice (which is free, and you get what you pay for). This for (unsigned int c = fgetc(file); c != EOF; c = fgetc(file)) appears to be working, since image files don't fail check50. However, since fgetc returns an int, c should be declared an int. I'm sure there are cases when it is necessary to cast the return value of function to something other than what it returns. I do not recall any such case in the context of CS50.
| improve this answer | |
  • Hi, thanks, I did not understand indexes at first. I also changed my load function, but it still gives me same errors.(I editede original post, see chancges above). – Garve Worm Oct 12 '16 at 9:48
  • Answer amended. – DinoCoderSaurus Oct 12 '16 at 12:54

You must log in to answer this question.

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