Like the tittle says everything works perfectly when i use but check comes up with this. any help would be appreciated
Check50
:) 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
:( Requesting hello.php? returns 200, text/html, and correct output
\ expected output, not an exit code of 0
:) Requesting hello.php?name=Alice returns 200, text/html, and correct output
:) 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
https://sandbox.cs50.net/checks/d0ce46c0ae8b43ae9202d9ba25044f1b
My testing
GET / HTTP/1.1
HTTP/1.1 200 OK
GET / HTTP/1.1
HTTP/1.1 200 OK
GET /cat.html HTTP/1.1
HTTP/1.1 200 OK
GET /cat.jpg HTTP/1.1
HTTP/1.1 200 OK
GET /cat.jpg HTTP/1.1
HTTP/1.1 200 OK
GET /favicon.ico HTTP/1.1
HTTP/1.1 200 OK
GET /hello.html HTTP/1.1
HTTP/1.1 200 OK
GET /hello.php HTTP/1.1
HTTP/1.1 200 OK
GET /hello.html HTTP/1.1
HTTP/1.1 200 OK
GET /hello.php?name=Dan HTTP/1.1
HTTP/1.1 200 OK
GET /test/ HTTP/1.1
HTTP/1.1 200 OK
GET /test/index.html HTTP/1.1
HTTP/1.1 200 OK
Parse
bool parse(const char* line, char* abs_path, char* query)
{
// Boolean to check for query
bool isQuery = false;
// Store method in
char method[LimitRequestLine];
// To iterate over line
int i = 0, j = 0;
// Iterate over method
while (line[i] != ' ')
{
method[j] = line[i];
i++;
j++;
}
// Move past whitespace charactor
i++;
// Check that method is GET
if(strcmp(method, "GET") != 0)
{
error(405);
return false;
}
// Temporary storage
char chr;
// For char* position
j = 0;
// Get absolute path, and query if there is one
while(line[i] != ' ')
{
chr = line[i];
// If there is a Query
if(chr == '?')
{
// Null terminate path
abs_path[j] = '\0';
// Move past ?
i++;
// Reset counter
j = 0;
while(line[i] != ' ')
{
chr = line[i];
query[j] = chr;
i++;
j++;
isQuery = true;
}
// Null terminate
query[j] = '\0';
break;
}
if(chr == '"')
{
error(400);
return false;
}
// Add char to abs_path
abs_path[j] = chr;
i++;
j++;
}
if(strchr(line, '"') != NULL)
{
error(400);
return false;
}
// Check for query, if not null terminate query and path
if(!isQuery)
{
// Null terminate path
abs_path[j] = '\0';
// Null terminate Query
query[0] = '\0';
}
// Move past whitespace charactor
i++;
if(abs_path[0] != '/')
{
error(501);
return false;
}
// Hold HTTP version
char HTTP_ver[LimitRequestLine];
// Reset counter
j = 0;
// Get HTTP version
while(line[i] != '\r')
{
HTTP_ver[j] = line[i];
i++;
j++;
}
// Null terminate
HTTP_ver[j] = '\0';
// Check if correct version
if(strcmp(HTTP_ver, "HTTP/1.1") != 0)
{
error(505);
return false;
}
// Must be valid
return true;
}
Lookup
const char* lookup(const char* path)
{
//temporary strorage to hold type
char type[10];
int j = 0;
// Find dot in string
for(int i = 0, len = strlen(path); i < len; i ++)
{
if(path[i] == '.')
{
// Get type
i++;
while(i < len)
{
type[j] = tolower(path[i]);
i++;
j++;
}
type[j] = '\0';
}
}
// return correct type
if(strcmp(type, "css") == 0)
{
return "text/css";
}
else if(strcmp(type, "html") == 0)
{
return "text/html";
}
else if(strcmp(type, "gif") == 0)
{
return "image/gif";
}
else if(strcmp(type, "ico") == 0)
{
return "image/x-icon";
}
else if(strcmp(type, "jpg") == 0)
{
return "image/jpeg";
}
else if(strcmp(type, "js") == 0)
{
return "text/javascript";
}
else if(strcmp(type, "php") == 0)
{
return "text/x-php";
}
else if(strcmp(type, "png") == 0)
{
return "image/png";
}
else
{
return NULL;
}
}
Load
bool load(FILE* file, BYTE** content, size_t* length)
{
// Variable to hold data
char* data = malloc(sizeof(BYTE) + 1);
if(data == NULL)
{
return false;
}
int size = 1, i = 0;
// Iterate through file
for(int c = fgetc(file); !feof(file); c = fgetc(file))
{
data = realloc(data, size * sizeof(BYTE));
if(data == NULL)
{
free(data);
return false;
}
data[i] = (char) c;
size++;
i++;
}
// Be sure content is pointing in the right place
*content = data;
*length = size;
return true;
}
indexes
char* indexes(const char* path)
{
int p_len = strlen(path);
char* indexPHP = malloc(p_len + 11);
strcpy(indexPHP, path);
strcat(indexPHP, "index.php/");
char* indexHTML = malloc(p_len + 12);
strcpy(indexHTML, path);
strcat(indexHTML, "index.html/");
if(access(indexPHP, F_OK) == 0)
{
return indexPHP;
}
if(access(indexHTML, F_OK) == 0)
{
return indexHTML;
}
// Else
return NULL;
}