2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
14 #include "php_variables.h"
16 PHP_RINIT_FUNCTION(http_env
)
18 /* populate form data on non-POST requests */
19 if (SG(request_info
).request_method
&& strcasecmp(SG(request_info
).request_method
, "POST") && SG(request_info
).content_type
&& *SG(request_info
).content_type
) {
20 uint ct_len
= strlen(SG(request_info
).content_type
);
21 char *ct_str
= estrndup(SG(request_info
).content_type
, ct_len
);
22 php_http_params_opts_t opts
;
25 php_http_params_opts_default_get(&opts
);
26 opts
.input
.str
= ct_str
;
27 opts
.input
.len
= ct_len
;
29 SG(request_info
).content_type_dup
= ct_str
;
31 ZEND_INIT_SYMTABLE(¶ms
);
32 if (php_http_params_parse(¶ms
, &opts
)) {
36 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(¶ms
, &key_str
, &key_num
)) {
37 sapi_post_entry
*post_entry
= NULL
;
39 if ((post_entry
= zend_hash_find_ptr(&SG(known_post_content_types
), key_str
))) {
41 SG(request_info
).post_entry
= post_entry
;
43 if (post_entry
->post_reader
) {
44 post_entry
->post_reader();
48 if (sapi_module
.default_post_reader
) {
49 sapi_module
.default_post_reader();
52 sapi_handle_post(&PG(http_globals
)[TRACK_VARS_POST
]);
55 * the rfc1867 handler is an awkward buddy
57 Z_TRY_ADDREF(PG(http_globals
)[TRACK_VARS_FILES
]);
58 zend_hash_str_update(&EG(symbol_table
).ht
, "_FILES", sizeof("_FILES"), &PG(http_globals
)[TRACK_VARS_FILES
]);
61 zend_hash_destroy(¶ms
);
65 PTR_SET(SG(request_info
).content_type_dup
, NULL
);
70 PHP_RSHUTDOWN_FUNCTION(http_env
)
72 if (PHP_HTTP_G
->env
.request
.headers
) {
73 zend_hash_destroy(PHP_HTTP_G
->env
.request
.headers
);
74 FREE_HASHTABLE(PHP_HTTP_G
->env
.request
.headers
);
75 PHP_HTTP_G
->env
.request
.headers
= NULL
;
77 if (PHP_HTTP_G
->env
.request
.body
) {
78 php_http_message_body_free(&PHP_HTTP_G
->env
.request
.body
);
81 if (PHP_HTTP_G
->env
.server_var
) {
82 zval_ptr_dtor(PHP_HTTP_G
->env
.server_var
);
83 PHP_HTTP_G
->env
.server_var
= NULL
;
89 void php_http_env_get_request_headers(HashTable
*headers TSRMLS_DC
)
91 php_http_arrkey_t key
;
94 if (!PHP_HTTP_G
->env
.request
.headers
) {
95 ALLOC_HASHTABLE(PHP_HTTP_G
->env
.request
.headers
);
96 ZEND_INIT_SYMTABLE(PHP_HTTP_G
->env
.request
.headers
);
98 if ((hsv
= php_http_env_get_superglobal(ZEND_STRL("_SERVER")))) {
99 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(hsv
), key
.h
, key
.key
, header
)
101 if (key
.key
&& key
.key
->len
> 5 && *key
.key
->val
== 'H' && !strncmp(key
.key
->val
, "HTTP_", 5)) {
102 size_t key_len
= key
.key
->len
- 5;
103 char *key_str
= php_http_pretty_key(estrndup(&key
.key
->val
[5], key_len
), key_len
, 1, 1);
105 Z_TRY_ADDREF_P(header
);
106 zend_symtable_str_update(PHP_HTTP_G
->env
.request
.headers
, key_str
, key_len
, header
);
109 } else if (key
.key
&& key
.key
->len
> 8 && *key
.key
->val
== 'C' && !strncmp(key
.key
->val
, "CONTENT_", 8)) {
110 char *key_str
= php_http_pretty_key(estrndup(key
.key
->val
, key
.key
->len
), key
.key
->len
, 1, 1);
112 Z_TRY_ADDREF_P(header
);
113 zend_symtable_str_update(PHP_HTTP_G
->env
.request
.headers
, key_str
, key
.key
->len
, header
);
118 ZEND_HASH_FOREACH_END();
123 array_copy(PHP_HTTP_G
->env
.request
.headers
, headers
);
127 char *php_http_env_get_request_header(const char *name_str
, size_t name_len
, size_t *len
, php_http_message_t
*request
)
129 HashTable
*request_headers
;
131 char *val
= NULL
, *key
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
134 request_headers
= &request
->hdrs
;
136 php_http_env_get_request_headers(NULL
);
137 request_headers
= PHP_HTTP_G
->env
.request
.headers
;
140 if ((zvalue
== zend_symtable_str_find(request_headers
, key
, name_len
))) {
141 zend_string
*zs
= zval_get_string(zvalue
);
143 val
= estrndup(zs
->val
, zs
->len
);
147 zend_string_release(zs
);
155 zend_bool
php_http_env_got_request_header(const char *name_str
, size_t name_len
, php_http_message_t
*request
)
157 HashTable
*request_headers
;
158 char *key
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
162 request_headers
= &request
->hdrs
;
164 php_http_env_get_request_headers(NULL
);
165 request_headers
= PHP_HTTP_G
->env
.request
.headers
;
167 got
= zend_symtable_str_exists(request_headers
, key
, name_len
);
173 zval
*php_http_env_get_superglobal(const char *key
, size_t key_len
)
176 zend_string
*key_str
= zend_string_init(key
, key_len
, 0);
178 zend_is_auto_global(key_str
);
179 hsv
= zend_hash_find(&EG(symbol_table
).ht
, key_str
);
180 zend_string_release(key_str
);
182 if (Z_TYPE_P(hsv
) != IS_ARRAY
) {
189 zval
*php_http_env_get_server_var(const char *key
, size_t key_len
, zend_bool check
)
193 /* if available, this is a lot faster than accessing $_SERVER * /
194 if (sapi_module.getenv) {
197 if ((!(env = sapi_module.getenv((char *) key, key_len))) || (check && !*env)) {
200 if (PHP_HTTP_G->env.server_var) {
201 zval_ptr_dtor(&PHP_HTTP_G->env.server_var);
203 MAKE_STD_ZVAL(PHP_HTTP_G->env.server_var);
204 ZVAL_STRING(PHP_HTTP_G->env.server_var, env, 1);
205 return PHP_HTTP_G->env.server_var;
209 if (!(hsv
= php_http_env_get_superglobal(ZEND_STRL("_SERVER")))) {
212 if (!(var
= zend_symtable_str_find(Z_ARRVAL_P(hsv
), key
, key_len
))) {
215 if (check
&& !((Z_TYPE_P(var
) == IS_STRING
) && Z_STRVAL_P(var
) && Z_STRLEN_P(var
))) {
221 php_http_message_body_t
*php_http_env_get_request_body(void)
223 if (!PHP_HTTP_G
->env
.request
.body
) {
224 php_stream
*s
= php_stream_temp_new();
225 php_stream
*input
= php_stream_open_wrapper("php://input", "r", 0, NULL
);
227 /* php://input does not support stat */
228 php_stream_copy_to_stream_ex(input
, s
, -1, NULL
);
229 php_stream_close(input
);
231 php_stream_rewind(s
);
232 PHP_HTTP_G
->env
.request
.body
= php_http_message_body_init(NULL
, s
);
235 return PHP_HTTP_G
->env
.request
.body
;
238 const char *php_http_env_get_request_method(php_http_message_t
*request
)
242 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, request
)) {
243 m
= request
->http
.info
.request
.method
;
245 m
= SG(request_info
).request_method
;
248 return m
? m
: "GET";
251 php_http_range_status_t
php_http_env_get_request_ranges(HashTable
*ranges
, size_t length
, php_http_message_t
*request
)
255 long begin
= -1, end
= -1, *ptr
;
257 if (!(range
= php_http_env_get_request_header(ZEND_STRL("Range"), NULL
, request
))) {
258 return PHP_HTTP_RANGE_NO
;
260 if (strncmp(range
, "bytes=", lenof("bytes="))) {
262 return PHP_HTTP_RANGE_NO
;
265 rp
= range
+ lenof("bytes=");
269 switch (c
= *(rp
++)) {
271 /* allow 000... - shall we? */
277 case '1': case '2': case '3':
278 case '4': case '5': case '6':
279 case '7': case '8': case '9':
281 * If the value of the pointer is already set (non-negative)
282 * then multiply its value by ten and add the current value,
283 * else initialise the pointers value with the current value
285 * This let us recognize empty fields when validating the
286 * ranges, i.e. a "-10" for begin and "12345" for the end
287 * was the following range request: "Range: bytes=0-12345";
288 * While a "-1" for begin and "12345" for the end would
289 * have been: "Range: bytes=-12345".
310 /* validate ranges */
318 return PHP_HTTP_RANGE_NO
;
326 if (length
<= (size_t) end
) {
337 if (end
== -1 || end
== -10) {
339 return PHP_HTTP_RANGE_ERR
;
341 begin
= length
- end
;
347 if (length
<= (size_t) begin
) {
349 return PHP_HTTP_RANGE_ERR
;
355 return PHP_HTTP_RANGE_ERR
;
364 if (length
<= (size_t) end
) {
366 } else if (end
< begin
) {
368 return PHP_HTTP_RANGE_ERR
;
377 add_index_long(&zentry
, 0, begin
);
378 add_index_long(&zentry
, 1, end
);
379 zend_hash_next_index_insert(ranges
, &zentry
);
389 return PHP_HTTP_RANGE_NO
;
394 return PHP_HTTP_RANGE_OK
;
397 static void grab_headers(void *data
, void *arg
)
399 php_http_buffer_appendl(PHP_HTTP_BUFFER(arg
), ((sapi_header_struct
*)data
)->header
);
400 php_http_buffer_appends(PHP_HTTP_BUFFER(arg
), PHP_HTTP_CRLF
);
403 static void grab_header(void *data
, void *arg
)
410 sapi_header_struct
*header
= data
;
412 if ( header
->header_len
> args
->name_len
413 && header
->header
[args
->name_len
] == ':'
414 && !strncmp(header
->header
, args
->name_str
, args
->name_len
)
416 args
->value_ptr
= &header
->header
[args
->name_len
+ 1];
420 ZEND_RESULT_CODE
php_http_env_get_response_headers(HashTable
*headers_ht
)
422 ZEND_RESULT_CODE status
;
423 php_http_buffer_t headers
;
425 php_http_buffer_init(&headers
);
426 zend_llist_apply_with_argument(&SG(sapi_headers
).headers
, grab_headers
, &headers
);
427 php_http_buffer_fix(&headers
);
429 status
= php_http_header_parse(headers
.data
, headers
.used
, headers_ht
, NULL
, NULL
);
430 php_http_buffer_dtor(&headers
);
435 char *php_http_env_get_response_header(const char *name_str
, size_t name_len
)
443 args
.name_str
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
444 args
.name_len
= name_len
;
445 args
.value_ptr
= NULL
;
446 zend_llist_apply_with_argument(&SG(sapi_headers
).headers
, grab_header
, &args
);
447 efree(args
.name_str
);
449 return args
.value_ptr
? estrdup(args
.value_ptr
) : NULL
;
452 long php_http_env_get_response_code(void)
454 long code
= SG(sapi_headers
).http_response_code
;
455 return code
? code
: 200;
458 ZEND_RESULT_CODE
php_http_env_set_response_code(long http_code
)
460 return sapi_header_op(SAPI_HEADER_SET_STATUS
, (void *) (zend_intptr_t
) http_code
);
463 ZEND_RESULT_CODE
php_http_env_set_response_status_line(long code
, php_http_version_t
*v
)
465 sapi_header_line h
= {NULL
, 0, 0};
466 ZEND_RESULT_CODE ret
;
468 h
.line_len
= spprintf(&h
.line
, 0, "HTTP/%u.%u %ld %s", v
->major
, v
->minor
, code
, php_http_env_get_response_status_for_code(code
));
469 ret
= sapi_header_op(SAPI_HEADER_REPLACE
, (void *) &h
);
475 ZEND_RESULT_CODE
php_http_env_set_response_protocol_version(php_http_version_t
*v
)
477 return php_http_env_set_response_status_line(php_http_env_get_response_code(), v
);
480 ZEND_RESULT_CODE
php_http_env_set_response_header(long http_code
, const char *header_str
, size_t header_len
, zend_bool replace
)
482 sapi_header_line h
= {estrndup(header_str
, header_len
), header_len
, http_code
};
483 ZEND_RESULT_CODE ret
= sapi_header_op(replace
? SAPI_HEADER_REPLACE
: SAPI_HEADER_ADD
, (void *) &h
);
488 ZEND_RESULT_CODE
php_http_env_set_response_header_va(long http_code
, zend_bool replace
, const char *fmt
, va_list argv
)
490 ZEND_RESULT_CODE ret
= FAILURE
;
491 sapi_header_line h
= {NULL
, 0, http_code
};
493 h
.line_len
= vspprintf(&h
.line
, 0, fmt
, argv
);
497 ret
= sapi_header_op(replace
? SAPI_HEADER_REPLACE
: SAPI_HEADER_ADD
, (void *) &h
);
504 ZEND_RESULT_CODE
php_http_env_set_response_header_format(long http_code
, zend_bool replace
, const char *fmt
, ...)
506 ZEND_RESULT_CODE ret
;
510 ret
= php_http_env_set_response_header_va(http_code
, replace
, fmt
, args
);
516 ZEND_RESULT_CODE
php_http_env_set_response_header_value(long http_code
, const char *name_str
, size_t name_len
, zval
*value
, zend_bool replace
)
519 sapi_header_line h
= {(char *) name_str
, name_len
, http_code
};
521 return sapi_header_op(SAPI_HEADER_DELETE
, (void *) &h
);
524 if (Z_TYPE_P(value
) == IS_ARRAY
|| Z_TYPE_P(value
) == IS_OBJECT
) {
527 HashTable
*ht
= HASH_OF(value
);
529 ZEND_HASH_FOREACH_VAL(ht
, data_ptr
)
531 if (SUCCESS
!= php_http_env_set_response_header_value(http_code
, name_str
, name_len
, data_ptr
, first
)) {
536 ZEND_HASH_FOREACH_END();
540 zend_string
*data
= zval_get_string(value
);
543 zend_string_release(data
);
544 return php_http_env_set_response_header_value(http_code
, name_str
, name_len
, NULL
, replace
);
547 ZEND_RESULT_CODE ret
;
549 if (name_len
> INT_MAX
) {
552 h
.response_code
= http_code
;
553 h
.line_len
= spprintf(&h
.line
, 0, "%.*s: %.*s", (int) name_len
, name_str
, data
->len
, data
->val
);
555 ret
= sapi_header_op(replace
? SAPI_HEADER_REPLACE
: SAPI_HEADER_ADD
, (void *) &h
);
557 zend_string_release(data
);
565 static PHP_HTTP_STRLIST(php_http_env_response_status
) =
566 PHP_HTTP_STRLIST_ITEM("Continue")
567 PHP_HTTP_STRLIST_ITEM("Switching Protocols")
568 PHP_HTTP_STRLIST_ITEM("Processing")
569 PHP_HTTP_STRLIST_NEXT
570 PHP_HTTP_STRLIST_ITEM("OK")
571 PHP_HTTP_STRLIST_ITEM("Created")
572 PHP_HTTP_STRLIST_ITEM("Accepted")
573 PHP_HTTP_STRLIST_ITEM("Non-Authoritative Information")
574 PHP_HTTP_STRLIST_ITEM("No Content")
575 PHP_HTTP_STRLIST_ITEM("Reset Content")
576 PHP_HTTP_STRLIST_ITEM("Partial Content")
577 PHP_HTTP_STRLIST_ITEM("Multi-Status")
578 PHP_HTTP_STRLIST_ITEM("Already Reported")
579 PHP_HTTP_STRLIST_ITEM("(Unused)")
580 PHP_HTTP_STRLIST_ITEM("(Unused)")
581 PHP_HTTP_STRLIST_ITEM("(Unused)")
582 PHP_HTTP_STRLIST_ITEM("(Unused)")
583 PHP_HTTP_STRLIST_ITEM("(Unused)")
584 PHP_HTTP_STRLIST_ITEM("(Unused)")
585 PHP_HTTP_STRLIST_ITEM("(Unused)")
586 PHP_HTTP_STRLIST_ITEM("(Unused)")
587 PHP_HTTP_STRLIST_ITEM("(Unused)")
588 PHP_HTTP_STRLIST_ITEM("(Unused)")
589 PHP_HTTP_STRLIST_ITEM("(Unused)")
590 PHP_HTTP_STRLIST_ITEM("(Unused)")
591 PHP_HTTP_STRLIST_ITEM("(Unused)")
592 PHP_HTTP_STRLIST_ITEM("(Unused)")
593 PHP_HTTP_STRLIST_ITEM("(Unused)")
594 PHP_HTTP_STRLIST_ITEM("(Unused)")
595 PHP_HTTP_STRLIST_ITEM("(Unused)")
596 PHP_HTTP_STRLIST_ITEM("IM Used")
597 PHP_HTTP_STRLIST_NEXT
598 PHP_HTTP_STRLIST_ITEM("Multiple Choices")
599 PHP_HTTP_STRLIST_ITEM("Moved Permanently")
600 PHP_HTTP_STRLIST_ITEM("Found")
601 PHP_HTTP_STRLIST_ITEM("See Other")
602 PHP_HTTP_STRLIST_ITEM("Not Modified")
603 PHP_HTTP_STRLIST_ITEM("Use Proxy")
604 PHP_HTTP_STRLIST_ITEM("(Unused)")
605 PHP_HTTP_STRLIST_ITEM("Temporary Redirect")
606 PHP_HTTP_STRLIST_ITEM("Permanent Redirect")
607 PHP_HTTP_STRLIST_NEXT
608 PHP_HTTP_STRLIST_ITEM("Bad Request")
609 PHP_HTTP_STRLIST_ITEM("Unauthorized")
610 PHP_HTTP_STRLIST_ITEM("Payment Required")
611 PHP_HTTP_STRLIST_ITEM("Forbidden")
612 PHP_HTTP_STRLIST_ITEM("Not Found")
613 PHP_HTTP_STRLIST_ITEM("Method Not Allowed")
614 PHP_HTTP_STRLIST_ITEM("Not Acceptable")
615 PHP_HTTP_STRLIST_ITEM("Proxy Authentication Required")
616 PHP_HTTP_STRLIST_ITEM("Request Timeout")
617 PHP_HTTP_STRLIST_ITEM("Conflict")
618 PHP_HTTP_STRLIST_ITEM("Gone")
619 PHP_HTTP_STRLIST_ITEM("Length Required")
620 PHP_HTTP_STRLIST_ITEM("Precondition Failed")
621 PHP_HTTP_STRLIST_ITEM("Request Entity Too Large")
622 PHP_HTTP_STRLIST_ITEM("Request URI Too Long")
623 PHP_HTTP_STRLIST_ITEM("Unsupported Media Type")
624 PHP_HTTP_STRLIST_ITEM("Requested Range Not Satisfiable")
625 PHP_HTTP_STRLIST_ITEM("Expectation Failed")
626 PHP_HTTP_STRLIST_ITEM("(Unused)")
627 PHP_HTTP_STRLIST_ITEM("(Unused)")
628 PHP_HTTP_STRLIST_ITEM("(Unused)")
629 PHP_HTTP_STRLIST_ITEM("(Unused)")
630 PHP_HTTP_STRLIST_ITEM("Unprocessible Entity")
631 PHP_HTTP_STRLIST_ITEM("Locked")
632 PHP_HTTP_STRLIST_ITEM("Failed Dependency")
633 PHP_HTTP_STRLIST_ITEM("(Reserved)")
634 PHP_HTTP_STRLIST_ITEM("Upgrade Required")
635 PHP_HTTP_STRLIST_ITEM("(Unused)")
636 PHP_HTTP_STRLIST_ITEM("Precondition Required")
637 PHP_HTTP_STRLIST_ITEM("Too Many Requests")
638 PHP_HTTP_STRLIST_ITEM("(Unused)")
639 PHP_HTTP_STRLIST_ITEM("Request Header Fields Too Large")
640 PHP_HTTP_STRLIST_NEXT
641 PHP_HTTP_STRLIST_ITEM("Internal Server Error")
642 PHP_HTTP_STRLIST_ITEM("Not Implemented")
643 PHP_HTTP_STRLIST_ITEM("Bad Gateway")
644 PHP_HTTP_STRLIST_ITEM("Service Unavailable")
645 PHP_HTTP_STRLIST_ITEM("Gateway Timeout")
646 PHP_HTTP_STRLIST_ITEM("HTTP Version Not Supported")
647 PHP_HTTP_STRLIST_ITEM("Variant Also Negotiates")
648 PHP_HTTP_STRLIST_ITEM("Insufficient Storage")
649 PHP_HTTP_STRLIST_ITEM("Loop Detected")
650 PHP_HTTP_STRLIST_ITEM("(Unused)")
651 PHP_HTTP_STRLIST_ITEM("Not Extended")
652 PHP_HTTP_STRLIST_ITEM("Network Authentication Required")
653 PHP_HTTP_STRLIST_STOP
656 const char *php_http_env_get_response_status_for_code(unsigned code
)
658 return php_http_strlist_find(php_http_env_response_status
, 100, code
);
661 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getRequestHeader
, 0, 0, 0)
662 ZEND_ARG_INFO(0, header_name
)
664 static PHP_METHOD(HttpEnv
, getRequestHeader
)
666 char *header_name_str
= NULL
;
667 size_t header_name_len
= 0;
669 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str
, &header_name_len
)) {
672 if (header_name_str
&& header_name_len
) {
673 size_t header_length
;
674 char *header_value
= php_http_env_get_request_header(header_name_str
, header_name_len
, &header_length
, NULL
);
677 RETURN_STR(php_http_cs2zs(header_value
, header_length
));
680 array_init(return_value
);
681 php_http_env_get_request_headers(Z_ARRVAL_P(return_value
));
685 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getRequestBody
, 0, 0, 0)
686 ZEND_ARG_INFO(0, body_class_name
)
688 static PHP_METHOD(HttpEnv
, getRequestBody
)
690 php_http_message_body_t
*body
;
691 php_http_message_body_object_t
*body_obj
;
692 zend_class_entry
*class_entry
= php_http_message_body_class_entry
;
694 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &class_entry
), invalid_arg
, return);
696 body
= php_http_env_get_request_body();
697 if (SUCCESS
== php_http_new((void *) &body_obj
, class_entry
, (php_http_new_t
) php_http_message_body_object_new_ex
, php_http_message_body_class_entry
, body
)) {
698 php_http_message_body_addref(body
);
699 RETVAL_OBJ(&body_obj
->zo
);
703 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseStatusForCode
, 0, 0, 1)
704 ZEND_ARG_INFO(0, code
)
706 static PHP_METHOD(HttpEnv
, getResponseStatusForCode
)
710 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
713 RETURN_STRING(php_http_env_get_response_status_for_code(code
));
716 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseStatusForAllCodes
, 0, 0, 0)
718 static PHP_METHOD(HttpEnv
, getResponseStatusForAllCodes
)
722 php_http_strlist_iterator_t i
;
724 if (SUCCESS
!= zend_parse_parameters_none()) {
728 array_init(return_value
);
729 for ( php_http_strlist_iterator_init(&i
, php_http_env_response_status
, 100);
730 *(s
= php_http_strlist_iterator_this(&i
, &c
));
731 php_http_strlist_iterator_next(&i
)
733 add_index_string(return_value
, c
, s
);
737 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseHeader
, 0, 0, 0)
738 ZEND_ARG_INFO(0, header_name
)
740 static PHP_METHOD(HttpEnv
, getResponseHeader
)
742 char *header_name_str
= NULL
;
743 size_t header_name_len
= 0;
745 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str
, &header_name_len
)) {
748 if (header_name_str
&& header_name_len
) {
749 char *header_value
= php_http_env_get_response_header(header_name_str
, header_name_len
);
752 RETURN_STR(php_http_cs2zs(header_value
, strlen(header_value
)));
755 array_init(return_value
);
756 php_http_env_get_response_headers(Z_ARRVAL_P(return_value
));
760 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseCode
, 0, 0, 0)
762 static PHP_METHOD(HttpEnv
, getResponseCode
)
764 if (SUCCESS
!= zend_parse_parameters_none()) {
767 RETURN_LONG(php_http_env_get_response_code());
770 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_setResponseHeader
, 0, 0, 1)
771 ZEND_ARG_INFO(0, header_name
)
772 ZEND_ARG_INFO(0, header_value
)
773 ZEND_ARG_INFO(0, response_code
)
774 ZEND_ARG_INFO(0, replace_header
)
776 static PHP_METHOD(HttpEnv
, setResponseHeader
)
778 char *header_name_str
;
780 zval
*header_value
= NULL
;
782 zend_bool replace_header
= 1;
784 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!lb", &header_name_str
, &header_name_len
, &header_value
, &code
, &replace_header
)) {
787 RETURN_BOOL(SUCCESS
== php_http_env_set_response_header_value(code
, header_name_str
, header_name_len
, header_value
, replace_header TSRMLS_CC
));
790 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_setResponseCode
, 0, 0, 1)
791 ZEND_ARG_INFO(0, code
)
793 static PHP_METHOD(HttpEnv
, setResponseCode
)
797 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code
)) {
800 RETURN_BOOL(SUCCESS
== php_http_env_set_response_code(code
));
803 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateLanguage
, 0, 0, 1)
804 ZEND_ARG_INFO(0, supported
)
805 ZEND_ARG_INFO(1, result_array
)
807 static PHP_METHOD(HttpEnv
, negotiateLanguage
)
809 HashTable
*supported
;
810 zval
*rs_array
= NULL
;
812 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "H|z", &supported
, &rs_array
)) {
817 array_init(rs_array
);
820 PHP_HTTP_DO_NEGOTIATE(language
, supported
, rs_array
);
823 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateCharset
, 0, 0, 1)
824 ZEND_ARG_INFO(0, supported
)
825 ZEND_ARG_INFO(1, result_array
)
827 static PHP_METHOD(HttpEnv
, negotiateCharset
)
829 HashTable
*supported
;
830 zval
*rs_array
= NULL
;
832 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported
, &rs_array
)) {
837 array_init(rs_array
);
839 PHP_HTTP_DO_NEGOTIATE(charset
, supported
, rs_array
);
842 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateEncoding
, 0, 0, 1)
843 ZEND_ARG_INFO(0, supported
)
844 ZEND_ARG_INFO(1, result_array
)
846 static PHP_METHOD(HttpEnv
, negotiateEncoding
)
848 HashTable
*supported
;
849 zval
*rs_array
= NULL
;
851 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported
, &rs_array
)) {
856 array_init(rs_array
);
858 PHP_HTTP_DO_NEGOTIATE(encoding
, supported
, rs_array
);
861 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateContentType
, 0, 0, 1)
862 ZEND_ARG_INFO(0, supported
)
863 ZEND_ARG_INFO(1, result_array
)
865 static PHP_METHOD(HttpEnv
, negotiateContentType
)
867 HashTable
*supported
;
868 zval
*rs_array
= NULL
;
870 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported
, &rs_array
)) {
875 array_init(rs_array
);
877 PHP_HTTP_DO_NEGOTIATE(content_type
, supported
, rs_array
);
880 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiate
, 0, 0, 2)
881 ZEND_ARG_INFO(0, params
)
882 ZEND_ARG_INFO(0, supported
)
883 ZEND_ARG_INFO(0, primary_type_separator
)
884 ZEND_ARG_INFO(1, result_array
)
886 static PHP_METHOD(HttpEnv
, negotiate
)
888 HashTable
*supported
, *rs
;
889 zval
*rs_array
= NULL
;
890 char *value_str
, *sep_str
= NULL
;
891 size_t value_len
, sep_len
= 0;
893 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sH|s!z", &value_str
, &value_len
, &supported
, &sep_str
, &sep_len
, &rs_array
)) {
900 array_init(rs_array
);
903 if ((rs
= php_http_negotiate(value_str
, value_len
, supported
, sep_str
, sep_len
))) {
904 PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs
, supported
, rs_array
);
906 PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported
, rs_array
);
910 static zend_function_entry php_http_env_methods
[] = {
911 PHP_ME(HttpEnv
, getRequestHeader
, ai_HttpEnv_getRequestHeader
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
912 PHP_ME(HttpEnv
, getRequestBody
, ai_HttpEnv_getRequestBody
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
914 PHP_ME(HttpEnv
, getResponseStatusForCode
, ai_HttpEnv_getResponseStatusForCode
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
915 PHP_ME(HttpEnv
, getResponseStatusForAllCodes
, ai_HttpEnv_getResponseStatusForAllCodes
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
917 PHP_ME(HttpEnv
, getResponseHeader
, ai_HttpEnv_getResponseHeader
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
918 PHP_ME(HttpEnv
, getResponseCode
, ai_HttpEnv_getResponseCode
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
919 PHP_ME(HttpEnv
, setResponseHeader
, ai_HttpEnv_setResponseHeader
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
920 PHP_ME(HttpEnv
, setResponseCode
, ai_HttpEnv_setResponseCode
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
922 PHP_ME(HttpEnv
, negotiateLanguage
, ai_HttpEnv_negotiateLanguage
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
923 PHP_ME(HttpEnv
, negotiateContentType
, ai_HttpEnv_negotiateContentType
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
924 PHP_ME(HttpEnv
, negotiateEncoding
, ai_HttpEnv_negotiateEncoding
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
925 PHP_ME(HttpEnv
, negotiateCharset
, ai_HttpEnv_negotiateCharset
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
926 PHP_ME(HttpEnv
, negotiate
, ai_HttpEnv_negotiate
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
931 #ifdef PHP_HTTP_HAVE_JSON
932 #include "ext/json/php_json.h"
934 static SAPI_POST_HANDLER_FUNC(php_http_json_post_handler
)
937 zend_string
*json
= NULL
;
939 if (SG(request_info
).request_body
) {
940 /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
941 php_stream_rewind(SG(request_info
).request_body
);
942 json
= php_stream_copy_to_mem(SG(request_info
).request_body
, PHP_STREAM_COPY_ALL
, 0);
950 php_json_decode(&zjson
, json
->val
, json
->len
, 1, PG(max_input_nesting_level
));
951 if (Z_TYPE(zjson
) != IS_NULL
) {
953 ZVAL_COPY_VALUE(zarg
, (&zjson
));
956 zend_string_release(json
);
960 static void php_http_env_register_json_handler(void)
962 sapi_post_entry entry
= {NULL
, 0, NULL
, NULL
};
964 entry
.post_reader
= sapi_read_standard_form_data
;
965 entry
.post_handler
= php_http_json_post_handler
;
967 entry
.content_type
= "text/json";
968 entry
.content_type_len
= lenof("text/json");
969 sapi_register_post_entry(&entry
);
971 entry
.content_type
= "application/json";
972 entry
.content_type_len
= lenof("application/json");
973 sapi_register_post_entry(&entry
);
977 zend_class_entry
*php_http_env_class_entry
;
979 PHP_MINIT_FUNCTION(http_env
)
981 zend_class_entry ce
= {0};
983 INIT_NS_CLASS_ENTRY(ce
, "http", "Env", php_http_env_methods
);
984 php_http_env_class_entry
= zend_register_internal_class(&ce
);
986 #ifdef PHP_HTTP_HAVE_JSON
987 php_http_env_register_json_handler();
999 * vim600: noet sw=4 ts=4 fdm=marker
1000 * vim<600: noet sw=4 ts=4