-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserverv5.c
More file actions
3094 lines (2399 loc) · 75.9 KB
/
httpserverv5.c
File metadata and controls
3094 lines (2399 loc) · 75.9 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
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
//gcc -o dsim httpserverv5.c -lpthread -g -Wall
//writen by ning 2012.11.26
// 2015-4 增加匀速发送文件测试
// 2015-4-21 改成cpp编译,清除warning
#define _GNU_SOURCE
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <poll.h>
#include <sys/sendfile.h>
#include <sys/select.h>
#include <signal.h>
//#define __x86_64__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct { int counter; } atomic_t;
#define ATOMIC_INIT(i) { (i) }
#define LOCK_PREFIX "lock ; "
static inline int atomic_read(const atomic_t *v)
{
return (*(volatile int *)&(v)->counter);
}
static inline void atomic_inc(atomic_t *v)
{
asm volatile(LOCK_PREFIX "incl %0"
: "+m" (v->counter));
}
static inline void atomic_dec(atomic_t *v)
{
asm volatile(LOCK_PREFIX "decl %0"
: "+m" (v->counter));
}
/**********************************************************************************************************/
struct obj_map
{
char obj_id[256];
char uri[256];
};
#define MAX_OBJMAP_NUM 58000
typedef unsigned uid_type_t;
struct obj_mem
{
struct obj_map obj_map_list[MAX_OBJMAP_NUM];
uid_type_t id_list[MAX_OBJMAP_NUM];
int obj_map_num ;
int start_index[10];
};
struct obj_mem g_local ;
struct obj_mem g_cache ;
/**********************************************************************************************************/
typedef struct
{
// you must use lock by yourself
int element_num; //the arry queue size
int head; //if (head == tail) , queue is empty
int tail; //if (tail == head-1) , queue is full
} queue_cb;
//
void init_queue(queue_cb *qcb, int element_num)
{
qcb->head = qcb-> tail = 0;
qcb->element_num = element_num ;
}
int get_queue_count(queue_cb *qcb)
{
return (qcb->tail + qcb->element_num -qcb->head) % qcb->element_num;
}
// 0 is not empty
int is_queue_empty(queue_cb *qcb)
{
return (qcb->head==qcb->tail);
}
// 0 is not full
int is_queue_full(queue_cb *qcb)
{
return ((qcb->tail + 1) % qcb->element_num == qcb->head );
}
//return >=0 , the index of the queue tail element
int en_queue(queue_cb *qcb)
{
int old_tail = qcb->tail;
int ptr = (old_tail + 1) % qcb->element_num;
if (ptr==qcb->head)
{
//queue full
return -1;
}
qcb->tail = ptr;
return old_tail;
}
//return >=0 , the index of the queue head element
int de_queue(queue_cb *qcb)
{
if ( is_queue_empty(qcb) )
{
//empty queue
return -1;
}
int old_head = qcb->head;
qcb->head = (old_head + 1)%qcb->element_num;
return old_head;
}
/**********************************************************************************************************/
typedef struct
{
int sock;
int file_fd;
off_t read_pos; //readed position
struct timeval start_tv;
//struct timeval next_send_tv;
}
http_req_cb;
http_req_cb todo[1024];
queue_cb todo_cb;
static pthread_mutex_t cb_lock = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;
#define MAX_STREAM_PERTHREAD 4096
/**********************************************************************************************************/
#ifndef WIN32
char * getTimeStr(char *str, int len)
{
struct tm *t, tbuf;
time_t tsec;
time(&tsec);
t = localtime_r(&tsec, &tbuf);
snprintf(str, len, "%02d-%02d-%02d:%02d:%02d:%02d", t->tm_year-100, t->tm_mon+1,
t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
return str;
}
#define LOG_FILE "/dev/shm/dsim.log"
//#define LOG_FILE_ROTATE "/var/log/dsim.log"
#define LOG_FILE_ROTATE LOG_FILE
int daemon(int nochdir, int noclose)
{
int fd;
struct stat statbuf;
char mvfname[256];
char time_str[100];
switch (fork()) {
case -1: return -1;
case 0: break;
default: _exit(0);
}
if (setsid()==-1) return -1;
if (noclose) return 0;
if (stat(LOG_FILE, &statbuf) != -1)
{ // log file exists. rename it
sprintf(mvfname, "%s-%s", LOG_FILE_ROTATE , getTimeStr(time_str, sizeof(time_str)));
if (rename(LOG_FILE, mvfname) != -1)
{
printf("Renamed %s to %s\n", LOG_FILE, mvfname);
}
else
{
printf("Failed to rename %s to %s. %s.\n",
LOG_FILE, mvfname, strerror(errno));
}
}
fd=open(LOG_FILE, O_CREAT|O_RDWR|O_APPEND, 0);
if (fd!=-1) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd>2) close(fd);
//fd= open("/var/log/dsim.log" ,O_CREAT|O_RDWR|O_APPEND,0660);
//dup2(fd, STDOUT_FILENO);
}
else
{
printf("open log err %s\n", strerror(errno));
exit(1);
}
printf("daemon...done\n");
return 0;
}
#endif
const char *logPrefix[3] =
{
" ",
" [ERROR]: ",
" [WARNING]: "
};
/********* LOG MARCO REDEFINED*************/
#define RSSLOG_LV_ERROR 0x8000
#define RSSLOG_LV_WARNING 0x4000
#define RSSLOG_LV_INFO 0x2000
#define RSSLOG_LV_TRACE 0x1000
//#define RSSLOG_LV_MASK (RSSLOG_LV_ERROR|RSSLOG_LV_WARNING|RSSLOG_LV_INFO)
#define RSSLOG_LV_MASK (RSSLOG_LV_ERROR|RSSLOG_LV_WARNING|RSSLOG_LV_INFO|RSSLOG_LV_TRACE)
volatile int logSize = 0;
#define TRACE printf
//#define TRACE
int print_log(int level, const char *fmt, ...)
{
int r;
struct tm *t, tbuf;
struct timeval t_currentTime;
va_list ap;
const char *prefix;
char log_line[1024];
if ((level & RSSLOG_LV_MASK) == 0)
return 0;
if ((level & RSSLOG_LV_ERROR) != 0)
{
prefix = logPrefix[1];
}
else if ((level & RSSLOG_LV_WARNING) != 0)
{
prefix = logPrefix[2];
}
else
{
prefix = logPrefix[0];
}
va_start(ap, fmt);
gettimeofday(&t_currentTime, (struct timezone *)0);
if (t_currentTime.tv_usec < 0)
{
t_currentTime.tv_sec--;
t_currentTime.tv_usec += 1000000;
}
else if (t_currentTime.tv_usec >= 1000000)
{
t_currentTime.tv_sec++;
t_currentTime.tv_usec -= 1000000;
}
t = localtime_r(&(t_currentTime.tv_sec), &tbuf);
r = sprintf( log_line, "%02d%02d%02d-%02d%02d%02d.%06d-%ld%s",
t->tm_year-100, t->tm_mon+1,
t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
(unsigned int)(t_currentTime.tv_usec), syscall(SYS_gettid), prefix);
//logSize += r;
vsnprintf(log_line+r , sizeof(log_line)-r, fmt, ap);
//r = vprintf(fmt, ap);
printf( "%s", log_line );
logSize += r;
va_end(ap);
if (logSize>512*1024)
{
//fflush(stdout);
//logSize = 0;
}
return r;
}
char* safe_strncpy(char* dst, const char* src, size_t size)
{
//assert((dst!=NULL) && (src!=NULL));
char* retAddr = dst; /**< retAddr is in static , char retAddr[] will in Stack, So... */
int i = 0;
while (((*(dst++) = *(src++))!='\0') && ((i++) < size))
{ ; }
*(retAddr+size-1)='\0'; /**< cut off String */
return retAddr;
}
#define RW_MAX_TTL 16
static ssize_t writen_ex(int fd, const void *buf, size_t size, int ms)
{
ssize_t offset = 0, left = size, len;
int ttl = RW_MAX_TTL;
struct pollfd pfd;
int i;
while (left > 0) {
pfd.fd = fd;
pfd.events = POLLOUT | POLLERR | POLLHUP | POLLNVAL;
errno = 0;
i = poll(&pfd, 1, ms);
if (i < 0)
{
if (errno == EINTR){
if (--ttl<=0) {
fprintf(stdout, "[writen_ex] ERROR: writen poll ttl overflow, errno:%d size:%lu\n" , errno, size);
return -6;
}
continue;
}
fprintf(stdout, "[writen_ex]: writen errno:%d\n" , errno );
return -1; // error
} else if (0==i) {
fprintf(stdout, "[writen_ex]: writen poll timeout\n" );
return -2;
}
if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
{
fprintf(stdout, "[writen_ex] ERROR: writen err revents:%x errno:%d len:%lu\n" , pfd.revents, errno, size);
return -3;
}
len = write(fd, (char*)buf + offset, left);
if (len < 0)
{
if (errno == EINTR){
if (--ttl<=0) {
fprintf(stdout, "[BMDP]: writen write ttl overflow:%d\n" , errno);
return -6;
}
continue;
}
return -4;
}
offset += len;
left -= len;
}
return offset;
}
static ssize_t writen(int fd, const void *buf, size_t size )
{
return writen_ex( fd, buf ,size , 2000);
}
static int read_time(int fd, void *buf, int size, int ms)
{
ssize_t len = 0;
int ttl = RW_MAX_TTL ;
struct pollfd pfd;
int i;
while (1) {
pfd.fd = fd;
pfd.events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
errno = 0;
i = poll(&pfd, 1, ms );
if (i < 0)
{
if (errno == EINTR){
if (--ttl<=0) {
fprintf(stdout, "[readn_ex] ERROR: readn poll ttl overflow, errno:%d size:%d\n" , errno, size);
return -6;
}
continue;
}
fprintf(stdout, "[readn_ex]: readn errno:%d\n" , errno );
return -1; // error
}
else if (0==i) {
fprintf(stdout, "[readn_ex]: readn poll timeout\n" );
return -2;
}
if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
{
fprintf(stdout, "[readn_ex] ERROR: readn err revents:%x errno:%d len:%d\n" , pfd.revents, errno, size);
return -3;
}
len = read(fd, (char*)buf, size);
if (len < 0)
{
if (errno == EINTR){
if (--ttl<=0) {
fprintf(stdout, "[readn_ex] ERROR: readn poll ttl overflow, errno:%d size:%d\n" , errno, size);
return -6;
}
continue;
}
return -4;
}
if (len == 0) //EOF ,is return -5 right???
{
fprintf(stdout, "[readn_ex] EOF\n");
return -5;
}
return len;
}
return -1;
}
static int stable_send(int fd, const char *buf,int size, int* sended_bytes) {
int ret;
if (sended_bytes)
*sended_bytes = 0;
while(size > 0) {
/* ret = send(fd, buf, size, 0 | MSG_NOSIGNAL); replaced by coming.ling */
ret = write(fd, buf, size);
if(ret < 0)
{
printf(" stable_send sock %d ERR %d:%s\n", fd, ret, strerror(errno));
return -1;
}
else if (ret==0)
{
printf("stable_send sock %d EOF %d\n", fd, ret);
return 0;
}
if (sended_bytes)
*sended_bytes += ret;
buf += ret;
size -= ret;
}
return 1;
}
/*
//璇诲彇鍥炲簲锛岀洿鍒拌鍒?\r\n\r\n 涓烘
static int read_response( int sockfd , void* buf , int len )
{
int ret ;
int byte_sum = 0;
char* p = (char*)buf;
int ttl = RW_MAX_TTL;
while((ret = read_time( sockfd , p , len , 500 ))>0 && ttl-->0) //500ms timeout
{
if( ret <= 0 )
{
//PTRACE("read_time ret %d\n", ret);
return ret;
}
p+=ret;
byte_sum+=ret;
if( byte_sum >= len )
break;
if (NULL!=strstr((const char*)buf, "\r\n\r\n"))
{
//PTRACE("--read response end flag\n");
break;
}
}
return byte_sum;
}
*/
#if 0
#define LOCK_PREFIX "lock ; "
typedef struct { volatile int counter; } atomic_t;
/**
* atomic_inc - increment atomic variable
* @v: pointer of type atomic_t
*
* Atomically increments @v by 1.
*/
static __inline__ void atomic_inc(atomic_t *v)
{
__asm__ __volatile__(
LOCK_PREFIX "incl %0"
:"=m" (v->counter)
:"m" (v->counter));
}
/**
* atomic_dec - decrement atomic variable
* @v: pointer of type atomic_t
*
* Atomically decrements @v by 1.
*/
static __inline__ void atomic_dec(atomic_t *v)
{
__asm__ __volatile__(
LOCK_PREFIX "decl %0"
:"=m" (v->counter)
:"m" (v->counter));
}
#endif
#define BUFFER_INIT_SIZE 640
enum {
pn_reading_header_stage,
pn_continue_reading_header_stage,
pn_d11_request_stage,
pn_d11_response_stage,
pn_writing_header_stage,
pn_writing_body_stage,
pn_writing_body_stage_thread,
pn_closing_stage
};
enum {
pn_http_ok,
pn_http_notfound,
pn_http_302,
pn_http_201,
pn_http_200,
pn_http_error
};
enum {
pn_method_get = 1,
pn_method_post =2 ,
pn_method_head =3 ,
pn_method_unknow =4
};
typedef struct {
char *buff;
char init_buf[BUFFER_INIT_SIZE];
int size;
int free;
} pn_buffer_t;
typedef struct {
int sock;
int sock_d11;
pn_buffer_t *request;
pn_buffer_t *response;
pn_buffer_t *d11_response;
int keepalive;
int method;
pn_buffer_t *uri;
char host[64];
int status;
int stage;
int handle_fd;
pn_buffer_t req_init;
pn_buffer_t res_init;
pn_buffer_t uri_init;
pn_buffer_t d11_init;
} pn_connection_t;
static int srv_sock;
pn_buffer_t *pn_buffer_new(pn_buffer_t *object) {
//pn_buffer_t *object;
if (NULL==object)
object = (pn_buffer_t *)malloc(sizeof(*object));
if (object) {
object->buff = object->init_buf;
/*
if (!object->buff) {
free(object);
return NULL;
}
*/
object->size = BUFFER_INIT_SIZE;
object->free = BUFFER_INIT_SIZE;
}
return object;
}
void pn_buffer_free_buff(pn_buffer_t *buf) {
if (!buf)
return;
if (buf->buff != buf->init_buf && NULL!=buf->buff)
free(buf->buff);
}
int pn_buffer_append_length(pn_buffer_t *buf, const void *data, int length) {
int lack, need = 0;
char *temp;
if (length >= buf->free) {
//we need after bufer[length] -> 0
lack = length - buf->free + 1;
while (need < lack)
need += BUFFER_INIT_SIZE;
temp = (char*)malloc( buf->size + need );
if (!temp)
return -1;
//memcpy(buf->buff, temp, buf->size);
memcpy(temp, buf->buff, buf->size);
pn_buffer_free_buff(buf);
buf->buff = temp;
buf->size += need;
buf->free += need;
}
memcpy(buf->buff + buf->size - buf->free, data, length);
buf->free -= length;
buf->buff[buf->size - buf->free] = '\0';
return 0;
}
int pn_buffer_append(pn_buffer_t *buf, const void *data) {
return pn_buffer_append_length(buf, data, strlen((char *)data));
}
int pn_buffer_find_string(const pn_buffer_t *buf, const char *str) {
int idx = buf->size - buf->free;
int slen = strlen(str);
int i;
for (i = 0; i < idx; i++) {
if (idx - i >= slen) {
if (!memcmp(buf->buff + i, str, slen)) return 1;
} else {
break;
}
}
return 0;
}
int pn_buffer_length(pn_buffer_t *buf) {
return buf->size - buf->free;
}
void pn_buffer_print(pn_buffer_t *buf) {
fprintf(stderr, "%s", buf->buff);
}
void pn_buffer_clean(pn_buffer_t *buf) {
buf->free = buf->size;
}
void pn_buffer_free(pn_buffer_t *buf) {
if (!buf)
return;
pn_buffer_free_buff(buf);
free(buf);
}
void pn_buffer_log(pn_buffer_t *buf) {
if (!buf)
return;
printf("pn_buffe size %d free %d:%s\n", buf->size, buf->free, buf->buff);
}
int pn_header_finish(pn_buffer_t *request) {
int end = request->size - request->free;
const char *head_end;
const char *content_lensz;
int content_len = 0;
int head_len = 0;
head_end = strstr(request->buff, "\r\n\r\n");
if (NULL==head_end)
return 0;
head_len = head_end - request->buff + 4;
content_lensz = strstr(request->buff, "Content-Length:");
if (NULL==content_lensz || content_lensz>head_end)
return 1;
//have content-length field
content_lensz += sizeof("Content-Length:");
content_len = atoi(content_lensz);
print_log(RSSLOG_LV_TRACE, "buflen %d head len %d contentlen %d\n", end, head_len, content_len);
if (content_len + head_len <= end )
return 1;
return 0;
}
const char* find_uri_byid(const char* obj_id , const struct obj_mem *objm, int nn_factor,int isNeedRef);
int IsNeedMapping(pn_connection_t *conn);
void pn_parse_header(pn_connection_t *conn) {
char *eol;
char method[16], uri[512], protocol[32];
char *host_field , tmp[32];
eol = strchr(conn->request->buff, '\n');
if (eol == NULL) {
conn->stage = pn_closing_stage;
return;
}
/*
if (*(eol-1) == '\r')
*(eol-1) = '\0';
*eol = '\0';
*/
sscanf(conn->request->buff, "%s %511s %s", method, uri, protocol);
host_field = strstr(conn->request->buff, "Host:");
if (host_field)
sscanf(host_field, "%s %s", tmp, conn->host);
print_log(RSSLOG_LV_INFO, "%d - method %s uri %s prot %s host %s size %d\n", conn->sock, method, uri, protocol, conn->host, pn_buffer_length(conn->request));
if (!strcmp(method, "GET")) {
conn->method = pn_method_get;
}
else if (!strcmp(method, "POST")) {
conn->method = pn_method_post;
} else if (!strcmp(method, "HEAD")) {
conn->method = pn_method_head;
} else {
conn->method = pn_method_unknow;
}
//pn_buffer_append(conn->uri, ".");
pn_buffer_append(conn->uri, uri);
if (pn_buffer_find_string(conn->uri, "..")) {
print_log(RSSLOG_LV_ERROR, "[x] pn found connection header exists (..)\n");
conn->stage = pn_closing_stage;
}
}
atomic_t g_request_cnt = ATOMIC_INIT(0);
void connection_reading_header(pn_connection_t *conn) {
char buff[1025];
int nrecv;
//nrecv = read_response(conn->sock, buff, 1024);
print_log(RSSLOG_LV_TRACE, "pre read\n");
nrecv = read_time(conn->sock, buff, 1024, 160);
if (nrecv > 0) {
print_log(RSSLOG_LV_TRACE, "read_time %d bytes\n", nrecv);
buff[1024]=0;
pn_buffer_append_length(conn->request, buff, nrecv);
//printf("REQUEST: %s\n", conn->request->buff);
if (pn_header_finish(conn->request)) {
pn_parse_header(conn);
conn->stage = pn_writing_header_stage;
}
else
print_log(RSSLOG_LV_WARNING, "header not finished buf %d %s\n", nrecv, conn->request->buff);
} else {
print_log(RSSLOG_LV_ERROR , "cannot read data from connection ret %d, %s buf %s\n", nrecv, strerror(errno), conn->request->buff);
//conn->stage = pn_closing_stage;
conn->stage = pn_writing_header_stage;//IF the testclient error
}
}
#define UDSI_PORT 8650
#define DSIWK_PORT 8080
#define REDIRECT_URI "/mnt/disk1/300K/300kbps_00001.mp4?AuthInfo=&path=%2Fmnt%2Fdisk1%2F300K%2F300kbps_00001.mp4"
/* {{{ URL缂栫爜锛屾彁鍙栬嚜PHP
鐢ㄦ硶锛歴tring urlencode(string str_source)
璇存槑锛氫粎涓嶇紪鐮?-_. 鍏朵綑鍏ㄩ儴缂栫爜锛岀┖鏍间細琚紪鐮佷负 +
鏃堕棿锛?012-8-13 By Dewei
*/
const char* urlencode(const char* in_str, char* sz_encoded)
{
int in_str_len = strlen(in_str);
int out_str_len = 0;
register unsigned char c;
unsigned char *to, *start;
unsigned char const *from, *end;
unsigned char hexchars[] = "0123456789ABCDEF";
from = (unsigned char *)in_str;
end = (unsigned char *)in_str + in_str_len;
to = start = (unsigned char *)sz_encoded; //start = to = (unsigned char *) malloc(3*in_str_len+1);
while (from < end) {
c = *from++;
if (c == ' ') {
*to++ = '+';
} else if ((c < '0' && c != '-' && c != '.') ||
(c < 'A' && c > '9') ||
(c > 'Z' && c < 'a' && c != '_') ||
(c > 'z')) {
to[0] = '%';
to[1] = hexchars[c >> 4];
to[2] = hexchars[c & 15];
to += 3;
} else {
*to++ = c;
}
}
*to = 0;
out_str_len = to - start;
return NULL;
}
typedef struct{
char mysql_host[64];
int mysql_db_port;
char mysql_user[64];
char mysql_pass[64];
char mysql_db_name[64];
}DB_CONFIG;
typedef struct {
int http_port;
int nn_factor ;
//char id_mask[128];
int pull_port_num; //just for dsi or dsiproxy
int dsi_pull_mod; // DSIWK = 1 ,use dsiwk to pull content ,
// 0 - use udsi ,
// 2 - use dsiproxy
// 3 - use dsim to post reqeust and dsiproxy to get content
// 4 - dsim to post / get content
int proxy_rate; // 0 :don't fake proxy / 1 : all proxy / 2 : 50%proxy / 3: 30%proxy
char dsi_host[64];
char dsi_host_2[64];
volatile int active_host_fail;
int dsi_port;
char upper_cdn_host[64];
int upper_cdn_port;
int thread_num;
int stress_interval_ms; //stress test interval , if (stress_interval_ms==0)always mapping else if ( terval > sress_interval_ms || !sprient ) don't mapping ,
} CONFIG;
CONFIG g_config ;
DB_CONFIG g_db_cfg_local;
DB_CONFIG g_db_cfg_cache;
void connection_make_302_header(pn_connection_t *conn, const char* path, const char* url_prefix, int redirect_port )
{
//char host[64];
//char *port ;
char location[1024];
/*
safe_strncpy(host, conn->host, sizeof(host));
//printf("host %s\n", host);
port = strchr( host , ':' );
if (port)
*port = '\0';
*/
//pn_buffer_log(conn->response);
conn->status = pn_http_302;
pn_buffer_append(conn->response, "HTTP/1.1 302 Found\r\n");
//pn_buffer_append(conn->response, "Transfer-Encoding: chunked\r\n");
//pn_buffer_log(conn->response);
sprintf(location, "Location: http://%s:%d%s%s\r\n", g_config.dsi_host, redirect_port ,url_prefix, path);
pn_buffer_append(conn->response, location);
pn_buffer_append(conn->response,"Connection: close\r\n"); //connection_make_get_header() will add \r\n to str end
//pn_buffer_append(conn->response, "\r\n\r\n0\r\n"); //for Transfer-Encoding: chunked
}
atomic_t port_dynamic = ATOMIC_INIT(0);
#define GET_DSI_HOST (g_config.active_host_fail?g_config.dsi_host_2:g_config.dsi_host)
void connection_make_201_header(pn_connection_t * conn, const char* url, const char* prefix, int direct_port)
{
char temp[] = "<?xml version='1.0' encoding='UTF-8'?>\n"
"<LocateCmdRes>\n"
"<TransferPort>%s:%d</TransferPort>\n"
"<AvailableRange>0-165128271</AvailableRange>\n"
"<TransferSessionID>%s%s</TransferSessionID>\n"
"<TransferTimeout>500</TransferTimeout>\n"
"<OpenForWrite>no</OpenForWrite>\n"
"</LocateCmdRes>\n";
char xml[1024] , conten_len[64];
/* char host[64];
safe_strncpy(host, conn->host, sizeof(host));
//printf("host %s\n", host);
char *port = strchr( host , ':' );
if (port)
*port = '\0';
*/
int xml_size = snprintf(xml, 1024, temp, GET_DSI_HOST, direct_port , prefix, url);
//pn_buffer_log(conn->response);
conn->status = pn_http_201;
sprintf(conten_len, "Content-Length: %d\r\n", xml_size);