-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwebserver.cpp
239 lines (193 loc) · 6.53 KB
/
webserver.cpp
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
238
239
#include <uThreads/uThreads.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include "include/http_parser.h"
#define PORT 8800
#define INPUT_BUFFER_LENGTH 1024 //4 KB
#define MAXIMUM_THREADS_PER_CLUSTER 8
/* HTTP responses*/
#define RESPONSE_METHOD_NOT_ALLOWED "HTTP/1.1 405 Method Not Allowed\r\n"
#define RESPONSE_NOT_FOUND "HTTP/1.1 404 Not Found\n" \
"Content-type: text/html\n" \
"\n" \
"<html>\n" \
" <body>\n" \
" <h1>Not Found</h1>\n" \
" <p>The requested URL was not found on this server.</p>\n" \
" </body>\n" \
"</html>\n"
// To avoid file io, only return a simple HelloWorld!
#define RESPONSE_OK "HTTP/1.1 200 OK\r\n" \
"Content-Length: 21\r\n" \
"Content-Type: text/html\r\n" \
"Connection: keep-alive\r\n" \
"Server: uThreads-http\r\n" \
"\r\n" \
"<p>Hello World!</p>\n"
/* Logging */
#define LOG(msg) puts(msg);
#define LOGF(fmt, params...) printf(fmt "\n", params);
#define LOG_ERROR(msg) perror(msg);
#define HTTP_REQUEST_HEADER_END "\n\r\n\r"
Connection* sconn; //Server socket
http_parser_settings settings;
typedef struct {
Connection* conn;
bool keep_alive;
char* url;
int url_length;
} custom_data_t;
ssize_t read_http_request(Connection& cconn, void *vptr, size_t n){
size_t nleft;
ssize_t nread;
char * ptr;
ptr = (char *)vptr;
nleft = n;
uThread::yield(); //yield before read
while(nleft >0){
if( (nread = cconn.recv( ptr, INPUT_BUFFER_LENGTH - 1, 0)) <0){
if (errno == EINTR)
nread =0;
else
return (-1);
}else if(nread ==0)
break;
nleft -= nread;
//Check whether we are at the end of the http_request
if(memcmp((const void*)&ptr[nread-4], HTTP_REQUEST_HEADER_END, 4))
break;
ptr += nread;
}
return (n-nleft);
}
ssize_t writen(Connection& cconn, const void *vptr, size_t n){
size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr = (char*)vptr;
nleft = n;
while(nleft > 0){
if( (nwritten = cconn.send(ptr, nleft, 0)) <= 0){
if(errno == EINTR)
nwritten = 0; /* If interrupted system call => call the write again */
else
return (-1);
}
nleft -= nwritten;
ptr += nwritten;
}
return (n);
}
int on_headers_complete(http_parser* parser){
//Check whether we should keep-alive or close
custom_data_t *my_data = (custom_data_t*)parser->data;
my_data->keep_alive = http_should_keep_alive(parser);
return 0;
}
int on_header_field(http_parser* parser, const char* header, long unsigned int size){
printf("%s\n", header);
return 0;
}
void intHandler(int sig){
sconn->close();
exit(1);
}
/* handle connection after accept */
void *handle_connection(void *arg){
Connection* cconn= (Connection*) arg;
custom_data_t *my_data = (custom_data_t*)malloc(sizeof(custom_data_t));
my_data->conn = cconn;
my_data->keep_alive = 0;
my_data->url = nullptr;
http_parser *parser = (http_parser *) malloc(sizeof(http_parser));
if(parser == nullptr)
exit(1);
http_parser_init(parser, HTTP_REQUEST);
//pass connection and custom data to the parser
parser->data = (void *) my_data;
char buffer[INPUT_BUFFER_LENGTH]; //read buffer from the socket
bzero(buffer, INPUT_BUFFER_LENGTH);
size_t nparsed;
ssize_t nrecvd; //return value for for the read() and write() calls.
do{
//Since we only accept GET, just try to read INPUT_BUFFER_LENGTH
nrecvd = read_http_request(*cconn, buffer, INPUT_BUFFER_LENGTH -1);
if(nrecvd<0){
//if RST packet by browser, just close the connection
//no need to show an error.
if(errno != ECONNRESET){
LOG_ERROR("Error reading from socket");
printf("fd %d\n", cconn->getFd());
}
break;
}
nparsed = http_parser_execute(parser, &settings, buffer, nrecvd);
if(nrecvd == 0) break;
if(nparsed != nrecvd){
LOG_ERROR("Erorr in Parsing the request!");
}else{
//We only handle GET Requests
if(parser->method == 1)
{
//Write the response
writen(*cconn, RESPONSE_OK, sizeof(RESPONSE_OK));
}else{
//Method is not allowed
writen(*cconn, RESPONSE_METHOD_NOT_ALLOWED, sizeof(RESPONSE_METHOD_NOT_ALLOWED));
}
}
//reset data
my_data->url_length =0;
}while(my_data->keep_alive);
cconn->close();
free(my_data);
delete cconn;
free(parser);
}
int main(int argc, char* argv[]) {
if ( argc != 2 ){
printf( "USAGE: %s NUMBER_OF_THREADS\n", argv[0] );
return 0;
}
//set total number of worker threads
size_t thread_count = atoi(argv[1]);
//Create clusters based on MAXIMUM_THREADS_PER_CLUSTER
size_t cluster_count = (thread_count/(MAXIMUM_THREADS_PER_CLUSTER+1))+1;
struct sockaddr_in serv_addr; //structure containing an internet address
bzero((char*) &serv_addr, sizeof(serv_addr));
Cluster& defaultCluster = Cluster::getDefaultCluster();
//Create kThreads, default thread is already started --> i=1
kThread* kThreads[thread_count-1];
for(size_t i=1; i < thread_count-1; i++)
kThreads[i] = new kThread(defaultCluster);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
//handle SIGINT to close the main socket
signal(SIGINT, intHandler);
settings.on_headers_complete = on_headers_complete;
try{
sconn = new Connection(AF_INET, SOCK_STREAM, 0);
if(sconn->bind((struct sockaddr *) &serv_addr, sizeof(serv_addr))<0) {
LOG_ERROR("Error on binding");
exit(1);
};
sconn->listen(65535);
while(true){
Connection* cconn = sconn->accept((struct sockaddr*)nullptr, nullptr);
uThread::create()->start(defaultCluster, (void*)handle_connection, (void*)cconn);
}
sconn->close();
}catch(std::system_error& error){
std::cout << "Error: " << error.code() << " - " << error.what() << '\n';
sconn->close();
}
for(kThread* kt: kThreads) delete kt;
return 0;
}