Skip to main content
8 of 8
delete redundant tag
DinoCoderSaurus
  • 28.6k
  • 2
  • 12
  • 31

Pset6 problem in indexes and load function

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

Edit: New check50 https://sandbox.cs50.net/checks/fa32659c01b742b78c18f1568d48b90d

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)*500);
    *content = NULL;
    int  count = 0;
   int c;
    
    //Read characters from file
   while((c = fgetc(file)) != EOF)
    {
        if( count%500 == 0)
            {
                buffer = (BYTE* )realloc(buffer, sizeof(BYTE)*(500 +count));
            }
        buffer[count] = c;    
        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 = malloc(strlen(php) + strlen(html) + 1);
 
 strcpy(path2,path);
 if(strcmp(&path2[strlen(path) - 1],"/") != 0)
    {
        strcat(path2, "/");
    }
 
 //concatenate "index.php" to the path and check if it exists
 strcat(path2,php);
 if(strstr(path2,php) != NULL)
    {
       if(access(path2,F_OK) != -1)
            {
                free(path2);
                return path2;
            }
    }
    //Concatenate "index.html" to the path and check if it exists
 else 
    { 
       strcpy(path2,path);
       strcat(path2,html);
       if(strstr(path2,html) != NULL)
        {
            if(access(path2,F_OK) != -1)
                {
                    free(path2);
                    return path2;
                }
        }
        else
            {
                return NULL;
            }
    }
  return path2;  
 }

My parse and lookup function(Got it corrected through check50 test 1) Updated parse function

/**
 * 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 = malloc(strlen(line) + 1);
l = strdup(line);
strcat(l,"\0");

//Allocating memory for method,request and version
char *method = malloc(strlen(line) + 1);
char *request = malloc(strlen(line) + 1);
char* http = malloc(strlen(line) + 1);
char *abs_path1;
char *query1;

//Token to store method
method = strtok(l, " ");

//Token to store request target
request = strtok(NULL," ");

//Store the HTTP version in a token
 http = strtok(NULL,"\r\n ");

//Store the absolute path in a token abs_path1 = strtok(request,"?");

//Store the query after ? in a token query1 = strtok(NULL,"?"); if(query1 == NULL) { query1 = " "; strcat(query1,"\0"); } strcat(query1,"\0");

strcat(query1,"\0");
strcat(abs_path1,"\0");
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;
}

free(l);
return true; 
}