I fixed some issues in this but I am still getting errors and can't figure out what is causing them.
:( Requesting cat.exe returns error code 501
Expecting the following on standard out ... 501 NOT IMPLEMENTED
... but received the following on standard out instead —
HTTP/1.1 200 OK
Content-Type: image/png
I ran cat.exe through GDB and didn't get what this check suggests.
:( Two spaces after GET returns error code
I'm not sure whether I am meant to search for multiple spaces then return error because I didn't see this on the specs or whether something else is causing this.
:( Requesting /test/ outputs /test/index.html
When running /test/ it seems to return the path to index.html. It says "todo".
:( Requesting directory containing index.php outputs index.php
Not at all sure what I need to do to fix this.
INDEXES
char* indexes(const char* path)
{
char* temp_path = malloc(strlen(path) + 1);
memcpy(temp_path, path, strlen(path) + 1);
char* php_index = "/index.php";
char* html_index = "/index.html";
char* php_path = malloc(strlen(path) + strlen(php_index) + 1);
char* html_path = malloc(strlen(path) + strlen(html_index) + 1);
memcpy(php_path, temp_path, strlen(path) + 1);
memcpy(html_path, temp_path, strlen(path) + 1);
strcat(php_path, php_index);
strcat(html_path, html_index);
if(access(php_path, F_OK) != -1)
{
return php_path;
}
else if(access(html_path, F_OK) != -1)
{
return html_path;
}
else
{
return NULL;
}
}
LOAD
bool load(FILE* file, BYTE** content, size_t* length)
{
if(file == NULL)
{
return false;
}
int expand = 2;
char* storage = malloc(sizeof(char) * expand); // Begin with a small amount of memory.
int c;
int byte_count = 0;
while(( c = fgetc(file)) != EOF) // Get one char at a time from file.
{
storage[byte_count] = c; // Store contents of file in storage.
byte_count++;
if(byte_count == expand - 1) // Expand exponentially if we only have one byte of memory left.
{
expand *= expand;
storage = realloc(storage, sizeof(char) * expand);
}
}
storage = realloc(storage, (sizeof(char) * byte_count) + 1); // Realloc correct size once reading is complete.
*content = storage; // Store pointer to
*length = byte_count; // Store number of bytes
return true;
}
LOOKUP
const char* lookup(const char* path)
{
char extension[6];
char* css = ".css";
char* html = ".html";
char* gif = ".gif";
char* xicon = ".ico";
char* jpeg = ".jpg";
char* java = ".js";
char* php = ".php";
char* png = ".png";
char* location = strrchr(path, '.');
int i = 0;
while(location[i] != '\0')
{
extension[i] = location[i];
i++;
}
if(strcasecmp(css, extension) == 0)
{
return "text/css";
}
else if(strcasecmp(html, extension) == 0)
{
return "text/html";
}
else if(strcasecmp(gif, extension) == 0)
{
return "image/gif";
}
else if(strcasecmp(xicon, extension) == 0)
{
return "image/x-icon";
}
else if(strcasecmp(jpeg, extension) == 0)
{
return "image/jpeg";
}
else if(strcasecmp(java, extension) == 0)
{
return "text/javascript";
}
else if(strcasecmp(php, extension) == 0)
{
return "text/x-php";
}
else if(strcasecmp(png, extension))
{
return "image/png";
}
else
{
return NULL;
}
}
PARSE
bool parse(const char* line, char* abs_path, char* query)
{
char line_copy[LimitRequestLine + 1];
strcpy(line_copy, line); // Copy line for manipulation
char* method = strtok(line_copy, " "); // Parse at first space to get method
if(strcmp(method, "GET") != 0) // Check method is 'GET'. If not return error.
{
error(405);
return false;
}
char* req_target = strtok(NULL, " "); // Parse at second space to get abs_path
if(req_target[0] != '/')
{
error(501);
return false;
}
if(strchr(req_target, '"') != NULL)
{
error(400);
return false;
}
char* version = strtok(NULL, "");
if(strncmp(version, "HTTP/1.1", 8) != 0)
{
error(505);
return false;
}
if(strchr(req_target, '?') != NULL)
{
char* temp = strtok(req_target, "?");
strcpy(abs_path, temp);
temp = strtok(NULL, "");
if(temp != NULL)
{
strcpy(query, temp);
}
}
else
{
strcpy(abs_path, req_target);
query = "";
}
return true;
}