0

EDIT Done with server.c and pset6! Thanks to everyone that helped me! EDIT

I really need some help understanding what my issue is with server.c. I have been working on this for a few days and cannot resolve my problems. When I run check50 I get these errors:

:) server.c exists

:) server compiles

:( Requesting cat.jpg returns 200, image/jpeg, and correct image

:( Requesting cat.html returns 200, text/html, and correct file

:( Requesting cat2.HTML returns 200, text/html, and correct file

:( Requesting cat3.HtMl returns 200, text/html, and correct file
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:( Requesting cat.gif returns 200, image/gif, and correct file

:( Requesting favicon.ico returns 200, image/x-icon, and correct file

:) Requesting test.css returns 200, text/css, and correct file

:( Requesting test.js returns 200, text/javascript, and correct file
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:) Requesting hello.php returns 200, text/html, and correct output

:) Requesting hello.php? returns 200, text/html, and correct output

:( Requesting hello.php?name=Alice returns 200, text/html, and correct 
output
   \ expected output, but not "X-Powered-By: PHP/5.5.7\r\nContent-type..."

:) Requesting /test redirects to /test/

:( Requesting /test/ outputs /test/index.html

:( Requesting directory containing index.php outputs index.php
   \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."

:( Requesting two files in a row (cat.html then cat.jpg) succeeds

Here is my code:

Lookup:

//extension variable
char* extension = strrchr(path, '.');

//if extension isnt null
if(extension != NULL)
{
    //compare extensions
    if(strcasecmp(extension, ".css") == 0)
    {
        return "text/css";   
    }
    else if(strcasecmp(extension, ".html") == 0)
    {
        return "text/html";
    }
    else if(strcasecmp(extension, ".gif") == 0)
    {
        return "image/gif";
    }
    else if(strcasecmp(extension, ".ico") == 0)
    {
        return "image/x-icon";
    }
    else if(strcasecmp(extension, ".jpg") == 0)
    {
        return "image/jpeg";
    }
    else if(strcasecmp(extension, ".js") == 0)
    {
        return "text/javascript";
    }
    else if(strcasecmp(extension, ".php") == 0)
    {
        return "text/x-php";
    }
    else if(strcasecmp(extension, ".png") == 0)
    {
        return "image/png";
    }
}
return NULL;

Parse:

//ensure HTTP method is GET
if (line[0] != 'G' || line[1] != 'E' || line[2] != 'T' || line[3] != ' ')
{
    error(405);
    return false;
}

//ensure line only has two spaces
int spaces = 0;
for (int i = 0, j = strlen(line); i < j; i++)
{
    if (line[i] == ' ')
    {
        spaces++;
    }

    if (spaces > 2)
    {
        error(400);
        return false; 
    }
}

//ensure request target starts with /
char* RT = strchr(line, ' ');
if (RT[1] != '/')
{
    error(501);
    return false;
}

//throw an error at user if RT has "
for (int i = 0; RT[i + 1] != ' '; i++)
{
    if (RT[i + 1] == '"')
    {
        error(400);
        return false;
    }
}

//iterate over line and store request target in abs_path
int count = 1;
while (RT[count] != ' ' && RT[count] != '?')
{
    abs_path[count - 1] = RT[count];
    abs_path[count] = '\0';
    count++;
}

//check if request target has a query
int qcount = 0;
if (RT[count] == '?')
{
    if (RT[count + 1] == ' ')
    {
        query[0] = '\0';
        count++;
    }
    else
    {
        count++;
        while (RT[count] != ' ')
        {
            query[qcount] = RT[count];
            query[qcount + 1] = '\0';
            count++;
        }
    }

}

//ensure HTTP version is version 1.1
if (RT[count + 1] != 'H' || RT[count + 2] != 'T' || RT[count + 3] != 'T' || RT[count + 4] != 'P' || RT[count + 5] != '/' || RT[count + 6] != '1' || RT[count + 7] != '.' || RT[count + 8] != '1')
{
    error(505);
    return false;
}

return true;

Load:

if (file == NULL)
{
    return false;
}

//create buffer
char* buffer = malloc(sizeof(file));
int c;

//keep count of bytes
int count = 0;

//iterate over file storing in buffer
while ((c = fgetc(file)) != EOF)
{
    buffer[count] = (char) c;
    count++;
}

//put buffer in content and count into length
*content = buffer;
*length = count;

return true;

Indexes:

//variables for index
char* html = "index.html";
char* php = "index.php";

//create a copy of path
char* copy = malloc(1000);

for (int i = 0, j = strlen(path); i < j; i++)
{
    copy[i] = path[i];
}

if (copy[strlen(copy - 1)] != '/')
{
    strcat(copy, "/");
}

//concatenate copy with index.html and index.php
char* html_index = strcat(copy, html);
char* php_index = strcat(copy, php);

//check if index.html or index.php actually exists
if (access(html_index, F_OK) == 0)
{
    free(copy);
    return html_index;
}
else if (access(php_index, F_OK) == 0)
{
    free(copy);
    return php_index;
}
else
{
    free(copy);
    return NULL;
}

Any help would be appreciated! Thanks!

2
  • Do you get that Backtrace message every time you run check50? Can you send me your full server.c file so that I can look into it?
    – Rob Bowden
    Jul 27 '16 at 5:19
  • I am kind of new to this website so I am not sure how to send a private message. Would you like me to email it to you? Thanks!
    – E. Sawyers
    Jul 27 '16 at 10:48
1

This line if (copy[strlen(copy - 1)] != '/') might be causing problems in your indexes function. It looks wrong in any case. Look carefully at your parentheses.

Your variable copy is not properly null-terminated, so there's no telling what the strcats will do. You might want to review the man page for calloc. [As an aside, the allocation of copy for 1000 bytes is very rando. You could get a more realistic approximation of the maximum length it will be. How about the length of path plus the length of index.html (the longer of the two) plus the pesky null-byte.]

Also, review the spec for indexes

given a /path/to/a/directory, returns /path/to/a/directory/index.php if index.php actually exists therein, or /path/to/a/directory/index.html if index.html actually exists therein, or NULL.

Does your code do what the spec requires? Order is significant.

You must log in to answer this question.

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