Skip to main content
deleted 3730 characters in body
Source Link

Lookup:

const char* lookup(const char* path)
{
    // To return MIME types
    char* MIME_type = strrchr(path, '.');
    
    if (strcasecmp(MIME_type, ".css") == 0)
        return "text/css";
    
    else if (strcasecmp(MIME_type, ".html") == 0)
        return "text/html";
    
    else if (strcasecmp(MIME_type, ".gif") == 0)
        return "image/gif";
        
    else if (strcasecmp(MIME_type, ".ico") == 0)
        return "image/x-icon";
    
    else if (strcasecmp(MIME_type, ".jpg") == 0)
        return "image/jpg";
    
    else if(strcasecmp(MIME_type, ".js") == 0)
        return "text/javascript";
    
    else if(strcasecmp(MIME_type, ".php") == 0)
        return "text/x-php";
    
    else if(strcasecmp(MIME_type, ".png") == 0)
        return "image/png";
    
    else
    return NULL;
}

Here is parse:

bool parse(const char* line, char* abs_path, char* query)
{
    // Initialize abs_path and query.
    memset(abs_path, '\0', LimitRequestLine + 1);
    memset(query, '\0', LimitRequestLine + 1);
    
    // Find where /,? and space are.
    char* first_space = strchr(line, ' ');
    char* question_mark = strchr(line, '?');
    char* HTTP_version = strstr(line, "HTTP");
    char* target_end = HTTP_version - 1;
    
    // Extract absolute-path and query.
    if (question_mark == NULL)
    {
        memcpy(abs_path, (first_space + 1), target_end - (first_space + 1));
    }
    
    else
    {
        memcpy(abs_path, (first_space + 1), question_mark - (first_space + 1));
        memcpy(query, (question_mark + 1), target_end - (question_mark + 1));
    }
    
    // Check line's consistency with the rules.
    // find substrings in line.
    
    char* get = strstr(line, "GET ");
    if (get == NULL || get != line)
    {
        error(405);
        return false;
    }
    
    if(strstr(line, "  ") != NULL)
    {
        error(400);
        return false;
    }

    if (strncmp(HTTP_version, "HTTP/1.1", 8) != 0)
    {
        error(505);
        return false;
    }
    
    char* quotation = strchr(line, '"');
    if(first_space < quotation && quotation < target_end)
    {
        error(400);
        return false;
    }
    
    if (* (first_space + 1) != '/')
    {
        error(501);
        return false;
    }
    
    return true;
}

and indexes:

char* indexes(const char* path)
{
    // return if index.php is present.
    char* indexphp = "/index.php";
    char* path_php = malloc(strlen(path) + strlen(indexphp) + 1);
    memset(path_php, '\0', strlen(path) + strlen(indexphp) + 1);
    strcpy(path_php, path);
    strcat(path_php, indexphp);
    
    if (access(path_php, F_OK) == 0)
    {
        return path_php;
    }

    else
    {
        char* indexhtml = "/index.html";
        char* path_html = malloc(strlen(path) + strlen(indexhtml) + 1);
        memset(path_html, '\0', strlen(path) + strlen(indexhtml) + 1);
        strcpy(path_html, path);
        strcat(path_html, indexhtml);
        
        if (access(path_html, F_OK) == 0)
        {
            return path_html;
        }
        
        else
        return NULL;
    }
}

Lookup:

const char* lookup(const char* path)
{
    // To return MIME types
    char* MIME_type = strrchr(path, '.');
    
    if (strcasecmp(MIME_type, ".css") == 0)
        return "text/css";
    
    else if (strcasecmp(MIME_type, ".html") == 0)
        return "text/html";
    
    else if (strcasecmp(MIME_type, ".gif") == 0)
        return "image/gif";
        
    else if (strcasecmp(MIME_type, ".ico") == 0)
        return "image/x-icon";
    
    else if (strcasecmp(MIME_type, ".jpg") == 0)
        return "image/jpg";
    
    else if(strcasecmp(MIME_type, ".js") == 0)
        return "text/javascript";
    
    else if(strcasecmp(MIME_type, ".php") == 0)
        return "text/x-php";
    
    else if(strcasecmp(MIME_type, ".png") == 0)
        return "image/png";
    
    else
    return NULL;
}

Here is parse:

bool parse(const char* line, char* abs_path, char* query)
{
    // Initialize abs_path and query.
    memset(abs_path, '\0', LimitRequestLine + 1);
    memset(query, '\0', LimitRequestLine + 1);
    
    // Find where /,? and space are.
    char* first_space = strchr(line, ' ');
    char* question_mark = strchr(line, '?');
    char* HTTP_version = strstr(line, "HTTP");
    char* target_end = HTTP_version - 1;
    
    // Extract absolute-path and query.
    if (question_mark == NULL)
    {
        memcpy(abs_path, (first_space + 1), target_end - (first_space + 1));
    }
    
    else
    {
        memcpy(abs_path, (first_space + 1), question_mark - (first_space + 1));
        memcpy(query, (question_mark + 1), target_end - (question_mark + 1));
    }
    
    // Check line's consistency with the rules.
    // find substrings in line.
    
    char* get = strstr(line, "GET ");
    if (get == NULL || get != line)
    {
        error(405);
        return false;
    }
    
    if(strstr(line, "  ") != NULL)
    {
        error(400);
        return false;
    }

    if (strncmp(HTTP_version, "HTTP/1.1", 8) != 0)
    {
        error(505);
        return false;
    }
    
    char* quotation = strchr(line, '"');
    if(first_space < quotation && quotation < target_end)
    {
        error(400);
        return false;
    }
    
    if (* (first_space + 1) != '/')
    {
        error(501);
        return false;
    }
    
    return true;
}

