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.