You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+18-10Lines changed: 18 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
# Proxy Server
2
+
2
3
Proxy is a simple concurrent HTTP proxy server that caches recently-requested web objects.
3
4
4
5
It's an application for learning about ***Socket Programming*** and ***Concurrent Programming***.
@@ -24,6 +25,7 @@ It's an application for learning about ***Socket Programming*** and ***Concurren
24
25
25
26
26
27
## Overview
28
+
27
29
A web proxy is a program that acts as a middleman between a Web browser and an end server.
28
30
- Instead of contacting the end server directly to get a Web page, the browser contacts the proxy, which forwards the request on to the end server. When the end server replies to the proxy, the proxy sends the reply on to the browser.
29
31
- It can deal with multiple concurrent connections using multi-threading.
@@ -34,22 +36,27 @@ A web proxy is a program that acts as a middleman between a Web browser and an e
34
36
35
37
It has the following detailed behavior:
36
38
-**Set up** the proxy to accept incoming connections:
37
-
1) Proxy creates a listening descriptor that is ready to receive connection requests on port ***`port`*** by calling ***`Open_listenfd()`*** funciton defined in **`socket.c`** file.
39
+
40
+
1. Proxy creates a listening descriptor that is ready to receive connection requests on port ***`port`*** by calling ***`Open_listenfd()`*** funciton defined in **`socket.c`** file.
38
41
39
42
-**Set up** the proxy to deal with multiple concurrent connections using **prethreading** technique:
40
-
1) After initializing buffer ***`sbuf`*** declared in **`sbuf.h`** file, the main thread creates the set of worker threads by calling ***`Pthread_create()`*** defined in **`thread.c`** file.
41
-
2) Main thread then enters an infinite loop, waiting for connection requests using ***`Accept()`*** function defined in **`socket.c`** file.
42
-
3) It then inserts the resulting connected descriptors in ***`sbuf`***.
43
-
4) Each worker thread waits until it is able to remove a connected descriptor from the buffer and then calls the ***`serve_client()`*** function to serve the client.
43
+
44
+
1. After initializing buffer ***`sbuf`*** declared in **`sbuf.h`** file, the main thread creates the set of worker threads by calling ***`Pthread_create()`*** defined in **`thread.c`** file.
45
+
2. Main thread then enters an infinite loop, waiting for connection requests using ***`Accept()`*** function defined in **`socket.c`** file.
46
+
3. It then inserts the resulting connected descriptors in ***`sbuf`***.
47
+
4. Each worker thread waits until it is able to remove a connected descriptor from the buffer and then calls the ***`serve_client()`*** function to serve the client.
48
+
44
49
-**Run*****`serve_client()`*** routine:
45
-
1) Read and parse the _HTTP_ request sent from the client by calling ***`read_HTTP_request()`*** function defined in **`serve.c`** file.
46
-
2) using ***`hash()`*** function defined in **`cache.c`** file, generate ***`HTTP_request_hash`*** that will be used to check if the cache contains the requested web object.
47
-
3) If the object is cached then read the object out of the cache rather than communicating again with the server by calling ***`service_from_cache()`*** function defined in **`service.c`** file.
48
-
4) Otherwise, using ***`service_from_server()`*** function, Try to connect the server then send it the ***`parsed_request`***, read its response, write it back to the client, and save it in an internal buffer for possible caching.
49
-
5) If ***`object_size`*** is less than ***`MAX_OBJECT_SIZE`*** write the object in a suitable cahce line.
50
+
51
+
1. Read and parse the _HTTP_ request sent from the client by calling ***`read_HTTP_request()`*** function defined in **`serve.c`** file.
52
+
2. using ***`hash()`*** function defined in **`cache.c`** file, generate ***`HTTP_request_hash`*** that will be used to check if the cache contains the requested web object.
53
+
3. If the object is cached then read the object out of the cache rather than communicating again with the server by calling ***`service_from_cache()`*** function defined in **`service.c`** file.
54
+
4. Otherwise, using ***`service_from_server()`*** function, Try to connect the server then send it the ***`parsed_request`***, read its response, write it back to the client, and save it in an internal buffer for possible caching.
55
+
5. If ***`object_size`*** is less than ***`MAX_OBJECT_SIZE`*** write the object in a suitable cahce line.
50
56
51
57
52
58
## How does proxy deal with concurrent requests?
59
+
53
60
- We incur non-trivial cost of creating a new thread for each new client.
54
61
- A proxy based on *prethreading* tries to reduce this overhead by using the **producer-consumer** model shown in the figure.
55
62
- The proxy consists of a ***main thread*** and a set of ***worker threads***, the main thread repeatedly accepts connection requests from clients and places the resulting connected descriptors in a bounded buffer.
@@ -59,6 +66,7 @@ It has the following detailed behavior:
59
66
60
67
61
68
## How does proxy synchronize accesses to cache?
69
+
62
70
- Accesses to the cache must be thread-safe, and free of race conditions, So as a matter of fact we have these special requirements:
63
71
1. Multiple threads must be able to simultaneously read from the cache.
64
72
2. No thread can write to specific object while another thread is reading it.
0 commit comments