0

I am running check50 and 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 :( 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 :( Requesting hello.php returns 200, text/html, and correct output

What I don't understand is that when I go through all the pages from the browser everything works fine with no errors but check50 says otherwise. Unfortunately I have been so busy at work I haven't looked at this for 2 months and its hard to find my mistakes. Could anyone possibly provide some help?

lookup

const char* lookup(const char* path)
{

// Convert the extension to lower case
char lowerCasePath[strlen(path) + 1];
for (int i = 0; i <= strlen(path); i++){
    lowerCasePath[i] = tolower(path[i]);
}


if ( strcmp(lowerCasePath, "css") == 0)
{
    return "text/css";
}

else if ( strcmp(lowerCasePath, "html") == 0)
{
    return "text/html";
}

else if ( strcmp(lowerCasePath, "gif") == 0)
{
    return "image/gif";
}

else if ( strcmp(lowerCasePath, "ico") == 0)
{
    return "image/x-icon";
}

else if ( strcmp(lowerCasePath, "jpg") == 0)
{
    return "image/jpeg";
}

else if ( strcmp(lowerCasePath, "js") == 0)
{
    return "text/javascript";
}

else if ( strcmp(lowerCasePath, "php") == 0)
{
    return "text/x-php";
}

else if ( strcmp(lowerCasePath, "png") == 0)
{
    return "image/png";
}


return NULL;
}

Parse

bool parse(const char* line, char* abs_path, char* query)
{
//parse request line: method SP request-target SP HTTP-version CRLF

// GET /cat.jpg HTTP/1.1\r\n

char method[4];
const char* space1 = strstr(line, " ");         //remove string section from first space
int size_method = space1 - line;                //size in bytes of method string

strncpy(method, line, size_method);
method[3] = '\0';



if ( strncmp(method, "GET", 3) != 0 )
{
    error(405);
    return false;
}

space1 = space1 + 1;                            //move the pointer past the first space
const char* space2 = strstr(space1, " ");       //remove string section at second space
int size_request = space2 - space1;         //size in bytes of request string with additional space at the start
char request[size_request+1];

strncpy(request, space1, size_request);
request[size_request] = '\0';

//printf("<%s>\n", request);

if ( strncmp(request, "/", 1) != 0)
{
    error(501);
    return false;
}

char* quotation_mark;
quotation_mark = strstr(request, "\"");
if ( quotation_mark != NULL )
{
    error(400);
    return false;
}

space2 = space2 + 1;
char* CRLF = strstr(space2, "\r");
int size_http = CRLF - space2;
char http[size_http + 1];

strncpy(http, space2, size_http);
http[size_http] = '\0';

//printf("<%s>\n", http);

if ( strcmp(http, "HTTP/1.1") != 0 )
{
    error(505);
    return false;
}

//point to the request

strcpy(abs_path, request);

char question[LimitRequestLine + 1];

char* qp = strstr(request, "?");       //point to query

if (qp == NULL)
{
    question[0] = '\0';
    strcpy(query, question);
}
else
{
    qp = qp + 1;                    
    strcpy(query, qp);                   //copy the entire pointer into question

}

return true;
}

Load

bool load(FILE* file, BYTE** content, size_t* length)
{

char* buffer = malloc(sizeof(char));

if( buffer == NULL ) 
{
    return false; 
}
int k = 0;
char c;

// read file
while (!feof(file))
{
    c = fgetc(file);

    //stores in dynamically allocated memory
    buffer[k] = (char) c;    
    k++;
    buffer = (char*) realloc(buffer, sizeof(char) * (k+1)); 
}

*content = buffer; // address of first byte
*length = k;       // stores the number of bytes

//printf("Data: %s\n", *content); 
//printf("Length: %zu\n", *length);  
return true;

}
0

The browser is very forgiving, what's one byte more or less? check50, on the other hand, is very precise. There is an extra byte in your content (and length) because of this usage:
while (!feof(file)). After the last character in the file is processed, the loop will do one additional read (fgetc) before the EOF is detected. Therefore one extra byte is processed. Week 4-> Shorts -> File I/O (from around the 7:30 mark) is a better explanation and explains a better approach.

| improve this answer | |
  • Thanks so much for your input. That is a valid point and I made the necessary changes but still getting the same response from check50. I am going to take another look this weekend, thanks again! – S.L.Blom May 27 '16 at 12:48

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