Skip to content

Commit df8b920

Browse files
committed
Update service
1 parent 85e3b23 commit df8b920

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

service.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ static const char *user_agent_hdr = "User-Agent: Mozilla/5.0 (X11; Linux x86_64;
88
/* Proxy/client and proxy/server functions prototypes */
99
static void serve_client(int connfd, cache_line_t *cache);
1010
static void service_from_cache(int connfd, cache_line_t *cache, size_t matched_line_idx);
11-
static void service_from_server(int connfd, URL_INFO url_info, char *parsed_request,
11+
static void service_from_server(int connfd, URL_INFO *url_info, char *parsed_request,
1212
cache_line_t *cache, size_t HTTP_request_hash);
13-
static int connect_server(URL_INFO url_info, char *parsed_request, int connfd, char *cache_buf);
13+
static int connect_server(URL_INFO *url_info, char *parsed_request, int connfd, char *cache_buf);
1414

1515
/* Manipulating HTTP requests functions prototypes */
1616
static int read_HTTP_request(int connfd, URL_INFO *url_infop, char *headers);
@@ -49,13 +49,13 @@ void *thread(void *vargp)
4949
*/
5050
static void serve_client(int connfd, cache_line_t *cache)
5151
{
52-
URL_INFO url_info;
52+
URL_INFO *url_info = Malloc(4 * sizeof(char*));
5353
char parsed_request[MAXLINE];
5454
size_t HTTP_request_hash;
5555
int matched_line_idx;
5656

5757
/* Read request line and headers from the client */
58-
if (read_HTTP_request(connfd, &url_info, parsed_request) < 0)
58+
if (read_HTTP_request(connfd, url_info, parsed_request) < 0)
5959
return;
6060

6161
HTTP_request_hash = hash(parsed_request);
@@ -82,6 +82,7 @@ static void serve_client(int connfd, cache_line_t *cache)
8282
/* Forward request on to the server */
8383
connect_server(url_info, parsed_request, connfd, cache_buf);
8484
#endif
85+
Free(url_info);
8586
}
8687

8788
/*
@@ -90,16 +91,16 @@ static void serve_client(int connfd, cache_line_t *cache)
9091
* response and forward it to client. if proxy has a cache, copy
9192
* the response content to cache_buf to save it in the cache.
9293
*/
93-
static int connect_server(URL_INFO url_info, char *parsed_request, int connfd, char *cache_buf)
94+
static int connect_server(URL_INFO *url_info, char *parsed_request, int connfd, char *cache_buf)
9495
{
9596
int clientfd;
9697
int total = 0;
9798
char *host, *port, buf[MAXBUF];
9899
rio_t rio;
99100
ssize_t nread;
100101

101-
host = url_info.host;
102-
port = url_info.port;
102+
host = url_info->host;
103+
port = url_info->port;
103104
if (!host || !port) return 0;
104105
if ((clientfd = open_clientfd(host, port)) < 0) {
105106
char msg[MAXLINE];
@@ -154,14 +155,13 @@ static int read_request_line(rio_t *rp, URL_INFO *url_infop, char *parsed_reques
154155
return -1;
155156
}
156157
// Check HTTP version
157-
if (strcmp("HTTP/1.0", version) && strcmp("HTTP/1.1", version)) {
158+
if (strcasecmp("HTTP/1.0", version) && strcasecmp("HTTP/1.1", version)) {
158159
clienterror(rp->rio_fd, version, "400", "Bad request",
159160
"Invalid HTTP version");
160161
return -1;
161162
} else {
162163
strcpy(version, "HTTP/1.0");
163164
}
164-
165165
parse_url(url_infop, url);
166166
if (!url_infop) {
167167
clienterror(rp->rio_fd, url, "400", "Bad request",
@@ -319,7 +319,7 @@ static void service_from_cache(int connfd, cache_line_t *cache, size_t matched_l
319319
* save object in the cache to read it quickly in case of
320320
* same future requests without connecting the server.
321321
*/
322-
static void service_from_server(int connfd, URL_INFO url_info, char *parsed_request,
322+
static void service_from_server(int connfd, URL_INFO *url_info, char *parsed_request,
323323
cache_line_t *cache, size_t HTTP_request_hash)
324324
{
325325
char cache_buf[MAX_OBJECT_SIZE];

0 commit comments

Comments
 (0)