Final
This commit is contained in:
@@ -7,21 +7,21 @@ Overview<br/>
|
||||
This is a simple TCP HTTP cache proxy server. It can cache responses for HTTP requests to upstream servers, and serve them in accordance with HTTP protocol.
|
||||
The cache is populated from 0, either filling the next open slot, or the first encountered expired entry.
|
||||
|
||||
This program has been tested to work with GET and HEAD request in proxy mode (-I for HEAD, -x for Proxy mode). All test cases listed publically on the server http://149.165.173.252:8000/ have been tested and verified that they work with the program. I know of no defects at this time.
|
||||
This program has been tested to work with GET and HEAD requests in proxy mode (-I for HEAD, -x for Proxy mode). All test cases listed publically on the server http://149.165.173.252:8000/ have been tested and verified that they work with the program. I know of no defects at this time.
|
||||
|
||||
This program has various configurable settings, which I will describe below along with their default values
|
||||
```c
|
||||
// Program Settings
|
||||
#define BOUND_IP "10.4.78.8" // IP Address to bind to
|
||||
#define PORT 8000 // The port to bind to
|
||||
#define BOUND_IP "10.4.78.8" // IP Address to bind to
|
||||
#define PORT 8000 // The port to bind to
|
||||
|
||||
#define DEBUG_MODE DEBUG_ASSIGNMENT // Print Debug Statement Level (Select one from below)
|
||||
#define DEBUG_MODE DEBUG_ASSIGNMENT // Print Debug Statement Level (Select one from below)
|
||||
|
||||
// Debug levels
|
||||
# define DEBUG_NONE 0 // No output at all
|
||||
# define DEBUG_INFO 1 // Basic Output (All the DEBUG from Pt.1 of the program, plus a few extra print statements)
|
||||
# define DEBUG_EXTENDED 2 // Extra Output I used for inspecting messages being constructed / sent.
|
||||
# define DEBUG_ASSIGNMENT -1 // Output for Assignment Submission (This is the default setting I will be turning this program in with)
|
||||
#define DEBUG_NONE 0 // No output at all
|
||||
#define DEBUG_INFO 1 // Basic Output (All the DEBUG from Pt.1 of the program, plus a few extra print statements)
|
||||
#define DEBUG_EXTENDED 2 // Extra Output I used for inspecting messages being constructed / sent.
|
||||
#define DEBUG_ASSIGNMENT -1 // Output for Assignment Submission (This is the default setting I will be turning this program in with)
|
||||
```
|
||||
|
||||
These can be edited and recompiled / executed
|
||||
+10
-6
@@ -541,6 +541,10 @@ void save_response_to_cache(struct req_http_message req_message, struct res_http
|
||||
|
||||
// Check if Response can be Cached
|
||||
int can_cache(struct res_http_message message) {
|
||||
if (atoi(message.start_line.status_code) != 200) {
|
||||
return 0; // Only cache 200 OK responses
|
||||
}
|
||||
|
||||
for (int i = 0; i < message.header_count; i++) {
|
||||
if (strcmp(message.headers[i].field_name, "Cache-Control") == 0) {
|
||||
if (strstr(message.headers[i].field_value, "no-store") != NULL) {
|
||||
@@ -686,6 +690,7 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
// If 304 Not Modified, retrieve from cache
|
||||
int was_304 = 0;
|
||||
if (cache_valid == CACHE_EXPIRED && atoi(response.start_line.status_code) == 304) {
|
||||
printf("[Info] Received 304 Not Modified from server\n");
|
||||
|
||||
@@ -708,18 +713,17 @@ int main(int argc, char *argv[]) {
|
||||
if (DEBUG_MODE == DEBUG_ASSIGNMENT) {
|
||||
printf("[Info] Returning cached object with 200 OK\n");
|
||||
}
|
||||
was_304 = 1;
|
||||
}
|
||||
|
||||
// Save Response to Cache
|
||||
if (can_cache(response)) {
|
||||
if (DEBUG_MODE == DEBUG_ASSIGNMENT && atoi(response.start_line.status_code) != 404) {
|
||||
if (can_cache(response) && !was_304) {
|
||||
if (DEBUG_MODE == DEBUG_ASSIGNMENT) {
|
||||
printf("[Info] Object is cacheable\n");
|
||||
}
|
||||
|
||||
if (atoi(response.start_line.status_code) != 404) {
|
||||
save_response_to_cache(received_message, response);
|
||||
}
|
||||
} else if (DEBUG_MODE == DEBUG_ASSIGNMENT) {
|
||||
save_response_to_cache(received_message, response);
|
||||
} else if (DEBUG_MODE == DEBUG_ASSIGNMENT & !was_304) {
|
||||
printf("[Info] Object is not cacheable\n");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user