0

check50 says this:

$ check50 2015.fall.pset6.server2 server.c
:) server.c exists
:) server compiles
:( Requesting cat.jpg returns 200, image/jpeg, and correct image
   \ killed by server
:( Requesting cat.html returns 200, text/html, and correct file
   \ killed by server
:( Requesting cat2.HTML returns 200, text/html, and correct file
   \ killed by server
:( Requesting cat3.HtMl returns 200, text/html, and correct file
   \ killed by server
:( Requesting cat.gif returns 200, image/gif, and correct file
   \ killed by server
:( Requesting favicon.ico returns 200, image/x-icon, and correct file
   \ killed by server
:( Requesting test.css returns 200, text/css, and correct file
   \ killed by server
:( Requesting test.js returns 200, text/javascript, and correct file
   \ killed by server
:( Requesting hello.php returns 200, text/html, and correct output
   \ killed by server
:( 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
   \ killed by server
:) 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
   \ killed by server
https://sandbox.cs50.net/checks/72d5bcae285e4790af5bc40512ac2346

I have been debugging with printfs and found that it stops running at function transfer:

void transfer(const char* path, const char* type)
{
    // ensure path is readable
    if (access(path, R_OK) == -1)
    {
        error(403);
        return;
    }
//printf("\n1028-%s\n%lu", path, strlen(path));

    // open file
    FILE* file = fopen(path, "r");
//printf("\n1035-%i", (int)file);
    if (file == NULL)
    {
//printf("\n1034");
        error(500);
        return;
    }
//printf("\n1037");

    // load file's content
    BYTE *content = NULL;
    size_t length;
    if (load(file, content, &length) == false)
    {
        error(500);
        return;
    }
//printf("\n1047");

    // close file
    fclose(file);
//printf("\n1051");

    // prepare response
    char* template = "Content-Type: %s\r\n";
    char headers[strlen(template) - 2 + strlen(type) + 1];
    if (sprintf(headers, template, type) < 0)
    {
        error(500);
        return;
    }
//printf("\n1061");

    // respond with file's content
    respond(200, headers, content, length);
//printf("\n1065");

    // free file's content
    free(content);
//printf("\n1069");
}

The printfs are commented to run check50

Here goes my debugging:

The output from the server is:

$ ./server public
Using /home/ubuntu/workspace/pset6/public for server's root
Listening on port 8080
GET /test/ HTTP/1.1

175

177

203

205

1/home/ubuntu/workspace/pset6/public/test/

2/home/ubuntu/workspace/pset6/public/test/index.php

3/home/ubuntu/workspace/pset6/public/test/index.html

4/home/ubuntu/workspace/pset6/public/test/index.html

217-1

223

232

242-/home/ubuntu/workspace/pset6/public/test/index.html
text/html

1028-/home/ubuntu/workspace/pset6/public/test/index.html
51
1035-16544096

The request was:

$ curl -i http://localhost:8080/test/
^C

I had to ctrl + C the request, however, to kill the server I had to close the tab.

The last thing that runs is the condition of the second if, but it does not run its contents nor continue.

The printf shows that file isn't NULL, so it should continue outside of the if.

Any clue?

EDIT:

Changed load() and now seems a little better.

The server doesn't hang, and everything seems to run, but the files don't load.

Content of load():

bool load(FILE *file, char *content, size_t *length)
{
    int64_t bl;
    bool eof = false;
    char buff[BYTES];
DBG_Print("\n598");
DBG_Pause;
    fseek(file, 0, SEEK_SET);
    for (bl = 1; !eof; bl++) {
        fseek(file, bl * BYTES, SEEK_SET);
        if (fread(buff, sizeof(buff), 1, file) != 1) {
            eof = true;
        }
    }
DBG_Print("\n607-%"PRId64"\n", bl);
DBG_Pause;
    *length = bl * BYTES;

DBG_Print("\n611-%"PRId64"\n", (int64_t) *length);
DBG_Pause;
    content = calloc(bl * BYTES, sizeof(*content));
    fseek(file, 0, SEEK_SET);
    fread(content, BYTES, bl, file);
    return true;
}

check50:

$ 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
:( 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
:) 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
https://sandbox.cs50.net/checks/4b0a053ff92d4256b1d6e906d6176dbc

Server output:

$ ./server public
Using /home/ubuntu/workspace/pset6/public for server's root
Listening on port 8080
GET /cat.jpg HTTP/1.1
GET / HTTP/1.1
HTTP/1.1 200 OK
GET /cat.jpg HTTP/1.1
GET /cat.html HTTP/1.1
GET /hello.html HTTP/1.1
GET /test HTTP/1.1
HTTP/1.1 301 Moved Permanently
GET /test/ HTTP/1.1

This output was for a command request and then some GUI requests. Command request:

$ curl -i http://localhost:8080/cat.jpg
HTTP/1.1 200 OK
Content-Type: image/jpeg

~/workspace/ $
4
  • Can I see your load() function? Nov 10 '16 at 19:51
  • And I am guessing you are getting all happy face in the previous check50 right? Nov 10 '16 at 19:52
  • Yes, check50 server1 is perfect.
    – alx
    Nov 11 '16 at 0:15
  • Here is load. I don't like my return true, but I don't know how to be able to expect a false in this function
    – alx
    Nov 11 '16 at 0:23
1

load() seems to be the only function that you should edit that gets called in transfer(). Instead of debugging transfer() I would make sure load() works.

If you are debugging transfer() because you think you might have edited something from the original code, you can always download the code from the beginning in a new folder.

You can create a new folder in your workspace like this:

mkdir newpset6

cd newpset6

Download the code with the line they give you:

wget http://cdn.cs50.net/2015/fall/psets/6/pset6/pset6.zip

unzip pset6

cd pset6

And plug all your parts in your new server.c.

3
  • Thank you, I forgot to use getchar() in my debugging. There was a bug that I expected on load(), but that I forgot later about it.
    – alx
    Nov 11 '16 at 0:14
  • Thanks to you! So is it solved? Or you still have errors? Nov 11 '16 at 13:08
  • ** That ** bug is solved, however I have one more bug. I'll post it in a separate question, as it's different.
    – alx
    Nov 11 '16 at 18:25

You must log in to answer this question.

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