-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.c
239 lines (190 loc) · 7.02 KB
/
request.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// request.c: Does the bulk of the work for the web server.
//
#include "cs537.h"
#include "request.h"
void requestError(int fd, char *cause, char *errnum, char *shortmsg, char *longmsg)
{
char buf[MAXLINE], body[MAXBUF];
printf("Request ERROR\n");
// Create the body of the error message
sprintf(body, "<html><title>CS537 Error</title>");
sprintf(body, "%s<body bgcolor=""fffff"">\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>CS537 Web Server\r\n", body);
// Write out the header information for this response
sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg);
Rio_writen(fd, buf, strlen(buf));
printf("%s", buf);
sprintf(buf, "Content-Type: text/html\r\n");
Rio_writen(fd, buf, strlen(buf));
printf("%s", buf);
sprintf(buf, "Content-Length: %lu\r\n\r\n", strlen(body));
Rio_writen(fd, buf, strlen(buf));
printf("%s", buf);
// Write out the content
Rio_writen(fd, body, strlen(body));
printf("%s", body);
}
//
// Reads and discards everything up to an empty text line
//
void requestReadhdrs(rio_t *rp)
{
char buf[MAXLINE];
Rio_readlineb(rp, buf, MAXLINE);
while (strcmp(buf, "\r\n")) {
Rio_readlineb(rp, buf, MAXLINE);
}
return;
}
//
// Return 1 if static, 0 if dynamic content
// Calculates filename (and cgiargs, for dynamic) from uri
//
int requestParseURI(char *uri, char *filename, char *cgiargs)
{
char *ptr;
if (!strstr(uri, "cgi")) {
// static
strcpy(cgiargs, "");
sprintf(filename, ".%s", uri);
if (uri[strlen(uri)-1] == '/') {
strcat(filename, "home.html");
}
return 1;
} else {
// dynamic
ptr = index(uri, '?');
if (ptr) {
strcpy(cgiargs, ptr+1);
*ptr = '\0';
} else {
strcpy(cgiargs, "");
}
sprintf(filename, ".%s", uri);
return 0;
}
}
//
// Fills in the filetype given the filename
//
void requestGetFiletype(char *filename, char *filetype)
{
if (strstr(filename, ".html"))
strcpy(filetype, "text/html");
else if (strstr(filename, ".gif"))
strcpy(filetype, "image/gif");
else if (strstr(filename, ".jpg"))
strcpy(filetype, "image/jpeg");
else
strcpy(filetype, "test/plain");
}
void requestServeDynamic(int fd, char *filename, char *cgiargs, long arrival, long dispatch, thread *worker)
{
char buf[MAXLINE], *emptylist[] = {NULL};
// The server does only a little bit of the header.
// The CGI script has to finish writing out the header.
sprintf(buf, "HTTP/1.0 200 OK\r\n");
sprintf(buf, "%sServer: CS537 Web Server\r\n", buf);
/* CS537: Your statistics go here -- fill in the 0's with something useful! */
sprintf(buf, "%s Stat-req-arrival: %ld\r\n", buf, arrival);
sprintf(buf, "%s Stat-req-dispatch: %d\r\n", buf, (int)(dispatch - arrival));
sprintf(buf, "%s Stat-thread-id: %d\r\n", buf, worker->id);
sprintf(buf, "%s Stat-thread-count: %d\r\n", buf, worker->count);
sprintf(buf, "%s Stat-thread-static: %d\r\n", buf, worker->statics);
sprintf(buf, "%s Stat-thread-dynamic: %d\r\n", buf, worker->dynamics);
Rio_writen(fd, buf, strlen(buf));
if (Fork() == 0) {
/* Child process */
Setenv("QUERY_STRING", cgiargs, 1);
/* When the CGI process writes to stdout, it will instead go to the socket */
Dup2(fd, STDOUT_FILENO);
Execve(filename, emptylist, environ);
}
Wait(NULL);
}
void requestServeStatic(int fd, char *filename, int filesize, long arrival, long dispatch, thread *worker)
{
int srcfd;
char *srcp, filetype[MAXLINE], buf[MAXBUF];
struct timeval beforeread, afterread;
int read, complete;
gettimeofday(&beforeread, NULL);
requestGetFiletype(filename, filetype);
srcfd = Open(filename, O_RDONLY, 0);
// Rather than call read() to read the file into memory,
// which would require that we allocate a buffer, we memory-map the file
srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);
Close(srcfd);
gettimeofday(&afterread, NULL);
read = ((afterread.tv_sec - beforeread.tv_sec) * 1000 + (afterread.tv_usec - beforeread.tv_usec)/1000.0) + 0.5;
complete = (((afterread.tv_sec) * 1000 + (afterread.tv_usec)/1000.0) + 0.5) - arrival;
// put together response
sprintf(buf, "HTTP/1.0 200 OK\r\n");
sprintf(buf, "%sServer: CS537 Web Server\r\n", buf);
// CS537: Your statistics go here -- fill in the 0's with something useful!
sprintf(buf, "%s Stat-req-arrival: %ld\r\n", buf, arrival);
sprintf(buf, "%s Stat-req-dispatch: %d\r\n", buf, (int)(dispatch - arrival));
sprintf(buf, "%s Stat-req-read: %d\r\n", buf, read);
sprintf(buf, "%s Stat-req-complete: %d\r\n", buf, complete);
sprintf(buf, "%s Stat-thread-id: %d\r\n", buf, worker->id);
sprintf(buf, "%s Stat-thread-count: %d\r\n", buf, worker->count);
sprintf(buf, "%s Stat-thread-static: %d\r\n", buf, worker->statics);
sprintf(buf, "%s Stat-thread-dynamic: %d\r\n", buf, worker->dynamics);
sprintf(buf, "%sContent-Length: %d\r\n", buf, filesize);
sprintf(buf, "%sContent-Type: %s\r\n\r\n", buf, filetype);
Rio_writen(fd, buf, strlen(buf));
// Writes out to the client socket the memory-mapped file
Rio_writen(fd, srcp, filesize);
Munmap(srcp, filesize);
}
// handle a request
void requestHandle(int fd, long arrival, long dispatch, thread *worker)
{
int is_static;
struct stat sbuf;
char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
char filename[MAXLINE], cgiargs[MAXLINE];
rio_t rio;
Rio_readinitb(&rio, fd);
Rio_readlineb(&rio, buf, MAXLINE);
sscanf(buf, "%s %s %s", method, uri, version);
printf("%s %s %s\n", method, uri, version);
if (strcasecmp(method, "GET")) {
requestError(fd, method, "501", "Not Implemented", "CS537 Server does not implement this method");
return;
}
requestReadhdrs(&rio);
is_static = requestParseURI(uri, filename, cgiargs);
if (stat(filename, &sbuf) < 0) {
requestError(fd, filename, "404", "Not found", "CS537 Server could not find this file");
return;
}
if (is_static) {
if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) {
requestError(fd, filename, "403", "Forbidden", "CS537 Server could not read this file");
return;
}
worker->statics++;
requestServeStatic(fd, filename, sbuf.st_size, arrival, dispatch, worker);
} else {
if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) {
requestError(fd, filename, "403", "Forbidden", "CS537 Server could not run this CGI program");
return;
}
worker->dynamics++;
requestServeDynamic(fd, filename, cgiargs, arrival, dispatch, worker);
}
}
long requestFileSize(int fd) {
char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
char filename[MAXLINE], cgiargs[MAXLINE];
struct stat s;
recv(fd, buf, sizeof(buf), MSG_PEEK);
sscanf(buf, "%s %s %s\n", method, uri, version);
requestParseURI(uri, filename, cgiargs);
Stat(filename, &s);
return (long)s.st_size;
}