I have been stuck on pset6 for almost a month now and I've tried everything to debug it but I absolutely cannot find what I'm doing wrong... It's probably because I've looked through it so many time that I think what I'm getting too used to my own code so I am hoping someone can help me take a look at it. I've ran the first check and what I'm getting is this :
:) server.c exists
:) server compiles
:) HTTP/1.0 returns error code 505
:) Method of 'abcGET' returns error code 405
:) Method of 'GETabc' returns error code 405
:) request-target without starting '/' returns error code 501
:) request-target of abc/hello.php returns error code 501
:( Requesting cat.exe returns error code 501
\ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:( Requesting non-existant file returns error code 404
\ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:) Requesting request-target with " returns error code 400
:) Two spaces after GET returns error code
:) A space within the request target returns error code
:) Two spaces before HTTP/1.1 returns error code
And here's my code:
indexes:
char* indexes(const char* path)
{
// TODO
if (path != NULL) {
char* storage = malloc(sizeof(char*));
storage = strstr (path,"index.php");
if (storage != NULL)
{
return storage = "/path/to/a/directory/index.php";
}
else{
storage = strstr (path,"index.html");
if (storage != NULL)
{
return storage = "/path/to/a/directory/index.html";
}
if (storage == NULL)
{
return NULL;
}
}
free(storage);
}
return NULL;
}
Load:
bool load(FILE* file, BYTE** content, size_t* length)
{
char*buffer =malloc(sizeof *file);
fread ( buffer, sizeof (buffer), BYTES, file);
*content = &buffer[0];
*length = sizeof (buffer);
return false;
}
Lookup:
const char* lookup(const char* path)
{
// TODO
if(path!= NULL)
{
if (strstr (path, ".css")==0)
{
return ".text/css";
}
if (strstr (path, ".html")==0)
{
return ".text/html";
}
if (strstr (path, ".gif")==0)
{
return ".image/gif";
}
if (strstr (path, ".ico")==0)
{
return ".image/x-icon";
}
if (strstr (path, ".jpg")==0)
{
return ".image/jpeg";
}
if (strstr (path, ".php")==0)
{
return ".text/x-php";
}
if (strstr (path, ".js")==0)
{
return ".text/javascript";
}
if (strstr (path, ".png")==0)
{
return ".image/png";
}
}
return NULL;
}
Parse:
#define Method 0
#define Request_Target 1
#define Http_Version 2
bool parse(const char* line, char* abs_path, char* query)
{
// TODO
char* parse_line = malloc(sizeof(line));
strcpy (parse_line,line);
char* Copy_parse_line[3] = { NULL, NULL, NULL};
int i = 0;
Copy_parse_line[Method] = strtok(parse_line," ");
while( Copy_parse_line[i] != NULL && i<2)
{
i++;
Copy_parse_line[i] = strtok(NULL," ");
}
strcpy ( abs_path,Copy_parse_line[Request_Target]);
if (strcmp (Copy_parse_line[Method],"GET")!=0)
{
error(405);
return false;
}
if (strncmp(Copy_parse_line[Request_Target], "/",1) !=0)
{
error(501);
return false;
}
if (strchr ( Copy_parse_line[Request_Target] , '"' )!=NULL)
{
error(400);
return false;
}
if (strcmp (Copy_parse_line[Http_Version] , "HTTP/1.1")!=0)
{
error(505);
return false;
}
if (strchr (Copy_parse_line[Request_Target],'?')==NULL)
{
query="";
}
else{
char* query_start = strstr(Copy_parse_line[Request_Target],"?");
if (query_start)
{
query = query_start + 1;
}
}
return NULL;
}
Thank you so much,
Leon