server.c is giving error(403) inside transfer function (written by default)
lookup function :
const char* lookup(const char* path)
{
char* s=(char*)calloc(20,1);
strcpy(s,"");
char *path1=(char*)calloc((strlen(path)+1),1);
for(int i=0;i<strlen(path);i++)
{
path1[i]=tolower(path[i]);
}
printf("path1 : %s",path1);
for(int i=0;i<strlen(path1);i++)
{
if(path1[i]=='.')
{
s[0]=path1[i];
s[1]=path1[i+1];
s[2]=path1[i+2];
s[3]='\0';
break;
}
}
// 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,"image/gif");
return(s);
}if(!strcmp(s,".ic"))
{
strcpy(s,"image/x-icon");
return(s);
}if(!strcmp(s,".jp"))
{
strcpy(s,"image/.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,"image/png");
return(s);
}
return NULL;
}
parse function :
bool parse(const char* line, char* abs_path, char* query)
{
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]!='/')
{
printf("*Here");
error(501);
return false;
}
// int f=0;
for(int i=0;i<strlen(line);i++)
{
int flag=0,j=0,q=0;
if(line[i]==' ')
{
flag=0;
q++;
}
if(line[i]=='/')
{
flag=1;
}
if(flag)
{
abs_path[j]=line[i];
j++;
}
if(q==2)
{
abs_path[j]='\0';
break;
}
if(line[i]=='?')
{
abs_path[j]='\0';
j=0;
for(int e=i+1;line[e]!=' ';e++)
{
query[j]=line[e];
j++;
}
query[j]='\0';
break;
}
}
int f=0;
for(int i=0;i<strlen(line);i++)
{
if(line[i]==' ')
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')
{
f++;
printf("%c %c %c %c %c %c %c %c",line[i+1],line[i+2],line[i+3],line[i+4],line[i+5],line[i+6],line[i+7],line[i+8]);
error(505);
return false;
}
else
break;
}
}
return true;
}
My valgrind is giving this error :
==6796== Conditional jump or move depends on uninitialised value(s)
==6796== at 0x4C2E0F8: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6796== by 0x4029EA: urldecode (server.c:1178)
==6796== by 0x4018A3: main (server.c:191)
==6796==
==6796== Conditional jump or move depends on uninitialised value(s)
==6796== at 0x4C2E0F8: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6796== by 0x402A32: urldecode (server.c:1186)
==6796== by 0x4018A3: main (server.c:191)
==6796==
==6796== Conditional jump or move depends on uninitialised value(s)
==6796== at 0x4C2E0F8: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6796== by 0x4019EE: main (server.c:221)
==6796==
I can't figure out the exact problem but I think it is somewhere related to abs_path
and urldecode
function. Please Help