Skip to content

Kevin Sooter - C-Web-Server#300

Open
kevin-sooter wants to merge 8 commits intobloominstituteoftechnology:masterfrom
kevin-sooter:master
Open

Kevin Sooter - C-Web-Server#300
kevin-sooter wants to merge 8 commits intobloominstituteoftechnology:masterfrom
kevin-sooter:master

Conversation

@kevin-sooter
Copy link

No description provided.

Copy link

@codejoncode codejoncode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int response_length = sprintf(response, "%s\n"
"Date: %s\n"
"Connection: close\n"
"Content-Length: %d\n"
"Content-Type: %s\n"
"\n"
"%s\n",
header, buffer, content_length, content_type, new_body);
Nice work Kevin. Sprintf provides a
Return Value
If successful, the total number of characters written is returned excluding the null-character appended at the end of the string, otherwise a negative number is returned in case of failure.
https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm

Copy link

@codejoncode codejoncode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time functionality seems to work well.

A different approach :

time_t t1 = time(NULL);
struct tm *ltime = localtime(&t1);

int response_length = sprintf(response,
    "%s\n"
    "Date: %s" // asctime adds its own newline
    "Connection: close\n"
    "Content-Length: %d\n"
    "Content-Type: %s\n"
    "\n", // End of HTTP header

    header,
    asctime(ltime),
    content_length,
    content_type
);

asctime automatically adds a new line for you.

Copy link

@codejoncode codejoncode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the alloc_entry function you have and the use of strdup.

This is pretty much what you are doing with it

struct cache_entry *alloc_entry(char *path, char *content_type, void *content, int content_length)
{
    struct cache_entry *ce = malloc(sizeof *ce);

    // Set the fields in the new cache entry
    ce->path = malloc(strlen(path) + 1);
    strcpy(ce->path, path);

    ce->content_type = malloc(strlen(content_type) + 1);
    strcpy(ce->content_type, content_type);

    ce->content_length = content_length;

    ce->content = malloc(content_length);
    memcpy(ce->content, content, content_length);

    return ce;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants