0

I can't figure out how to even get the / list to show up in the browser. I believe the reason based on printf statements comes from the fact that lookup is returning null. However, when the path is /home/ubuntu/workspace/pset6/public/ to start off with it, shouldn't it be returning null? However, the fact that the first request returns null gives the 501 error. I can't figure out how to not get the first path to show null?

  const char* lookup(const char* path)
{
// TODO
int path_length = strlen(path);
printf("%s", path);
char ending[6];
for(int i = 0; i < path_length; i++)
{
    if(path[i]=='.')
    {
        for(int j = 0; j < 6; j++)
        {
            if(i < path_length)
            {
                ending[j] = path[i];
                i++;
            }
            else
            {
                ending[j] = '\0';
                break;
            }

        }
    }

}
if(strcasecmp(ending, ".css") == 0)
{
    const char* mime = "text/css";
    return mime;
}
if(strcasecmp(ending, ".html") == 0)
{
    const char* mime = "text/html";
    return mime;
}
if(strcasecmp(ending, ".gif") == 0)
{
    const char* mime = "image/gif";
    return mime;
}
if(strcasecmp(ending, ".ico") == 0)
{
    const char* mime = "image/x-icon";
    return mime;
}
if(strcasecmp(ending, ".jpg") == 0)
{
    const char* mime = "image/jpeg";
    return mime;
}
if(strcasecmp(ending, ".js") == 0)
{
    const char* mime = "text/javascript";
    return mime;
}
if(strcasecmp(ending, ".phm") == 0)
{
    const char* mime = "text/x-php";
    return mime;
}
if(strcasecmp(ending, ".png") == 0)
{
    const char* mime = "image/png";
    return mime;
}

return NULL;
}
1
  • The line: if ( i < path_length) is a problem since it always passes. Therefore you will never get to the else statement. It is written inside a loop where i is defined to be less than path_length. Have you tried for (int i = 0; i =< path_length; i++) instead of (int i = 0; i < path_length; i++) Go through it with gdb debugger to solve this. – Jacob Hamfeldt Kold Oct 4 '16 at 16:33
0

If your printf statement (I only see one) is showing null, then the path that's being passed to lookup must be null. When you start server, are you giving it a valid path to use? What is being done to the path before it is passed to lookup? I don't think the problem is in lookup(). More likely, it's a code problem earlier on, or in the way that you are invoking the server program itself.

This may not be an answer, but it should get you going in the right direction.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I think I'm giving it a valid path. For implementing the staff solution, I just use public. However, when I use ./server public it's not displaying in the window. ./server public doesn't seem to be requesting anything other than GET / HTTP/1.1. However, this looks like the staff solution as well. However, when it's GET / - that means the path doesn't have any .html/.css, etc. So, lookup returns null. Should it not be returning null for public? – LauraK10 Mar 27 '16 at 19:22
0

The line: if ( i < path_length) is a problem since it always passes. Therefore you will never get to the else statement. It is written inside a loop where i is defined to be less than path_length by definition. Have you tried for (int i = 0; i =< path_length; i++) instead of (int i = 0; i < path_length; i++) I believe you need the equal sign since you need an extra character for the NULL terminator. Try going through it in gdb and if fx your ending needs to be .css then print out ending[3] to make sure there is a null character after .css in ending. Otherwise I dont think strcasecmp will get it right.

I hope I haven't written anything wrong since I am still fairly new to programming relative to many other people.

You must log in to answer this question.

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