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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #include <main/SAPI.h>
17 PHP_RINIT_FUNCTION(http_env
)
19 PHP_HTTP_G
->env
.request
.time
= sapi_get_request_time(TSRMLS_C
);
24 PHP_RSHUTDOWN_FUNCTION(http_env
)
26 if (PHP_HTTP_G
->env
.request
.headers
) {
27 zend_hash_destroy(PHP_HTTP_G
->env
.request
.headers
);
28 FREE_HASHTABLE(PHP_HTTP_G
->env
.request
.headers
);
29 PHP_HTTP_G
->env
.request
.headers
= NULL
;
31 if (PHP_HTTP_G
->env
.request
.body
) {
32 php_http_message_body_free(&PHP_HTTP_G
->env
.request
.body
);
35 if (PHP_HTTP_G
->env
.server_var
) {
36 zval_ptr_dtor(&PHP_HTTP_G
->env
.server_var
);
37 PHP_HTTP_G
->env
.server_var
= NULL
;
43 PHP_HTTP_API
void php_http_env_get_request_headers(HashTable
*headers TSRMLS_DC
)
45 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
49 if (!PHP_HTTP_G
->env
.request
.headers
) {
50 ALLOC_HASHTABLE(PHP_HTTP_G
->env
.request
.headers
);
51 zend_hash_init(PHP_HTTP_G
->env
.request
.headers
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
53 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC
);
55 if (SUCCESS
== zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void *) &hsv
) && Z_TYPE_PP(hsv
) == IS_ARRAY
) {
56 FOREACH_KEY(pos
, *hsv
, key
) {
57 if (key
.type
== HASH_KEY_IS_STRING
&& key
.len
> 6 && !strncmp(key
.str
, "HTTP_", 5)) {
59 key
.str
= php_http_pretty_key(estrndup(key
.str
+ 5, key
.len
- 1), key
.len
- 1, 1, 1);
61 zend_hash_get_current_data_ex(Z_ARRVAL_PP(hsv
), (void *) &header
, &pos
);
63 zend_symtable_update(PHP_HTTP_G
->env
.request
.headers
, key
.str
, key
.len
, (void *) header
, sizeof(zval
*), NULL
);
72 zend_hash_copy(headers
, PHP_HTTP_G
->env
.request
.headers
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
76 PHP_HTTP_API
char *php_http_env_get_request_header(const char *name_str
, size_t name_len TSRMLS_DC
)
79 char *val
= NULL
, *key
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
81 php_http_env_get_request_headers(NULL TSRMLS_CC
);
83 if (SUCCESS
== zend_symtable_find(PHP_HTTP_G
->env
.request
.headers
, key
, name_len
+ 1, (void *) &zvalue
)) {
84 zval
*zcopy
= php_http_ztyp(IS_STRING
, *zvalue
);
86 val
= estrndup(Z_STRVAL_P(zcopy
), Z_STRLEN_P(zcopy
));
87 zval_ptr_dtor(&zcopy
);
95 PHP_HTTP_API
int php_http_env_got_request_header(const char *name_str
, size_t name_len TSRMLS_DC
)
97 char *key
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
100 php_http_env_get_request_headers(NULL TSRMLS_CC
);
101 got
= zend_symtable_exists(PHP_HTTP_G
->env
.request
.headers
, key
, name_len
+ 1);
107 PHP_HTTP_API zval
*php_http_env_get_server_var(const char *key
, size_t key_len
, zend_bool check TSRMLS_DC
)
112 /* if available, this is a lot faster than accessing $_SERVER */
113 if (sapi_module
.getenv
) {
114 if ((!(env
= sapi_module
.getenv((char *) key
, key_len TSRMLS_CC
))) || (check
&& !*env
)) {
117 if (PHP_HTTP_G
->env
.server_var
) {
118 zval_ptr_dtor(&PHP_HTTP_G
->env
.server_var
);
120 MAKE_STD_ZVAL(PHP_HTTP_G
->env
.server_var
);
121 ZVAL_STRING(PHP_HTTP_G
->env
.server_var
, env
, 1);
122 return PHP_HTTP_G
->env
.server_var
;
125 zend_is_auto_global(ZEND_STRL("_SERVER") TSRMLS_CC
);
127 if ((SUCCESS
!= zend_hash_find(&EG(symbol_table
), ZEND_STRS("_SERVER"), (void *) &hsv
)) || (Z_TYPE_PP(hsv
) != IS_ARRAY
)) {
130 if ((SUCCESS
!= zend_symtable_find(Z_ARRVAL_PP(hsv
), key
, key_len
+ 1, (void *) &var
))) {
133 if (check
&& !((Z_TYPE_PP(var
) == IS_STRING
) && Z_STRVAL_PP(var
) && Z_STRLEN_PP(var
))) {
139 PHP_HTTP_API php_http_message_body_t
*php_http_env_get_request_body(TSRMLS_D
)
141 if (!PHP_HTTP_G
->env
.request
.body
) {
142 php_stream
*s
= NULL
;
144 if (SG(request_info
).post_data
|| SG(request_info
).raw_post_data
) {
145 if ((s
= php_stream_temp_new())) {
146 /* php://input does not support seek() */
147 if (SG(request_info
).raw_post_data
) {
148 php_stream_write(s
, SG(request_info
).raw_post_data
, SG(request_info
).raw_post_data_length
);
150 php_stream_write(s
, SG(request_info
).post_data
, SG(request_info
).post_data_length
);
152 php_stream_rewind(s
);
154 } else if (sapi_module
.read_post
&& !SG(read_post_bytes
)) {
155 if ((s
= php_stream_temp_new())) {
156 char *buf
= emalloc(4096);
159 while (0 < (len
= sapi_module
.read_post(buf
, 4096 TSRMLS_CC
))) {
160 SG(read_post_bytes
) += len
;
161 php_stream_write(s
, buf
, len
);
169 php_stream_rewind(s
);
172 PHP_HTTP_G
->env
.request
.body
= php_http_message_body_init(NULL
, s TSRMLS_CC
);
175 return PHP_HTTP_G
->env
.request
.body
;
178 PHP_HTTP_API php_http_range_status_t
php_http_env_get_request_ranges(HashTable
*ranges
, size_t length TSRMLS_DC
)
182 long begin
= -1, end
= -1, *ptr
;
184 if (!(range
= php_http_env_get_request_header(ZEND_STRL("Range") TSRMLS_CC
))) {
185 return PHP_HTTP_RANGE_NO
;
187 if (strncmp(range
, "bytes=", lenof("bytes="))) {
189 return PHP_HTTP_RANGE_NO
;
192 rp
= range
+ lenof("bytes=");
196 switch (c
= *(rp
++)) {
198 /* allow 000... - shall we? */
204 case '1': case '2': case '3':
205 case '4': case '5': case '6':
206 case '7': case '8': case '9':
208 * If the value of the pointer is already set (non-negative)
209 * then multiply its value by ten and add the current value,
210 * else initialise the pointers value with the current value
212 * This let us recognize empty fields when validating the
213 * ranges, i.e. a "-10" for begin and "12345" for the end
214 * was the following range request: "Range: bytes=0-12345";
215 * While a "-1" for begin and "12345" for the end would
216 * have been: "Range: bytes=-12345".
237 /* validate ranges */
245 return PHP_HTTP_RANGE_NO
;
253 if (length
<= (size_t) end
) {
264 if (end
== -1 || end
== -10) {
266 return PHP_HTTP_RANGE_ERR
;
268 begin
= length
- end
;
274 if (length
<= (size_t) begin
) {
276 return PHP_HTTP_RANGE_ERR
;
282 return PHP_HTTP_RANGE_ERR
;
291 if (length
<= (size_t) end
) {
293 } else if (end
< begin
) {
295 return PHP_HTTP_RANGE_ERR
;
303 MAKE_STD_ZVAL(zentry
);
305 add_index_long(zentry
, 0, begin
);
306 add_index_long(zentry
, 1, end
);
307 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
317 return PHP_HTTP_RANGE_NO
;
322 return PHP_HTTP_RANGE_OK
;
325 static void grab_headers(void *data
, void *arg TSRMLS_DC
)
327 php_http_buffer_appendl(PHP_HTTP_BUFFER(arg
), ((sapi_header_struct
*)data
)->header
);
328 php_http_buffer_appends(PHP_HTTP_BUFFER(arg
), PHP_HTTP_CRLF
);
331 PHP_HTTP_API STATUS
php_http_env_get_response_headers(HashTable
*headers_ht TSRMLS_DC
)
334 php_http_buffer_t headers
;
336 php_http_buffer_init(&headers
);
337 zend_llist_apply_with_argument(&SG(sapi_headers
).headers
, grab_headers
, &headers TSRMLS_CC
);
338 php_http_buffer_fix(&headers
);
340 status
= php_http_headers_parse(PHP_HTTP_BUFFER_VAL(&headers
), PHP_HTTP_BUFFER_LEN(&headers
), headers_ht
, NULL
, NULL TSRMLS_CC
);
341 php_http_buffer_dtor(&headers
);
346 PHP_HTTP_API
char *php_http_env_get_response_header(const char *name_str
, size_t name_len TSRMLS_DC
)
351 zend_hash_init(&headers
, 0, NULL
, NULL
, 0);
352 if (SUCCESS
== php_http_env_get_response_headers(&headers TSRMLS_CC
)) {
354 char *key
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
356 if (SUCCESS
== zend_symtable_find(&headers
, key
, name_len
+ 1, (void *) &zvalue
)) {
357 zval
*zcopy
= php_http_ztyp(IS_STRING
, *zvalue
);
359 val
= estrndup(Z_STRVAL_P(zcopy
), Z_STRLEN_P(zcopy
));
360 zval_ptr_dtor(&zcopy
);
365 zend_hash_destroy(&headers
);
370 PHP_HTTP_API
long php_http_env_get_response_code(TSRMLS_D
)
372 long code
= SG(sapi_headers
).http_response_code
;
373 return code
? code
: 200;
376 PHP_HTTP_API STATUS
php_http_env_set_response_code(long http_code TSRMLS_DC
)
378 return sapi_header_op(SAPI_HEADER_SET_STATUS
, (void *) http_code TSRMLS_CC
);
381 PHP_HTTP_API STATUS
php_http_env_set_response_status_line(long code
, php_http_version_t
*v TSRMLS_DC
)
383 sapi_header_line h
= {0};
386 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
));
387 ret
= sapi_header_op(SAPI_HEADER_REPLACE
, (void *) &h TSRMLS_CC
);
393 PHP_HTTP_API STATUS
php_http_env_set_response_protocol_version(php_http_version_t
*v TSRMLS_DC
)
395 return php_http_env_set_response_status_line(php_http_env_get_response_code(TSRMLS_C
), v TSRMLS_CC
);
398 PHP_HTTP_API STATUS
php_http_env_set_response_header(long http_code
, const char *header_str
, size_t header_len
, zend_bool replace TSRMLS_DC
)
400 sapi_header_line h
= {estrndup(header_str
, header_len
), header_len
, http_code
};
401 STATUS ret
= sapi_header_op(replace
? SAPI_HEADER_REPLACE
: SAPI_HEADER_ADD
, (void *) &h TSRMLS_CC
);
406 PHP_HTTP_API STATUS
php_http_env_set_response_header_format(long http_code
, zend_bool replace TSRMLS_DC
, const char *fmt
, ...)
409 STATUS ret
= FAILURE
;
410 sapi_header_line h
= {NULL
, 0, http_code
};
413 h
.line_len
= vspprintf(&h
.line
, 0, fmt
, args
);
418 ret
= sapi_header_op(replace
? SAPI_HEADER_REPLACE
: SAPI_HEADER_ADD
, (void *) &h TSRMLS_CC
);
425 PHP_HTTP_API STATUS
php_http_env_set_response_header_value(long http_code
, const char *name_str
, size_t name_len
, zval
*value
, zend_bool replace TSRMLS_DC
)
428 sapi_header_line h
= {(char *) name_str
, name_len
, http_code
};
430 return sapi_header_op(SAPI_HEADER_DELETE
, (void *) &h TSRMLS_CC
);
433 if(Z_TYPE_P(value
) == IS_ARRAY
|| Z_TYPE_P(value
) == IS_OBJECT
) {
438 FOREACH_HASH_VAL(pos
, HASH_OF(value
), data_ptr
) {
439 if (SUCCESS
!= php_http_env_set_response_header_value(http_code
, name_str
, name_len
, *data_ptr
, first TSRMLS_CC
)) {
447 zval
*data
= php_http_ztyp(IS_STRING
, value
);
449 if (!Z_STRLEN_P(data
)) {
450 zval_ptr_dtor(&data
);
451 return php_http_env_set_response_header_value(http_code
, name_str
, name_len
, NULL
, replace TSRMLS_CC
);
456 if (name_len
> INT_MAX
) {
459 h
.response_code
= http_code
;
460 h
.line_len
= spprintf(&h
.line
, 0, "%.*s: %.*s", (int) name_len
, name_str
, Z_STRLEN_P(data
), Z_STRVAL_P(data
));
462 ret
= sapi_header_op(replace
? SAPI_HEADER_REPLACE
: SAPI_HEADER_ADD
, (void *) &h TSRMLS_CC
);
464 zval_ptr_dtor(&data
);
473 static PHP_HTTP_STRLIST(php_http_env_response_status
) =
474 PHP_HTTP_STRLIST_ITEM("Continue")
475 PHP_HTTP_STRLIST_ITEM("Switching Protocols")
476 PHP_HTTP_STRLIST_NEXT
477 PHP_HTTP_STRLIST_ITEM("OK")
478 PHP_HTTP_STRLIST_ITEM("Created")
479 PHP_HTTP_STRLIST_ITEM("Accepted")
480 PHP_HTTP_STRLIST_ITEM("Non-Authoritative Information")
481 PHP_HTTP_STRLIST_ITEM("No Content")
482 PHP_HTTP_STRLIST_ITEM("Reset Content")
483 PHP_HTTP_STRLIST_ITEM("Partial Content")
484 PHP_HTTP_STRLIST_NEXT
485 PHP_HTTP_STRLIST_ITEM("Multiple Choices")
486 PHP_HTTP_STRLIST_ITEM("Moved Permanently")
487 PHP_HTTP_STRLIST_ITEM("Found")
488 PHP_HTTP_STRLIST_ITEM("See Other")
489 PHP_HTTP_STRLIST_ITEM("Not Modified")
490 PHP_HTTP_STRLIST_ITEM("Use Proxy")
491 PHP_HTTP_STRLIST_ITEM("(Unused)")
492 PHP_HTTP_STRLIST_ITEM("Temporary Redirect")
493 PHP_HTTP_STRLIST_NEXT
494 PHP_HTTP_STRLIST_ITEM("Bad Request")
495 PHP_HTTP_STRLIST_ITEM("Unauthorized")
496 PHP_HTTP_STRLIST_ITEM("Payment Required")
497 PHP_HTTP_STRLIST_ITEM("Forbidden")
498 PHP_HTTP_STRLIST_ITEM("Not Found")
499 PHP_HTTP_STRLIST_ITEM("Method Not Allowed")
500 PHP_HTTP_STRLIST_ITEM("Not Acceptable")
501 PHP_HTTP_STRLIST_ITEM("Proxy Authentication Required")
502 PHP_HTTP_STRLIST_ITEM("Request Timeout")
503 PHP_HTTP_STRLIST_ITEM("Conflict")
504 PHP_HTTP_STRLIST_ITEM("Gone")
505 PHP_HTTP_STRLIST_ITEM("Length Required")
506 PHP_HTTP_STRLIST_ITEM("Precondition Failed")
507 PHP_HTTP_STRLIST_ITEM("Request Entity Too Large")
508 PHP_HTTP_STRLIST_ITEM("Request URI Too Long")
509 PHP_HTTP_STRLIST_ITEM("Unsupported Media Type")
510 PHP_HTTP_STRLIST_ITEM("Requested Range Not Satisfiable")
511 PHP_HTTP_STRLIST_ITEM("Expectation Failed")
512 PHP_HTTP_STRLIST_NEXT
513 PHP_HTTP_STRLIST_ITEM("Internal Server Error")
514 PHP_HTTP_STRLIST_ITEM("Not Implemented")
515 PHP_HTTP_STRLIST_ITEM("Bad Gateway")
516 PHP_HTTP_STRLIST_ITEM("Service Unavailable")
517 PHP_HTTP_STRLIST_ITEM("Gateway Timeout")
518 PHP_HTTP_STRLIST_ITEM("HTTP Version Not Supported")
519 PHP_HTTP_STRLIST_STOP
522 PHP_HTTP_API
const char *php_http_env_get_response_status_for_code(unsigned code
)
524 return php_http_strlist_find(php_http_env_response_status
, 100, code
);
527 zend_class_entry
*php_http_env_class_entry
;
529 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpEnv, method, 0, req_args)
530 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpEnv, method, 0)
531 #define PHP_HTTP_ENV_ME(method) PHP_ME(HttpEnv, method, PHP_HTTP_ARGS(HttpEnv, method), ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
533 PHP_HTTP_BEGIN_ARGS(getRequestHeader
, 0)
534 PHP_HTTP_ARG_VAL(header_name
, 0)
537 PHP_HTTP_BEGIN_ARGS(getRequestBody
, 0)
538 PHP_HTTP_ARG_VAL(body_class_name
, 0)
541 PHP_HTTP_BEGIN_ARGS(getResponseStatusForCode
, 1)
542 PHP_HTTP_ARG_VAL(code
, 0)
545 PHP_HTTP_BEGIN_ARGS(getResponseHeader
, 0)
546 PHP_HTTP_ARG_VAL(header_name
, 0)
549 PHP_HTTP_EMPTY_ARGS(getResponseCode
);
551 PHP_HTTP_BEGIN_ARGS(setResponseHeader
, 1)
552 PHP_HTTP_ARG_VAL(header_name
, 0)
553 PHP_HTTP_ARG_VAL(header_value
, 0)
554 PHP_HTTP_ARG_VAL(response_code
, 0)
555 PHP_HTTP_ARG_VAL(replace_header
, 0)
558 PHP_HTTP_BEGIN_ARGS(setResponseCode
, 1)
559 PHP_HTTP_ARG_VAL(code
, 0)
562 PHP_HTTP_BEGIN_ARGS(negotiateLanguage
, 1)
563 PHP_HTTP_ARG_VAL(supported
, 0)
564 PHP_HTTP_ARG_VAL(result_array
, 1)
567 PHP_HTTP_BEGIN_ARGS(negotiateContentType
, 1)
568 PHP_HTTP_ARG_VAL(supported
, 0)
569 PHP_HTTP_ARG_VAL(result_array
, 1)
572 PHP_HTTP_BEGIN_ARGS(negotiateCharset
, 1)
573 PHP_HTTP_ARG_VAL(supported
, 0)
574 PHP_HTTP_ARG_VAL(result_array
, 1)
577 PHP_HTTP_BEGIN_ARGS(negotiate
, 2)
578 PHP_HTTP_ARG_VAL(value
, 0)
579 PHP_HTTP_ARG_VAL(supported
, 0)
580 PHP_HTTP_ARG_VAL(result_array
, 1)
583 PHP_HTTP_EMPTY_ARGS(persistentHandlesStat
);
585 PHP_HTTP_BEGIN_ARGS(persistentHandlesClean
, 0)
586 PHP_HTTP_ARG_VAL(name
, 0)
587 PHP_HTTP_ARG_VAL(ident
, 0)
590 PHP_HTTP_BEGIN_ARGS(parseParams
, 1)
591 PHP_HTTP_ARG_VAL(params
, 0)
592 PHP_HTTP_ARG_VAL(flags
, 0)
595 zend_function_entry php_http_env_method_entry
[] = {
596 PHP_HTTP_ENV_ME(getRequestHeader
)
597 PHP_HTTP_ENV_ME(getRequestBody
)
599 PHP_HTTP_ENV_ME(getResponseStatusForCode
)
601 PHP_HTTP_ENV_ME(getResponseHeader
)
602 PHP_HTTP_ENV_ME(getResponseCode
)
603 PHP_HTTP_ENV_ME(setResponseHeader
)
604 PHP_HTTP_ENV_ME(setResponseCode
)
606 PHP_HTTP_ENV_ME(negotiateLanguage
)
607 PHP_HTTP_ENV_ME(negotiateContentType
)
608 PHP_HTTP_ENV_ME(negotiateCharset
)
609 PHP_HTTP_ENV_ME(negotiate
)
611 PHP_HTTP_ENV_ME(persistentHandlesStat
)
612 PHP_HTTP_ENV_ME(persistentHandlesClean
)
614 PHP_HTTP_ENV_ME(parseParams
)
619 PHP_METHOD(HttpEnv
, getRequestHeader
)
621 char *header_name_str
;
624 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!", &header_name_str
, &header_name_len
)) {
625 if (header_name_str
&& header_name_len
) {
626 char *header_value
= php_http_env_get_request_header(header_name_str
, header_name_len TSRMLS_CC
);
629 RETURN_STRING(header_value
, 0);
633 array_init(return_value
);
634 php_http_env_get_request_headers(Z_ARRVAL_P(return_value
) TSRMLS_CC
);
641 PHP_METHOD(HttpEnv
, getRequestBody
)
643 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
644 zend_class_entry
*class_entry
= php_http_message_body_class_entry
;
646 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|C", &class_entry
)) {
647 zend_object_value ov
;
648 php_http_message_body_t
*body
= php_http_env_get_request_body(TSRMLS_C
);
650 if (SUCCESS
== php_http_new(&ov
, class_entry
, (php_http_new_t
) php_http_message_body_object_new_ex
, php_http_message_body_class_entry
, body
, NULL TSRMLS_CC
)) {
651 RETURN_OBJVAL(ov
, 0);
654 } end_error_handling();
657 PHP_METHOD(HttpEnv
, getResponseStatusForCode
)
661 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
662 RETURN_STRING(php_http_env_get_response_status_for_code(code
), 1);
667 PHP_METHOD(HttpEnv
, getResponseHeader
)
669 char *header_name_str
;
672 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!", &header_name_str
, &header_name_len
)) {
673 if (header_name_str
&& header_name_len
) {
674 char *header_value
= php_http_env_get_response_header(header_name_str
, header_name_len TSRMLS_CC
);
677 RETURN_STRING(header_value
, 0);
681 array_init(return_value
);
682 php_http_env_get_response_headers(Z_ARRVAL_P(return_value
) TSRMLS_CC
);
689 PHP_METHOD(HttpEnv
, getResponseCode
)
691 if (SUCCESS
== zend_parse_parameters_none()) {
692 RETURN_LONG(php_http_env_get_response_code(TSRMLS_C
));
697 PHP_METHOD(HttpEnv
, setResponseHeader
)
699 char *header_name_str
;
703 zend_bool replace_header
= 1;
705 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|z!lb", &header_name_str
, &header_name_len
, &header_value
, &code
, &replace_header
)) {
706 RETURN_SUCCESS(php_http_env_set_response_header_value(code
, header_name_str
, header_name_len
, header_value
, replace_header TSRMLS_CC
));
711 PHP_METHOD(HttpEnv
, setResponseCode
)
715 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
716 RETURN_SUCCESS(php_http_env_set_response_code(code TSRMLS_CC
));
722 #define PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported) \
726 zend_hash_internal_pointer_reset((supported)); \
727 if (SUCCESS == zend_hash_get_current_data((supported), (void *) &value)) { \
728 RETVAL_ZVAL(*value, 1, 0); \
734 #define PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array) \
735 PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported); \
740 FOREACH_HASH_VAL(pos, supported, value_ptr) { \
741 zval *value = php_http_ztyp(IS_STRING, *value_ptr); \
742 add_assoc_double(rs_array, Z_STRVAL_P(value), 1.0); \
743 zval_ptr_dtor(&value); \
747 #define PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array) \
753 if (zend_hash_num_elements(result) && HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
754 RETVAL_STRINGL(key, key_len-1, 0); \
756 PHP_HTTP_DO_NEGOTIATE_DEFAULT(supported); \
760 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
763 zend_hash_destroy(result); \
764 FREE_HASHTABLE(result); \
767 #define PHP_HTTP_DO_NEGOTIATE(type, supported, rs_array) \
770 if ((result = php_http_negotiate_ ##type(supported TSRMLS_CC))) { \
771 PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array); \
773 PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array); \
777 PHP_METHOD(HttpEnv
, negotiateLanguage
)
779 HashTable
*supported
;
780 zval
*rs_array
= NULL
;
782 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "H|z", &supported
, &rs_array
)) {
785 array_init(rs_array
);
788 PHP_HTTP_DO_NEGOTIATE(language
, supported
, rs_array
);
793 PHP_METHOD(HttpEnv
, negotiateCharset
)
795 HashTable
*supported
;
796 zval
*rs_array
= NULL
;
798 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "H|z", &supported
, &rs_array
)) {
801 array_init(rs_array
);
803 PHP_HTTP_DO_NEGOTIATE(charset
, supported
, rs_array
);
808 PHP_METHOD(HttpEnv
, negotiateContentType
)
810 HashTable
*supported
;
811 zval
*rs_array
= NULL
;
813 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "H|z", &supported
, &rs_array
)) {
816 array_init(rs_array
);
818 PHP_HTTP_DO_NEGOTIATE(content_type
, supported
, rs_array
);
823 PHP_METHOD(HttpEnv
, negotiate
)
825 HashTable
*supported
;
826 zval
*rs_array
= NULL
;
830 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sH|z", &value_str
, &value_len
, &supported
, &rs_array
)) {
835 array_init(rs_array
);
838 if ((rs
= php_http_negotiate(value_str
, supported
, php_http_negotiate_default_func TSRMLS_CC
))) {
839 PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs
, supported
, rs_array
);
841 PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported
, rs_array
);
847 PHP_METHOD(HttpEnv
, persistentHandlesStat
)
849 if (SUCCESS
== zend_parse_parameters_none()) {
850 object_init(return_value
);
851 if (php_http_persistent_handle_statall(HASH_OF(return_value
) TSRMLS_CC
)) {
854 zval_dtor(return_value
);
859 PHP_METHOD(HttpEnv
, persistentHandlesClean
)
861 char *name_str
= NULL
, *ident_str
= NULL
;
862 int name_len
= 0, ident_len
= 0;
864 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|ss", &name_str
, &name_len
, &ident_str
, &ident_len
)) {
865 php_http_persistent_handle_cleanup(name_str
, name_len
, ident_str
, ident_len TSRMLS_CC
);
869 PHP_METHOD(HttpEnv
, parseParams
)
873 long flags
= PHP_HTTP_PARAMS_DEFAULT
;
875 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", ¶m_str
, ¶m_len
, &flags
)) {
879 array_init(return_value
);
880 if (SUCCESS
!= php_http_params_parse(param_str
, flags
, php_http_params_parse_default_func
, Z_ARRVAL_P(return_value
) TSRMLS_CC
)) {
881 zval_dtor(return_value
);
886 zend_class_entry
*php_http_env_request_class_entry
;
888 #undef PHP_HTTP_BEGIN_ARGS
889 #undef PHP_HTTP_EMPTY_ARGS
890 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpEnvRequest, method, 0, req_args)
891 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpEnvRequest, method, 0)
892 #define PHP_HTTP_ENV_REQUEST_ME(method, visibility) PHP_ME(HttpEnvRequest, method, PHP_HTTP_ARGS(HttpEnvRequest, method), visibility)
894 PHP_HTTP_EMPTY_ARGS(__construct
);
896 zend_function_entry php_http_env_request_method_entry
[] = {
897 PHP_HTTP_ENV_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
902 PHP_METHOD(HttpEnvRequest
, __construct
)
904 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
905 if (SUCCESS
== zend_parse_parameters_none()) {
906 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
908 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
909 obj
->message
= php_http_message_init_env(obj
->message
, PHP_HTTP_REQUEST TSRMLS_CC
);
910 } end_error_handling();
912 } end_error_handling();
915 PHP_MINIT_FUNCTION(http_env
)
917 PHP_HTTP_REGISTER_CLASS(http
, Env
, http_env
, NULL
, 0);
919 zend_declare_class_constant_long(php_http_env_class_entry
, ZEND_STRL("PARAMS_ALLOW_COMMA"), PHP_HTTP_PARAMS_ALLOW_COMMA TSRMLS_CC
);
920 zend_declare_class_constant_long(php_http_env_class_entry
, ZEND_STRL("PARAMS_ALLOW_FAILURE"), PHP_HTTP_PARAMS_ALLOW_FAILURE TSRMLS_CC
);
921 zend_declare_class_constant_long(php_http_env_class_entry
, ZEND_STRL("PARAMS_RAISE_ERROR"), PHP_HTTP_PARAMS_RAISE_ERROR TSRMLS_CC
);
922 zend_declare_class_constant_long(php_http_env_class_entry
, ZEND_STRL("PARAMS_DEFAULT"), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC
);
923 zend_declare_class_constant_long(php_http_env_class_entry
, ZEND_STRL("PARAMS_COLON_SEPARATOR"), PHP_HTTP_PARAMS_COLON_SEPARATOR TSRMLS_CC
);
925 PHP_HTTP_REGISTER_CLASS(http
\\Env
, Request
, http_env_request
, php_http_message_class_entry
, 0);
936 * vim600: noet sw=4 ts=4 fdm=marker
937 * vim<600: noet sw=4 ts=4