-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.c
127 lines (105 loc) · 3.19 KB
/
proxy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "proxy_cache/cache.h"
#include "proxy_serve/serve.h"
#include "socket_interface/interface.h"
#define safe_free(ptr) sfree((void **) &(ptr))
typedef struct sockaddr SA;
typedef struct vargp {
int clientfd;
Cache *proxy_cache;
} Vargp;
static void*
client_serve(void* vargp);
static void
sfree(void **ptr);
static void
free_resources(Request *client_request, Response *server_response);
int
main(int argc, char **argv)
{
int connfd, listenfd;
char hostname[MAX_LINE], port[PORT_LEN];
socklen_t client_len;
struct sockaddr_storage client_addr;
pthread_t tid;
Cache proxy_cache;
Vargp *vargp;
signal(SIGPIPE, SIG_IGN);
/* Check command-line args */
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
listenfd = open_listenfd(argv[1]);
cache_init(&proxy_cache);
while (1) {
client_len = sizeof(client_addr);
if ((connfd = accept(listenfd, (SA* ) &client_addr, &client_len)) < 0) {
fprintf(stderr, "Connection to (%s, %s) failed\n", hostname, port);
continue;
}
vargp = malloc(sizeof(Vargp));
vargp->clientfd = connfd;
vargp->proxy_cache = &proxy_cache;
pthread_create(&tid,NULL, client_serve, vargp);
}
}
static void*
client_serve(void *vargp)
{
int clientfd;
Request client_request;
Response server_respone;
Cache *proxy_cache;
clientfd = ((Vargp *)vargp)->clientfd;
proxy_cache = ((Vargp *)vargp)->proxy_cache;
/* freeing the allocated memeory that vargp points to */
safe_free(vargp);
/* detach the thread after ending its job */
pthread_detach(pthread_self());
/* initialize client_request and server_response resources with zero */
memset(&client_request, 0, sizeof(Request));
memset(&server_respone, 0, sizeof(Response));
/* parse HTTP request */
if (!(parse_request(clientfd, &client_request) < 0)) {
/* forward the request to the server if it parsed successfully */
if (!(forward_request(&client_request, proxy_cache,
&server_respone) < 0)) {
/* forward server response to the client after requesting
* successfully */
if (!(forward_response(clientfd, &server_respone) < 0)) {
/* code region if any dependent action after successfull
* server_respone */
}
}
}
free_resources(&client_request, &server_respone);
close(clientfd);
return NULL;
}
static void
sfree(void **ptr)
{
if (ptr != NULL && *ptr != NULL) {
free(*ptr);
*ptr = NULL;
}
}
static void
free_resources(Request *client_request, Response *server_response)
{
safe_free(client_request->rq_headers);
safe_free(client_request->rq_hostname);
safe_free(client_request->rq_method);
safe_free(client_request->rq_port);
safe_free(client_request->rq_uri);
safe_free(server_response->rs_content);
safe_free(server_response->rs_headers);
safe_free(server_response->rs_line);
}