and indexes:

char* indexes(const char* path)
{
    // return if index.php is present.
    char* indexphp = "/index.php";
    char* path_php = malloc(strlen(path) + strlen(indexphp) + 1);
    memset(path_php, '\0', strlen(path) + strlen(indexphp) + 1);
    strcpy(path_php, path);
    strcat(path_php, indexphp);
    
    if (access(path_php, F_OK) == 0)
    {
        return path_php;
    }

    else
    {
        char* indexhtml = "/index.html";
        char* path_html = malloc(strlen(path) + strlen(indexhtml) + 1);
        memset(path_html, '\0', strlen(path) + strlen(indexhtml) + 1);
        strcpy(path_html, path);
        strcat(path_html, indexhtml);
        
        if (access(path_html, F_OK) == 0)
        {
            return path_html;
        }
        
        else
        return NULL;
    }
}
deleted 247 characters in body
Source Link
 /**
 * 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)
{
    // bsic settings
    int counter = 0;
    char* buffer = malloc(2);
   BYTE* memset(buffer, '\0', 2);
    * content = malloc(1);
    
    // Realloc *content to the size needed.
    while (fread(buffer, 1, 1, file) == 1)
    {
        counter = counter + 1;
    }
 
    free(buffer);

    * contentbuffer = realloc (* contentbuffer, counter + 1);
    memsetfread(*contentbuffer, '\0'1, counter +, 1file);
 
    // Read and store all the bytes to *content.
    fread(* content, counter, 1, file(buffer+counter);
 = '\0';
  if (* content 
 == NULL)
  *content = {buffer;
     *length = counter return+ false;1;
    }

    * length =if strlen(* content) + 1;
  == NULL if|| (length == NULL)
    {
        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)
{
    // bsic settings
    int counter = 0;
    char* buffer = malloc(2);
    memset(buffer, '\0', 2);
    * content = malloc(1);
    
    // Realloc *content to the size needed.
    while (fread(buffer, 1, 1, file) == 1)
    {
        counter = counter + 1;
    }
 
    free(buffer);

    * content = realloc (* content, counter + 1);
    memset(*content, '\0', counter + 1);
 
    // Read and store all the bytes to *content.
    fread(* content, counter, 1, file);
    if (* content == NULL)
    {
        return false;
    }

    * length = strlen(* content) + 1;
    if (length == NULL)
    {
        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)
{
    // bsic settings
    int counter = 0;
    BYTE* buffer = malloc(1);
    
    while(fread(buffer, 1, 1, file) == 1)
    {
        counter = counter + 1;
    }
    
    buffer = realloc(buffer, counter + 1);
    fread(buffer, 1, counter, file);
    *(buffer+counter) = '\0';
     
    *content = buffer;
    *length = counter + 1;
    
    if (content == NULL || length == NULL)
    {
        return false;
    }
    
    return true;
    
}
deleted 73 characters in body
Source Link

Here are lookup andis load:

 /**
 * ReturnsLoads MIMEa typefile forinto supportedmemory extensions,dynamically elseallocated NULLon heap.
 */
