I can't find why this doesn't work, can someon help me? Thanks.
My code fails the following in check50 server1:
:( Requesting cat.exe returns error code 501
\ expected output, but not "HTTP/1.1 404 Not Found\r\nContent-Type:..."
https://sandbox.cs50.net/checks/9d5b4521abb5459bbc4c59f3905e5c5b
And the following in check50 server2:
:( 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
\ expected output, not an exit code of 0
:( Requesting hello.php?name=Alice returns 200, text/html, and correct output
\ expected output, not an exit code of 0
:( 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 301 Moved Permanently\r\nLocat..."
:( Requesting two files in a row (cat.html then cat.jpg) succeeds
https://sandbox.cs50.net/checks/ebfe53568033401a8385c1927e979dbc
My code is as follows:
INDEX
char* indexes(const char* path)
{
char *php_file_dir = malloc(sizeof(path) + sizeof("index.php"));
char *html_file_dir = malloc(sizeof(path) + sizeof("index.html"));
strcat(php_file_dir, path);
strcat(php_file_dir, "index.php\0");
strcat(html_file_dir, path);
strcat(html_file_dir, "index.html\0");
FILE *php_file = fopen(php_file_dir, "r");;
FILE *html_file = fopen(html_file_dir, "r");
if (php_file != NULL)
{
free(html_file_dir);
fclose(php_file);
fclose(html_file);
return php_file_dir;
}
if (html_file != NULL)
{
free(php_file_dir);
fclose(php_file);
fclose(html_file);
return html_file_dir;
}
return NULL;
}
LOAD
bool load(FILE* file, BYTE** content, size_t* length)
{
if (file == NULL)
{
return false;
}
fseek(file, 0l, SEEK_END);
size_t size = ftell(file);
fseek(file, 0l, SEEK_SET);
*length = size;
*content = malloc(size);
if (*content == NULL)
{
return false;
}
for (int i = 0; i < size; i++)
{
fread(content, 1, size, file);
}
if (*length != size)
{
return false;
}
return true;
}
LOOKUP
const char* lookup(const char* path)
{
char *f_end = strrchr(path, '.');
if (strcasecmp(f_end, ".css") == 0)
{
return "text/css";
}
if (strcasecmp(f_end, ".html") == 0)
{
return "text/html";
}
if (strcasecmp(f_end, ".gif") == 0)
{
return "image/gif";
}
if (strcasecmp(f_end, ".ico") == 0)
{
return "image/x-icon";
}
if (strcasecmp(f_end, ".jpg") == 0)
{
return "image/jpeg";
}
if (strcasecmp(f_end, ".js") == 0)
{
return "text/javascript";
}
if (strcasecmp(f_end, ".php") == 0)
{
return "text/x-php";
}
if (strcasecmp(f_end, ".png") == 0)
{
return "image/png";
}
return NULL;
}
PARSE
bool parse(const char* line, char* abs_path, char* query)
{
// Checks if method is GET
if (strncmp(line, "GET ", 4) != 0)
{
error(405);
return false;
}
// To point at the start of the request instead of the first space
char *req_pos = strchr(line, ' ');
req_pos += 1;
// To point at the start of the version instead of the second space
char *v_pos = strrchr(line, ' ');
v_pos += 1;
char *request = malloc(v_pos - req_pos);
strncpy(request, req_pos, (v_pos - req_pos - 1));
char *version = malloc( &(line[strlen(line) - 1]) - v_pos);
strncpy(version, v_pos, &(line[strlen(line) - 1]) - v_pos);
request[strlen(request) - 1] = '\0';
if ( (strstr(line, " ")) || (strchr(request, ' ')) || (strchr(version, ' ')) )
{
error(400);
free(request);
free(version);
return false;
}
if (request[0] != '/')
{
error(501);
free(request);
free(version);
return false;
}
if (strncmp(version, "HTTP/1.1", 8) != 0)
{
error(505);
free(request);
free(version);
return false;
}
if (strchr(request, '"') != NULL)
{
error(400);
free(request);
free(version);
return false;
}
if (strchr(request, '?') == NULL)
{
strcpy(query, "");
strcpy(abs_path, request);
free(request);
free(version);
return true;
}
if (strchr(request, '?') != NULL)
{
if (request[strlen(request) - 2] == '?')
{
request[strlen(request) - 2] = '\0';
strcpy(query, "");
strcpy(abs_path, request);
free(request);
free(version);
return true;
}
// To start query after the "?"
char *q_pos = strchr(request, '?');
q_pos += 1;
strcpy(query, q_pos);
free(request);
free(version);
return true;
}
free(request);
free(version);
error(501);
return false;
}