0

Here are my implemented functions

const char* lookup(const char* path)
{   
char ext[6];
int j = 0;
for(int i=0,n=strlen(path);i<n;i++)
{
    if(path[i]=='.')
    {
        ext[j] = path[i];
        j++;
    }
    else if(j>0)
    {
        ext[j] = path[i];
        j++;
    }

}
ext[j+1] ='\0';
if(strcmp(ext,".css") == 0)
return "text/css";
else if(strcmp(ext,".html") == 0)
return "text/html";
else if(strcmp(ext,".jpg") == 0)
return "image/jpeg";
else if(strcmp(ext,".gif") == 0)
return "image/gif";
else if(strcmp(ext,".js") == 0)
return "text/javascript";
else if(strcmp(ext,".php") == 0)
return "text/x-php";
else if(strcmp(ext,".png") == 0)
return "image/png";
else if(strcmp(ext,".ico") == 0)
return "image/x-icon";
else
return NULL;
}

bool parse(const char* line, char* abs_path, char* query)
{
char method[5];
strncpy(method,line,4);
method[4]='\0';
if(strcmp(method,"GET ")!=0)
{
    error(405);
    return false;
}
int i = 4;
int j = 0;
if(line[i]!='/')
{
    error(501);
    return false;
}
if(line[i+1]==' ')
i++;
else if(line[i+1]!=' ')
{
while(line[i]!='\n' || line[i]!='?')
{
    if(line[i]=='\"' || line[i]==' ' )
    {
        error(400);
        return false;
    }
    abs_path[j] = line[i];
    j++;
    i++;
}

if(line[i]=='?' && line[i+1]!=' ')
{   
    int u = 0;
    while(line[i]!=' ')
    {
      query[u] = line[i];
      u++;
      i++;
    }
}
}
char* http = malloc(sizeof(char)*10);
int u = 0;
while(line[i]!='\r')
{
    http[u] = line[i];
    u++;
    i++;
}
http[u+1]='\0'; 
if(strcmp(http," HTTP/1.1")!=0)
{
    error(505);
    return false;
}


return true;
}
bool load(FILE* file, BYTE** content, size_t* length)
{   
char buffer[1];
char* buffer2 = NULL;
buffer2 = malloc(sizeof(char));
int i = 2;
while(!feof(file))
{   
    fread(buffer,1,1,file);
    buffer2 = realloc(*content,sizeof(char)*i);
    i++;
}
fseek(file,0,SEEK_SET);
while(feof(file))
{
    fread(buffer2,1,i,file);

}
fclose(file);
return true;
}
char* indexes(const char* path)
{
char* c = NULL;
char* u = strcpy(c,path);
char* r = "index.html";
char* s = "index.php";
char* b = strcat(u,r);
FILE* file = fopen(b,"r");
if(file==NULL)
return NULL;
else
{
    fclose(file);
    return b;
}
char* n = strcat(u,s);
FILE* file2 = fopen(n,"r");
if(file2==NULL)
return NULL;
else
{
    fclose(file2);
    return n;
}
}
0

Some things to know:

  • a 301 (generally) comes when abs_path is blank.
  • When you invoke Web Server from the CS50 IDE tab, the request-line to server is
    GET / HTTP/1.1\r\n.

Now, let's take that call through the parse function starting here if(line[i+1]==' '):

i = 4. What is line[5]? It is blank. So i is incremented and the else clause is not executed; control goes to this line if(line[i]=='?' && line[i+1]!=' '). abs_path is never populated.

Use the tools. You will likely find debug50 and/or gdb very helpful while troubleshooting the server functions. There may be other problems in these functions, it is worth the time to learn how to use a debugger for this and all future programming projects.

1
  • Could you explain me what does load function does in this program? I could not wrap my head around it. – Ranjan Shrestha Dec 17 '16 at 14:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .