0

Am really trying to solve server.c though I don't really understand the specification for load and indexes functions.

My parse and lookup seems to be working well, they passed check50 for server1 while server2 check50 failed woefully.

Please any pointer in the right direction will be greatly appreciated.

I'll also appreciate more explanation concerning what to do in load and indexes.

Here is my check50 output

~/workspace/pset6/ $ check50 2015.fall.pset6.server2 server.c
:) 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
   \ expected output, not an exit code of 0
:( 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, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."
:( Requesting two files in a row (cat.html then cat.jpg) succeeds  

-

bool load(FILE* file, BYTE** content, size_t* length)
{
    // TODO
    if(file == NULL)
    {
        return false;
    }
    *content = NULL;
    *length = 0;
   BYTE* buf = (BYTE*)malloc(BYTES * sizeof(BYTE));

   int counter =0;
while(fread(buf, BYTES, 1, file) != 0)
{
    length++;
    counter++;
    if(counter == 1)
    {
        content = &buf;
    }
    buf = (BYTE*)realloc(buf, BYTES * sizeof(BYTE));
    if(buf == NULL)
   {
       return false;
   }
}

    return true;
}  

-

char* indexes(const char* path)
{
    // TODO
    size_t len = strlen(path);
    char* new_path= malloc(len * sizeof(char));
    strcpy(new_path, path);
    char* new_pathp = malloc((len +10) * sizeof(char));
    char* new_pathh = malloc((len + 11) * sizeof(char));
    char* index1 = "index.php";
    char* index2 = "index.html";
    new_pathp = strcat(new_path, index1);
    new_pathh = strcat(new_path, index2);

    if(access(new_pathp, F_OK) != -1)
    {
        free(new_path);
        return new_pathp;
    }
    else if(access(new_pathh, F_OK) != -1)
    {
        free(new_path);
        return new_pathh;
    }
    else
    return NULL;
}

1 Answer 1

0

load

This

*content = NULL;
*length = 0; 

is on the right track of understanding pointer-to-a-pointer. Why didn't you carry that notation through, eg length++;? (And SPOILER: beware of *length++ because of operator precedence. There are other ways to increment it).

You're also on the right track with building *content. A few things to contemplate:

  • This fread(buf, BYTES, 1, file) always reads 512 bytes into the first byte of buf.
  • This buf = (BYTE*)realloc(buf, BYTES * sizeof(BYTE)); always allocates buf for 512 bytes. How's it going to "fit" cat.jpg at 26860 bytes?

indexes

This function demonstrates a total understanding of what indexes should do. One flaw, however, is here new_pathp = strcat(new_path, index1). new_pathp is local to the function. The memory will be "destroyed" when control returns to main. Remember strcat modifies its first argument, much the same as strcpy.

parse

This \ expected output, not an exit code of 0 is a seg fault, probably indicating some "problem" with building query.


debug50 and gdb would be the goto tools to help you troubleshoot and debug server. Here's some hints for gdb viz. load to get you started.

  • In terminal one:

    $ gdb ./server
    (gdb) break load
    (gdb) run ./public
    
  • In terminal two: curl -i http://localhost:8080/hello.html

  • Back in terminal one, gdb should be waiting at the breakpoint in load. Let the troubleshooting begin!

Remember, *length must be the exact number of bytes in the file. To find the length to expect for hello.html, you can ls -al hello.html. That will return something like
-rw-rw-r-- 1 ubuntu ubuntu 316 Oct 24 2015 hello.html, where 316 is length. Using curl requests while getting used to either of the debuggers is a little easier because it doesn't load favicon.ico.

With practice, patience and perseverance using a debugger will get you to the goal.

You must log in to answer this question.

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