0

I am trying to check my code with check50 and I am getting a bunch of errors:

:) server.c exists :( server compiles \ expected output, not standard error of "server4.c: In function 'parse':\nserver..." :| Requesting cat.jpg returns 200, image/jpeg, and correct image \ can't check until a frown turns upside down :| Requesting cat.html returns 200, text/html, and correct file \ can't check until a frown turns upside down :| Requesting cat2.HTML returns 200, text/html, and correct file \ can't check until a frown turns upside down :| Requesting cat3.HtMl returns 200, text/html, and correct file \ can't check until a frown turns upside down :| Requesting cat.gif returns 200, image/gif, and correct file \ can't check until a frown turns upside down :| Requesting favicon.ico returns 200, image/x-icon, and correct file \ can't check until a frown turns upside down :| Requesting test.css returns 200, text/css, and correct file \ can't check until a frown turns upside down :| Requesting test.js returns 200, text/javascript, and correct file \ can't check until a frown turns upside down :| Requesting hello.php returns 200, text/html, and correct output \ can't check until a frown turns upside down :| Requesting hello.php? returns 200, text/html, and correct output \ can't check until a frown turns upside down :| Requesting hello.php?name=Alice returns 200, text/html, and correct output \ can't check until a frown turns upside down :| Requesting /test redirects to /test/ \ can't check until a frown turns upside down :| Requesting /test/ outputs /test/index.html \ can't check until a frown turns upside down :| Requesting directory containing index.php outputs index.php \ can't check until a frown turns upside down :| Requesting two files in a row (cat.html then cat.jpg) succeeds \ can't check until a frown turns upside down https://sandbox.cs50.net/checks/57aac34a37ab4169a77395cfca4d8596

My code for indexes is:

char* indexes(const char* path) { // TODO //malloc space to contain the directory path, index file name and the null char* buffer = malloc(sizeof(char) * (strlen(path) + strlen("/index.html") + 1)); strcpy(buffer, path); strcat(buffer,"/index.php");

FILE* fp = (fopen(buffer, "r"));
if (fp != NULL)
{
    return buffer;
}
strcpy(buffer, path);
strcat(buffer,"/index.html");

if (fp != NULL)
{
    return buffer;
}
free(buffer);

return NULL;

}

My load code:

bool load(FILE* file, BYTE** content, size_t* length) { // TODO if (*content == NULL) { return false; }

char* buffer = malloc(sizeof(char));

if(buffer == NULL) 
{
    return false; 
}

int counter = 0;
int c = fgetc(file);

while(c != EOF)
{

    buffer[counter] = (char) c;
    counter++;
    buffer = (char*) realloc(buffer, counter+1);
    c = fgetc(file);

*content = buffer;
*length = counter;
printf("Data: %s\n", *content);
printf("Length: %zu\n", *length);
return true;

}

My lookup code:

const char* lookup(const char* path) { //TODO // find the last 'dot' char* dot = strrchr(path,'.');

//if path is not null, then
if (path != NULL)
{
    // text/css for any file whose path ends in .css
    if (strcasecmp(dot,".css") == 0)
    {
        return "text/css";
    }

    // text/html for any file whose path ends in .html
    if (strcasecmp(dot,".html") == 0)
    {
        return "text/html";
    }

    // image/gif for any file whose path ends in .gif
    if (strcasecmp(dot,".gif") == 0)
    {
        return "image/gif";
    }

    // image/x-icon for any file whose path ends in .ico
    if (strcasecmp(dot,".ico") == 0)
    {
        return "image/x-icon";
    }

    // image/jpeg for any file whose path ends in .jpg
    if (strcasecmp(dot,".jpg") == 0)
    {
        return "image/jpeg";
    }

    // text/javascript for any file whose path ends in .js
    if (strcasecmp(dot,".js") == 0)
    {
        return "text/javascript";
    }

    // text/x-php for any file whose path ends in .php
    if (strcasecmp(dot,".php") == 0)
    {
        return "text/x-php";
    }

    // image/png for any file whose path ends in .png
    if (strcasecmp(dot,".png") == 0)
    {
        return "image/png";
    }
}

// null otherwise
return NULL;

}

My parse code:

bool parse(const char* line, char* abs_path, char* query) { // TODO // 2 SP in the request-line char* SP = strchr(line,' ');

// if there are no spaces (SP), then respond to the browser 400 Bad Request and return false
if (SP == NULL)
{
    error(400);
    return false; 
}

int i = 0;
while (space != NULL)
{
     if (i == 0)
     {

        // if request-target does not begin with /, then respond to the browser 501 Not Implemented and return false
        if (strncmp(" /", SP, 2) != 0)
        {
            error(501);
            return false;
        }

        // if request-target contains ", then respond to the browser 400 Bad Request and return false
        if (strchr(SP, '"') != NULL && (strchr(SP+1,' ') != NULL && strchr(strchr(SP+1,' '), '"') != NULL ))
        {
            error(400);
            return false;
        }

       int n = strlen(SP+1) - strlen(strchr(SP+1,' '));
       char numq[n + 1];

       strncpy(numq, SP+1, n);
       numq[n] = '\0';

       if (strchr(numq, '?') != NULL)
       {

         char* numq3 = strstr(numq, ".php" );

        // if absolute-path is followed by ? without a query then respond to the browser with 400 Bad Request and return false
         if (strncmp(".php?", numq3, 5) !=0)
         {
              error(400);
              return false;
         }

         // store query in char* query
        strcpy(query, (strchr(numq, '?' ) + 1));
        printf("Query = %s\n",query);

       }   

        //if there is a php file type without query, then set query to " "
        else if (strstr(numq, ".php") != NULL)
          {
              strcpy(query, (strstr(numq, ".php" ) + 1));
              printf("Query = %s\n",query);
          }
     }

    if (i == 1) 
    {
         // if there is no HTTP after 2nd SP, then respond to the browser with 400 Bad Request and return false
        if (strncmp(" HTTP", SP, 5) != 0)
        {
        error(400);
        return false;
        }

        // if there is no CRLF, then respond to the browser with 400 Bad Request and return false
        if (strstr(SP,"\r\n\0") == NULL)
        {
           error(400);
            return false;
        }

        // store absolut-path at the address in abs_path
        int p = strlen(strchr(line,' ') + 1) - strlen(SP);
        strncpy(abs_path, strchr(line,' ') + 1, p);
        abs_path[p] = '\0';
        printf("Abs_path = %s\n", abs_path);

    }

    i++;
    SP = strchr(SP+1, ' ');
}


//if the number SP is not two, then respond to the browser with 400 Bad Request and return false
if (i != 2)
{
    error(400);
    return false; 
}


// if method is not GET, then respond to the browser with 405 Method not allowed and return false
if (strncmp("GET ", line, 4) != 0)
{
    error(405);
    return false;
}

// otherwise return true
return true;    

}

Can you please help me??

  • I had debugged it and I fixed some error. Now my server.c is compiled and I will try to fix some other errors!!! Thank you!!! – edxnewbie Apr 13 '16 at 15:09
  • Here's a hint for posting code: select your code block, then click the '{}' icon in the edit pane header. – DinoCoderSaurus Apr 13 '16 at 16:28

You must log in to answer this question.

Browse other questions tagged .