0

I've been working my way through the second half of PSET6, server 2, and this is what I get when I try 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
:) 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, not an exit code of 0
:( Requesting two files in a row (cat.html then cat.jpg) succeeds

It's weird, because the way I check jpg, html, and the rest is the same way I check for php, and only PHP is working. Here's my lookup(), just in case I have an error there:

const char* lookup(const char* path)
{
// TODO
int len = strlen(path);
printf("%i", len);
char* s = NULL;
s = strrchr(path, '.');
printf("%s", s);
//FIND OUT WHAT'S IN S
    if(strcasecmp(s, ".css") == 0) {
        s = "text/css";
    } else if(strcasecmp(s, ".html") == 0) {
        s = "text.html";
    } else if(strcasecmp(s, ".gif") == 0) {
        s = "image/gif";
    } else if(strcasecmp(s, ".ico") == 0) {
        s = "image/x-icon";
    } else if(strcasecmp(s, ".jpg") == 0) {
        s = "image/jpeg";
    } else if(strcasecmp(s, ".js") == 0) {
        s = "text/javascript";
    } else if(strcasecmp(s, ".php") == 0) {
        s = "text/x-php";
    } else if(strcasecmp(s, ".png") == 0) {
        s = "image/png";
    } else  {
        s = NULL;

}
return s;
}

I think, however, that the main error for the lower problems resides in indexes(), so here is that function

char* indexes(const char* path)
{
// TODO
char* check = NULL;
char* check2 = NULL;
int leng = strlen(path);
if(strncmp(&path[leng-2], "/", 1) == 0) {
    sprintf(check, "%sindex.php", path);
    sprintf(check2, "%sindex.html", path);
} else {
    sprintf(check, "%s/index.php", path);
    sprintf(check2, "%s/index.html", path);
}
printf("%s", check);
if(access(check, F_OK) == 0) {
    return check;
} else if (access(check2, F_OK) == 0) {
    return check2;
} else {
    return NULL;
}
}

I've been reading around, looking at stuff that could help me, but I can't find anything that helps. If I could get a tip to help me on my way, that'd be great.

1
  • I think that most probably the problem lies in your implementation of parse function. I have submitted the pset by not implementing indexes and I was able to pass all test cases leaving one or two. Also note that even after passing server1 check I faced the same problem like you are facing now with server2 check. It only means that you are not null terminating the strings in parse function or if you get 501 not implemented error then it has to do with your load function. Commented Aug 27, 2016 at 16:49

1 Answer 1

2

indexes

As you predicted, these failures

:( Requesting /test/ outputs /test/index.html
:( Requesting directory containing index.php outputs index.php \ expected output, not an exit code of 0

come from indexes(). This \ expected output, not an exit code of 0 is usually a segfault

check and check2 have no memory allocated.

lookup

Your use of 's' in lookup is problematic.

Here it is declared, then set to an address within path. (strrchr returns a pointer).

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

So far so good. Since path is already allocated, then any pointer to an address within path is pointing to allocated memory.

The problem comes here s = "text/css";. s doesn't have any memory of it's own, so you cannot put "stuff" in it. Remembering that you do not have to specifically allocate memory for a string literal (e.g. char* str = "abcde";), you could simply return a string literal (eg "text/css"), and let the computer worry about allocating it.

You must log in to answer this question.

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