-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_conn.cpp
1143 lines (967 loc) · 27.5 KB
/
http_conn.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "http_conn.h"
#include "http_respon_info.h"
int setnonblocking( int fd)
{
int old_opt = fcntl(fd, F_GETFL);
int new_pot = old_opt |O_NONBLOCK;
fcntl(fd,F_SETFL,new_pot);
return old_opt;
}
void addfd(int epollfd, int fd, bool one_shot)
{
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN| EPOLLET| EPOLLRDHUP;
if(one_shot)
{
event.events |= EPOLLONESHOT;
}
epoll_ctl(epollfd,EPOLL_CTL_ADD,fd, &event);
setnonblocking(fd);
}
void removefd(int epollfd, int fd)
{
epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,NULL);
close(fd);
}
void modfd(int epollfd, int fd, int ev_op)
{
epoll_event event;
event.data.fd = fd;
event.events = ev_op | EPOLLET| EPOLLONESHOT| EPOLLRDHUP;
epoll_ctl(epollfd,EPOLL_CTL_MOD,fd,&event);
}
int http_conn::m_user_count = 0;
int http_conn::m_epollfd = -1;
void http_conn::close_conn(bool real_close)
{
if(real_close && (m_sockfd != -1))
{
removefd(m_epollfd,m_sockfd);
m_sockfd = -1;
m_user_count --;
}
}
void http_conn::init( int sockfd, const sockaddr_in & addr)
{
m_sockfd = sockfd;
m_address = addr;
// to aovid TIME_WAIT, only for debug, remove in real establish
//int reuse = 1;
//setsockopt(m_sockfd,SOL_SOCKET,SO_REUSEADDR, & reuse, sizeof(reuse));
addfd( m_epollfd, sockfd, true);
m_user_count++;
init();
}
void http_conn::init()
{
m_check_state = CHECK_STATE_REQUESTLINE;
m_linger = false;
m_bigfile = false;
m_method = GET;
m_url = 0;
m_version = 0;
m_content_length = 0;
m_host = 0;
m_start_line = 0;
m_checked_idx = 0;
m_read_idx = 0;
m_write_idx = 0;
m_redis_request = 0;
m_mysql_request = 0;
m_content = 0;
memset(m_read_buf,'\0',READ_BUFFER_SIZE);
memset(m_write_buf,'\0',WRITE_BUFFER_SIZE);
memset(m_real_file,'\0',FILENAME_LEN);
memset(m_database_response,'\0',DATABASE_BUFFER_SIZE);
}
// slave state machine
http_conn::LINE_STATUS http_conn::parse_line()
{
char temp;
for(; m_checked_idx < m_read_idx; ++m_checked_idx)
{
temp = m_read_buf[m_checked_idx];
if( temp == '\r')
{
if((m_checked_idx+1) == m_read_idx)
{
return LINE_OPEN;
}
else if(m_read_buf[m_checked_idx + 1] == '\n')
{
m_read_buf[m_checked_idx++] = '\0';
m_read_buf[m_checked_idx++] = '\0';
return LINE_OK;
}
return LINE_BAD;
}
else if(temp == '\n')
{
if((m_checked_idx >1) && (m_read_buf[m_checked_idx-1] == '\r'))
{
m_read_buf[m_checked_idx++] = '\0';
m_read_buf[m_checked_idx++] = '\0';
return LINE_OK;
}
return LINE_BAD;
}
}
return LINE_OPEN;
}
bool http_conn::read()
{
if(m_read_idx >= READ_BUFFER_SIZE)
{
printf("idx bigger than read buffer size\n");
return false;
}
int bytes_read = 0;
while(true)
{
bytes_read = recv(m_sockfd,m_read_buf+m_read_idx,
READ_BUFFER_SIZE - m_read_idx,0);
if(bytes_read == -1)
{
if(errno == EAGAIN || errno == EWOULDBLOCK)
{
break;
}
return false;
}
else if( bytes_read == 0)
{
printf("maybe the client closed\n");
return false;
}
m_read_idx += bytes_read;
}
return true;
}
//to get the request method, URL ,and version
http_conn::HTTP_CODE http_conn::parse_request_line(char *text)
{
printf("in parse_request_line, we got text: %s\n",text);
m_url = strpbrk(text," \t");
printf("m_url is %s\n",m_url);
if( !m_url )
{
printf("no url!\n");
return BAD_REQUEST;
}
*m_url++ = '\0';
//parse the requst method
char* method = text;
if(strcasecmp(method,"GET") == 0)
{
printf("we got a GET request\n");
m_method = GET;
}
else if(strcasecmp(method,"POST") == 0)
{
printf("we got a POST request\n");
m_method = POST;
}
else if(strcasecmp(method,"PUT") == 0)
{
printf("we got a POST PUT\n");
m_method = PUT;
}
else if(strcasecmp(method,"DELETE") == 0)
{
printf("we got a DELETE request\n");
m_method = DELETE;
}
else
{
printf("unknowing requst method\n");
return BAD_REQUEST;
}
m_url += strspn( m_url, " \t");
m_version = strpbrk( m_url, " \t");
if(!m_version)
{
printf("no version!\n");
return BAD_REQUEST;
}
*m_version++ = '\0';
m_version += strspn( m_version, " \t");
if ( strcasecmp( m_version, "HTTP/1.1") != 0)
{
printf("wrong HTTP version\n");
return BAD_REQUEST;
}
if( strncasecmp( m_url, "http://", 7) == 0)
{
m_url += 7;
m_url = strchr(m_url, '/');
}
if( !m_url || m_url[0] != '/')
{
printf("wrong url\n");
return BAD_REQUEST;
}
m_check_state = CHECK_STATE_HEADER;
return NO_REQUEST;
}
//parse the header
http_conn::HTTP_CODE http_conn::parse_headers( char* text)
{
//when we got a empty line
//finish the parse
if( text[0] == '\0')
{
//if threre is request body
if(m_content_length != 0)
{
printf("content is not empty\n");
m_check_state = CHECK_STATE_CONTENT;
return NO_REQUEST;
}
//otherwise, we got a complete HTTP request
return GET_REQUEST;
}
//process Connection
else if( strncasecmp(text, "Connection:", 11) == 0)
{
text += 11;
text += strspn(text, "\t");
if(strcasecmp(text, "keep-alive") == 0)
{
m_linger = true;
}
}
//process Content-Length
else if(strncasecmp(text, "Content-Length:",15) == 0)
{
text += 15;
text += strspn(text,"\t");
m_content_length = atol(text);
}
//process Host
else if(strncasecmp(text, "Host:", 5) == 0)
{
text +=5;
text += strspn(text,"\t");
m_host = text;
}
else if(strncasecmp(text, "BigFileUpload:", 14) == 0)
{
text += 14;
text += strspn(text,"\t");
if(strcasecmp(text, "Yes") == 0)
{
m_bigfile = true;
}
}
else
{
printf("unknown header %s\n",text);
}
return NO_REQUEST;
}
//HTTP request body, we don't parse it
http_conn::HTTP_CODE http_conn::parse_content(char* text)
{
//we dont parse content here
// just get content message
// and parse it in each api
printf("in paser_content\n");
if(m_content_length == 0 && m_method == GET)
{
return GET_REQUEST;
}
else if(m_content_length == 0)
{
printf("not GET request and no content\n");
return BAD_REQUEST;
}
else
{
m_content = text;
//need to be check here;
text[m_content_length] = '\0';
}
/*
if (m_read_idx >= (m_content_length + m_checked_idx))
{
text[m_content_length] = '\0';
return GET_REQUEST;
}
*/
return NO_REQUEST;
}
http_conn::HTTP_CODE http_conn::process_read()
{
LINE_STATUS line_status = LINE_OK;
HTTP_CODE ret = NO_REQUEST;
char* text = 0;
while( ( (m_check_state == CHECK_STATE_CONTENT) && (line_status == LINE_OK) )
|| ( ( line_status = parse_line() ) == LINE_OK ))
{
//get the text start positon in buffer;
text = get_line();
//point the start position in the end;
m_start_line = m_checked_idx;
printf("got 1 http line: %s\n", text);
switch( m_check_state )
{
case CHECK_STATE_REQUESTLINE:
{
printf("in CHECK_STATE_REQUESTLINE\n");
ret = parse_request_line(text);
if(ret == BAD_REQUEST)
{
printf("its a bad request\n");
return BAD_REQUEST;
}
break;
}
case CHECK_STATE_HEADER:
{
printf("in CHECK_STATE_HEADER\n");
ret = parse_headers(text);
if(ret == BAD_REQUEST)
{
printf("its a bad request\n");
return BAD_REQUEST;
}
else if(ret == GET_REQUEST)
{
printf("its a correct get request\n");
return do_request();
}
break;
}
case CHECK_STATE_CONTENT:
{
//
printf("in CHECK_STATE_CONTENT\n");
if( m_bigfile)
{
//dosomething for bigfile
line_status = LINE_OPEN;
return NO_REQUEST;
}
ret = parse_content(text);
if( ret == BAD_REQUEST )
{
return BAD_REQUEST;
}
return do_request();
break;
}
default:
return INTERNAL_ERROR;
}
}
return NO_REQUEST;
}
//when we got a correct complete HTTP request
//then analyse the target file
http_conn::HTTP_CODE http_conn::do_request()
{
printf("in do request\n");
//parse api request here;
if(strncasecmp(m_url,"/login",6) == 0)
{
return api_login();
}
else if (strncasecmp(m_url,"/artical",8) == 0)
{
return api_artical();
}
else if (strncasecmp(m_url,"/signup",8) == 0)
{
return api_signup();
}
else if (strncasecmp(m_url,"/unlike",7) == 0)
{
return api_like(false);
}
else if(strncasecmp(m_url,"/like",5) == 0)
{
return api_like(true);
}
else if(strlen(m_url) != 1)
{
printf("we dont support other api now\n");
return BAD_REQUEST;
}
//process file request here:
//the file path inited to the root path
//strcpy( m_real_file, doc_root);
//int len = strlen(doc_root);
strcpy(m_real_file,default_html);
int len = strlen(default_html);
//add url to the tail of root path
//strncpy( m_real_file + len, m_url, FILENAME_LEN - len -1);
printf("file is %s \n",m_real_file);
//get file attributs and put it to the buf
if( stat(m_real_file, &m_file_stat) <0 )
{
printf("We got no this file\n");
return NO_RESOURCE;
}
if ( ! (m_file_stat.st_mode & S_IROTH))
{
printf("This file cannot be access\n");
return FORBIDDEN_REQUEST;
}
if( S_ISDIR( m_file_stat.st_mode))
{
printf("The url is a DIR, not a file name\n");
return BAD_REQUEST;
}
//map file to the shared memory
int fd = open( m_real_file, O_RDONLY);
m_file_address = (char *)mmap( 0, m_file_stat.st_size, PROT_READ,
MAP_PRIVATE, fd, 0 );
close(fd);
return FILE_REQUEST;
}
http_conn::HTTP_CODE http_conn::api_login()
{
if(m_method != POST)
{
printf("login must be POST method!\n");
return BAD_REQUEST;
}
account_t m_account;
char Account_buf[30];
memset(Account_buf,'\0',30);
char Password_buf[30];
memset(Password_buf,'\0',30);
char * ptr = 0;
m_account.Account = 0;
m_account.Password = 0;
m_content = strstr(m_content,"Account:");
if(m_content && strncasecmp(m_content,"Account:", 8) == 0)
{
strncpy(Account_buf, m_content,30);
// partition
ptr = strpbrk(Account_buf,"\r");
*ptr = '\0';
ptr = strpbrk(Account_buf," \t");
m_account.Account = ptr+1;
m_content = strpbrk(m_content, "\n");
*m_content++ ='\0';
}
m_content = strstr(m_content,"Password:");
if(m_content && strncasecmp(m_content,"Password:",9) == 0)
{
strncpy(Password_buf, m_content,30);
// partition
ptr = strpbrk(Password_buf,"\r");
*ptr = '\0';
ptr = strpbrk(Password_buf," \t");
m_account.Password = ptr+1;
m_content = strpbrk(m_content, "\n");
*m_content++ ='\0';
}
if(!m_account.Account || !m_account.Password)
{
return BAD_REQUEST;
}
char query[100];
memset(query,'\0',100);
sprintf(query,"SELECT password FROM user_account WHERE account = %s",m_account.Account);
printf("query is %s\n",query);
MYSQL* m_mysql_connect = mysql_init(NULL);
m_mysql_connect = mysql_real_connect(m_mysql_connect,"localhost","van","van653","account",0,NULL,0);
if(m_mysql_connect == NULL)
{
printf("MYSQL ERROR\n");
return INTERNAL_ERROR;
}
int res = mysql_query(m_mysql_connect,query);
if(res)
{
printf("mysql_query: %s\n", mysql_error(m_mysql_connect));
return BAD_REQUEST;
}
MYSQL_RES *result = mysql_store_result(m_mysql_connect);
MYSQL_ROW row = mysql_fetch_row(result);
mysql_free_result(result);
if(!row)
{
printf("have not this user account\n");
return BAD_LOGIN_REQUEST;
}
printf("password is %s, len is %d\n",m_account.Password,strlen(m_account.Password));
printf("row[0] is %s, len is %d\n",row[0], strlen(row[0]));
char reply_password[30];
sprintf(reply_password,"'%s'",row[0]);
printf("reply password is %s\n",reply_password);
if(strcmp(m_account.Password,reply_password) == 0)
{
printf("right password\n");
return SUCCESS_LOGIN_REQUEST;
}
printf("wrong password\n");
return BAD_LOGIN_REQUEST;
}
http_conn::HTTP_CODE http_conn::api_signup()
{
printf("in signup\n");
if(m_method != POST)
{
printf("signup must be requested by POST\n");
return BAD_REQUEST;
}
account_t m_account;
char Account_buf[30];
memset(Account_buf,'\0',30);
char Password_buf[30];
memset(Password_buf,'\0',30);
char Username_buf[30];
memset(Username_buf,'\0',30);
m_account.Account = 0;
m_account.Password = 0;
m_account.Username = 0;
char * ptr = 0;
m_content = strstr(m_content,"Account:");
if(m_content && strncasecmp(m_content,"Account:", 8) == 0)
{
strncpy(Account_buf, m_content,30);
// partition
printf("Account_buf is %s\n",Account_buf);
ptr = strpbrk(Account_buf,"\r");
*ptr = '\0';
ptr = strpbrk(Account_buf," \t");
m_account.Account = ptr+1;
m_content = strpbrk(m_content, "\n");
*m_content++ ='\0';
}
m_content = strstr(m_content,"Password:");
if(m_content && strncasecmp(m_content,"Password:",9) == 0)
{
strncpy(Password_buf, m_content,30);
// partition
printf("Password_buf is %s\n",Password_buf);
ptr = strpbrk(Password_buf,"\r");
*ptr = '\0';
ptr = strpbrk(Password_buf," \t");
m_account.Password = ptr+1;
m_content = strpbrk(m_content, "\n");
*m_content++ ='\0';
}
m_content = strstr(m_content,"Username:");
if(m_content && strncasecmp(m_content,"Username:",9) == 0)
{
strncpy(Username_buf, m_content,30);
// partition
printf("Username_buf is %s\n",Password_buf);
ptr = strpbrk(Username_buf,"\r");
*ptr = '\0';
ptr = strpbrk(Username_buf," \t");
m_account.Username = ptr+1;
m_content = strpbrk(m_content, "\n");
*m_content++ ='\0';
}
if(!m_account.Username){m_account.Username = "'default name'";}
if(!m_account.Account || !m_account.Password || !m_account.Username )
{
return BAD_REQUEST;
}
char query[100];
memset(query,'\0',100);
sprintf(query,"INSERT INTO user_account values( %s , %s , %s, 1, CURDATE())",
m_account.Account,m_account.Password,m_account.Username);
printf("query is %s\n",query);
MYSQL* m_mysql_connect = mysql_init(NULL);
m_mysql_connect = mysql_real_connect(m_mysql_connect,"localhost","van","van653","account",0,NULL,0);
if(m_mysql_connect == NULL)
{
printf("MYSQL ERROR\n");
return INTERNAL_ERROR;
}
int res = mysql_query(m_mysql_connect,query);
if(res)
{
printf("mysql_query: %s\n", mysql_error(m_mysql_connect));
return BAD_SIGNUP_REQUEST;
}
printf("SIGNUP successfully\n");
return SUCCESS_SIGNUP_REQUEST;
}
http_conn::HTTP_CODE http_conn::api_like(bool incrase)
{
if(m_method != PUT)
{
printf("wrong method in like\n");
return BAD_REQUEST;
}
zset_t m_zset;
char setname_buf[50];
char username_buf[50];
char score_buf[30];
memset(setname_buf,'\0',sizeof(setname_buf));
memset(username_buf,'\0',sizeof(username_buf));
memset(score_buf,'\0',sizeof(score_buf));
m_zset.set_name = 0;
m_zset.user_name = 0;
char * ptr;
if(strncasecmp(m_content,"Setname:", 8) == 0)
{
strncpy(setname_buf, m_content,30);
// partition
ptr = strpbrk(setname_buf,"\r");
*ptr = '\0';
ptr = strpbrk(setname_buf," \t");
m_zset.set_name = ptr+1;
m_content = strpbrk(m_content, "\n");
*m_content++ ='\0';
}
if(strncasecmp(m_content,"Username:", 9) == 0)
{
strncpy(username_buf, m_content,30);
// partition
ptr = strpbrk(username_buf,"\r");
*ptr = '\0';
ptr = strpbrk(username_buf," \t");
m_zset.user_name = ptr+1;
m_content = strpbrk(m_content, "\n");
*m_content++ ='\0';
}
if(!m_zset.set_name || !m_zset.user_name)
{
return BAD_REQUEST;
}
int score = incrase?1:-1;
char query[100];
memset(query,'\0',100);
sprintf(query,"zincrby %s %d %s",m_zset.set_name, score,m_zset.user_name);
printf("query is %s\n",query);
redisContext* m_redis_connect = redisConnect("127.0.0.1",6379);
if(m_redis_connect == NULL || m_redis_connect->err)
{
printf("REDIS ERROR\n");
return INTERNAL_ERROR;
}
redisReply* _reply = (redisReply*)redisCommand(m_redis_connect,query);
if(_reply->type == REDIS_REPLY_NIL)
{
printf("none request\n");
freeReplyObject(_reply);
redisFree(m_redis_connect);
return BAD_LIKE_REQUEST;
}
else
{
printf("redis reply is %s\n",_reply->str);
printf("good,request\n");
freeReplyObject(_reply);
redisFree(m_redis_connect);
return SUCCESS_LIKE_REQUEST;
}
}
http_conn::HTTP_CODE http_conn::api_artical(){}
//unmap the shared memory
void http_conn::unmap()
{
if(m_file_address)
{
munmap(m_file_address,m_file_stat.st_size);
m_file_address = 0;
}
}
//write the HTTP response
bool http_conn::write()
{
int temp = 0;
int bytes_have_send = 0;
int bytes_to_send = m_write_idx;
//if no data to send, re-init the connection
printf("in write\n");
if(bytes_to_send == 0)
{
printf("no data to send\n");
modfd( m_epollfd, m_sockfd, EPOLLIN);
init();
return true;
}
while(1)
{
temp = writev( m_sockfd, m_iv, m_iv_count);
if(m_iv_count == 1)
{
printf("i send what\n %s\n",m_iv[0].iov_base);
}
else if(m_iv_count == 2)
{
printf("i send what\n %s\n %s\n",m_iv[0].iov_base, m_iv[1].iov_base);
}
else
{
printf("m_iv_count == 0\n");
}
if( temp <= -1)
{
//if TCP write buffer got no space
//wait for the next EPOLLOUT event
if(errno == EAGAIN)
{
printf("no enough space for writev, wait for next time\n");
modfd( m_epollfd, m_sockfd, EPOLLOUT);
return true;
}
printf("something wrong in writev\n");
unmap();
return false;
}
bytes_to_send -= temp;
bytes_have_send +=temp;
//respon successfully
if(bytes_to_send <= bytes_have_send)
{
printf("write finished\n");
unmap();
// if keep alive
// re-init connection
if( m_linger)
{
printf("dont close, keep alive!\n");
init();
modfd( m_epollfd, m_sockfd, EPOLLIN);
return true;
}
//otherwise
else
{
printf("close the connection\n");
modfd(m_epollfd,m_sockfd, EPOLLIN);
return false;
}
}
}
}
//write data to write buf
bool http_conn::add_response(const char* format, ...)
{
if( m_write_idx >= WRITE_BUFFER_SIZE)
{
printf("write data size exceeds the limit\n");
return false;
}
va_list arg_list;
va_start( arg_list,format);
int len = vsnprintf(m_write_buf + m_write_idx, WRITE_BUFFER_SIZE-1-m_write_idx,
format,arg_list);
if( len >= (WRITE_BUFFER_SIZE -1 -m_write_idx))
{
return false;
}
m_write_idx +=len;
va_end(arg_list);
return true;
}
bool http_conn::add_status_line ( int status,const char* title)
{
return add_response("%s %d %s\r\n","HTTP/1.1", status,title);
}
bool http_conn::add_headers( int content_len)
{
add_content_length( content_len );
add_linger();
add_blank_line();
}
bool http_conn::add_content_length( int content_len )
{
return add_response( "Content-Length: %d\r\n", content_len);
}
bool http_conn::add_linger()
{
return add_response( "Connection: %s\r\n",
(m_linger == true)?"keep-alive":"close");
}
bool http_conn::add_blank_line()
{
return add_response( "%s", "\r\n");
}
bool http_conn::add_content( const char* content)
{
return add_response("%s",content);
}
//accroding to the process result
//response to the client
bool http_conn::process_write(HTTP_CODE ret)
{
switch(ret)
{
case INTERNAL_ERROR:
{
add_status_line( 500, error_500_title);
add_headers( strlen( error_500_form));
if( !add_content( error_500_form) )
{
printf("in INTERNAL_ERROR, add content failed\n");
return false;
}
break;
}
case BAD_REQUEST:
{
add_status_line(400, error_400_title);
add_headers( strlen(error_400_form));
if( !add_content( error_400_form))
{
printf("in BAD_REQUEST, add content failed\n");
return false;
}
break;
}
case FORBIDDEN_REQUEST:
{
add_status_line(403, error_403_title);
add_headers(strlen(error_403_form));
if( ! add_content(error_403_form))
{
printf("in FORBIDDEN_REQUEST, add content failed\n");
return false;
}
break;
}
case FILE_REQUEST:
{
add_status_line( 200 , ok_200_title);
if( m_file_stat.st_size != 0)
{
add_headers( m_file_stat.st_size);
m_iv[0].iov_base = m_write_buf;
m_iv[0].iov_len = m_write_idx;
m_iv[1].iov_base = m_file_address;
m_iv[1].iov_len = m_file_stat.st_size;
m_iv_count = 2;
return true;
}
else
{