@@ -133,6 +133,7 @@ typedef struct php_cli_server_request {
133
133
char * query_string ;
134
134
size_t query_string_len ;
135
135
HashTable headers ;
136
+ HashTable headers_original_case ;
136
137
char * content ;
137
138
size_t content_len ;
138
139
const char * ext ;
@@ -435,6 +436,75 @@ static const char *get_mime_type(const char *ext, size_t ext_len) /* {{{ */
435
436
return NULL ;
436
437
} /* }}} */
437
438
439
+ PHP_FUNCTION (apache_request_headers ) /* {{{ */
440
+ {
441
+ php_cli_server_client * client ;
442
+ HashTable * headers ;
443
+ char * key ;
444
+ uint key_len ;
445
+ char * * value_pointer ;
446
+ HashPosition pos ;
447
+
448
+ if (zend_parse_parameters_none () == FAILURE ) {
449
+ return ;
450
+ }
451
+
452
+ client = SG (server_context );
453
+ headers = & client -> request .headers_original_case ;
454
+
455
+ array_init_size (return_value , zend_hash_num_elements (headers ));
456
+
457
+ zend_hash_internal_pointer_reset_ex (headers , & pos );
458
+ while (zend_hash_get_current_data_ex (headers , (void * * )& value_pointer , & pos ) == SUCCESS ) {
459
+ zend_hash_get_current_key_ex (headers , & key , & key_len , NULL , 0 , & pos );
460
+ add_assoc_string_ex (return_value , key , key_len , * value_pointer , 1 );
461
+ zend_hash_move_forward_ex (headers , & pos );
462
+ }
463
+ }
464
+ /* }}} */
465
+
466
+ static void add_response_header (sapi_header_struct * h , zval * return_value TSRMLS_DC ) /* {{{ */
467
+ {
468
+ char * s , * p ;
469
+ int len ;
470
+ ALLOCA_FLAG (use_heap )
471
+
472
+ if (h -> header_len > 0 ) {
473
+ p = strchr (h -> header , ':' );
474
+ len = p - h -> header ;
475
+ if (p && (len > 0 )) {
476
+ while (len > 0 && (h -> header [len - 1 ] == ' ' || h -> header [len - 1 ] == '\t' )) {
477
+ len -- ;
478
+ }
479
+ if (len ) {
480
+ s = do_alloca (len + 1 , use_heap );
481
+ memcpy (s , h -> header , len );
482
+ s [len ] = 0 ;
483
+ do {
484
+ p ++ ;
485
+ } while (* p == ' ' || * p == '\t' );
486
+ add_assoc_stringl_ex (return_value , s , len + 1 , p , h -> header_len - (p - h -> header ), 1 );
487
+ free_alloca (s , use_heap );
488
+ }
489
+ }
490
+ }
491
+ }
492
+ /* }}} */
493
+
494
+ PHP_FUNCTION (apache_response_headers ) /* {{{ */
495
+ {
496
+ if (zend_parse_parameters_none () == FAILURE ) {
497
+ return ;
498
+ }
499
+
500
+ if (!& SG (sapi_headers ).headers ) {
501
+ RETURN_FALSE ;
502
+ }
503
+ array_init (return_value );
504
+ zend_llist_apply_with_argument (& SG (sapi_headers ).headers , (llist_apply_with_arg_func_t )add_response_header , return_value TSRMLS_CC );
505
+ }
506
+ /* }}} */
507
+
438
508
/* {{{ cli_server module
439
509
*/
440
510
@@ -479,9 +549,15 @@ zend_module_entry cli_server_module_entry = {
479
549
};
480
550
/* }}} */
481
551
552
+ ZEND_BEGIN_ARG_INFO (arginfo_no_args , 0 )
553
+ ZEND_END_ARG_INFO ()
554
+
482
555
const zend_function_entry server_additional_functions [] = {
483
556
PHP_FE (cli_set_process_title , arginfo_cli_set_process_title )
484
557
PHP_FE (cli_get_process_title , arginfo_cli_get_process_title )
558
+ PHP_FE (apache_request_headers , arginfo_no_args )
559
+ PHP_FE (apache_response_headers , arginfo_no_args )
560
+ PHP_FALIAS (getallheaders , apache_request_headers , arginfo_no_args )
485
561
{NULL , NULL , NULL }
486
562
};
487
563
@@ -1300,6 +1376,7 @@ static int php_cli_server_request_ctor(php_cli_server_request *req) /* {{{ */
1300
1376
req -> query_string = NULL ;
1301
1377
req -> query_string_len = 0 ;
1302
1378
zend_hash_init (& req -> headers , 0 , NULL , (void (* )(void * ))char_ptr_dtor_p , 1 );
1379
+ zend_hash_init (& req -> headers_original_case , 0 , NULL , NULL , 1 );
1303
1380
req -> content = NULL ;
1304
1381
req -> content_len = 0 ;
1305
1382
req -> ext = NULL ;
@@ -1325,6 +1402,7 @@ static void php_cli_server_request_dtor(php_cli_server_request *req) /* {{{ */
1325
1402
pefree (req -> query_string , 1 );
1326
1403
}
1327
1404
zend_hash_destroy (& req -> headers );
1405
+ zend_hash_destroy (& req -> headers_original_case );
1328
1406
if (req -> content ) {
1329
1407
pefree (req -> content , 1 );
1330
1408
}
@@ -1569,6 +1647,7 @@ static int php_cli_server_client_read_request_on_header_value(php_http_parser *p
1569
1647
{
1570
1648
char * header_name = zend_str_tolower_dup (client -> current_header_name , client -> current_header_name_len );
1571
1649
zend_hash_add (& client -> request .headers , header_name , client -> current_header_name_len + 1 , & value , sizeof (char * ), NULL );
1650
+ zend_hash_add (& client -> request .headers_original_case , client -> current_header_name , client -> current_header_name_len + 1 , & value , sizeof (char * ), NULL );
1572
1651
efree (header_name );
1573
1652
}
1574
1653
0 commit comments