0

In server.c to ensure the path exists I use this code:

if (access(path, F_OK) == -1)
{
    error(404);
    continue;
}

This leads to a 404 error even when the path exists. For example, if the request (using telnet) is

GET /cat.html HTTP/1.1

Using gdb or printf statements, path is:

/home/jharvard/Dropbox/pset6/public/cat.html

Yet the return value of access (if stored in a variable and printed) is -1. What is causing this?

UPDATE: This is how I form path:

// concatenate root and absolute-path
        char path[strlen(root) + rt_l + 1];
        strcpy(path, root);
        if (qptr == NULL)
        {
            strcpy(path + strlen(root), request_target);
        }
        else
        {
            strncpy(path + strlen(root), request_target, qptr - space1 - 1);
            path[qptr - space1] = '\0';
        }

rt_l is is simply the length of the request target

5
  • Are you absolutely sure that the path that is being used is what you said, at the point that it is failing? Specifically, did you printf("path=%s\n); inside the if statement, just before error(404);? And have you checked that all the permissions are right? Since you're getting a -1, the server code thinks it doesn't exist. Either it doesn't exist, it's not visible to the server code, or the path/filename aren't an exact match, including case.
    – Cliff B
    Commented Dec 21, 2015 at 20:09
  • @CliffB I actually have a printf statement just before the if statement. If i run under gdb printf("%s\n", path) produces /home/jharvard/Dropbox/pset6/public/cat.html It then enters the if statement and steps in meaning the statement is true and gives error 404
    – minniebot
    Commented Dec 21, 2015 at 20:58
  • I added a printf statement for the output of strcmp(path, "/home/jharvard/Dropbox/pset6/public/cat.html") and I get 32. What would cause this?
    – minniebot
    Commented Dec 21, 2015 at 21:06
  • It means that the two strings don't match. You might want to output the content of path as hex values to see if there are some non-printable characters embedded, or exactly what is stored in path. I'd also look for leading spaces or anything that might be distorting the content of path.
    – Cliff B
    Commented Dec 21, 2015 at 21:14
  • @CliffB how do I do that?
    – minniebot
    Commented Dec 21, 2015 at 21:22

1 Answer 1

0

Turns out I had lots of errors sprinkled throughout my code. I basically debugged my code line by line on paper and checking how strings and pointers were updating. I had many errors in my pointer arithmetic where I was subtracting pointers to dynamically determine string lengths such as the length of the method string. For example, I was subtracting a pointer in the request-target string from a pointer in the original line string or copying strings in the same manner and it would result in random behavior and errors.

1
  • Thanks to @CliffB for your help
    – minniebot
    Commented Jan 10, 2016 at 23:43

You must log in to answer this question.

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