Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a minimal web server designed to serve my blog. I'm writing it to be rob
# Specs
- Only runs on Linux
- HTTP/1.1 support with pipelining and keep-alive
* HTTPS (TLS 1.2 using BearSSL)
- HTTPS (TLS 1.2 using BearSSL)
- Uses request and connection timeouts
- Access log, log file rotation, hard disk usage limits
- No `Transfer-Encoding: Chunked` (when receiving a chunked request the server responds with `411 Length Required`, prompting the client to try again with the `Content-Length` header)
Expand Down
17 changes: 13 additions & 4 deletions serve.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
#define LOG_DIRECTORY_SIZE_LIMIT_MB 100
#endif

#define KEEPALIVE_MAXREQS 1000

#define LOG_DIRECTORY "logs"

#define HTTPS_KEY_FILE "key.pem"
Expand Down Expand Up @@ -1064,7 +1066,7 @@ bool should_keep_alive(Connection *conn)
return false;

// Don't keep alive if we served a lot of requests to this connection
if (conn->served_count > 100)
if (conn->served_count > KEEPALIVE_MAXREQS)
return false;

// Don't keep alive if the server is more than 70% full
Expand Down Expand Up @@ -1201,11 +1203,11 @@ bool respond_to_available_requests(Connection *conn)
// Reset the request timer
conn->start_time = now;

conn->keep_alive = false;
conn->keep_alive = true;
string keep_alive_header;
if (find_header(&request, LIT("Connection"), &keep_alive_header)) {
if (string_match_case_insensitive(trim(keep_alive_header), LIT("Keep-Alive")))
conn->keep_alive = true;
if (string_match_case_insensitive(trim(keep_alive_header), LIT("Close")))
conn->keep_alive = false;
}
// Respond
ResponseBuilder builder;
Expand Down Expand Up @@ -1543,6 +1545,13 @@ int main(int argc, char **argv)

init_globals();

log_format("BACKTRACE=%d\n", BACKTRACE);
log_format("EOPALLOC=%d\n", EOPALLOC);
log_format("PROFILE=%d\n", PROFILE);
log_format("ACCESS_LOG=%d\n", ACCESS_LOG);
log_format("SHOW_IO=%d\n", SHOW_IO);
log_format("SHOW_REQUESTS=%d\n", SHOW_REQUESTS);

DEBUG("Globals initialized\n");

uint64_t last_log_time = 0;
Expand Down