0

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;
}

1 Answer 1

1

All things being equal, the only place that server returns a 505 is the parse function. If this parse function reaches for(int i=0;i<strlen(line);i++), it will always return a 505. Howzat? Assuming a correct HTTP version, when you reach the second space, f == 2, this line if(line[i+1]!='H'||.... is true. What happens next? i is incremented, line[i] is not a space, f is still 2 , if(line[i+1]!='H'||.... is false and program reports error(505) and returns false.

4
  • Okay, I have modified value of 'f' inside that block but it is still showing same result. Is there some other error in code?
    – Salil Jain
    Commented Jun 23, 2016 at 16:45
  • Also, Is there a way to check only lookup() function?
    – Salil Jain
    Commented Jun 23, 2016 at 16:50
  • One problem in lookup: strcmp returns integer not boolean. There may be other errors in the code. If you are still getting the '505 not supported', there is still a problem in that routine. gdb is the best troubleshooting tool. Commented Jun 23, 2016 at 19:14
  • even if strcmp returns int not bool my condition is correct though as strcmp returns 0 when both strings are same.
    – Salil Jain
    Commented Jun 24, 2016 at 8:40

You must log in to answer this question.

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