forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_cli_server.c
2564 lines (2293 loc) · 69.9 KB
/
php_cli_server.c
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
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Moriyoshi Koizumi <[email protected]> |
| Xinchen Hui <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id: php_cli.c 306938 2011-01-01 02:17:06Z felipe $ */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#ifdef PHP_WIN32
# include <process.h>
# include <io.h>
# include "win32/time.h"
# include "win32/signal.h"
# include "win32/php_registry.h"
# include <sys/timeb.h>
#else
# include "php_config.h"
#endif
#ifdef __riscos__
#include <unixlib/local.h>
#endif
#if HAVE_TIME_H
#include <time.h>
#endif
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#if HAVE_SETLOCALE
#include <locale.h>
#endif
#if HAVE_DLFCN_H
#include <dlfcn.h>
#endif
#include "SAPI.h"
#include "php.h"
#include "php_ini.h"
#include "php_main.h"
#include "php_globals.h"
#include "php_variables.h"
#include "zend_hash.h"
#include "zend_modules.h"
#include "fopen_wrappers.h"
#include "http_status_codes.h"
#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_highlight.h"
#include "zend_exceptions.h"
#include "php_getopt.h"
#ifndef PHP_WIN32
# define php_select(m, r, w, e, t) select(m, r, w, e, t)
# define SOCK_EINVAL EINVAL
# define SOCK_EAGAIN EAGAIN
# define SOCK_EINTR EINTR
# define SOCK_EADDRINUSE EADDRINUSE
#else
# include "win32/select.h"
# define SOCK_EINVAL WSAEINVAL
# define SOCK_EAGAIN WSAEWOULDBLOCK
# define SOCK_EINTR WSAEINTR
# define SOCK_EADDRINUSE WSAEADDRINUSE
#endif
#include "ext/standard/file.h" /* for php_set_sock_blocking() :-( */
#include "zend_smart_str.h"
#include "ext/standard/html.h"
#include "ext/standard/url.h" /* for php_raw_url_decode() */
#include "ext/standard/php_string.h" /* for php_dirname() */
#include "ext/date/php_date.h" /* for php_format_date() */
#include "php_network.h"
#include "php_http_parser.h"
#include "php_cli_server.h"
#include "mime_type_map.h"
#include "php_cli_process_title.h"
#define OUTPUT_NOT_CHECKED -1
#define OUTPUT_IS_TTY 1
#define OUTPUT_NOT_TTY 0
typedef struct php_cli_server_poller {
fd_set rfds, wfds;
struct {
fd_set rfds, wfds;
} active;
php_socket_t max_fd;
} php_cli_server_poller;
typedef struct php_cli_server_request {
enum php_http_method request_method;
int protocol_version;
char *request_uri;
size_t request_uri_len;
char *vpath;
size_t vpath_len;
char *path_translated;
size_t path_translated_len;
char *path_info;
size_t path_info_len;
char *query_string;
size_t query_string_len;
HashTable headers;
HashTable headers_original_case;
char *content;
size_t content_len;
const char *ext;
size_t ext_len;
zend_stat_t sb;
} php_cli_server_request;
typedef struct php_cli_server_chunk {
struct php_cli_server_chunk *next;
enum php_cli_server_chunk_type {
PHP_CLI_SERVER_CHUNK_HEAP,
PHP_CLI_SERVER_CHUNK_IMMORTAL
} type;
union {
struct { void *block; char *p; size_t len; } heap;
struct { const char *p; size_t len; } immortal;
} data;
} php_cli_server_chunk;
typedef struct php_cli_server_buffer {
php_cli_server_chunk *first;
php_cli_server_chunk *last;
} php_cli_server_buffer;
typedef struct php_cli_server_content_sender {
php_cli_server_buffer buffer;
} php_cli_server_content_sender;
typedef struct php_cli_server_client {
struct php_cli_server *server;
php_socket_t sock;
struct sockaddr *addr;
socklen_t addr_len;
char *addr_str;
size_t addr_str_len;
php_http_parser parser;
unsigned int request_read:1;
char *current_header_name;
size_t current_header_name_len;
unsigned int current_header_name_allocated:1;
size_t post_read_offset;
php_cli_server_request request;
unsigned int content_sender_initialized:1;
php_cli_server_content_sender content_sender;
int file_fd;
} php_cli_server_client;
typedef struct php_cli_server {
php_socket_t server_sock;
php_cli_server_poller poller;
int is_running;
char *host;
int port;
int address_family;
char *document_root;
size_t document_root_len;
char *router;
size_t router_len;
socklen_t socklen;
HashTable clients;
HashTable extension_mime_types;
} php_cli_server;
typedef struct php_cli_server_http_response_status_code_pair {
int code;
const char *str;
} php_cli_server_http_response_status_code_pair;
static php_cli_server_http_response_status_code_pair template_map[] = {
{ 400, "<h1>%s</h1><p>Your browser sent a request that this server could not understand.</p>" },
{ 404, "<h1>%s</h1><p>The requested resource <code class=\"url\">%s</code> was not found on this server.</p>" },
{ 500, "<h1>%s</h1><p>The server is temporarily unavailable.</p>" },
{ 501, "<h1>%s</h1><p>Request method not supported.</p>" }
};
#if HAVE_UNISTD_H
static int php_cli_output_is_tty = OUTPUT_NOT_CHECKED;
#endif
static size_t php_cli_server_client_send_through(php_cli_server_client *client, const char *str, size_t str_len);
static php_cli_server_chunk *php_cli_server_chunk_heap_new_self_contained(size_t len);
static void php_cli_server_buffer_append(php_cli_server_buffer *buffer, php_cli_server_chunk *chunk);
static void php_cli_server_logf(const char *format, ...);
static void php_cli_server_log_response(php_cli_server_client *client, int status, const char *message);
ZEND_DECLARE_MODULE_GLOBALS(cli_server);
/* {{{ static char php_cli_server_css[]
* copied from ext/standard/info.c
*/
static const char php_cli_server_css[] = "<style>\n" \
"body { background-color: #fcfcfc; color: #333333; margin: 0; padding:0; }\n" \
"h1 { font-size: 1.5em; font-weight: normal; background-color: #9999cc; min-height:2em; line-height:2em; border-bottom: 1px inset black; margin: 0; }\n" \
"h1, p { padding-left: 10px; }\n" \
"code.url { background-color: #eeeeee; font-family:monospace; padding:0 2px;}\n" \
"</style>\n";
/* }}} */
#ifdef PHP_WIN32
int php_cli_server_get_system_time(char *buf) {
struct _timeb system_time;
errno_t err;
if (buf == NULL) {
return -1;
}
_ftime(&system_time);
err = ctime_s(buf, 52, &(system_time.time) );
if (err) {
return -1;
}
return 0;
}
#else
int php_cli_server_get_system_time(char *buf) {
struct timeval tv;
struct tm tm;
gettimeofday(&tv, NULL);
/* TODO: should be checked for NULL tm/return vaue */
php_localtime_r(&tv.tv_sec, &tm);
php_asctime_r(&tm, buf);
return 0;
}
#endif
static void char_ptr_dtor_p(zval *zv) /* {{{ */
{
pefree(Z_PTR_P(zv), 1);
} /* }}} */
static char *get_last_error() /* {{{ */
{
return pestrdup(strerror(errno), 1);
} /* }}} */
static int status_comp(const void *a, const void *b) /* {{{ */
{
const http_response_status_code_pair *pa = (const http_response_status_code_pair *) a;
const http_response_status_code_pair *pb = (const http_response_status_code_pair *) b;
if (pa->code < pb->code) {
return -1;
} else if (pa->code > pb->code) {
return 1;
}
return 0;
} /* }}} */
static const char *get_status_string(int code) /* {{{ */
{
http_response_status_code_pair needle = {code, NULL},
*result = NULL;
result = bsearch(&needle, http_status_map, http_status_map_len, sizeof(needle), status_comp);
if (result) {
return result->str;
}
/* Returning NULL would require complicating append_http_status_line() to
* not segfault in that case, so let's just return a placeholder, since RFC
* 2616 requires a reason phrase. This is basically what a lot of other Web
* servers do in this case anyway. */
return "Unknown Status Code";
} /* }}} */
static const char *get_template_string(int code) /* {{{ */
{
size_t e = (sizeof(template_map) / sizeof(php_cli_server_http_response_status_code_pair));
size_t s = 0;
while (e != s) {
size_t c = MIN((e + s + 1) / 2, e - 1);
int d = template_map[c].code;
if (d > code) {
e = c;
} else if (d < code) {
s = c;
} else {
return template_map[c].str;
}
}
return NULL;
} /* }}} */
static void append_http_status_line(smart_str *buffer, int protocol_version, int response_code, int persistent) /* {{{ */
{
if (!response_code) {
response_code = 200;
}
smart_str_appendl_ex(buffer, "HTTP", 4, persistent);
smart_str_appendc_ex(buffer, '/', persistent);
smart_str_append_long_ex(buffer, protocol_version / 100, persistent);
smart_str_appendc_ex(buffer, '.', persistent);
smart_str_append_long_ex(buffer, protocol_version % 100, persistent);
smart_str_appendc_ex(buffer, ' ', persistent);
smart_str_append_long_ex(buffer, response_code, persistent);
smart_str_appendc_ex(buffer, ' ', persistent);
smart_str_appends_ex(buffer, get_status_string(response_code), persistent);
smart_str_appendl_ex(buffer, "\r\n", 2, persistent);
} /* }}} */
static void append_essential_headers(smart_str* buffer, php_cli_server_client *client, int persistent) /* {{{ */
{
char *val;
struct timeval tv = {0};
if (NULL != (val = zend_hash_str_find_ptr(&client->request.headers, "host", sizeof("host")-1))) {
smart_str_appendl_ex(buffer, "Host", sizeof("Host") - 1, persistent);
smart_str_appendl_ex(buffer, ": ", sizeof(": ") - 1, persistent);
smart_str_appends_ex(buffer, val, persistent);
smart_str_appendl_ex(buffer, "\r\n", 2, persistent);
}
if (!gettimeofday(&tv, NULL)) {
zend_string *dt = php_format_date("r", 1, tv.tv_sec, 1);
smart_str_appendl_ex(buffer, "Date: ", 6, persistent);
smart_str_appends_ex(buffer, dt->val, persistent);
smart_str_appendl_ex(buffer, "\r\n", 2, persistent);
zend_string_release(dt);
}
smart_str_appendl_ex(buffer, "Connection: close\r\n", sizeof("Connection: close\r\n") - 1, persistent);
} /* }}} */
static const char *get_mime_type(const php_cli_server *server, const char *ext, size_t ext_len) /* {{{ */
{
return (const char*)zend_hash_str_find_ptr(&server->extension_mime_types, ext, ext_len);
} /* }}} */
PHP_FUNCTION(apache_request_headers) /* {{{ */
{
php_cli_server_client *client;
HashTable *headers;
zend_string *key;
char *value;
zval tmp;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
client = SG(server_context);
headers = &client->request.headers_original_case;
array_init_size(return_value, zend_hash_num_elements(headers));
ZEND_HASH_FOREACH_STR_KEY_PTR(headers, key, value) {
ZVAL_STRING(&tmp, value);
zend_symtable_update(Z_ARRVAL_P(return_value), key, &tmp);
} ZEND_HASH_FOREACH_END();
}
/* }}} */
static void add_response_header(sapi_header_struct *h, zval *return_value) /* {{{ */
{
char *s, *p;
ptrdiff_t len;
ALLOCA_FLAG(use_heap)
if (h->header_len > 0) {
p = strchr(h->header, ':');
len = p - h->header;
if (p && (len > 0)) {
while (len > 0 && (h->header[len-1] == ' ' || h->header[len-1] == '\t')) {
len--;
}
if (len) {
s = do_alloca(len + 1, use_heap);
memcpy(s, h->header, len);
s[len] = 0;
do {
p++;
} while (*p == ' ' || *p == '\t');
add_assoc_stringl_ex(return_value, s, (uint)len, p, h->header_len - (p - h->header));
free_alloca(s, use_heap);
}
}
}
}
/* }}} */
PHP_FUNCTION(apache_response_headers) /* {{{ */
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
array_init(return_value);
zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t)add_response_header, return_value);
}
/* }}} */
/* {{{ cli_server module
*/
static void cli_server_init_globals(zend_cli_server_globals *cg)
{
cg->color = 0;
}
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("cli_server.color", "0", PHP_INI_ALL, OnUpdateBool, color, zend_cli_server_globals, cli_server_globals)
PHP_INI_END()
static PHP_MINIT_FUNCTION(cli_server)
{
ZEND_INIT_MODULE_GLOBALS(cli_server, cli_server_init_globals, NULL);
REGISTER_INI_ENTRIES();
return SUCCESS;
}
static PHP_MSHUTDOWN_FUNCTION(cli_server)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
static PHP_MINFO_FUNCTION(cli_server)
{
DISPLAY_INI_ENTRIES();
}
zend_module_entry cli_server_module_entry = {
STANDARD_MODULE_HEADER,
"cli_server",
NULL,
PHP_MINIT(cli_server),
PHP_MSHUTDOWN(cli_server),
NULL,
NULL,
PHP_MINFO(cli_server),
PHP_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
ZEND_BEGIN_ARG_INFO(arginfo_no_args, 0)
ZEND_END_ARG_INFO()
const zend_function_entry server_additional_functions[] = {
PHP_FE(cli_set_process_title, arginfo_cli_set_process_title)
PHP_FE(cli_get_process_title, arginfo_cli_get_process_title)
PHP_FE(apache_request_headers, arginfo_no_args)
PHP_FE(apache_response_headers, arginfo_no_args)
PHP_FALIAS(getallheaders, apache_request_headers, arginfo_no_args)
{NULL, NULL, NULL}
};
static int sapi_cli_server_startup(sapi_module_struct *sapi_module) /* {{{ */
{
if (php_module_startup(sapi_module, &cli_server_module_entry, 1) == FAILURE) {
return FAILURE;
}
return SUCCESS;
} /* }}} */
static size_t sapi_cli_server_ub_write(const char *str, size_t str_length) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
if (!client) {
return 0;
}
return php_cli_server_client_send_through(client, str, str_length);
} /* }}} */
static void sapi_cli_server_flush(void *server_context) /* {{{ */
{
php_cli_server_client *client = server_context;
if (!client) {
return;
}
if (!ZEND_VALID_SOCKET(client->sock)) {
php_handle_aborted_connection();
return;
}
if (!SG(headers_sent)) {
sapi_send_headers();
SG(headers_sent) = 1;
}
} /* }}} */
static int sapi_cli_server_discard_headers(sapi_headers_struct *sapi_headers) /* {{{ */{
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
/* }}} */
static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
smart_str buffer = { 0 };
sapi_header_struct *h;
zend_llist_position pos;
if (client == NULL || SG(request_info).no_headers) {
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
if (SG(sapi_headers).http_status_line) {
smart_str_appends(&buffer, SG(sapi_headers).http_status_line);
smart_str_appendl(&buffer, "\r\n", 2);
} else {
append_http_status_line(&buffer, client->request.protocol_version, SG(sapi_headers).http_response_code, 0);
}
append_essential_headers(&buffer, client, 0);
h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
while (h) {
if (h->header_len) {
smart_str_appendl(&buffer, h->header, h->header_len);
smart_str_appendl(&buffer, "\r\n", 2);
}
h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
}
smart_str_appendl(&buffer, "\r\n", 2);
php_cli_server_client_send_through(client, ZSTR_VAL(buffer.s), ZSTR_LEN(buffer.s));
smart_str_free(&buffer);
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
/* }}} */
static char *sapi_cli_server_read_cookies(void) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
char *val;
if (NULL == (val = zend_hash_str_find_ptr(&client->request.headers, "cookie", sizeof("cookie")-1))) {
return NULL;
}
return val;
} /* }}} */
static size_t sapi_cli_server_read_post(char *buf, size_t count_bytes) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
if (client->request.content) {
size_t content_len = client->request.content_len;
size_t nbytes_copied = MIN(client->post_read_offset + count_bytes, content_len) - client->post_read_offset;
memmove(buf, client->request.content + client->post_read_offset, nbytes_copied);
client->post_read_offset += nbytes_copied;
return nbytes_copied;
}
return 0;
} /* }}} */
static void sapi_cli_server_register_variable(zval *track_vars_array, const char *key, const char *val) /* {{{ */
{
char *new_val = (char *)val;
size_t new_val_len;
if (NULL == val) {
return;
}
if (sapi_module.input_filter(PARSE_SERVER, (char*)key, &new_val, strlen(val), &new_val_len)) {
php_register_variable_safe((char *)key, new_val, new_val_len, track_vars_array);
}
} /* }}} */
static int sapi_cli_server_register_entry_cb(char **entry, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */ {
zval *track_vars_array = va_arg(args, zval *);
if (hash_key->key) {
char *real_key, *key;
uint i;
key = estrndup(ZSTR_VAL(hash_key->key), ZSTR_LEN(hash_key->key));
for(i=0; i<ZSTR_LEN(hash_key->key); i++) {
if (key[i] == '-') {
key[i] = '_';
} else {
key[i] = toupper(key[i]);
}
}
spprintf(&real_key, 0, "%s_%s", "HTTP", key);
if (strcmp(key, "CONTENT_TYPE") == 0 || strcmp(key, "CONTENT_LENGTH") == 0) {
sapi_cli_server_register_variable(track_vars_array, key, *entry);
}
sapi_cli_server_register_variable(track_vars_array, real_key, *entry);
efree(key);
efree(real_key);
}
return ZEND_HASH_APPLY_KEEP;
}
/* }}} */
static void sapi_cli_server_register_variables(zval *track_vars_array) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
sapi_cli_server_register_variable(track_vars_array, "DOCUMENT_ROOT", client->server->document_root);
{
char *tmp;
if ((tmp = strrchr(client->addr_str, ':'))) {
char addr[64], port[8];
strncpy(port, tmp + 1, 8);
port[7] = '\0';
strncpy(addr, client->addr_str, tmp - client->addr_str);
addr[tmp - client->addr_str] = '\0';
sapi_cli_server_register_variable(track_vars_array, "REMOTE_ADDR", addr);
sapi_cli_server_register_variable(track_vars_array, "REMOTE_PORT", port);
} else {
sapi_cli_server_register_variable(track_vars_array, "REMOTE_ADDR", client->addr_str);
}
}
{
char *tmp;
spprintf(&tmp, 0, "PHP %s Development Server", PHP_VERSION);
sapi_cli_server_register_variable(track_vars_array, "SERVER_SOFTWARE", tmp);
efree(tmp);
}
{
char *tmp;
spprintf(&tmp, 0, "HTTP/%d.%d", client->request.protocol_version / 100, client->request.protocol_version % 100);
sapi_cli_server_register_variable(track_vars_array, "SERVER_PROTOCOL", tmp);
efree(tmp);
}
sapi_cli_server_register_variable(track_vars_array, "SERVER_NAME", client->server->host);
{
char *tmp;
spprintf(&tmp, 0, "%i", client->server->port);
sapi_cli_server_register_variable(track_vars_array, "SERVER_PORT", tmp);
efree(tmp);
}
sapi_cli_server_register_variable(track_vars_array, "REQUEST_URI", client->request.request_uri);
sapi_cli_server_register_variable(track_vars_array, "REQUEST_METHOD", SG(request_info).request_method);
sapi_cli_server_register_variable(track_vars_array, "SCRIPT_NAME", client->request.vpath);
if (SG(request_info).path_translated) {
sapi_cli_server_register_variable(track_vars_array, "SCRIPT_FILENAME", SG(request_info).path_translated);
} else if (client->server->router) {
char *temp;
spprintf(&temp, 0, "%s/%s", client->server->document_root, client->server->router);
sapi_cli_server_register_variable(track_vars_array, "SCRIPT_FILENAME", temp);
efree(temp);
}
if (client->request.path_info) {
sapi_cli_server_register_variable(track_vars_array, "PATH_INFO", client->request.path_info);
}
if (client->request.path_info_len) {
char *tmp;
spprintf(&tmp, 0, "%s%s", client->request.vpath, client->request.path_info);
sapi_cli_server_register_variable(track_vars_array, "PHP_SELF", tmp);
efree(tmp);
} else {
sapi_cli_server_register_variable(track_vars_array, "PHP_SELF", client->request.vpath);
}
if (client->request.query_string) {
sapi_cli_server_register_variable(track_vars_array, "QUERY_STRING", client->request.query_string);
}
zend_hash_apply_with_arguments(&client->request.headers, (apply_func_args_t)sapi_cli_server_register_entry_cb, 1, track_vars_array);
} /* }}} */
static void sapi_cli_server_log_message(char *msg) /* {{{ */
{
char buf[52];
if (php_cli_server_get_system_time(buf) != 0) {
memmove(buf, "unknown time, can't be fetched", sizeof("unknown time, can't be fetched"));
} else {
size_t l = strlen(buf);
if (l > 0) {
buf[l - 1] = '\0';
} else {
memmove(buf, "unknown", sizeof("unknown"));
}
}
fprintf(stderr, "[%s] %s\n", buf, msg);
} /* }}} */
/* {{{ sapi_module_struct cli_server_sapi_module
*/
sapi_module_struct cli_server_sapi_module = {
"cli-server", /* name */
"Built-in HTTP server", /* pretty name */
sapi_cli_server_startup, /* startup */
php_module_shutdown_wrapper, /* shutdown */
NULL, /* activate */
NULL, /* deactivate */
sapi_cli_server_ub_write, /* unbuffered write */
sapi_cli_server_flush, /* flush */
NULL, /* get uid */
NULL, /* getenv */
php_error, /* error handler */
NULL, /* header handler */
sapi_cli_server_send_headers, /* send headers handler */
NULL, /* send header handler */
sapi_cli_server_read_post, /* read POST data */
sapi_cli_server_read_cookies, /* read Cookies */
sapi_cli_server_register_variables, /* register server variables */
sapi_cli_server_log_message, /* Log message */
NULL, /* Get request time */
NULL, /* Child terminate */
STANDARD_SAPI_MODULE_PROPERTIES
}; /* }}} */
static int php_cli_server_poller_ctor(php_cli_server_poller *poller) /* {{{ */
{
FD_ZERO(&poller->rfds);
FD_ZERO(&poller->wfds);
poller->max_fd = -1;
return SUCCESS;
} /* }}} */
static void php_cli_server_poller_add(php_cli_server_poller *poller, int mode, php_socket_t fd) /* {{{ */
{
if (mode & POLLIN) {
PHP_SAFE_FD_SET(fd, &poller->rfds);
}
if (mode & POLLOUT) {
PHP_SAFE_FD_SET(fd, &poller->wfds);
}
if (fd > poller->max_fd) {
poller->max_fd = fd;
}
} /* }}} */
static void php_cli_server_poller_remove(php_cli_server_poller *poller, int mode, php_socket_t fd) /* {{{ */
{
if (mode & POLLIN) {
PHP_SAFE_FD_CLR(fd, &poller->rfds);
}
if (mode & POLLOUT) {
PHP_SAFE_FD_CLR(fd, &poller->wfds);
}
#ifndef PHP_WIN32
if (fd == poller->max_fd) {
while (fd > 0) {
fd--;
if (PHP_SAFE_FD_ISSET(fd, &poller->rfds) || PHP_SAFE_FD_ISSET(fd, &poller->wfds)) {
break;
}
}
poller->max_fd = fd;
}
#endif
} /* }}} */
static int php_cli_server_poller_poll(php_cli_server_poller *poller, struct timeval *tv) /* {{{ */
{
memmove(&poller->active.rfds, &poller->rfds, sizeof(poller->rfds));
memmove(&poller->active.wfds, &poller->wfds, sizeof(poller->wfds));
return php_select(poller->max_fd + 1, &poller->active.rfds, &poller->active.wfds, NULL, tv);
} /* }}} */
static int php_cli_server_poller_iter_on_active(php_cli_server_poller *poller, void *opaque, int(*callback)(void *, php_socket_t fd, int events)) /* {{{ */
{
int retval = SUCCESS;
#ifdef PHP_WIN32
struct socket_entry {
SOCKET fd;
int events;
} entries[FD_SETSIZE * 2];
size_t i;
struct socket_entry *n = entries, *m;
for (i = 0; i < poller->active.rfds.fd_count; i++) {
n->events = POLLIN;
n->fd = poller->active.rfds.fd_array[i];
n++;
}
m = n;
for (i = 0; i < poller->active.wfds.fd_count; i++) {
struct socket_entry *e;
SOCKET fd = poller->active.wfds.fd_array[i];
for (e = entries; e < m; e++) {
if (e->fd == fd) {
e->events |= POLLOUT;
}
}
if (e == m) {
assert(n < entries + FD_SETSIZE * 2);
n->events = POLLOUT;
n->fd = fd;
n++;
}
}
{
struct socket_entry *e = entries;
for (; e < n; e++) {
if (SUCCESS != callback(opaque, e->fd, e->events)) {
retval = FAILURE;
}
}
}
#else
php_socket_t fd;
const php_socket_t max_fd = poller->max_fd;
for (fd=0 ; fd<=max_fd ; fd++) {
if (PHP_SAFE_FD_ISSET(fd, &poller->active.rfds)) {
if (SUCCESS != callback(opaque, fd, POLLIN)) {
retval = FAILURE;
}
}
if (PHP_SAFE_FD_ISSET(fd, &poller->active.wfds)) {
if (SUCCESS != callback(opaque, fd, POLLOUT)) {
retval = FAILURE;
}
}
}
#endif
return retval;
} /* }}} */
static size_t php_cli_server_chunk_size(const php_cli_server_chunk *chunk) /* {{{ */
{
switch (chunk->type) {
case PHP_CLI_SERVER_CHUNK_HEAP:
return chunk->data.heap.len;
case PHP_CLI_SERVER_CHUNK_IMMORTAL:
return chunk->data.immortal.len;
}
return 0;
} /* }}} */
static void php_cli_server_chunk_dtor(php_cli_server_chunk *chunk) /* {{{ */
{
switch (chunk->type) {
case PHP_CLI_SERVER_CHUNK_HEAP:
if (chunk->data.heap.block != chunk) {
pefree(chunk->data.heap.block, 1);
}
break;
case PHP_CLI_SERVER_CHUNK_IMMORTAL:
break;
}
} /* }}} */
static void php_cli_server_buffer_dtor(php_cli_server_buffer *buffer) /* {{{ */
{
php_cli_server_chunk *chunk, *next;
for (chunk = buffer->first; chunk; chunk = next) {
next = chunk->next;
php_cli_server_chunk_dtor(chunk);
pefree(chunk, 1);
}
} /* }}} */
static void php_cli_server_buffer_ctor(php_cli_server_buffer *buffer) /* {{{ */
{
buffer->first = NULL;
buffer->last = NULL;
} /* }}} */
static void php_cli_server_buffer_append(php_cli_server_buffer *buffer, php_cli_server_chunk *chunk) /* {{{ */
{
php_cli_server_chunk *last;
for (last = chunk; last->next; last = last->next);
if (!buffer->last) {
buffer->first = chunk;
} else {
buffer->last->next = chunk;
}
buffer->last = last;
} /* }}} */
static void php_cli_server_buffer_prepend(php_cli_server_buffer *buffer, php_cli_server_chunk *chunk) /* {{{ */
{
php_cli_server_chunk *last;
for (last = chunk; last->next; last = last->next);
last->next = buffer->first;
if (!buffer->last) {
buffer->last = last;
}
buffer->first = chunk;
} /* }}} */
static size_t php_cli_server_buffer_size(const php_cli_server_buffer *buffer) /* {{{ */
{
php_cli_server_chunk *chunk;
size_t retval = 0;
for (chunk = buffer->first; chunk; chunk = chunk->next) {
retval += php_cli_server_chunk_size(chunk);
}
return retval;
} /* }}} */
static php_cli_server_chunk *php_cli_server_chunk_immortal_new(const char *buf, size_t len) /* {{{ */
{
php_cli_server_chunk *chunk = pemalloc(sizeof(php_cli_server_chunk), 1);
if (!chunk) {
return NULL;
}
chunk->type = PHP_CLI_SERVER_CHUNK_IMMORTAL;
chunk->next = NULL;
chunk->data.immortal.p = buf;
chunk->data.immortal.len = len;
return chunk;
} /* }}} */
static php_cli_server_chunk *php_cli_server_chunk_heap_new(void *block, char *buf, size_t len) /* {{{ */
{
php_cli_server_chunk *chunk = pemalloc(sizeof(php_cli_server_chunk), 1);
if (!chunk) {
return NULL;
}
chunk->type = PHP_CLI_SERVER_CHUNK_HEAP;
chunk->next = NULL;
chunk->data.heap.block = block;
chunk->data.heap.p = buf;
chunk->data.heap.len = len;
return chunk;
} /* }}} */
static php_cli_server_chunk *php_cli_server_chunk_heap_new_self_contained(size_t len) /* {{{ */
{
php_cli_server_chunk *chunk = pemalloc(sizeof(php_cli_server_chunk) + len, 1);
if (!chunk) {
return NULL;
}
chunk->type = PHP_CLI_SERVER_CHUNK_HEAP;
chunk->next = NULL;
chunk->data.heap.block = chunk;
chunk->data.heap.p = (char *)(chunk + 1);
chunk->data.heap.len = len;
return chunk;
} /* }}} */
static void php_cli_server_content_sender_dtor(php_cli_server_content_sender *sender) /* {{{ */
{
php_cli_server_buffer_dtor(&sender->buffer);
} /* }}} */
static void php_cli_server_content_sender_ctor(php_cli_server_content_sender *sender) /* {{{ */
{
php_cli_server_buffer_ctor(&sender->buffer);
} /* }}} */
static int php_cli_server_content_sender_send(php_cli_server_content_sender *sender, php_socket_t fd, size_t *nbytes_sent_total) /* {{{ */
{
php_cli_server_chunk *chunk, *next;
size_t _nbytes_sent_total = 0;
for (chunk = sender->buffer.first; chunk; chunk = next) {
#ifdef PHP_WIN32
int nbytes_sent;
#else
ssize_t nbytes_sent;
#endif
next = chunk->next;
switch (chunk->type) {
case PHP_CLI_SERVER_CHUNK_HEAP:
#ifdef PHP_WIN32