0

Working on this problem set for many days, but now couldn't identify in which function lies the issue. Using check50 2015.fall.pset6.server2 server.c I get the following output :

~/workspace/pset6 $ check50 2015.fall.pset6.server2 server.c
:) server.c exists
:) server compiles
:( Requesting cat.jpg returns 200, image/jpeg, and correct image
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting cat.html returns 200, text/html, and correct file
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting cat2.HTML returns 200, text/html, and correct file
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting cat3.HtMl returns 200, text/html, and correct file
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting cat.gif returns 200, image/gif, and correct file
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting favicon.ico returns 200, image/x-icon, and correct file
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting test.css returns 200, text/css, and correct file
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting test.js returns 200, text/javascript, and correct file
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:) Requesting hello.php returns 200, text/html, and correct output
:) Requesting hello.php? returns 200, text/html, and correct output
:) Requesting hello.php?name=Alice returns 200, text/html, and correct output
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting directory containing index.php outputs index.php
   \ expected output, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting two files in a row (cat.html then cat.jpg) succeeds
   \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
https://sandbox.cs50.net/checks/fbda548953624a8c95ef3bba508d544a

So, there is a standard error of some backtrace for all the checks except a few. I would like to know what is causing this error. Relevant parts of my code are as follows :

Indexes function :

char* indexes(const char* path)
{
    // TODO

    char* mypath1=malloc(sizeof(char));
    char* mypath2=malloc(sizeof(char));
    strcpy(mypath1,path);
    strcpy(mypath2,path);
    strcat(mypath1,"/index.php");
    strcat(mypath2,"/index.html");

    FILE *f1 = fopen(mypath1, "r");
    FILE *f2 = fopen(mypath2, "r");
    if(f1==NULL&&f2==NULL)
    {
    fclose(f1);
    fclose(f2);
    return NULL;
    }
    else 
    {
        if(f1!=NULL)
        {
            fclose(f1);
            fclose(f2);
            return mypath1;
        }
        else if(f2!=NULL)
        {
            fclose(f1);
            fclose(f2);
            return mypath2;
        }
    }
    return NULL;
}

Load Function :

bool load(FILE* file, BYTE** content, size_t* length)
{
    // TODO
    long int i;
    fseek(file,0,SEEK_END);
    const long int len=ftell(file);
    fseek(file,0,SEEK_SET);
    BYTE* buffer[len];
    for(i=0;i<len;i++)
    buffer[i]=malloc(sizeof(BYTE));
    while(fread(buffer,1,1,file) == 1)
    {
        /*if(feof(file))
        break;*/
    }
    *content=buffer[0];
    *length=len;
    return true;
}

Lookup function :

const char* lookup(const char* path)
{
    // TODO
    char *ext=strrchr(path,'.');
    char *ret=malloc(sizeof(char));
    if(strcasecmp(ext,".css")==0)
    {
        strcpy(ret,"text/css");
        return ret;
    }
    else if(strcasecmp(ext,".html")==0)
    {
        strcpy(ret,"text/html");
        return ret;
    }
    else if(strcasecmp(ext,".gif")==0)
    {
        strcpy(ret,"image/gif");
        return ret;
    }
    else if(strcasecmp(ext,".ico")==0)
    {
        strcpy(ret,"image/x-icon");
        return ret;
    }
    else if(strcasecmp(ext,".jpg")==0)
    {
        strcpy(ret,"image/jpeg");
        return ret;
    }
    else if(strcasecmp(ext,".js")==0)
    {
        strcpy(ret,"text/javascript");
        return ret;
    }
    else if(strcasecmp(ext,".php")==0)
    {
        strcpy(ret,"text/x-php");
        return ret;
    }
    else if(strcasecmp(ext,".png")==0)
    {
        strcpy(ret,"image/png");
        return ret;
    }
    else
    return NULL;
}

Parse Function :

bool parse(const char* line, char* abs_path, char* query)
{
    // TODO
    int k=0,l=0,i,flag=0;
    const long len=strlen(line);
    char mystr1[len],mystr2[len],mystr3[len],ab[len],que[len];
    while(line[k]!=32)
    {
        mystr1[l++]=line[k++];
    }
    mystr1[l]='\0';
    if(strcmp(mystr1,"GET")!=0)
    {
        error(405);
        return false;
    }
    l=0;
    k++;
    if(line[k]!='/')
    {
        error(501);
        return false;
    }
    while(line[k]!=32)
    {
        mystr2[l++]=line[k++];
    }
    mystr2[l]='\0';
    for(i=0;i<strlen(mystr2);i++)
    {
        if(mystr2[i]==34)
        {
            error(400);
            return false;
        }
    }
    l=0;
    k++;
    while(line[k]!='\r')
    {
        mystr3[l++]=line[k++];
    }
    mystr3[l]='\0';
    if(strcmp(mystr3,"HTTP/1.1")!=0)
    {
        error(505);
        return false;
    }
    l=0;
    k=0;
    while(mystr2[k]!=63&&mystr2[k]!='\0')
    {
        ab[l++]=mystr2[k++];
    }
    ab[l]='\0';
    l=0;
    if(mystr2[k]!='\0')
    {
        k++;
        while(mystr2[k]!='\0')
        {
            que[l++]=mystr2[k++];
        }
        que[l]='\0';
        flag=1;
    }
    strcpy(abs_path,ab);
    if(flag==1)
    strcpy(query,que);
    else
    strcpy(query,"");

    return true;
}

Please do help!!

If there are any queries regarding how the code works please comment.

1

The Backtrace message is a seg fault, which leads me to believe you did not send requests to server before you sent it to check50. It is always a good idea to test your program before you let someone (or something!) else check it. You want to be relatively certain it works before you submit it to your boss or a customer for instance.

The heart of your problem is probably here *content=buffer[0];. You are setting a pointer (*content) to a character (buffer[0]). You might want to read up on memcpy (man memcpy). That is not the only solution.

You have some allocation issues, which may or may not contribute to the failures, but they sure are memory leaks. In indexes, you allocate mypath1 and 2 for one byte, and then proceed to stuff a whole bunch of stuff in there. It is very easy to calculate the exact allocation you need for those vars (think strlen). Don't forget to allocate room for the null-byte. Ditto ret in lookup. But why create another variable at all? Consider simply returning the appropriate string literal.

Once you fix some "things", you are bound to run into other "things". Time to break out gdb. You'll find some starter info here.

You must log in to answer this question.

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