0

I have been stuck on pset6 for almost a month now and I've tried everything to debug it but I absolutely cannot find what I'm doing wrong... It's probably because I've looked through it so many time that I think what I'm getting too used to my own code so I am hoping someone can help me take a look at it. I've ran the first check and what I'm getting is this :

:) server.c exists
:) server compiles
:) HTTP/1.0 returns error code 505
:) Method of 'abcGET' returns error code 405
:) Method of 'GETabc' returns error code 405
:) request-target without starting '/' returns error code 501
:) request-target of abc/hello.php returns error code 501
:( Requesting cat.exe returns error code 501
\ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:( Requesting non-existant file returns error code 404
\ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:) Requesting request-target with " returns error code 400
:) 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

And here's my code:

indexes:

char* indexes(const char* path)
{
// TODO
   if (path != NULL) { 

     char* storage = malloc(sizeof(char*));

     storage =  strstr (path,"index.php");

     if (storage != NULL) 
     {
         return storage = "/path/to/a/directory/index.php"; 
     }

     else{

     storage = strstr (path,"index.html");

         if (storage != NULL) 

         {
             return storage = "/path/to/a/directory/index.html";  
     }
        if (storage == NULL)
        {
                 return NULL; 
        }

     }

     free(storage); 
 } 

 return NULL;
}

Load:

bool load(FILE* file, BYTE** content, size_t* length)
{

   char*buffer =malloc(sizeof *file);

fread ( buffer, sizeof (buffer), BYTES, file);

*content = &buffer[0];
*length = sizeof (buffer);


return false;

}

Lookup:

const char* lookup(const char* path)
{
// TODO 
if(path!= NULL)
{

if (strstr (path, ".css")==0)
{
    return ".text/css";
}

if (strstr (path, ".html")==0)
{
    return ".text/html";
}

if (strstr (path, ".gif")==0)
{
    return ".image/gif";
}

if (strstr (path, ".ico")==0)
{
    return ".image/x-icon";
}

if (strstr (path, ".jpg")==0)
{
    return ".image/jpeg";
}

if (strstr (path, ".php")==0)
{
    return ".text/x-php";
}

if (strstr (path, ".js")==0)
{
    return ".text/javascript";
}

if (strstr (path, ".png")==0)
{
    return ".image/png";
}

}
return NULL;
}

Parse:

#define Method 0
#define Request_Target 1
#define Http_Version 2

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

 char* parse_line = malloc(sizeof(line));

strcpy (parse_line,line);

char* Copy_parse_line[3] = { NULL, NULL, NULL};

int i = 0;

Copy_parse_line[Method] = strtok(parse_line," ");

while( Copy_parse_line[i] != NULL && i<2) 
{
i++;
Copy_parse_line[i] = strtok(NULL," ");
}

strcpy ( abs_path,Copy_parse_line[Request_Target]);

if (strcmp (Copy_parse_line[Method],"GET")!=0)
{
error(405);
return false;
}

if (strncmp(Copy_parse_line[Request_Target], "/",1) !=0)

{
 error(501);
    return false;
}

if (strchr ( Copy_parse_line[Request_Target] , '"' )!=NULL)

{
    error(400);
    return false;
}

if (strcmp (Copy_parse_line[Http_Version] , "HTTP/1.1")!=0)

{
error(505);
return false;
}

if (strchr (Copy_parse_line[Request_Target],'?')==NULL)
{
   query="";
}

else{
        char* query_start = strstr(Copy_parse_line[Request_Target],"?");

        if (query_start)
        {
            query = query_start + 1;
}
}

return NULL;
}

Thank you so much,

Leon

2

There is (should be) only one error(505) in server, and it comes from parse. That should immediately draw your attention to this if (strcmp (Copy_parse_line[Http_Version] , "HTTP/1.1")!=0)

From the instructions:

Per 3.1.1 of http://tools.ietf.org/html/rfc7230, a request-line is defined as

method SP request-target SP HTTP-version CRLF
...CRLF represents \r\n

You could use strncmp or change the string literal to "HTTP/1.1\r\n"


Dude, this code is not ready for prime time. Or check50. Or handling a request from a browser or curl. By your own admission, you have been working on this for a month, but it appears that you have never even sent a request to your server. This check50 message \ expected output, not an exit code of 0 indicates a seg fault. Writing code is not all there is.

In programming, Paredo's principle applies. 20% of programming is writing the code. 80% is testing, troubleshooting, debugging, fixing, testing, troubleshooting, debugging, fixing. Lather, rinse, repeat. (Actually, it may be closer to Edison's genius ratio!)

First things first. Go through the instructions again, and do the exercise of using the staff's implementation, sending browser requests and curl requests. Your eyes are the first line of defense against bugs. See what success looks like. What does server report? What does browser do? What does curl do?

Next, let the troubleshooting begin. Start with printfs. Check the values of the variables you create (e.g. abs_path) in the functions, and more important, when you return to main. This print format "%s<-\n" has proved helpful in making sure that string variables are properly null terminated. Sometimes non-printing characters can fool the eyes, so this makes sure that what you see is what you get. When you are ready to move up to gdb, you will find some starter tips here.

Now for some low-hanging fruit to get you started on your troubleshooting journey.

parse

Trouble starts early, right here char* parse_line = malloc(sizeof(line));. line is a pointer. sizeof(line) is 4 or 8 depending on architecture. Think strlen. And don't forget a byte for the null-terminator.

The "two-spaces" problem is because you are using strtok. Here's a hint at one possible solution. Think strstr.

indexes

In the instructions /path/to/a/directory' is a placeholder. There (likely) is no such animal. The path in question is the argument to the function. This char* storage = malloc(sizeof(char*)); is a bad allocation. The point of this exercise is to see whether index.php or index.html exists in the path (argument). printfs/gdb will help here. path will be something like '/' or '/test/'. It is up to you to see if either of those files exist. Think access.

load

This char*buffer =malloc(sizeof *file); is an inauspicious start. This exercise is a lot like recover in pset4. You have to read "all available bytes from file". The main difference being, that you are storing the result in "dynamically allocated memory" and not writing to disk. You are going to need realloc.

lookup

All things being equal, lookup looks fine.

This pset really challenges one to exhibit a solid understanding of pointers and memory allocation and learning to use new functions; coalescing everything learned thus far. It can never be said that it's easy.

3
  • Hi DinoCoderSarus, I have tried making that change but it's giving me even more error: :( Requesting cat.exe returns error code 501 \ expected output, not an exit code of 0 :( Requesting non-existant file returns error code 404 \ expected output, not an exit code of 0 <br/> :( Two spaces after GET returns error code \ expected output, not an exit code of 0 :( Two spaces before HTTP/1.1 returns error code \ expected output, not an exit code of 0 – Leon Aug 8 '16 at 0:15
  • (sorry for the poor formatting of the previous comment) – Leon Aug 8 '16 at 0:22
  • answer amended. -oo- – DinoCoderSaurus Aug 8 '16 at 12:17

You must log in to answer this question.

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