Skip to main content

Pset6 problem in indexes and load function.

Check 50 returns a lot of errors https://sandbox.cs50.net/checks/caefd03d8ac2485b820743724297b846 My load function

            /**
             * Loads a file into memory dynamically allocated on heap.
             * Stores address thereof in *content and length thereof in *length.
             */
            bool load(FILE* file, BYTE** content, size_t* length)
            {
                if(file == NULL)
                    {
                        return false;
                    }
                //Allocate memory on heap
                BYTE* buffer = malloc(sizeof(BYTE)*1000);
                *content = NULL;
                int  count = 0;
                int c;
                
                //Read characters from file
               while((c=fgetc(file))!= EOF)
                {
                    if( count%1000 == 0)
                        {
                            buffer = (BYTE* )realloc(buffer, sizeof(BYTE)*(1000 +count));
                        }
                    count++;
                *content = buffer    ;
                }
                *length = count;
            return true;
            }    
    
    My indexes function
    
    /**
     * Checks, in order, whether index.php or index.html exists inside of path.
     * Returns path to first match if so, else NULL.
     */
    char* indexes(const char* path)
    {
     char* php = "/index.php";
     char* html = "/index.html";
     char* path2 = NULL;
     
    strcpy(path2,path);
     
     //Check if path to php exists
     if(access(path2, F_OK) == 1)
        {    
            strcat(path2,php);
            return path2;
        }
        //Check if path to html exists
      else
        {
            if(access(path2,F_OK) == 1)
                {
                    strcat(path2,html);
                    return path2;
                }
            else
                {
                    return NULL;
                }
        }
    }

My parse and lookup function(Got it corrected through check50 test 1)
/**
 * Returns MIME type for supported extensions, else NULL.
 */
const char* lookup(const char* path)
{
    // http://www.tutorialspoint.com/c_standard_library/c_function_strchr.htm
    const char ch = '.';
    
    //Stores string after the .
    char* end =  strchr(path,ch);
    
    //Initialize string for storing the return value
    char* ret;
    
    //Check the file extension 
    if(strcasecmp(end,"css") == 0)
        {
         ret = "text/css";
         return ret;
        }    
    if(strcasecmp(end,"html") == 0)
        {
         ret = "text/html";
         return ret;
        }    
                                  
    if(strcasecmp(end,"gif") == 0)
       {
        ret = "image/gif";
        return ret;
       }    
                                     
    if(strcasecmp(end,"ico") == 0)
        {
         ret = "image/x-icon";
         return ret;
        }    
   
    if(strcasecmp(end,"jpg") == 0)
       {
        ret = "image/jpeg";
        return ret;
       }    
            
    if(strcasecmp(end,"js") == 0)
        {
         ret = "text/javascript";
         return ret;
        }    
               
     if(strcasecmp(end,"php") == 0)
         {
            ret = "text/x-php";
            return ret;
         }
         
     if(strcasecmp(end,"png") == 0)
         {
            ret = "image/png";
            return ret;
         }
     else     
       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
   /* Request-line consists of method SP request-target SP HTTP-version CRLF*/
    //The request target consists of the absolute path and the query in this form : absolute-path [ "?" query ]
    //We will use strtok to store tokens in different strings
    //Referenced from stackoverflow and tutorialspoint.com
    //Copy the line into  a new string
    char *l = strdup(line);
    
    //Token to store method
    char* method = strtok(l, " ");
    
    //Token to store request target
    char *request = strtok(NULL," ");
    
    //Store the HTTP version in a token
    char* http = strtok(NULL,"\r\n ");
   
   //Store the absolute path in a token
   char* abs_path1 = strtok(request,"?");
   
   //Store the query after ? in a token
   char* query1 = strtok(NULL,"?");
   if(query1 == NULL)
     query1 = "";
   
   strcpy(query,query1);
   strcpy(abs_path,abs_path1);
   
    if (strcmp(method, "GET") != 0)
    {
        error(405);
        return false;
    }
    if (strchr(query, '"'))
    {
        error(400);
        return false;
    }
    if (request[0] != '/')
    {
        error(501);
        return false;
    }
    if (strstr(request, "/ ") != 0)
    {
        error(404);
        return false;
    }  
    if (strcmp(http, "HTTP/1.1") != 0)
    {
        error(505);
        return false;
    }
    
    return true;    
}