1

I have been having trouble with server.c

My version seems to work with hello.html. However, when I try and use the following with telnet

GET /cat.jpg HTML/1.1

I get HTTP/1.1 404 file not found

Then, through gdb, I found that the path is correct for files such as hello.html, but is incorrect for cat.jpg

(gdb) print path
$1 = 0xbfffee60 "/home/jharvard/Dropbox/Medical School/CS50/pset6/public/cat.jpg\b"

I am unsure where the "\b" is coming from, as it doesn't occur with hello.html. Below is (I think) the relevant code around absolute path and path.

        // extract absolutepath
        char absolutepath[strlen(request_target) - strlen(query)];
        if (query == NULL)
        {
            strcpy(absolutepath, request_target);
        }
        else
        {
            strncpy(absolutepath, request_target, strlen(request_target)-strlen(query));  
        } 

        // validate absolutepath
        if (strchr(absolutepath, '.') == NULL)          // need to have a '.'
        {
            error(501);
            continue;
        }

        // TODO: concatenate root and absolute-path
        char path[strlen(root) + strlen(absolutepath)];
        strcpy(path, root);
        strcat(path, absolutepath);

        // TODO: ensure path exists
        if (access(path, F_OK) == -1)
        {
            error(404);
            continue;
        }

        // TODO: ensure path is readable
        if (access(path, R_OK) == -1)
        {
            error(403);
            continue;
        }

Also, it seemed that in other questions, the dprintf was the problem, so here is mine below

            // TODO: respond to client
            if (dprintf(cfd, "HTTP/1.1 200 OK\r\n") < 0)
            {
                continue;
            }
            if (dprintf(cfd, "Connection: close\r\n") < 0)
            {
                continue;
            }
            if (dprintf(cfd, "Content-Length: %i\r\n", length) < 0)
            {
                continue;
            }
            if (dprintf(cfd, "Content-Type: %s\r\n\r\n", type) < 0)
            {
                continue;
            }
            if (write(cfd, body, length) == -1)
            {
                continue;
            }

Thanks for any help in the matter.

1
  • I have the exact same problem, all the way down to it having a random \b at the end. – Norcim133 Jan 1 '16 at 5:11
1

I solved the problem on mine. Basically the error was introduced because my declaration of absoluteTarget had an extra space. That is, you need your strings exactly the right length. I found this by looking back in GDB until I found the line where the \b was added.

One thing I can see in yours, it doesn't look like you're adding null terminators to your strings. That could cause some problems. For example, absolute-path you have strlens of request-target - query. Well, that should give you the number of CHARACTERS in absolute path... but the SIZE of absolute-path should be that plus 1 for the null terminator.

0

From what you printed above, the path to cat.jpg is has also the second space of the request, maybe that's about how you are extracting the path from request.

You must log in to answer this question.

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