Repository navigation
Expand file tree
/
Copy pathserver.c
More file actions
2606 lines (2246 loc) · 65.6 KB
/
Copy pathserver.c
File metadata and controls
2606 lines (2246 loc) · 65.6 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
#include "server.h"
#include <arpa/inet.h>
#include <signal.h>
#ifdef linux
#include <sys/prctl.h>
#include <sys/socket.h>
#endif
#include <sys/un.h>
#include <sys/stat.h>
#include "http_request_parser.h"
#include "response.h"
#include "log.h"
#include "client.h"
#include "util.h"
#include "input.h"
#include "timer.h"
#include "heapq.h"
#ifdef WITH_GREENLET
#include "greensupport.h"
#endif
#define ACCEPT_TIMEOUT_SECS 1
#define READ_TIMEOUT_SECS 30
#define READ_BUF_SIZE 1024 * 64
typedef struct {
TimerObject **q;
uint32_t size;
uint32_t max;
} pending_queue_t;
static char *server_name = "127.0.0.1";
static uint16_t server_port = 8000;
/* static int listen_sock; // listen socket */
static PyObject *listen_socks = NULL; // listen socket
static volatile sig_atomic_t loop_done;
static volatile sig_atomic_t call_shutdown = 0;
static volatile sig_atomic_t catch_signal = 0;
static picoev_loop* main_loop = NULL; //main loop
static heapq_t *g_timers;
static pending_queue_t *g_pendings = NULL;
// active event cnt
static int activecnt = 0;
static PyObject *wsgi_app = NULL; //wsgi app
static uint8_t watch_loop = 0;
static PyObject *watchdog = NULL; //watchdog
static char is_write_access_log = 0;
static int is_keep_alive = 0; //keep alive support
static int keep_alive_timeout = 5;
uint64_t max_content_length = 1024 * 1024 * 16; //max_content_length
int client_body_buffer_size = 1024 * 500; //client_body_buffer_size
static char *unix_sock_name = NULL;
static int backlog = 1024 * 4; // backlog size
static int max_fd = 1024 * 4; // picoev max_fd
// greenlet hub switch value
static PyObject *hub_switch_value;
PyObject* current_client;
PyObject* timeout_error;
/* reuse object */
static PyObject *client_key = NULL; //meinheld.client
static PyObject *wsgi_input_key = NULL; //wsgi.input key
static PyObject *status_code_key = NULL; //STATUS_CODE
static PyObject *bytes_sent_key = NULL; // SEND_BYTES
static PyObject *request_time_key = NULL; // REQUEST_TIME
static PyObject *local_time_key = NULL; // LOCAL_TIME
static PyObject *empty_string = NULL; //""
static PyObject *app_handler_func = NULL;
/* gunicorn */
static int spinner = 0;
static int tempfile_fd = 0;
static int gtimeout = 0;
static int ppid = 0;
#define CLIENT_MAXFREELIST 1024
static client_t *client_free_list[CLIENT_MAXFREELIST];
static int client_numfree = 0;
static void
read_callback(picoev_loop* loop, int fd, int events, void* cb_arg);
#ifndef WITH_GREENLET
static void
write_callback(picoev_loop* loop, int fd, int events, void* cb_arg);
#endif
static void
kill_callback(picoev_loop* loop, int fd, int events, void* cb_arg);
static void
trampoline_callback(picoev_loop* loop, int fd, int events, void* cb_arg);
static PyObject*
internal_schedule_call(int seconds, PyObject *cb, PyObject *args, PyObject *kwargs, PyObject *greenlet);
static int
prepare_call_wsgi(client_t *client);
static void
call_wsgi_handler(client_t *client);
static int
check_status_code(client_t *client);
static pending_queue_t*
init_pendings(void)
{
pending_queue_t *pendings = NULL;
pendings = PyMem_Malloc(sizeof(pending_queue_t));
if (pendings == NULL) {
return NULL;
}
pendings->size = 0;
pendings->max= 1024;
pendings->q = (TimerObject**)malloc(sizeof(TimerObject*) * pendings->max);
if (pendings->q == NULL) {
PyMem_Free(pendings);
return NULL;
}
return pendings;
}
static int
realloc_pendings(void)
{
TimerObject **new_heap;
uint32_t max;
pending_queue_t *pendings = g_pendings;
if (pendings->size >= pendings->max) {
//realloc
max = pendings->max * 2;
new_heap = (TimerObject**)realloc(pendings->q, sizeof(TimerObject*) * max);
if (new_heap == NULL) {
PyErr_SetString(PyExc_Exception, "size over timer queue");
return -1;
}
pendings->max = max;
pendings->q = new_heap;
RDEBUG("realloc max:%d", pendings->max);
}
return 1;
}
static void
destroy_pendings(void)
{
int i = 0, len;
TimerObject *timer = NULL;
TimerObject **t = g_pendings->q;
if (g_pendings == NULL) {
return;
}
len = g_pendings->size;
t += i;
while(len--) {
timer = *t;
Py_DECREF(timer);
t++;
}
free(g_pendings->q);
PyMem_Free(g_pendings);
g_pendings = NULL;
}
static void
client_t_list_fill(void)
{
client_t *client;
while (client_numfree < CLIENT_MAXFREELIST) {
client = (client_t *)PyMem_Malloc(sizeof(client_t));
client_free_list[client_numfree++] = client;
}
}
static void
client_t_list_clear(void)
{
client_t *op;
while (client_numfree) {
op = client_free_list[--client_numfree];
PyMem_Free(op);
}
}
static client_t*
alloc_client_t(void)
{
client_t *client;
if (client_numfree) {
client = client_free_list[--client_numfree];
GDEBUG("use pooled %p", client);
} else {
client = (client_t *)PyMem_Malloc(sizeof(client_t));
GDEBUG("alloc %p", client);
}
memset(client, 0, sizeof(client_t));
return client;
}
static void
dealloc_client(client_t *client)
{
if (client_numfree < CLIENT_MAXFREELIST) {
client_free_list[client_numfree++] = client;
GDEBUG("back to pool %p", client);
} else {
PyMem_Free(client);
}
}
static client_t *
new_client_t(int client_fd, char *remote_addr, uint32_t remote_port)
{
client_t *client;
client = alloc_client_t();
//client = PyMem_Malloc(sizeof(client_t));
//memset(client, 0, sizeof(client_t));
client->fd = client_fd;
client->complete = 1;
client->request_queue = new_request_queue();
client->remote_addr = remote_addr;
client->remote_port = remote_port;
/* client->body_type = BODY_TYPE_NONE; */
GDEBUG("client alloc %p", client);
return client;
}
static void
set_log_value(client_t *client, PyObject *environ, uintptr_t delta_msec)
{
PyObject *status_code = NULL, *bytes = NULL, *request_time = NULL, *local_time = NULL;
status_code = PyLong_FromLong(client->status_code);
bytes = PyLong_FromLong(client->write_bytes);
request_time = PyLong_FromLong(delta_msec);
local_time = NATIVE_FROMSTRING((char*)http_log_time);
if (status_code) {
PyDict_SetItem(environ, status_code_key, status_code);
Py_DECREF(status_code);
}
if (bytes) {
PyDict_SetItem(environ, bytes_sent_key, bytes);
Py_DECREF(bytes);
}
if (request_time) {
PyDict_SetItem(environ, request_time_key, request_time);
Py_DECREF(request_time);
}
if (local_time) {
PyDict_SetItem(environ, local_time_key, local_time);
Py_DECREF(local_time);
}
}
static void
clean_client(client_t *client)
{
PyObject *environ = NULL;
uintptr_t end, delta_msec = 0;
request *req = client->current_req;
if (is_write_access_log) {
DEBUG("write access log");
cache_time_update();
if (req) {
environ = req->environ;
end = current_msec;
if (req->start_msec > 0){
delta_msec = end - req->start_msec;
}
set_log_value(client, environ, delta_msec);
call_access_logger(environ);
} else {
if (client->status_code != 408) {
environ = new_environ(client);
set_log_value(client, environ, delta_msec);
call_access_logger(environ);
}
}
}
Py_CLEAR(client->http_status);
Py_CLEAR(client->headers);
Py_CLEAR(client->response_iter);
Py_CLEAR(client->response);
if (req == NULL) {
goto init;
}
DEBUG("status_code:%d env:%p", client->status_code, req->environ);
if (req->environ) {
/* PyDict_Clear(client->environ); */
/* DEBUG("CLEAR environ"); */
Py_CLEAR(req->environ);
}
if (req->body) {
if (req->body_type == BODY_TYPE_TMPFILE) {
fclose(req->body);
} else {
free_buffer(req->body);
}
req->body = NULL;
}
free_request(req);
init:
client->current_req = NULL;
client->header_done = 0;
client->response_closed = 0;
client->chunked_response = 0;
client->content_length_set = 0;
client->content_length = 0;
client->write_bytes = 0;
}
static void
close_client(client_t *client)
{
client_t *new_client = NULL;
int ret;
if (!client->response_closed) {
close_response(client);
}
DEBUG("start close client:%p fd:%d status_code %d", client, client->fd, client->status_code);
if (picoev_is_active(main_loop, client->fd)) {
if (!picoev_del(main_loop, client->fd)) {
activecnt--;
DEBUG("activecnt:%d", activecnt);
}
DEBUG("picoev_del client:%p fd:%d", client, client->fd);
}
clean_client(client);
DEBUG("remain http pipeline size :%d", client->request_queue->size);
if (client->request_queue->size > 0) {
if (check_status_code(client) > 0) {
//process pipeline
if (prepare_call_wsgi(client) > 0) {
call_wsgi_handler(client);
}
}
return ;
}
if (client->http_parser != NULL) {
/* PyMem_Free(client->http_parser); */
dealloc_parser(client->http_parser);
}
free_request_queue(client->request_queue);
if (!client->keep_alive) {
close(client->fd);
BDEBUG("close client:%p fd:%d", client, client->fd);
} else {
BDEBUG("keep alive client:%p fd:%d", client, client->fd);
new_client = new_client_t(client->fd, client->remote_addr, client->remote_port);
new_client->keep_alive = 1;
init_parser(new_client, server_name, server_port);
ret = picoev_add(main_loop, new_client->fd, PICOEV_READ, keep_alive_timeout, read_callback, (void *)new_client);
if (ret == 0) {
activecnt++;
}
}
//clear old client
dealloc_client(client);
}
static void init_main_loop(void)
{
if (main_loop == NULL) {
/* init picoev */
picoev_init(max_fd);
/* create loop */
main_loop = picoev_create_loop(60);
}
}
static void
kill_callback(picoev_loop* loop, int fd, int events, void* cb_arg)
{
picoev_del(loop, fd);
if ((events & PICOEV_TIMEOUT) != 0) {
DEBUG("force shutdown...");
loop_done = 0;
}
}
static inline void
kill_server(int timeout)
{
int listen_sock = 0;
PyObject *iter = NULL, *item;
int set_callback = 0;
if (main_loop == NULL) {
return;
}
iter = PyObject_GetIter(listen_socks);
if (PyErr_Occurred()){
call_error_logger();
return;
}
while((item = PyIter_Next(iter))){
#ifdef PY3
if (PyLong_Check(item)) {
listen_sock = (int)PyLong_AsLong(item);
#else
if (PyInt_Check(item)) {
listen_sock = (int)PyInt_AsLong(item);
#endif
//stop accepting
if (!picoev_del(main_loop, listen_sock)) {
activecnt--;
DEBUG("activecnt:%d", activecnt);
}
if (!set_callback) {
//shutdown timeout
if (timeout > 0) {
//set timeout
(void)picoev_add(main_loop, listen_sock, PICOEV_TIMEOUT, timeout, kill_callback, NULL);
} else {
(void)picoev_add(main_loop, listen_sock, PICOEV_TIMEOUT, 1, kill_callback, NULL);
}
set_callback = 1;
}
} else {
//TODO WARN???
}
Py_DECREF(item);
}
Py_DECREF(iter);
}
static inline void
set_current_request(client_t *client)
{
request *req;
req = shift_request(client->request_queue);
client->current_req = req;
}
static void
set_bad_request_code(client_t *client, int status_code)
{
request *req;
req = client->request_queue->tail;
req->bad_request_code = status_code;
DEBUG("set bad request code %d", status_code);
}
static int
check_status_code(client_t *client)
{
request *req;
req = client->request_queue->head;
if (req && req->bad_request_code > 200) {
//error
//shift
DEBUG("bad status code %d", req->bad_request_code);
set_current_request(client);
client->status_code = req->bad_request_code;
send_error_page(client);
close_client(client);
return -1;
}
return 1;
}
static PyObject *
app_handler(PyObject *self, PyObject *args)
{
int ret, active;
PyObject *wsgi_args = NULL, *start = NULL, *current = NULL, *parent = NULL, *res = NULL;
PyObject *env = NULL;
ClientObject *pyclient;
client_t *client;
request *req;
response_status status;
if (!PyArg_ParseTuple(args, "O:app_handler", &env)) {
return NULL;
}
pyclient = (ClientObject*)PyDict_GetItem(env, client_key);
client = pyclient->client;
req = client->current_req;
start = create_start_response(client);
if (!start) {
return NULL;
}
DEBUG("call wsgi app");
wsgi_args = PyTuple_Pack(2, env, start);
res = PyObject_CallObject(wsgi_app, wsgi_args);
Py_DECREF(wsgi_args);
DEBUG("called wsgi app");
//check response & PyErr_Occurred
if (res && res == Py_None) {
PyErr_SetString(PyExc_Exception, "response must be a iter or sequence object");
goto error;
}
//Check wsgi_app error
if (PyErr_Occurred()) {
goto error;
}
client->response = res;
if (client->response_closed) {
//closed
close_client(client);
Py_RETURN_NONE;
}
status = response_start(client);
#ifdef WITH_GREENLET
while(status != STATUS_OK) {
if (status == STATUS_ERROR) {
// Internal Server Error
req->bad_request_code = 500;
goto error;
} else {
active = picoev_is_active(main_loop, client->fd);
ret = picoev_add(main_loop, client->fd, PICOEV_WRITE, 300, trampoline_callback, (void *)pyclient);
if ((ret == 0 && !active)) {
activecnt++;
}
// switch to hub
current = pyclient->greenlet;
parent = greenlet_getparent(current);
/* Py_INCREF(hub_switch_value); */
res = greenlet_switch(parent, hub_switch_value, NULL);
Py_XDECREF(res);
// try again after event switch
status = process_body(client);
}
}
status = close_response(client);
if (status == STATUS_ERROR) {
//TODO logging error
}
// send OK
close_client(client);
#else
switch(status) {
case STATUS_ERROR:
// Internal Server Error
req->bad_request_code = 500;
goto error;
case STATUS_SUSPEND:
// continue
// set callback
active = picoev_is_active(main_loop, client->fd);
ret = picoev_add(main_loop, client->fd, PICOEV_WRITE, 300, write_callback, (void *)pyclient);
if ((ret == 0 && !active)) {
activecnt++;
}
default:
// send OK
close_client(client);
}
#endif
Py_RETURN_NONE;
error:
client->status_code = 500;
status = close_response(client);
if (status == STATUS_ERROR) {
//TODO logging error
}
/* write_error_log(__FILE__, __LINE__); */
call_error_logger();
send_error_page(client);
close_client(client);
Py_RETURN_NONE;
}
static PyMethodDef app_handler_def = {"_app_handler", (PyCFunction)app_handler, METH_VARARGS, 0};
static PyObject*
get_app_handler(void)
{
if (app_handler_func == NULL) {
app_handler_func = PyCFunction_NewEx(&app_handler_def, (PyObject *)NULL, NULL);
}
//Py_INCREF(app_handler_func);
return app_handler_func;
}
#ifdef WITH_GREENLET
static void
resume_greenlet(PyObject *greenlet)
{
PyObject *res = NULL;
PyObject *err_type, *err_val, *err_tb;
if (PyErr_Occurred()) {
PyErr_Fetch(&err_type, &err_val, &err_tb);
PyErr_Clear();
//set error
res = greenlet_throw(greenlet, err_type, err_val, err_tb);
} else {
/* Py_INCREF(hub_switch_value); */
res = greenlet_switch(greenlet, hub_switch_value, NULL);
if (res == NULL) {
call_error_logger();
/* PyErr_Fetch(&err_type, &err_val, &err_tb); */
/* PyErr_Clear(); */
//set error
/* res = greenlet_throw(greenlet, err_type, err_val, err_tb); */
}
}
Py_XDECREF(res);
if (greenlet_dead(greenlet)) {
Py_DECREF(greenlet);
}
}
static void
resume_wsgi_handler(ClientObject *pyclient)
{
PyObject *res = NULL;
PyObject *err_type, *err_val, *err_tb;
client_t *old_client;
client_t *client = pyclient->client;
//swap bind client_t
old_client = start_response->cli;
start_response->cli = client;
current_client = (PyObject *)pyclient;
if (PyErr_Occurred()) {
PyErr_Fetch(&err_type, &err_val, &err_tb);
PyErr_Clear();
//set error
res = greenlet_throw(pyclient->greenlet, err_type, err_val, err_tb);
} else {
res = greenlet_switch(pyclient->greenlet, pyclient->args, pyclient->kwargs);
}
start_response->cli = old_client;
Py_CLEAR(pyclient->args);
Py_CLEAR(pyclient->kwargs);
Py_XDECREF(res);
}
#endif
static void
call_wsgi_handler(client_t *client)
{
PyObject *handler, *greenlet, *args, *res;
ClientObject *pyclient;
request *req = NULL;
handler = get_app_handler();
req = client->current_req;
current_client = PyDict_GetItem(req->environ, client_key);
pyclient = (ClientObject *)current_client;
args = PyTuple_Pack(1, req->environ);
#ifdef WITH_GREENLET
//new greenlet
greenlet = greenlet_new(handler, NULL);
// set_greenlet
pyclient->greenlet = greenlet;
Py_INCREF(pyclient->greenlet);
res = greenlet_switch(greenlet, args, NULL);
//res = PyObject_CallObject(wsgi_app, args);
Py_DECREF(args);
Py_DECREF(greenlet);
#else
pyclient->greenlet = NULL;
res = PyObject_CallObject(handler, args);
Py_DECREF(args);
#endif
Py_XDECREF(res);
}
#ifdef WITH_GREENLET
static void
timeout_error_callback(picoev_loop* loop, int fd, int events, void* cb_arg)
{
ClientObject *pyclient = (ClientObject *)(cb_arg);
client_t *client = pyclient->client;
if ((events & PICOEV_TIMEOUT) != 0) {
DEBUG("timeout_error_callback pyclient:%p client:%p fd:%d", pyclient, pyclient->client, pyclient->client->fd);
if (!picoev_del(loop, fd)) {
activecnt--;
DEBUG("activecnt:%d", activecnt);
}
pyclient->suspended = 0;
/* pyclient->resumed = 1; */
PyErr_SetString(timeout_error, "timeout");
set_so_keepalive(client->fd, 0);
resume_wsgi_handler(pyclient);
}
}
static void
timeout_callback(picoev_loop* loop, int fd, int events, void* cb_arg)
{
ClientObject *pyclient = (ClientObject *)(cb_arg);
client_t *client = pyclient->client;
if ((events & PICOEV_TIMEOUT) != 0) {
DEBUG("timeout_callback pyclient:%p client:%p fd:%d", pyclient, pyclient->client, pyclient->client->fd);
//next intval 30sec
picoev_set_timeout(loop, client->fd, 30);
// is_active ??
if (write(client->fd, "", 0) < 0) {
if (!picoev_del(loop, fd)) {
activecnt--;
DEBUG("activecnt:%d", activecnt);
}
//resume
pyclient->suspended = 0;
/* pyclient->resumed = 1; */
PyErr_SetFromErrno(PyExc_IOError);
DEBUG("closed");
set_so_keepalive(client->fd, 0);
resume_wsgi_handler(pyclient);
}
}
}
static void
trampoline_callback(picoev_loop* loop, int fd, int events, void* cb_arg)
{
PyObject *o = NULL;
ClientObject *pyclient = NULL;
client_t *client = NULL;
if (!picoev_del(loop, fd)) {
activecnt--;
DEBUG("activecnt:%d", activecnt);
}
YDEBUG("call trampoline_callback fd:%d event:%d cb_arg:%p", fd, events, cb_arg);
o = (PyObject*)cb_arg;
if (CheckClientObject(o)) {
pyclient = (ClientObject*)cb_arg;
client = pyclient->client;
if ((events & PICOEV_TIMEOUT) != 0) {
RDEBUG("** trampoline_callback timeout **");
//timeout
client->keep_alive = 0;
PyErr_SetString(timeout_error, "timeout");
}
YDEBUG("resume_wsgi_handler");
resume_wsgi_handler(pyclient);
} else if (greenlet_check(o)) {
YDEBUG("resume_greenlet");
resume_greenlet(o);
}
}
#endif
#ifndef WITH_GREENLET
static void
write_callback(picoev_loop* loop, int fd, int events, void* cb_arg)
{
ClientObject *pyclient = (ClientObject*)cb_arg;
client_t *client = pyclient->client;
int ret;
DEBUG("call write_callback");
current_client = (PyObject*)pyclient;
if ((events & PICOEV_TIMEOUT) != 0) {
DEBUG("** write_callback timeout **");
//timeout
client->keep_alive = 0;
close_client(client);
} else if ((events & PICOEV_WRITE) != 0) {
ret = process_body(client);
DEBUG("process_body ret %d", ret);
if (ret != 0) {
//ok or die
close_client(client);
}
}
}
#endif
static int
check_http_expect(client_t *client)
{
PyObject *c = NULL;
char *val = NULL;
int ret;
request *req = client->current_req;
if (client->http_parser->http_minor == 1) {
///TODO CHECK
c = PyDict_GetItemString(req->environ, "HTTP_EXPECT");
if (c) {
val = PyBytes_AS_STRING(c);
if (!strncasecmp(val, "100-continue", 12)) {
ret = write(client->fd, "HTTP/1.1 100 Continue\r\n\r\n", 25);
if (ret < 0) {
//fail
PyErr_SetFromErrno(PyExc_IOError);
/* write_error_log(__FILE__, __LINE__); */
call_error_logger();
client->keep_alive = 0;
client->status_code = 500;
send_error_page(client);
close_client(client);
return -1;
}
} else {
//417
client->keep_alive = 0;
client->status_code = 417;
send_error_page(client);
close_client(client);
return -1;
}
}
return 1;
}
return 0;
}
#ifdef PY3
static int
set_input_file(client_t *client)
{
PyObject *input;
int fd;
request *req = client->current_req;
FILE *tmp = (FILE *)req->body;
fflush(tmp);
rewind(tmp);
fd = fileno(tmp);
input = PyFile_FromFd(fd, "<tmpfile>", "r", -1, NULL, NULL, NULL, 1);
if (input == NULL) {
fclose(tmp);
req->body = NULL;
return -1;
}
//env["wsgi.input"] = tmpfile
//
PyDict_SetItem((PyObject *)req->environ, wsgi_input_key, input);
Py_DECREF(input);
req->body = NULL;
return 1;
}
#else
static int
set_input_file(client_t *client)
{
PyObject *input;
request *req = client->current_req;
FILE *tmp = (FILE *)req->body;
fflush(tmp);
rewind(tmp);
input = PyFile_FromFile(tmp, "<tmpfile>", "r", fclose);
if (input == NULL) {
fclose(tmp);
req->body = NULL;
return -1;
}
//env["wsgi.input"] = tmpfile
//
PyDict_SetItem((PyObject *)req->environ, wsgi_input_key, input);
Py_DECREF(input);
req->body = NULL;
return 0;
}
#endif
static int
set_input_object(client_t *client)
{
PyObject *input = NULL;
request *req = client->current_req;
if (req->body_type == BODY_TYPE_BUFFER) {
input = InputObject_New((buffer_t*)req->body);
} else {
if (req->body) {
input = InputObject_New((buffer_t*)req->body);
} else {
input = InputObject_New(new_buffer(0, 0));
}
}
if (input == NULL) {
return -1;
}
PyDict_SetItem((PyObject *)req->environ, wsgi_input_key, input);
Py_DECREF(input);
req->body = NULL;
return 1;
}
/*
static void
setting_keepalive(client_t *client)
{
PyObject *c;
char *val;
if (is_keep_alive) {
//support keep-alive
c = PyDict_GetItemString(client->environ, "HTTP_CONNECTION");
if (client->http_parser->http_minor == 1) {
//HTTP 1.1
if (c) {
val = PyBytes_AS_STRING(c);
if (!strcasecmp(val, "close")) {
client->keep_alive = 0;
} else {
client->keep_alive = 1;
}
} else {
client->keep_alive = 1;
}
} else {
//HTTP 1.0
if (c) {
val = PyBytes_AS_STRING(c);
if (!strcasecmp(val, "keep-alive")) {
client->keep_alive = 1;
} else {
client->keep_alive = 0;
}
} else {
client->keep_alive = 0;
}
}
}
}
*/
static int
prepare_call_wsgi(client_t *client)