0

I am getting a 404 from below.

if (parse(line, abs_path, query))
            {
                // URL-decode absolute-path
                char* p = urldecode(abs_path);
                //printf("URL decoded abs path is: %s\n",p);
                if (p == NULL)
                {
                    error(500);
                    continue;
                }

                // resolve absolute-path to local path
                path = malloc(strlen(root) + strlen(p) + 1);
                if (path == NULL)
                {
                    error(500);
                    continue;
                }
                strcpy(path, root);
                strcat(path, p);
                free(p);

                // ensure path exists
                if (access(path, F_OK) == -1)
                {
                    //printf("Path is: %s\n",path);
                    error(404);
                    continue;
                }

And my console is telling me

Using /home/ubuntu/workspace/pset6/public for server's root
Listening on port 8080
GET / HTTP/1.1
Absolute path is / 
Query is 
Path is: /home/ubuntu/workspace/pset6/public/ 
HTTP/1.1 404 Not Found

I believe I have accounted for all exceptions required by the specification, and the path should indeed be / and nothing else, but the server program gives a 404. Any thoughts?

-----EDIT - CODE BELOW---------

Parse:

char* request_line = malloc(sizeof(char)*(LimitRequestLine+1));
char* method = malloc(sizeof(char)*4);
char* http_version = malloc(sizeof(char)*9);
strncpy(method, line, 3);
method[3] = '\0';

if(strcmp("GET", method)!=0){
    //printf("Method is not GET");
    error(405);
    return false;
}

const char* question_mark = "?";
//const char* slash = "/";

request_line = strchr(line, 47);
http_version = strrchr(line, 72);
http_version[8] = '\0';
//printf("http_version is: %s\n", http_version);
if(strcmp("HTTP/1.1", http_version)!=0){
    error(505);
    return false;
}

int http_index = http_version - request_line;
//printf("request_line is: %s\n", request_line);

//printf("http_index is: %i\n",http_index);

request_line[http_index]='\0';

//printf("request_line after null pointing is: %s\n", request_line);
/*for (int i = 0; i<(strlen(request_line)-http_index)){
    request_line[i]='\0';
}*/

//printStr(request_line);

//printStr(http_version);

char* request_start = strchr(line,32);
request_start = NULL; // Temporary
char* q_start = strchr(line, 63);

if(q_start==NULL){
    strcpy(abs_path, request_line);
}else{
    //Separate query from line
    strncpy(abs_path, request_line, (int)q_start);
    query = strstr(request_line, question_mark);
}
//Abs path, request line and query should be complete now. Check for further errors follows.
if(request_line[0]!='/'){
    error(501);
    return false;
}
if(strchr(request_line,34)!=NULL){
    error(400);
    return false;
}

printf("Absolute path is %s\n", abs_path);
printf("Query is %s\n", query);

return true;
1
0

Your path is not supposed to be / only. It is supposed to be /index.html for example. If you provide the code of your functions it will be more clear to answer. And if you dont know query is supposed to be name=anyname. If no query then query is supposed to be \0.

2
  • Sorry, my IDE was messing up, so I updated the console output. Indexes function is not called until after the check that gives 404 in main(). The first request is GET / HTTP/1.1, so wouldn't the correctly parsed absolute path just be"/"? And of course, the query would be blank. Right after is where I get the 404 error.
    – user10805
    Mar 23 '16 at 21:56
  • Yes for this input it is. Can you provide your code? Mar 24 '16 at 1:07

You must log in to answer this question.

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