-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy_part1.c
193 lines (166 loc) · 6.28 KB
/
proxy_part1.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include "csapp.h"
/* Recommended max cache and object sizes */
#define MAX_CACHE_SIZE 1049000
#define MAX_OBJECT_SIZE 102400
/* You won't lose style points for including this long line in your code */
static const char *requestline_format = "GET %s HTTP/1.0\r\n";
static const char *user_agent_header = "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120305 Firefox/10.0.3\r\n";
static const char *host_header_format = "Host: %s:%s\r\n";
static const char *connection_header = "Connection: close\r\n";
static const char *proxy_connection_header = "Proxy-Connection: close\r\n";
static const char *blank_line = "\r\n";
void doit(int fd);
void parse_uri(char* uri, char* hostname, char* path, char* port);
void build_request_header(char* request_header, char* hostname,
char* path, char* port, rio_t* pclient_rio);
void clienterror(int fd, char *cause, char *errnum,
char *shortmsg, char *longmsg);
int main(int argc,char **argv)
{
int listenfd, connfd;
char hostname[MAXLINE], port[MAXLINE];
socklen_t clientlen;
struct sockaddr_storage clientaddr;
/* Check command line args */
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
listenfd = Open_listenfd(argv[1]);
while (1) {
clientlen = sizeof(clientaddr);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE,
port, MAXLINE, 0);
printf("Accepted connection from (%s, %s)\n", hostname, port);
doit(connfd);
Close(connfd);
}
}
void doit(int fd)
{
int serverfd;
char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
char hostname[MAXLINE], path[MAXLINE], port[MAXLINE],
request_header[MAXLINE];
rio_t rio, server_rio;
/* Read request line and headers */
Rio_readinitb(&rio, fd);
if (!Rio_readlineb(&rio, buf, MAXLINE))
return;
printf("%s", buf);
sscanf(buf, "%s %s %s", method, uri, version);
if (strcasecmp(method, "GET")) {
clienterror(fd, method, "501", "Not Implemented",
"Tiny does not implement this method");
return;
}
/* Parse URI from GET request */
parse_uri(uri, hostname, path, port);
/* Build the request header*/
build_request_header(request_header, hostname, path, port, &rio);
/*Connect to server*/
serverfd = Open_clientfd(hostname, port);
if(serverfd<0){
printf("connection failed\n");
return;
}
/*Write the http header to server*/
Rio_readinitb(&server_rio, serverfd);
Rio_writen(serverfd, request_header, strlen(request_header));
/*receive message from end server and send to the client*/
size_t n;
while( (n=Rio_readlineb(&server_rio,buf,MAXLINE))!=0 )
{
printf("proxy received %zu bytes,then send\n",n);
Rio_writen(fd, buf, n);
}
Close(serverfd);
}
/*
* parse_uri - parse the uri, get hostname, path and port information
*/
void parse_uri(char* uri, char* hostname, char* path, char* port)
{
char buf[MAXLINE];
/*position to "//", the first"/", ":" */
char* pstart, *ppath, *pport;
strcpy(buf, uri);
pstart = strstr(buf, "//") + 2;
ppath = strchr(pstart, '/');
if(!ppath){
strcpy(path, "/");
}else{
strcpy(path, ppath);
*ppath = 0;
}
pport = strchr(pstart, ':');
if(!pport){
strcpy(port, "80");
strcpy(hostname, pstart);
}else{
strcpy(port, pport+1);
*pport = 0;
strcpy(hostname, pstart);
}
}
/*
* build_request_header - build request_header with the Host,
* User-Agent, Connection, and Proxy-Connection headers.
* It is possible that web browsers will attach their own Host headers to their HTTP requests.
* If that isthe case, the proxy will use the same Host header as the browser.
*/
void build_request_header(char* request_header, char* hostname,
char* path, char* port, rio_t* client_rio)
{
char buf[MAXLINE], host_header[MAXLINE], other_header[MAXLINE];
strcpy(host_header, "\0");
sprintf(request_header, requestline_format, path);
/*check host header and get other request header for client rio then change it */
while(Rio_readlineb(client_rio, buf, MAXLINE) >0){
if(strcmp(buf, blank_line)==0) break;//EOF
if(strncasecmp("Host:", buf, strlen("Host:"))){
strcpy(host_header, buf);
continue;
}
//when this line is not proxy_connection header or connection header or user-agent:
if( !strncasecmp("Connection:", buf, strlen("Connection:"))
&& !strncasecmp("Proxy-Connection:", buf, strlen("Proxy-Connection:"))
&& !strncasecmp("User-Agent:", buf, strlen("User-Agent:")) )
{
strcat(other_header, buf);
}
}
if(strlen(host_header) == 0){
sprintf(host_header, host_header_format, hostname, port);
}
strcat(request_header, host_header);
strcat(request_header, user_agent_header);
strcat(request_header, other_header);
strcat(request_header, connection_header);
strcat(request_header, proxy_connection_header);
strcat(request_header, blank_line);
}
/*
* clienterror - returns an error message to the client
*/
void clienterror(int fd, char *cause, char *errnum,
char *shortmsg, char *longmsg)
{
char buf[MAXLINE], body[MAXBUF];
/* Build the HTTP response body */
sprintf(body, "<html><title>Tiny Error</title>");
sprintf(body, "%s<body bgcolor=""ffffff"">\r\n", body);
sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg);
sprintf(body, "%s<p>%s: %s\r\n", body, longmsg, cause);
sprintf(body, "%s<hr><em>The Tiny Web server</em>\r\n", body);
/* Print the HTTP response */
sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg);
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "Content-type: text/html\r\n");
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "Content-length: %d\r\n\r\n", (int)strlen(body));
Rio_writen(fd, buf, strlen(buf));
Rio_writen(fd, body, strlen(body));
}