:) 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 1 :( 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 \ 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 404 Not Found\r\nContent-Type:..."
:( Requesting two files in a row (cat.html then cat.jpg) succeeds https://sandbox.cs50.net/checks/acc428f7e55d4fb3903909a5dc69b732
const char* lookup(const char* path)
{
// TODO
char A[6];
char* p=strrchr(path,'.');
int i=0;
for ( i=0;p[i]!='\0';i++)
{
A[i]=p[i];
}
A[i]='\0';
if (p!=NULL)
{
if (strcmp(A,".css")==0)
{
return "text/css";
}
else if (strcmp(A,".html")==0)
{
return "text/html";
}
else if (strcmp(A,".gif")==0)
{
return "image/gif";
}
else if (strcmp(A,".ico")==0)
{
return "image/x-icon";
}
else if (strcmp(A,".jpg")==0)
{
return "image/jpeg";
}
else if (strcmp(A,".js")==0)
{
return "text/javascript";
}
else if (strcmp(A,".js")==0)
{
return "text/javascript";
}
else if (strcmp(A,".php")==0)
{
return "text/x-php";
}
else if (strcmp(A,".png")==0)
{
return "image/png";
}
}
return NULL;
}
bool parse(const char* line, char* abs_path, char* query)
{
// TODO
char A[5];
char B[LimitRequestLine + 1];
char C[LimitRequestLine + 1];
int i=0,j=0,len;
char* p=strchr(line,' ');
char* n=strrchr(line,' ');
char* q=strstr(line," ");
for (i=0;line[i]!=' ';i++)
{
A[i]=line[i];
}
A[i]='\0';
if (strcmp(A,"GET")!=0)
{
error(405);
return false;
}
for (i=1,j=0;p[i]!=' ';i++,j++)
{
B[j]=p[i];
}
B[j]='\0';
len=j;
if (B[0]!='/')
{
error(501);
return false;
}
for (j=0;B[j]!='\0';j++)
{
if (B[j]=='\"')
{
error(400);
return false;
}
}
for (i=1,j=0;n[i]!='\r';i++,j++)
{
C[j]=n[i];
}
C[j]='\0';
if (strcmp(C,"HTTP/1.1")!=0)
{
error(505);
return false;
}
char* exist=strchr(B,'.');
if (exist==NULL)
{
error(404);
return false;
}
if (q!=NULL)
{
error(505);
return false;
}
char* k=strchr(line,'?');
if (k==NULL || k[1]==' ')
{
strcpy(abs_path,B);
strcpy(query,"");
query[0]='\0';
}
/*else if (k[1]==' ')
{
//strcpy(abs_path,B);
strcpy(query,"");
query[0]='\0';
}
*/
else if (k[1]=='q')
{
//if query present,remove query from abs_path
for (i=1,j=0;k[i]!=' ';i++,j++)
{
query[j]=k[i];
}
query[j]='\0';
char path[LimitRequestLine + 1];
strncpy(path,B,len-j-1);
path[len-j]='\0';
strcpy(abs_path,path);
}
return true;
}
bool load(FILE* file, BYTE** content, size_t* length)
{
// TODO
int i=0;
char* buffer=malloc(1000*sizeof(char));
if (buffer==NULL)
{
free(buffer);
return false;
}
char ch;
while (true)
{
ch=fgetc(file);
if (ch==EOF)
{
break;
}
buffer[i]=ch;
i=i+1;
if (i%1000==0)
{
//extending buffer if it exceeds 1000 bytes
buffer=(char*)realloc(buffer,sizeof(char)*(i+1000));
if (buffer==NULL)
{
free(buffer);
return false;
}
}
}
//reallocating finally to the exact size
buffer=(char*)realloc(buffer,sizeof(char)*i);
*content=buffer;
*length=i;
return true;
}
char* indexes(const char* path)
{
// TODO
char* ch1=malloc(strlen(path)+12);
if (ch1==NULL)
{
free(ch1);
return NULL;
}
char* op1="/index.php";
char* ch2=malloc(strlen(path)+12);
if (ch2==NULL)
{
free(ch2);
return NULL;
}
char* op2="/index.html";
strcpy(ch1,path);
strcat(ch1,op1);
strcpy(ch2,path);
strcat(ch2,op2);
if (access(ch1, R_OK | X_OK) == 0)
{
return ch1;
}
else
{
free(ch1);
}
if (access(ch2, R_OK | X_OK) == 0)
{
return ch2;
}
else
{
free(ch2);
}
return NULL;
}