When running server.c, and requesting a site, I get this output
Using /home/ubuntu/workspace/pset6/public for server's root
Listening on port 8080
GET / HTTP/1.1
HTTP/1.1 501 Not Implemented
*** Error in `/home/ubuntu/workspace/pset6/server': free(): invalid pointer: 0x00007fffffff9dc0 ***
Could anybody explain to me what is going on? Thanks a lot!
Here is my code:
lookup:
const char* lookup(const char* path)
{
// TODO
char* fileEx = strrchr(path, '.');
if (fileEx != NULL) {
if(strcasecmp(fileEx, ".css") == 0) {
return "text/css";
}
else if (strcasecmp(fileEx, ".html") == 0) {
return "text/html";
}
else if (strcasecmp(fileEx, ".gif") == 0) {
return "image/gif";
}
else if (strcasecmp(fileEx, ".ico") == 0) {
return "image/x-icon";
}
else if (strcasecmp(fileEx, ".jpg") == 0) {
return "image/jpeg";
}
else if (strcasecmp(fileEx, ".js") == 0) {
return "text/javascript";
}
else if (strcasecmp(fileEx, ".php") == 0) {
return "text/x-php";
}
else if (strcasecmp(fileEx, ".png") == 0) {
return "image/png";
}
}
return NULL;
}
parse:
bool parse(const char* line, char* abs_path, char* query)
{
// TODO
if(strstr(line, "GET") == NULL) {
error(405);
return false;
}
if (line[4] != '/') {
error(501);
return false;
}
int placeInPath = 0;
int currentPlaceInLine = 4;
bool queryRequest = false;
for (int i = 4; line[i] != ' '; i++) {
if (line[i] == '?') {
queryRequest = true;
currentPlaceInLine = i;
break;
}
abs_path[placeInPath] = line[i];
placeInPath++;
currentPlaceInLine = i;
}
int placeInQuery = 0;
if (queryRequest) {
for(int j = currentPlaceInLine+1; line[j] != ' '; j++) {
if (line[j] == '"') {
error(400);
return false;
}
query[placeInQuery] = line[j];
currentPlaceInLine = j;
placeInQuery++;
}
query[placeInQuery+1] = '\0';
}
else {
query[0] = '\0';
}
if(strstr(line, "HTTP/1.1") == NULL) {
error(505);
return false;
}
return true;
}
load:
bool load(FILE* file, BYTE** content, size_t* length)
{
// TODO
*length = 0;
*content = NULL;
fseek(file, 0L, SEEK_END);
unsigned long sz = ftell(file);
fseek(file, 0L, SEEK_SET);
BYTE* loaded = malloc(sizeof(BYTE)*sz);
fread(loaded, sz, 1, file);
content = &loaded;
*length = sz;
return true;
}
indexes:
char* indexes(const char* path)
{
// TODO
int lenOfPath = strlen(path);
char newPath[lenOfPath];
char* indexPHP = strcat(newPath, "index.php");
char* indexHTML = strcat(newPath, "index.html");
if(access(indexPHP, F_OK)) {
return indexPHP;
}
if(access(indexHTML, F_OK)) {
return indexHTML;
}
else {
return NULL;
}
}