-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-server.c
More file actions
411 lines (366 loc) · 11.1 KB
/
http-server.c
File metadata and controls
411 lines (366 loc) · 11.1 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* http-server */
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <assert.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
void *Create_80_port(void *);
void *Create_443_port(void *);
void *Handle_HTTP_Request(int cs);
void *Handle_HTTPS_Request(void *args);
void https_serve_response(SSL *ssl,char *filename, int range_start, int range_end);
void cat(SSL *ssl, FILE *resource, int range_start, int range_end);
SSL_CTX * InitSSL();
struct param
{
SSL *ssl;
int csock;
};
int main(int argc, const char *argv[]){
// create 2 threads for listening 80 and 443 ports
pthread_t new_thread_80;
pthread_t new_thread_443;
if (pthread_create(&new_thread_80, NULL, Create_80_port, NULL) != 0)
perror("pthread_create failed!");
if (pthread_create(&new_thread_443, NULL, Create_443_port, NULL) != 0)
perror("pthread_create failed!");
while(1){
sleep(1);
}
return 0;
}
/************************************************************************************************/
void *Create_80_port(void * no){
int s, cs;
struct sockaddr_in server, client;
// create socket
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("create socket failed!");
fflush(stdout);
return NULL;
}
printf("socket created.\n");
// prepare the sockaddr_in structure
int port_num = 80;
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port_num);
// bind socketfd with the addr
if (bind(s,(struct sockaddr *)&server, sizeof(server)) < 0) {
perror("bind failed!");
fflush(stdout);
return NULL;
}
printf("bind done.\n");
// listen
listen(s, 100);
printf("waiting for incoming connections...\n");
while (1)
{
// accept connection from an incoming client
int c = sizeof(struct sockaddr_in);
if ((cs = accept(s, (struct sockaddr *)&client, (socklen_t *)&c)) < 0) {
perror("accept failed!");
fflush(stdout);
return NULL;
}
printf("connection accepted from port 80.\n");
// create a thread to handle the request
pthread_t new_thread;
printf("csock:%d\n",cs);
if (pthread_create(&new_thread, NULL, Handle_HTTP_Request, cs) != 0)
perror("pthread_create failed");
}
}
/*
* all http requests return 301, new https URL
* GET http://10.0.0.1/index.html HTTP/1.1\r\n or notfound.html or dir/index.html
* Range: bytes=100-200\r\n
* ......
*/
void get_path(char *msg,char *path){
char *ptr;
ptr=strstr(msg,"/");
int i=0;
while(*ptr!=' '){
path[i]=*ptr;
ptr++;
i++;
}
path[i]='\0';
}
void *Handle_HTTP_Request(int csock){
printf("csock:%d\n",csock);
char msg[256];
int msg_len = 0;
// receive a message from client
while ((msg_len = recv(csock, msg, sizeof(msg), 0)) > 0) {
/* parse the request */
//only support GET method
char path[50];
char url[100];
memset(path,0,sizeof(path));
printf("%s\n",msg);
get_path(msg, path);
strcpy(url,"https://10.0.0.1");
strcat(url,path);
printf("%s\n",url);
/* *******************************
* respond with 301 new location:
* HTTP/1.1 301 Moved Permanently
* Location: https://10.0.0.1/index.html
* *******************************/
char buf[1024];
strcpy(buf, "HTTP/1.1 301 Moved Permanently\r\n");
strcat(buf, "Location: ");
strcat(buf,url);
strcat(buf,"\r\n\r\n\r\n");
printf("%s\n",buf);
write(csock, buf, strlen(buf));
// close server socket
close(csock);
}
if (msg_len == 0) {
printf("client disconnected!");
fflush(stdout);
}
return;
}
/**************************************************************************************************/
void *Create_443_port(void * no){
// init ssl certification
SSL_CTX *ctx = InitSSL();
if(ctx==NULL){
perror("init ssl error!");
return NULL;
}
// create socket
int s, cs;
struct sockaddr_in server, client;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("create socket failed!");
fflush(stdout);
return NULL;
}
printf("secure socket created.\n");
// prepare the sockaddr_in structure
int port_num = 443;
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port_num);
// bind socketfd with the addr
if (bind(s,(struct sockaddr *)&server, sizeof(server)) < 0) {
perror("bind failed!");
fflush(stdout);
return NULL;
}
printf("bind done.\n");
// listen
listen(s, 100);
printf("waiting for incoming connections...\n");
SSL *ssl;
while (1)
{
// accept connection from an incoming client
int c = sizeof(struct sockaddr_in);
if ((cs = accept(s, (struct sockaddr *)&client, (socklen_t *)&c)) < 0) {
perror("accept failed!");
fflush(stdout);
return NULL;
}
// create ssl connection
ssl = SSL_new(ctx);
if(ssl == NULL){
perror("ssl new wrong!");
return NULL;
}
printf("new ctx;\n");
SSL_set_accept_state(ssl);
// connect ssl with socket fd
printf("set accept state\n");
SSL_set_fd(ssl, cs);
// ssl handshake
printf("set fd\n");
if (SSL_accept(ssl) == -1){
perror("ssl get error!");
return NULL;
}
printf("ssl connection accepted.\n");
// create a thread to handle ssl_read/write
pthread_t new_thread;
struct param param1 = {ssl,cs};
if (pthread_create(&new_thread, NULL, Handle_HTTPS_Request, ¶m1) != 0)
perror("pthread_create failed");
}
// ssl shutdown
SSL_shutdown(ssl);
SSL_free(ssl);
// close server socket
close(cs);
SSL_CTX_free(ctx);
}
SSL_CTX * InitSSL(){
// init ssl environment
SSL_library_init();
SSL_load_error_strings();
// create ssl context
SSL_CTX *ctx = SSL_CTX_new(SSLv23_method());
// load server's cert and private key
SSL_CTX_use_certificate_file(ctx, "./keys/cnlab.cert", SSL_FILETYPE_PEM);
SSL_CTX_use_PrivateKey_file(ctx, "./keys/cnlab.prikey", SSL_FILETYPE_PEM);
// 判定私钥是否正确
assert(SSL_CTX_check_private_key(ctx));
return ctx;
}
/*
* GET https://10.0.0.1/index.html HTTP/1.1\r\n or notfound.html or dir/index.html
* Range: bytes=100-200\r\n
* ......
*/
void *Handle_HTTPS_Request(void *args){
struct param *p = (struct param *)args;
SSL *ssl = (*p).ssl;
int csock = (*p).csock;
char msg[256];
int msg_len = 0;
// receive a message from client and parse the request
if((msg_len = SSL_read(ssl, msg, sizeof(msg))) > 0) {
/* parse the request */
char *str_start = NULL;
int range_start = -1;
int range_end = -1;
char path[50];
memset(path,0,sizeof(path));
path[0]='.';
printf("%s\n",msg);
char *ptr;
//only support GET method
ptr=&msg[4];
int i=1;
while(*ptr!=' '){
path[i]=*ptr;
ptr++;
i++;
}
printf("path:%s\n",path);
str_start = strstr(msg,"Range");
if (str_start != NULL)
{
str_start += 13;
char tmp[10];
int i=0;
while (*str_start != '-'){
tmp[i++]=*str_start;
str_start++;
}
tmp[i]='\0';
range_start=atoi(tmp);
i=0;str_start++;
while (*str_start != '\r'){
tmp[i++]=*str_start;
str_start++;
}
if (i!=0){
tmp[i]='\0';
range_end=atoi(tmp)+1;
}
}
/* look up the file and make response */
printf("args:%p,%p,%d,%d\n",ssl,path,range_start,range_end);
https_serve_response(ssl,path, range_start, range_end);
}
if (msg_len == 0) {
printf("client disconnected!");
fflush(stdout);
}
return NULL;
}
void https_serve_response(SSL *ssl,char *filename, int range_start, int range_end)
{
FILE *file = NULL;
struct stat file_stat;
char buf[1024];
char *buffer = NULL;
memset(buf,0,1024);
//SSL *ssl = NULL;
if (stat(filename, &file_stat) == -1) {
/*no such file*/
strcpy(buf, "HTTP/1.1 404 File Not Found\r\n");
strcat(buf,"Server: 10.0.0.1\r\n");
strcat(buf,"Connection: close\r\n");
strcat(buf,"Content-Length: 0\r\n");
strcat(buf,"\r\n");
SSL_write(ssl, buf, strlen(buf));
}else {
if((file = fopen(filename, "r"))==NULL){
perror("open failed");
return;
}
fseek(file, 0, SEEK_END);
int st_size = ftell(file);
int state_range = 0;
if(range_start != -1){
if(range_end != -1)
st_size = range_end - range_start;
else{
range_end = st_size;
st_size = st_size - range_start;
}
state_range = 1;
}else{
range_start = 0;
if(range_end != -1)
st_size = range_end - range_start;
else{
range_end = st_size;
st_size = st_size - range_start;
}
}
fseek(file, 0, 0);
/*send HTTP header */
buffer = (char *)malloc(2000);
memset(buffer,0,2);
if(!state_range)
strcpy(buffer, "HTTP/1.1 200 OK\r\n");
else
strcpy(buffer, "HTTP/1.1 206 Partial Content\r\n");
sprintf(buf, "Content-Length: %d\r\n",st_size);
strcat(buffer,buf);
strcpy(buf,"Server: 10.0.0.1\r\n");
strcat(buffer,buf);
strcat(buffer,"\r\n");
SSL_write(ssl, buffer, strlen(buffer));
/*send file body*/
cat(ssl, file ,range_start, range_end);
fclose(file);
free(buffer);
}
}
/**********************************************************************/
/* Put the entire contents of a file out on a socket.
* Parameters: the client socket descriptor
* FILE pointer for the file to cat */
/**********************************************************************/
void cat(SSL *ssl, FILE *resource, int range_start, int range_end)
{
int total_size = range_end - range_start;
int send_size = 0;
fseek(resource,range_start,SEEK_SET);
char buf[1024];
while(send_size < total_size){
if(total_size - send_size >= 1024){
send_size+=fread(buf,1,1024,resource);
SSL_write(ssl,buf,1024);
}else{
int sz = total_size - send_size;
send_size+=fread(buf,1,total_size - send_size,resource);
SSL_write(ssl,buf,sz);
}
}
}