0

I am in pset6. I am getting an error of 501 not implemented. I do no think this error comes from the validating line part because a couple of days ago i was having a 403 error. So up to that point everything was fine, hence the 501 error has to deal with the extension. The path is both readable and exists since now 403 and 404 errors are not returned. However, I cant see why or how my extension is being wrongly stored. Thanks for helping me in advance

//define elements to be checked
int length = strlen(line);
char method_checker[4];
method_checker[3] = '\0';

//Check if method is get
for (int i = 0; i < 3; i++)
{
    method_checker[i] = line[i];
}

//continue man page http://www.tutorialspoint.com/cprogramming/c_continue_statement.htm
if (strcmp(method_checker, "GET") != 0)
{
    error(405);
    continue;
}

//the first char of request is 5 element in line
if (line[4] != '/'){

    error(501);
    continue;
}

//Finds second space in request line
int second_space = 0;

for (int i = 4; i < (length-4); i++)
{
    if(line[i] ==  ' ')
    {
        second_space = i;
    }
}

//Check the request target
int request_end= second_space - 1;

int counter =0;

for (int i = 5; i < request_end; i++)
{
    if (line[i] == '.')
    {
        counter += 1;
    }

    if (line[i] == '\"')
    {
        error(400);
        continue;
    }
}

if (counter==0)
{
    error(501);
    continue;
}

//Check the http protocol
int http_start= second_space +1;

char protocol[9];
protocol[8]= '\0';

for (int i = http_start; i < http_start + 8; i++)
{
    protocol[(i-http_start)] = line[i];
}

char* http= "HTTP/1.1";

if (strcmp(protocol, http) != 0)
{
    error(505);
    continue;
}

//  extract query from request-target

//Find the length of the request
//Subtract position of second space with first space, then add 1 to account for null terminator
int request_length = second_space - 4;

char request[request_length];
request[request_length] = '\0';

strncpy(request, line + 4, second_space - 4 + 1);

//create variable that will keep track of start of query
int index = 0;

//find start of query
for (int i = 0; i < request_length; i++)
{
    if (request[i] == '?')
    {
        index = i;
    }
}

char query[request_length -index +1];
//if there is no query start then set size of query to one byte
if (index == 0)
{
    query[0] = '\0';
}

//copy the query from line to query
//for third param request_length-1 -index +1

strncpy(query, request+index, request_length - index);

//concatenate root and absolute-path

//create variable to hold absolute path
char path[index+1];

//copy absolute path from request to absolute path
strncpy(path, request + 0, index - 0 + 1);
path[index+1]= '\0';

strcat(root, path);

//ensure path exists

//Man page for access http://linux.die.net/man/2/access

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

// ensure path is readable

//Man page for access http://linux.die.net/man/2/access

if (access(path, R_OK) == -1)
{
    error(403);
    continue;
}

// extract path's extension
int path_length = strlen(path);
int dot_index = 0;

for (int i = 0; i < path_length; i++)
{
    if (path[i] == '.')
    {
        dot_index = i;
    }
}

//set extension initially to null
char extension[path_length-dot_index + 2];

strncpy(extension, path + dot_index + 1, path_length - (dot_index + 1) + 1);

extension[path_length-dot_index+2] = '\0';

1 Answer 1

0

Your code is correct, but not so sure about the rest of your implementation. If for example a string example_file.php is passed, the extension string will hold .php and not just php. So later in your code you should compare extension with .php and .html, not just php or html. Or even better and probably easier, make extension not hold the '.' too. That is the only bug I can see that could cause the 501.

Edit after comments

I'm not gonna solve this problem for you, I'm just going to point you to where it is. I rerun your code adding some printf()s after some basic variables had been set and this is what I got:

For a command of

GET /cat.html HTTP/1.1

your variables have the following values: (I added #'s to make whitespace visible)

GET /cat.html HTTP/1.1
request == #/cat.html #
query == #/cat.html#
path == #/#
extension == ##
Got outside on third 501!
HTTP/1.1 501 Not Implemented

For a command of

GET /cat.html?q=alice HTTP/1.1

your variables have the following values:

GET /cat.html?q=alice HTTP/1.1
request == #/cat.html?q=alice ���� #
query == #?q=alice�#
HTTP/1.1 404 Not Found

where � are unicode characters that are not represented in ASCII so they are definitely not supposed to be in any place in your output. Also see that in some variables like request there is an extra space after the string itself. Also query and path are totally wrong.

Here is how the output should look like instead:

GET /cat.html HTTP/1.1
request == #/cat.html#
query == ##
path == #/cat.html#
extension == #html#
HTTP/1.1 200 OK

and

GET /cat.html?q=alice HTTP/1.1
request == #/cat.html?q=alice#
query == #q=alice#
path == #/cat.html#
extension == #html#
HTTP/1.1 200 OK

See what you have different/wrong, use some printf()s yourself to see the contents of your variables and fix your problems. If I told you once again what the problem is, you would never learn. Re-read the instructions of how to validate the request line and try to understand what each variable should contain.

And just to answer your question, you get the 501 because your extension variable is "", (empty string), and thus lookup() returns NULL.


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

14
  • As I said it would be better to change the implementation of your extension and not the comparisons or lookup. Revert your changes and change just the extension. If it still doesn't work then post more code of how you validate the line.
    – ChrisG
    Commented Sep 17, 2015 at 18:56
  • when I added printf() functions myself.. i did not see anything.. well more work hahah Commented Sep 18, 2015 at 7:41
  • Did you re-compile your program after you added the printf()s?
    – ChrisG
    Commented Sep 18, 2015 at 7:42
  • of course but still i was not able to see something. I also tried gdb but nothing.. anyway thnx now I have a framework to work with Commented Sep 18, 2015 at 8:12
  • btw i forgot to ask yesterday. How can unicode characters be present in my variables. How where they created Commented Sep 19, 2015 at 9:12

You must log in to answer this question.

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