I have almost completed lookup() and parse() function but check50(version 1) shows :(
in 2 cases. Below is my check50 result.
:) 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
https://sandbox.cs50.net/checks/d90422604d084bfab5778dc2017d7212
Please help me figuring out the error. This is my lookup() function for reference.
const char* lookup(const char* path)
{
char* s=(char*)malloc(15);
strcpy(s,"");
int flag=0,j=0;
char path1[strlen(path)+1];
for(int i=0;i<strlen(path);i++)
{
path1[i]=tolower(path[i]);
}
for(int i=0;i<strlen(path);i++)
{
if(path1[i]=='.')
flag=1;
if(flag)
{
if(j==3)
break;
s[j]=path1[i];
j++;
}
}
s[3]='\0';
if(!strcmp(s,".cs"))
{
strcpy(s,"text/css");
return(s);
}
if(!strcmp(s,".ht"))
{
strcpy(s,"text/html");
return(s);
}if(!strcmp(s,".gi"))
{
strcpy(s,"text/gif");
return(s);
}if(!strcmp(s,".ic"))
{
strcpy(s,"text/x-icon");
return(s);
}if(!strcmp(s,".jp"))
{
strcpy(s,"text/.jpeg");
return(s);
}if(!strcmp(s,".js"))
{
strcpy(s,"text/javascript");
return(s);
}if(!strcmp(s,".ph"))
{
strcpy(s,"text/x-php");
return(s);
}if(!strcmp(s,".pn"))
{
strcpy(s,"text/png");
return(s);
}
return NULL;
}
And this is my parse() function.
bool parse(const char* line, char* abs_path, char* query)
{
// char s[strlen(line)];
for(int i=0;i<strlen(line);i++)
{
if(line[i]==34)
{
error(400);
return false;
}
}
if(line[0]!='G'||line[1]!='E'||line[2]!='T'||line[3]!=32)
{
error(405);
return false;
}
if(line[4]!='/')
{
error(501);
return false;
}
int f=0;
for(int i=0;i<strlen(line);i++)
{
if(line[i]==32)
f++;
if(f==2)
{
if(line[i+1]!='H'||line[i+2]!='T'||line[i+3]!='T'||line[i+4]!='P'||line[i+5]!='/'||line[i+6]!='1'||line[i+7]!='.'||line[i+8]!='1')
{
error(505);
return false;
}
}
}
for(int i=0;i<strlen(line);i++)
{
int flag=0,flag1=0,j=0;
if(line[i]=='/')
{
flag=1;
}
if(flag)
{
abs_path[j]=line[i];
j++;
}
if(line[i]=='?'||line[i]==' ')
{
abs_path[j-1]='\0';
j=0;
if(line[i]==' ')
break;
i++;
flag=0;
flag1=1;
}
if(flag1)
{
query[j]=line[i];
j++;
if(line[i]==' ')
break;
}
}
return true;
}