0

My Server runs fine when I use it, I have also resolved the valgrind issues so there are no memory leaks as well. However my check50 fails on the second test, I see a few errors. enter image description here

I sometimes also see the "Unexpected end of input" error on the same check even if I don't change the code

PARSE FUNCTION

bool parse(const char* line, char* abs_path, char* query)
{

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

LOOKUP Function

 const char* lookup(const char* path)
{

//search for the file extension in path
char* ext;
ext = strrchr(path,'.');
if (ext==NULL) 
{
    return NULL;
}
//dont need to convert to lowercase  using strcasecmp

//determine the content type of file through the extension. 

    if ((strcasecmp(ext,".css"))==0)
    {
        const char* content = "text/css";
         return content;

    }
    else if ((strcasecmp(ext,".html")) ==0)
    {
       const char* content ="text/html";
       return content;


    }
    else if ((strcasecmp(ext,".php")) ==0)
    {
        const char* content = "text/x-php";
        return content;


    }
    else if ((strcasecmp(ext,".js")) ==0)
    {
       const char* content ="text/javascript";
      return content;

    }
    else if ((strcasecmp(ext,".gif")) ==0)
    {
        const char* content ="image/gif";
        return content;


    }
     else if ((strcasecmp(ext,".png")) ==0)
    {
        const char* content ="image/png";
        return content;


    }

    else if ((strcasecmp(ext,".ico")) ==0)
    {
        const char* content ="image/x-icon";
        return content;


    }
    else if ((strcasecmp(ext,".jpg")) ==0)
    {
        const char* content ="image/jpeg";
        return content;

    }
    else 
    {

        return NULL;
    }





}

LOAD Function

bool load(FILE* file, BYTE** content, size_t* length)
{
  BYTE* block = malloc(sizeof(BYTE)); 
  if (block == NULL)
  {
     return false;
  }
 int size = 0;
  for (int c = fgetc(file) ; c!=EOF ; c = fgetc(file)) //reading chars one  by one from file
  {
     block [size] = c;
     size++;
     block = realloc(block,(size+1)); //increasing size of block by 1 for the next iteration

  }
  block[size]='\0'; //adding a null terminator at the end of the contents of the file 
 *length = size+1; // and not strlen(block) because that would just work for   TEXT files and not IMAGE files
 *content = block;  //you cant do content = &block i.e. u cant change content but u can chnge what it points to
  return true;
}

INDEXES Function

char* indexes(const char* path)
{

    struct dirent** namelist = NULL;
    char* index = "index.php";
   char* html = "index.html";
   //memory allocation for new path including index.php or index.html
   char* new_path = malloc(sizeof(char)*(strlen(path)+strlen(html) + 10));
   strcpy(new_path,path);

   //scanning a directory and checking the entries one by one   http://pubs.opengroup.org/onlinepubs/9699919799/functions/alphasort.html
  int n = scandir(path, &namelist,NULL, alphasort);
  for (int i = 0; i < n; i++)
  {
    // check if the list contains index.html or index.php
     if ( (strcmp((namelist[i]->d_name),index)==0) )
     {
          strcat(new_path,"/index.php");
           free(namelist[i]);
           free(namelist);
           return new_path;
     }
     else if  ((strcmp((namelist[i]->d_name),html)==0))
     {
         strcat(new_path,"/index.html");
         free(namelist[i]);
         free(namelist);
         return new_path;
     }
     else
     {
         free(namelist[i]); //namelist stores the entries of directory
     }


  }
 free(namelist);
 free(new_path);
 return NULL;

}
1

Remember, the browser is very forgiving. check50 is very precise. If one pixel was "wrong" in cat.jpg, neither the browser nor the human eye would detect it. check50, on the other hand, is looking for an exact "copy" of cat.jpg. This load function is modifying at least one byte in each file it processes right here block[size]='\0'; block is not "string" data, regardless of whether content is text or image. Don't forget to consider length when you make correction(s). gdb might prove a valuable tool.

Some (unsolicited) comments on indexes. First: is it necessary to look at all the files in path to determine whether index.php|html exist? Consider how you might use access() to accomplish this task faster and with much less memory and disk access. The use of scandir is creating a memory leak (maybe that's why check50 gives "..end of input" message?). Since you return from the function when you find a match, it will not do the necessary and appropriate freeing of the remaining namelist nodes.

The "...end of input" message has often been attributed to a check50 server problem. Since there's one day left for honor certificate, perhaps check50 server is exceptionally busy. Conventional wisdom suggests you should try again.

1
  • 1
    Thank you! All I did was remove the null terminator and changed the length, and it worked. I didn't change the Indexes though because Valgrind was showing me the green flag i.e No memory leakage possible. Actually someone on Facebook told me my content wasn't Null terminated so I added the \0 at the end, I might have misinterpreted or something but its working now. Thanks again. And yeah, it was the check50. It runs great now May 1 '16 at 20:42

You must log in to answer this question.

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