0

I have pasted my four codes for server.c... I am getting the following two errors on running the final check50:

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

can anyone tell where is the problem in my code

char* indexes(const char* path)
{
    // TODO
        char* ind_php = "/index.php";
        char* ind_html = "/index.html";

        char* inx = malloc(sizeof (strlen(path) + strlen(ind_html) + 1));
        strcpy(inx, path);
        strcat(inx, ind_php);

        if (access(inx, F_OK)==0)
        {
             return inx;
        }


        strcpy(inx, path);
        strcat(inx, ind_html);

        if (access(inx, F_OK))
        {
           return inx;
        }

     free(inx);
     return NULL;
}  

.

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

    int i = 0;
    // read file
    for (int c = fgetc(file); c != EOF; c = fgetc(file))
    {
        data[i] = (char) c;    // stores in dynamically allocated memory
        i++;
        data = (char*) realloc(data, sizeof(char) * (i+1)); // increase buffer
    }

    *content = data; // stores the address of the first of those bytes
    *length = i;     // stores the number of bytes
    return true;
}

.

const char* lookup(const char* path)
{
    // TODO
    char* c = strrchr(path, '.');

    if (strcasecmp(c, ".css") == 0) 
    {
        return "text/css";
    }
    if (strcasecmp(c, ".html") == 0) 
    {
        return "text/html";
    }
    if (strcasecmp(c, ".gif") == 0) 
    {
        return "image/gif";
    }
    if (strcasecmp(c, ".ico") == 0) 
    {
        return "image/x-icon";
    }
    if (strcasecmp(c, ".jpg") == 0) 
    {
        return "image/jpeg";
    }
    if (strcasecmp(c, ".js") == 0) 
    {
        return "text/javascript";
    }
    if (strcasecmp(c, ".php") == 0) 
    {
        return "text/x-php";
    }
    if (strcasecmp(c, ".png") == 0) 
    {
        return "image/png";
    }
    return NULL;
}

.

/**
 * Parses a request-line, storing its absolute-path at abs_path 
 * and its query string at query, both of which are assumed
 * to be at least of length LimitRequestLine + 1.
 */
bool parse(const char* line, char* abs_path, char* query)
{
    // TODO
   int counter=0; //for the number of spaces. 

   //method not supported
   if (strncasecmp(line,"GET ",4) !=0)
  {
    error(405);
    return false;
  }

  //path does not begin with a '/' NOT IMPLEMENTED
 if (line[4]!='/')
 {
  error(501);
  return false;
  }

 char* after_get = strchr(line,'/'); //from reqst target



//version not supported
if ((strstr(after_get," HTTP/1.1\r\n")) == NULL) // one space after GET    checked , the second one before HTTP
{
  error(505);
  return false;
 }

 //making sure that the space detected right above was the only one in      after_get 
for (int i=0;i<strlen(after_get);i++) 
{
   if (after_get[i]==' ') counter++;
}


 //if spaces are not 1
 if ((counter!=1))  
 {
    error(400);
    return false;
 }
 //request path must not contain a "
 if ((strchr(after_get,'"'))!=NULL)
 {
    error(400);
    return false;
  }


 //transferring the contents to abs_path and the query string
 int i=0, c=0 , k =0;

while (after_get[i]!=' ')
{
 if (after_get[i]=='?')
 {
    i++;
    while (after_get[i]!=' ')
    {
       query[k]=after_get[i];
       i++;k++;
     }

  }

 else 
 {
    abs_path[c]=after_get[i];
    i++;
    c++;
  }
 }
  abs_path[c]='\0';  //null terminating the query and abs path
  query[k] = '\0';
  return true;

}
1

Some guidelines. Look at this:

char* inx = malloc(sizeof (strlen(path) + strlen(ind_html) + 1));

This entire expression is an int: (strlen(path) + strlen(ind_html) + 1)

Can you tell what is the sizeof an int?

And as a side note:

You don't need the slash / here:

char* ind_php = "/index.php";
char* ind_html = "/index.html"; 

You can use just "index.php" and "index.html", because earlier in the code of server.c, the staff makes sure the path ALWAYS gets to the indexes function with a / as the last character. Check these line of server.c:

// redirect from absolute-path to absolute-path/
if (abs_path[strlen(abs_path) - 1] != '/')
{
    char uri[strlen(abs_path) + 1 + 1];
    strcpy(uri, abs_path);
    strcat(uri, "/");
    redirect(uri);
    continue;
}

You must log in to answer this question.

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