const char* lookup(const char*Stores path)
{
address thereof in *content //and Tolength returnthereof MIMEin types*length.
   */
bool char*load(FILE* MIME_typefile, =BYTE** strrchr(pathcontent, '.'size_t* length);
{
    
 // bsic settings
  if (strcasecmp(MIME_type, ".css") ==int 0)
counter = 0;
    char* buffer return= "text/css";malloc(2);
    
 memset(buffer, '\0', 2);
  else if (strcasecmp(MIME_type,* ".html")content === 0malloc(1);
     
    return "text/html";
/ Realloc *content to 
 the size needed.
  else if while (strcasecmpfread(MIME_typebuffer, ".gif"1, 1, file) == 01)
     {
   return "image/gif";
    counter = counter + 1;
    else if}

 (strcasecmp(MIME_type, ".ico") == 0free(buffer);
  
    * content = returnrealloc "image/x-icon";
(* content, counter + 1);
    else if (strcasecmpmemset(MIME_type*content, ".jpg")'\0', ==counter 0+ 1);
  
    // Read and returnstore "image/jpg";
all the bytes to *content.
    else if(strcasecmpfread(MIME_type* content, ".js")counter, ==1, 0file);
     if (* content return== "text/javascript";NULL)
    {
    else if(strcasecmp(MIME_type, ".php") == 0)return false;
    }

    return* "text/x-php";
length = strlen(* content) 
 + 1;
   else if(strcasecmp(MIME_type, ".png")(length == 0NULL)
    {
    return "image/png";
   return false;
    else}

    return NULL;true;
}
 

Lookup:

const char* lookup(const char* path)
{
    // To return MIME types
    char* MIME_type = strrchr(path, '.');
    char buffer[strlen(MIME_type) + 1];
    memset(buffer, '\0', strlen(MIME_type) + 1);
    strcpy(buffer, MIME_type);
    if (strcasecmp(bufferMIME_type, ".css") == 0)
        return "text/css";
    
    else if (strcasecmp(bufferMIME_type, ".html") == 0)
        return "text/html";
    
    else if (strcasecmp(bufferMIME_type, ".gif") == 0)
        return "image/gif";
        
    else if (strcasecmp(bufferMIME_type, ".ico") == 0)
        return "image/x-icon";
    
    else if (strcasecmp(bufferMIME_type, ".jpg") == 0)
        return "image/jpg";
    
    else if(strcasecmp(bufferMIME_type, ".js") == 0)
        return "text/javascript";
    
    else if(strcasecmp(bufferMIME_type, ".php") == 0)
        return "text/x-php";
    
    else if(strcasecmp(bufferMIME_type, ".png") == 0)
        return "image/png";
    
    else
    return NULL;
}

Here are lookup and load:

 /**
 * Returns MIME type for supported extensions, else NULL.
 */
const char* lookup(const char* path)
{
    // To return MIME types
    char* MIME_type = strrchr(path, '.');
    
     if (strcasecmp(MIME_type, ".css") == 0)
        return "text/css";
    
     else if (strcasecmp(MIME_type, ".html") == 0)
        return "text/html";
    
     else if (strcasecmp(MIME_type, ".gif") == 0)
        return "image/gif";
        
    else if (strcasecmp(MIME_type, ".ico") == 0)
        return "image/x-icon";
    
    else if (strcasecmp(MIME_type, ".jpg") == 0)
        return "image/jpg";
    
    else if(strcasecmp(MIME_type, ".js") == 0)
        return "text/javascript";
    
    else if(strcasecmp(MIME_type, ".php") == 0)
        return "text/x-php";
    
     else if(strcasecmp(MIME_type, ".png") == 0)
        return "image/png";
    
    else
    return NULL;
}
 
const char* lookup(const char* path)
{
    // To return MIME types
    char* MIME_type = strrchr(path, '.');
    char buffer[strlen(MIME_type) + 1];
    memset(buffer, '\0', strlen(MIME_type) + 1);
    strcpy(buffer, MIME_type);
    if (strcasecmp(buffer, ".css"))
        return "text/css";
    
    else if (strcasecmp(buffer, ".html"))
        return "text/html";
    
    else if (strcasecmp(buffer, ".gif"))
        return "image/gif";
        
    else if (strcasecmp(buffer, ".ico"))
        return "image/x-icon";
    
    else if (strcasecmp(buffer, ".jpg"))
        return "image/jpg";
    
    else if(strcasecmp(buffer, ".js"))
        return "text/javascript";
    
    else if(strcasecmp(buffer, ".php"))
        return "text/x-php";
    
    else if(strcasecmp(buffer, ".png"))
        return "image/png";
    
    else
    return NULL;
}

Here is load:

 /**
 * 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)
{
    // bsic settings
    int counter = 0;
    char* buffer = malloc(2);
    memset(buffer, '\0', 2);
    * content = malloc(1);
     
    // Realloc *content to the size needed.
    while (fread(buffer, 1, 1, file) == 1)
    {
        counter = counter + 1;
    }

    free(buffer);
 
    * content = realloc (* content, counter + 1);
    memset(*content, '\0', counter + 1);
 
    // Read and store all the bytes to *content.
    fread(* content, counter, 1, file);
    if (* content == NULL)
    {
        return false;
    }

    * length = strlen(* content) + 1;
    if (length == NULL)
    {
        return false;
    }

    return true;
}

Lookup:

const char* lookup(const char* path)
{
    // To return MIME types
    char* MIME_type = strrchr(path, '.');
    
    if (strcasecmp(MIME_type, ".css") == 0)
        return "text/css";
    
    else if (strcasecmp(MIME_type, ".html") == 0)
        return "text/html";
    
    else if (strcasecmp(MIME_type, ".gif") == 0)
        return "image/gif";
        
    else if (strcasecmp(MIME_type, ".ico") == 0)
        return "image/x-icon";
    
    else if (strcasecmp(MIME_type, ".jpg") == 0)
        return "image/jpg";
    
    else if(strcasecmp(MIME_type, ".js") == 0)
        return "text/javascript";
    
    else if(strcasecmp(MIME_type, ".php") == 0)
        return "text/x-php";
    
    else if(strcasecmp(MIME_type, ".png") == 0)
        return "image/png";
    
    else
    return NULL;
}
deleted 307 characters in body
Source Link
Loading
deleted 5 characters in body
Source Link
Loading
deleted 3 characters in body
Source Link
Loading
Source Link
Loading