1

Hey so basically I've implemented code for lookup, load, and parse that compiles and (in my own eyes) should work logically. When I run it, the program tells me "Get HTML/1.1" but then nothing on my webpage loads. It keeps spinning, so I assume it is stuck in a loop- even with printfs included, nothing prints to the screen, which confused me even further. Below are my implementations of the three functions- can anyone tell me what's going on? Thanks so much!

/**
 * 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)
{

    // read bytes from file
    // malloc them
    // store addresses in *content
    // store #bytes (bytlen) in *length

    int bytlen = 0;
    content = malloc(512);
    while (fread(content, 1, 1, file) == 1)
    {
        if (bytlen == 512)
        {
            content = realloc(content, 512);
        }
        fread (content, 1, 1, file);
        bytlen++;
    }
    *length = bytlen;
    printf("The length is %i/n", bytlen);
    return false;
}

/**
 * Returns MIME type for supported extensions, else NULL.
 */
const char* lookup(const char* path)
{
    // Reads into path to find the . indicating a file extesnion to follow
    char* extension = strrchr (".", *path);

    // Check if extension is nonnull (if null, return NULL)
    if (extension != NULL)
    {
        // Compare the included file types against the extension file type and 
        // return the corresponding MIME type
        if (strncmp("css", &path[extension - path + 1], 3) == 0)
        return "text/css";

        if (strncmp("html", &path[extension - path + 1], 3) == 0)
        return "text/html";

        if (strncmp("gif", &path[extension - path + 1], 3) == 0)
        return "image/gif";

        if (strncmp("ico", &path[extension - path + 1], 3) == 0)
        return "image/x-icon";

        if (strncmp("jpg", &path[extension - path + 1], 3) == 0)
        return "image/jpeg";

        if (strncmp("js", &path[extension - path + 1], 3) == 0)
        return "text/javascript";

        if (strncmp("php", &path[extension - path + 1], 3) == 0)
        return "text/x-php";

        if (strncmp("png", &path[extension - path + 1], 3) == 0)
        return "image/png";
    }
    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)
{
    // Checks that the method is get
    if (strncmp("GET", line, 3) != 0)
    {
        reason(405);
        return false;
    }

    // Checks if request-target begins with /
    if (strcmp("/", &line[4]) != 0)
    {
        reason(501);
        return false;
    }

    // Checks for " in request-target
    if (strchr("", *line) != NULL)
    {
        reason(400);
        return false;
    }

    // Checks for correct HTTP version
    char* HTTP = strrchr(" ", *line);
    if (strncmp("HTTP/1.1", &line[HTTP - line + 1], 8) != 0)
    {
        reason(505);
        return false;
    }

    // Find and store abs_path using prototype information (knowing to start at
    // the 4th index and end if a "?"" or " " is next)
    int ctr = 0;
    int linlen = strlen(line);
    for (int k = 0, i = 4; i < linlen; i++, k++)
    {
        abs_path[k] = line[i];
        ctr++;
        if (line[i + 1] == ("?" || " "))
        {
            break;
        }

    }
    printf("The absolute path is %s/n", abs_path);
    // Increase the counter so that the question mark gets skipped over, then
    // find and store query using prototype information (knowing to start after
    // the "?" and break if the next character is a space)
    ctr++;
    for (int j = 0; j < linlen; j++)
    {
        if (strncmp(&line[ctr + 1], " ", 1) == 0)
        {
            break;
        }
        query[j] = line[ctr];
        ctr++;
    }

    // If query is never stored, because there was no question mark, set the
    // query to ""
    if (strlen(query) == 0)
    {
        query = "";
    }
    printf("The query is %s/n", query);
    return true;
}
2

You must log in to answer this question.

Browse other questions tagged .