0

:) 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;

}

  • When i used check50 1st case ie lookup & parse then there were all green ticks but when i used check50 for the whole prog i got the following errors. – mohit chattlani Aug 25 '16 at 15:30
2

lookup

:( Requesting cat2.HTML returns 200, text/html, and correct file
:( Requesting cat3.HtMl returns 200, text/html, and correct file

from the spec
text/html for any file whose path ends in .html (or any capitalization thereof),
. . .
Odds are you’ll find functions like strcasecmp, strcpy, and/or strrchr of help!

"any capitalization thereof" means "no matter what the case is". Read the man pages for strcmp and strcasecmp and understand the difference.

indexes

:( 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:..."
from the spec
if index.php actually exists therein,
from man access
The mode specifies the accessibility check(s) to be performed, and is either the value F_OK, or a mask consisting of the bitwise OR of one or more of R_OK, W_OK, and X_OK. F_OK tests for the existence of the file. R_OK, W_OK, and X_OK test whether the file exists and grants read, write, and execute permissions, respectively.

These errors should direct your attention immediately to indexes, because they seem to be testing exactly what was required of indexes. I suspect your indexes is failing because of this (access(ch1, R_OK | X_OK). This tests for read and execute permission. The spec only asks to check for existence. access has a "shortcut" for that.

load

:( Requesting cat.jpg returns 200, image/jpeg, and correct image
:( Requesting cat.gif returns 200, image/gif, and correct file

These errors directed my attention to load because they are image files. One problem in load is "false eof" because of this char ch; declaration. You will find the whys and wherefores here (in the "load" section). (This also provokes the "two files in a row" because the cat.jpg load fails).

parse

:( 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

First this message \ expected output, not an exit code of 0 indicates a seg fault. Since it happens in the two places where a query is requested, it should direct your attention to parse where you build query. Well, actually, you are not ever building query! WAT????? Follow your code from here else if (k[1]=='q') with request line hello.php?name=Alice from check50 to discover why. You could do it "on paper", or use gdb or printfs to help troubleshoot.

This may not be a complete list of problems with server. You would find gdb an excellent troublshooting partner. Some tips here.

| improve this answer | |
  • Thank you, I still cannot correct my indexes fn() I replaced R_OK /X_OK by F_OK to check for file existence per the spec but it still does not work – mohit chattlani Aug 27 '16 at 14:34

You must log in to answer this question.

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