Skip to main content
delete redundant tag
Link
DinoCoderSaurus
  • 28.6k
  • 2
  • 12
  • 31
Updated parse function.
Source Link

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 =(char* )malloc(sizeofstrlen(line) + 1);
    l = strdup(line);
    
    //Token to store method
    char* method = strtokstrcat(l, " ""\0");
    
    //TokenAllocating tomemory storefor method,request targetand version
char *method = malloc(strlen(line) + 1);
char *request = strtokmalloc(NULL,"strlen(line) "+ 1);
 char* http = 
 malloc(strlen(line) + 1);
char *abs_path1;
char *query1;

//Store the HTTP versionToken into astore tokenmethod
    char* httpmethod = strtok(NULLl,"\r\n " ");
   
   //Store the absoluteToken pathto instore arequest tokentarget
   char* abs_path1request = strtok(requestNULL,"? ");
   
   //Store the query afterHTTP ?version in a token
   char* query1http = strtok(NULL,"?"\r\n ");
   if

//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 == NULL)
     query1 = "";
   
  
   strcpy(query,query1"\0");
   strcat(queryabs_path1,"\0");
   strcpy(abs_pathquery,abs_path1query1);
   strcatstrcpy(abs_path,"\0"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; 
}

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 =(char* )malloc(sizeof(line));
    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);
   strcat(query,"\0");
   strcpy(abs_path,abs_path1);
   strcat(abs_path,"\0");
   
    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;
}

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; 
}
Edited after debugging.
Source Link

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

    /**
     * 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;
    size_tint 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;
}  
    
    /**
     * 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 = (char* )malloc(sizeof(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;  
 }
/**
 * 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"".css") == 0)
    {
        ret = "text/css";
        return ret;
    }
    if(strcasecmp(end,"html"".html") == 0)
    {
        ret = "text/html";
        return ret;
    }

    if(strcasecmp(end,"gif"".gif") == 0)
    {
        ret = "image/gif";
        return ret;
    }

    if(strcasecmp(end,"ico"".ico") == 0)
    {
        ret = "image/x-icon";
        return ret;
    }

    if(strcasecmp(end,"jpg"".jpg") == 0)
    {
        ret = "image/jpeg";
        return ret;
    }

    if(strcasecmp(end,"js"".js") == 0)
    {
        ret = "text/javascript";
        return ret;
    }

    if(strcasecmp(end,"php"".php") == 0)
    {
        ret = "text/x-php";
        return ret;
    }

    if(strcasecmp(end,"png"".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
    charchar* *ll =(char* )malloc(sizeof(line));
    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);
   strcat(query,"\0");
   strcpy(abs_path,abs_path1);
   strcat(abs_path,"\0");
   
    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;
}
    /**
     * 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;
    size_t 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;
}  
    
    /**
     * 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 = (char* )malloc(sizeof(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 
    {
       strcat(path2,html);
       if(strstr(path2,html) != NULL)
        {
            if(access(path2,F_OK) != -1)
                {
                    free(path2);
                    return path2;
                }
        }
        else
            {
                return NULL;
            }
    }
  return path2;  
 }
/**
 * 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;
}

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

    /**
     * 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;
}  
    
    /**
     * 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;  
 }
/**
 * 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 =(char* )malloc(sizeof(line));
    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);
   strcat(query,"\0");
   strcpy(abs_path,abs_path1);
   strcat(abs_path,"\0");
   
    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;
}
code format
Source Link
DinoCoderSaurus
  • 28.6k
  • 2
  • 12
  • 31
Loading
added 313 characters in body
Source Link
Loading
Code formatting
Source Link
Luke Van In
  • 2.7k
  • 14
  • 26
Loading
Source Link
Loading