add bool http\Message::isMultipart([&$boundary]) and http\Message http\Message::split...
[m6w6/ext-http] / php_http_env.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 PHP_RINIT_FUNCTION(http_env)
16 {
17 PHP_HTTP_G->env.request.time = sapi_get_request_time(TSRMLS_C);
18
19 return SUCCESS;
20 }
21
22 PHP_RSHUTDOWN_FUNCTION(http_env)
23 {
24 if (PHP_HTTP_G->env.request.headers) {
25 zend_hash_destroy(PHP_HTTP_G->env.request.headers);
26 FREE_HASHTABLE(PHP_HTTP_G->env.request.headers);
27 PHP_HTTP_G->env.request.headers = NULL;
28 }
29 if (PHP_HTTP_G->env.request.body) {
30 php_http_message_body_free(&PHP_HTTP_G->env.request.body);
31 }
32
33 if (PHP_HTTP_G->env.server_var) {
34 zval_ptr_dtor(&PHP_HTTP_G->env.server_var);
35 PHP_HTTP_G->env.server_var = NULL;
36 }
37
38 return SUCCESS;
39 }
40
41 PHP_HTTP_API void php_http_env_get_request_headers(HashTable *headers TSRMLS_DC)
42 {
43 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
44 zval **hsv, **header;
45 HashPosition pos;
46
47 if (!PHP_HTTP_G->env.request.headers) {
48 ALLOC_HASHTABLE(PHP_HTTP_G->env.request.headers);
49 zend_hash_init(PHP_HTTP_G->env.request.headers, 0, NULL, ZVAL_PTR_DTOR, 0);
50
51 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
52
53 if (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv) && Z_TYPE_PP(hsv) == IS_ARRAY) {
54 FOREACH_KEY(pos, *hsv, key) {
55 if (key.type == HASH_KEY_IS_STRING && key.len > 6 && !strncmp(key.str, "HTTP_", 5)) {
56 key.len -= 5;
57 key.str = php_http_pretty_key(estrndup(key.str + 5, key.len - 1), key.len - 1, 1, 1);
58
59 zend_hash_get_current_data_ex(Z_ARRVAL_PP(hsv), (void *) &header, &pos);
60 Z_ADDREF_P(*header);
61 zend_symtable_update(PHP_HTTP_G->env.request.headers, key.str, key.len, (void *) header, sizeof(zval *), NULL);
62
63 efree(key.str);
64 }
65 }
66 }
67 }
68
69 if (headers) {
70 zend_hash_copy(headers, PHP_HTTP_G->env.request.headers, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
71 }
72 }
73
74 PHP_HTTP_API char *php_http_env_get_request_header(const char *name_str, size_t name_len, size_t *len TSRMLS_DC)
75 {
76 zval **zvalue;
77 char *val = NULL, *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
78
79 php_http_env_get_request_headers(NULL TSRMLS_CC);
80
81 if (SUCCESS == zend_symtable_find(PHP_HTTP_G->env.request.headers, key, name_len + 1, (void *) &zvalue)) {
82 zval *zcopy = php_http_ztyp(IS_STRING, *zvalue);
83
84 val = estrndup(Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy));
85 if (len) {
86 *len = Z_STRLEN_P(zcopy);
87 }
88 zval_ptr_dtor(&zcopy);
89 }
90
91 efree(key);
92
93 return val;
94 }
95
96 PHP_HTTP_API int php_http_env_got_request_header(const char *name_str, size_t name_len TSRMLS_DC)
97 {
98 char *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
99 int got;
100
101 php_http_env_get_request_headers(NULL TSRMLS_CC);
102 got = zend_symtable_exists(PHP_HTTP_G->env.request.headers, key, name_len + 1);
103 efree(key);
104
105 return got;
106 }
107
108 PHP_HTTP_API zval *php_http_env_get_superglobal(const char *key, size_t key_len TSRMLS_DC)
109 {
110 zval **hsv;
111
112 zend_is_auto_global(key, key_len TSRMLS_CC);
113
114 if ((SUCCESS != zend_hash_find(&EG(symbol_table), key, key_len + 1, (void *) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
115 return NULL;
116 }
117
118 return *hsv;
119 }
120
121 PHP_HTTP_API zval *php_http_env_get_server_var(const char *key, size_t key_len, zend_bool check TSRMLS_DC)
122 {
123 zval *hsv, **var;
124 char *env;
125
126 /* if available, this is a lot faster than accessing $_SERVER */
127 if (sapi_module.getenv) {
128 if ((!(env = sapi_module.getenv((char *) key, key_len TSRMLS_CC))) || (check && !*env)) {
129 return NULL;
130 }
131 if (PHP_HTTP_G->env.server_var) {
132 zval_ptr_dtor(&PHP_HTTP_G->env.server_var);
133 }
134 MAKE_STD_ZVAL(PHP_HTTP_G->env.server_var);
135 ZVAL_STRING(PHP_HTTP_G->env.server_var, env, 1);
136 return PHP_HTTP_G->env.server_var;
137 }
138
139 if (!(hsv = php_http_env_get_superglobal(ZEND_STRL("_SERVER") TSRMLS_CC))) {
140 return NULL;
141 }
142 if ((SUCCESS != zend_symtable_find(Z_ARRVAL_P(hsv), key, key_len + 1, (void *) &var))) {
143 return NULL;
144 }
145 if (check && !((Z_TYPE_PP(var) == IS_STRING) && Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
146 return NULL;
147 }
148 return *var;
149 }
150
151 PHP_HTTP_API php_http_message_body_t *php_http_env_get_request_body(TSRMLS_D)
152 {
153 if (!PHP_HTTP_G->env.request.body) {
154 php_stream *s = NULL;
155
156 if (SG(request_info).post_data || SG(request_info).raw_post_data) {
157 if ((s = php_stream_temp_new())) {
158 /* php://input does not support seek() */
159 if (SG(request_info).raw_post_data) {
160 php_stream_write(s, SG(request_info).raw_post_data, SG(request_info).raw_post_data_length);
161 } else {
162 php_stream_write(s, SG(request_info).post_data, SG(request_info).post_data_length);
163 }
164 php_stream_rewind(s);
165 }
166 } else if (sapi_module.read_post && !SG(read_post_bytes)) {
167 if ((s = php_stream_temp_new())) {
168 char *buf = emalloc(4096);
169 int len;
170
171 while (0 < (len = sapi_module.read_post(buf, 4096 TSRMLS_CC))) {
172 SG(read_post_bytes) += len;
173 php_stream_write(s, buf, len);
174
175 if (len < 4096) {
176 break;
177 }
178 }
179 efree(buf);
180
181 php_stream_rewind(s);
182 }
183 }
184 PHP_HTTP_G->env.request.body = php_http_message_body_init(NULL, s TSRMLS_CC);
185 }
186
187 return PHP_HTTP_G->env.request.body;
188 }
189
190 PHP_HTTP_API php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_t length TSRMLS_DC)
191 {
192 zval *zentry;
193 char *range, *rp, c;
194 long begin = -1, end = -1, *ptr;
195
196 if (!(range = php_http_env_get_request_header(ZEND_STRL("Range"), NULL TSRMLS_CC))) {
197 return PHP_HTTP_RANGE_NO;
198 }
199 if (strncmp(range, "bytes=", lenof("bytes="))) {
200 STR_FREE(range);
201 return PHP_HTTP_RANGE_NO;
202 }
203
204 rp = range + lenof("bytes=");
205 ptr = &begin;
206
207 do {
208 switch (c = *(rp++)) {
209 case '0':
210 /* allow 000... - shall we? */
211 if (*ptr != -10) {
212 *ptr *= 10;
213 }
214 break;
215
216 case '1': case '2': case '3':
217 case '4': case '5': case '6':
218 case '7': case '8': case '9':
219 /*
220 * If the value of the pointer is already set (non-negative)
221 * then multiply its value by ten and add the current value,
222 * else initialise the pointers value with the current value
223 * --
224 * This let us recognize empty fields when validating the
225 * ranges, i.e. a "-10" for begin and "12345" for the end
226 * was the following range request: "Range: bytes=0-12345";
227 * While a "-1" for begin and "12345" for the end would
228 * have been: "Range: bytes=-12345".
229 */
230 if (*ptr > 0) {
231 *ptr *= 10;
232 *ptr += c - '0';
233 } else {
234 *ptr = c - '0';
235 }
236 break;
237
238 case '-':
239 ptr = &end;
240 break;
241
242 case ' ':
243 break;
244
245 case 0:
246 case ',':
247
248 if (length) {
249 /* validate ranges */
250 switch (begin) {
251 /* "0-12345" */
252 case -10:
253 switch (end) {
254 /* "0-" */
255 case -1:
256 STR_FREE(range);
257 return PHP_HTTP_RANGE_NO;
258
259 /* "0-0" */
260 case -10:
261 end = 0;
262 break;
263
264 default:
265 if (length <= (size_t) end) {
266 end = length - 1;
267 }
268 break;
269 }
270 begin = 0;
271 break;
272
273 /* "-12345" */
274 case -1:
275 /* "-", "-0" */
276 if (end == -1 || end == -10) {
277 STR_FREE(range);
278 return PHP_HTTP_RANGE_ERR;
279 }
280 begin = length - end;
281 end = length - 1;
282 break;
283
284 /* "12345-(NNN)" */
285 default:
286 if (length <= (size_t) begin) {
287 STR_FREE(range);
288 return PHP_HTTP_RANGE_ERR;
289 }
290 switch (end) {
291 /* "12345-0" */
292 case -10:
293 STR_FREE(range);
294 return PHP_HTTP_RANGE_ERR;
295
296 /* "12345-" */
297 case -1:
298 end = length - 1;
299 break;
300
301 /* "12345-67890" */
302 default:
303 if (length <= (size_t) end) {
304 end = length - 1;
305 } else if (end < begin) {
306 STR_FREE(range);
307 return PHP_HTTP_RANGE_ERR;
308 }
309 break;
310 }
311 break;
312 }
313 }
314
315 MAKE_STD_ZVAL(zentry);
316 array_init(zentry);
317 add_index_long(zentry, 0, begin);
318 add_index_long(zentry, 1, end);
319 zend_hash_next_index_insert(ranges, &zentry, sizeof(zval *), NULL);
320
321 begin = -1;
322 end = -1;
323 ptr = &begin;
324
325 break;
326
327 default:
328 STR_FREE(range);
329 return PHP_HTTP_RANGE_NO;
330 }
331 } while (c != 0);
332
333 STR_FREE(range);
334 return PHP_HTTP_RANGE_OK;
335 }
336
337 static void grab_headers(void *data, void *arg TSRMLS_DC)
338 {
339 php_http_buffer_appendl(PHP_HTTP_BUFFER(arg), ((sapi_header_struct *)data)->header);
340 php_http_buffer_appends(PHP_HTTP_BUFFER(arg), PHP_HTTP_CRLF);
341 }
342
343 PHP_HTTP_API STATUS php_http_env_get_response_headers(HashTable *headers_ht TSRMLS_DC)
344 {
345 STATUS status;
346 php_http_buffer_t headers;
347
348 php_http_buffer_init(&headers);
349 zend_llist_apply_with_argument(&SG(sapi_headers).headers, grab_headers, &headers TSRMLS_CC);
350 php_http_buffer_fix(&headers);
351
352 status = php_http_headers_parse(PHP_HTTP_BUFFER_VAL(&headers), PHP_HTTP_BUFFER_LEN(&headers), headers_ht, NULL, NULL TSRMLS_CC);
353 php_http_buffer_dtor(&headers);
354
355 return status;
356 }
357
358 PHP_HTTP_API char *php_http_env_get_response_header(const char *name_str, size_t name_len TSRMLS_DC)
359 {
360 char *val = NULL;
361 HashTable headers;
362
363 zend_hash_init(&headers, 0, NULL, NULL, 0);
364 if (SUCCESS == php_http_env_get_response_headers(&headers TSRMLS_CC)) {
365 zval **zvalue;
366 char *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
367
368 if (SUCCESS == zend_symtable_find(&headers, key, name_len + 1, (void *) &zvalue)) {
369 zval *zcopy = php_http_ztyp(IS_STRING, *zvalue);
370
371 val = estrndup(Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy));
372 zval_ptr_dtor(&zcopy);
373 }
374
375 efree(key);
376 }
377 zend_hash_destroy(&headers);
378
379 return val;
380 }
381
382 PHP_HTTP_API long php_http_env_get_response_code(TSRMLS_D)
383 {
384 long code = SG(sapi_headers).http_response_code;
385 return code ? code : 200;
386 }
387
388 PHP_HTTP_API STATUS php_http_env_set_response_code(long http_code TSRMLS_DC)
389 {
390 return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) http_code TSRMLS_CC);
391 }
392
393 PHP_HTTP_API STATUS php_http_env_set_response_status_line(long code, php_http_version_t *v TSRMLS_DC)
394 {
395 sapi_header_line h = {0};
396 STATUS ret;
397
398 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));
399 ret = sapi_header_op(SAPI_HEADER_REPLACE, (void *) &h TSRMLS_CC);
400 efree(h.line);
401
402 return ret;
403 }
404
405 PHP_HTTP_API STATUS php_http_env_set_response_protocol_version(php_http_version_t *v TSRMLS_DC)
406 {
407 return php_http_env_set_response_status_line(php_http_env_get_response_code(TSRMLS_C), v TSRMLS_CC);
408 }
409
410 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)
411 {
412 sapi_header_line h = {estrndup(header_str, header_len), header_len, http_code};
413 STATUS ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC);
414 efree(h.line);
415 return ret;
416 }
417
418 PHP_HTTP_API STATUS php_http_env_set_response_header_format(long http_code, zend_bool replace TSRMLS_DC, const char *fmt, ...)
419 {
420 va_list args;
421 STATUS ret = FAILURE;
422 sapi_header_line h = {NULL, 0, http_code};
423
424 va_start(args, fmt);
425 h.line_len = vspprintf(&h.line, 0, fmt, args);
426 va_end(args);
427
428 if (h.line) {
429 if (h.line_len) {
430 ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC);
431 }
432 efree(h.line);
433 }
434 return ret;
435 }
436
437 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)
438 {
439 if (!value) {
440 sapi_header_line h = {(char *) name_str, name_len, http_code};
441
442 return sapi_header_op(SAPI_HEADER_DELETE, (void *) &h TSRMLS_CC);
443 }
444
445 if(Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
446 HashPosition pos;
447 int first = replace;
448 zval **data_ptr;
449
450 FOREACH_HASH_VAL(pos, HASH_OF(value), data_ptr) {
451 if (SUCCESS != php_http_env_set_response_header_value(http_code, name_str, name_len, *data_ptr, first TSRMLS_CC)) {
452 return FAILURE;
453 }
454 first = 0;
455 }
456
457 return SUCCESS;
458 } else {
459 zval *data = php_http_ztyp(IS_STRING, value);
460
461 if (!Z_STRLEN_P(data)) {
462 zval_ptr_dtor(&data);
463 return php_http_env_set_response_header_value(http_code, name_str, name_len, NULL, replace TSRMLS_CC);
464 } else {
465 sapi_header_line h;
466 STATUS ret;
467
468 if (name_len > INT_MAX) {
469 name_len = INT_MAX;
470 }
471 h.response_code = http_code;
472 h.line_len = spprintf(&h.line, 0, "%.*s: %.*s", (int) name_len, name_str, Z_STRLEN_P(data), Z_STRVAL_P(data));
473
474 ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC);
475
476 zval_ptr_dtor(&data);
477 STR_FREE(h.line);
478
479 return ret;
480 }
481 }
482 }
483
484
485 static PHP_HTTP_STRLIST(php_http_env_response_status) =
486 PHP_HTTP_STRLIST_ITEM("Continue")
487 PHP_HTTP_STRLIST_ITEM("Switching Protocols")
488 PHP_HTTP_STRLIST_NEXT
489 PHP_HTTP_STRLIST_ITEM("OK")
490 PHP_HTTP_STRLIST_ITEM("Created")
491 PHP_HTTP_STRLIST_ITEM("Accepted")
492 PHP_HTTP_STRLIST_ITEM("Non-Authoritative Information")
493 PHP_HTTP_STRLIST_ITEM("No Content")
494 PHP_HTTP_STRLIST_ITEM("Reset Content")
495 PHP_HTTP_STRLIST_ITEM("Partial Content")
496 PHP_HTTP_STRLIST_NEXT
497 PHP_HTTP_STRLIST_ITEM("Multiple Choices")
498 PHP_HTTP_STRLIST_ITEM("Moved Permanently")
499 PHP_HTTP_STRLIST_ITEM("Found")
500 PHP_HTTP_STRLIST_ITEM("See Other")
501 PHP_HTTP_STRLIST_ITEM("Not Modified")
502 PHP_HTTP_STRLIST_ITEM("Use Proxy")
503 PHP_HTTP_STRLIST_ITEM("(Unused)")
504 PHP_HTTP_STRLIST_ITEM("Temporary Redirect")
505 PHP_HTTP_STRLIST_NEXT
506 PHP_HTTP_STRLIST_ITEM("Bad Request")
507 PHP_HTTP_STRLIST_ITEM("Unauthorized")
508 PHP_HTTP_STRLIST_ITEM("Payment Required")
509 PHP_HTTP_STRLIST_ITEM("Forbidden")
510 PHP_HTTP_STRLIST_ITEM("Not Found")
511 PHP_HTTP_STRLIST_ITEM("Method Not Allowed")
512 PHP_HTTP_STRLIST_ITEM("Not Acceptable")
513 PHP_HTTP_STRLIST_ITEM("Proxy Authentication Required")
514 PHP_HTTP_STRLIST_ITEM("Request Timeout")
515 PHP_HTTP_STRLIST_ITEM("Conflict")
516 PHP_HTTP_STRLIST_ITEM("Gone")
517 PHP_HTTP_STRLIST_ITEM("Length Required")
518 PHP_HTTP_STRLIST_ITEM("Precondition Failed")
519 PHP_HTTP_STRLIST_ITEM("Request Entity Too Large")
520 PHP_HTTP_STRLIST_ITEM("Request URI Too Long")
521 PHP_HTTP_STRLIST_ITEM("Unsupported Media Type")
522 PHP_HTTP_STRLIST_ITEM("Requested Range Not Satisfiable")
523 PHP_HTTP_STRLIST_ITEM("Expectation Failed")
524 PHP_HTTP_STRLIST_NEXT
525 PHP_HTTP_STRLIST_ITEM("Internal Server Error")
526 PHP_HTTP_STRLIST_ITEM("Not Implemented")
527 PHP_HTTP_STRLIST_ITEM("Bad Gateway")
528 PHP_HTTP_STRLIST_ITEM("Service Unavailable")
529 PHP_HTTP_STRLIST_ITEM("Gateway Timeout")
530 PHP_HTTP_STRLIST_ITEM("HTTP Version Not Supported")
531 PHP_HTTP_STRLIST_STOP
532 ;
533
534 PHP_HTTP_API const char *php_http_env_get_response_status_for_code(unsigned code)
535 {
536 return php_http_strlist_find(php_http_env_response_status, 100, code);
537 }
538
539 zend_class_entry *php_http_env_class_entry;
540
541 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpEnv, method, 0, req_args)
542 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpEnv, method, 0)
543 #define PHP_HTTP_ENV_ME(method) PHP_ME(HttpEnv, method, PHP_HTTP_ARGS(HttpEnv, method), ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
544
545 PHP_HTTP_BEGIN_ARGS(getRequestHeader, 0)
546 PHP_HTTP_ARG_VAL(header_name, 0)
547 PHP_HTTP_END_ARGS;
548
549 PHP_HTTP_BEGIN_ARGS(getRequestBody, 0)
550 PHP_HTTP_ARG_VAL(body_class_name, 0)
551 PHP_HTTP_END_ARGS;
552
553 PHP_HTTP_BEGIN_ARGS(getResponseStatusForCode, 1)
554 PHP_HTTP_ARG_VAL(code, 0)
555 PHP_HTTP_END_ARGS;
556
557 PHP_HTTP_BEGIN_ARGS(getResponseHeader, 0)
558 PHP_HTTP_ARG_VAL(header_name, 0)
559 PHP_HTTP_END_ARGS;
560
561 PHP_HTTP_EMPTY_ARGS(getResponseCode);
562
563 PHP_HTTP_BEGIN_ARGS(setResponseHeader, 1)
564 PHP_HTTP_ARG_VAL(header_name, 0)
565 PHP_HTTP_ARG_VAL(header_value, 0)
566 PHP_HTTP_ARG_VAL(response_code, 0)
567 PHP_HTTP_ARG_VAL(replace_header, 0)
568 PHP_HTTP_END_ARGS;
569
570 PHP_HTTP_BEGIN_ARGS(setResponseCode, 1)
571 PHP_HTTP_ARG_VAL(code, 0)
572 PHP_HTTP_END_ARGS;
573
574 PHP_HTTP_BEGIN_ARGS(negotiateLanguage, 1)
575 PHP_HTTP_ARG_VAL(supported, 0)
576 PHP_HTTP_ARG_VAL(result_array, 1)
577 PHP_HTTP_END_ARGS;
578
579 PHP_HTTP_BEGIN_ARGS(negotiateContentType, 1)
580 PHP_HTTP_ARG_VAL(supported, 0)
581 PHP_HTTP_ARG_VAL(result_array, 1)
582 PHP_HTTP_END_ARGS;
583
584 PHP_HTTP_BEGIN_ARGS(negotiateCharset, 1)
585 PHP_HTTP_ARG_VAL(supported, 0)
586 PHP_HTTP_ARG_VAL(result_array, 1)
587 PHP_HTTP_END_ARGS;
588
589 PHP_HTTP_BEGIN_ARGS(negotiateEncoding, 1)
590 PHP_HTTP_ARG_VAL(supported, 0)
591 PHP_HTTP_ARG_VAL(result_array, 1)
592 PHP_HTTP_END_ARGS;
593
594 PHP_HTTP_BEGIN_ARGS(negotiate, 2)
595 PHP_HTTP_ARG_VAL(value, 0)
596 PHP_HTTP_ARG_VAL(supported, 0)
597 PHP_HTTP_ARG_VAL(primary_type_separator, 0)
598 PHP_HTTP_ARG_VAL(result_array, 1)
599 PHP_HTTP_END_ARGS;
600
601 PHP_HTTP_EMPTY_ARGS(statPersistentHandles);
602
603 PHP_HTTP_BEGIN_ARGS(cleanPersistentHandles, 0)
604 PHP_HTTP_ARG_VAL(name, 0)
605 PHP_HTTP_ARG_VAL(ident, 0)
606 PHP_HTTP_END_ARGS;
607
608 zend_function_entry php_http_env_method_entry[] = {
609 PHP_HTTP_ENV_ME(getRequestHeader)
610 PHP_HTTP_ENV_ME(getRequestBody)
611
612 PHP_HTTP_ENV_ME(getResponseStatusForCode)
613
614 PHP_HTTP_ENV_ME(getResponseHeader)
615 PHP_HTTP_ENV_ME(getResponseCode)
616 PHP_HTTP_ENV_ME(setResponseHeader)
617 PHP_HTTP_ENV_ME(setResponseCode)
618
619 PHP_HTTP_ENV_ME(negotiateLanguage)
620 PHP_HTTP_ENV_ME(negotiateContentType)
621 PHP_HTTP_ENV_ME(negotiateEncoding)
622 PHP_HTTP_ENV_ME(negotiateCharset)
623 PHP_HTTP_ENV_ME(negotiate)
624
625 PHP_HTTP_ENV_ME(statPersistentHandles)
626 PHP_HTTP_ENV_ME(cleanPersistentHandles)
627
628 EMPTY_FUNCTION_ENTRY
629 };
630
631 PHP_METHOD(HttpEnv, getRequestHeader)
632 {
633 char *header_name_str = NULL;
634 int header_name_len = 0;
635
636 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
637 if (header_name_str && header_name_len) {
638 size_t header_length;
639 char *header_value = php_http_env_get_request_header(header_name_str, header_name_len, &header_length TSRMLS_CC);
640
641 if (header_value) {
642 RETURN_STRINGL(header_value, header_length, 0);
643 }
644 RETURN_NULL();
645 } else {
646 array_init(return_value);
647 php_http_env_get_request_headers(Z_ARRVAL_P(return_value) TSRMLS_CC);
648 return;
649 }
650 }
651 RETURN_FALSE;
652 }
653
654 PHP_METHOD(HttpEnv, getRequestBody)
655 {
656 with_error_handling(EH_THROW, php_http_exception_class_entry) {
657 zend_class_entry *class_entry = php_http_message_body_class_entry;
658
659 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|C", &class_entry)) {
660 zend_object_value ov;
661 php_http_message_body_t *body = php_http_env_get_request_body(TSRMLS_C);
662
663 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, php_http_message_body_copy(body, NULL, 0), NULL TSRMLS_CC)) {
664 RETURN_OBJVAL(ov, 0);
665 }
666 }
667 } end_error_handling();
668 }
669
670 PHP_METHOD(HttpEnv, getResponseStatusForCode)
671 {
672 long code;
673
674 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
675 RETURN_STRING(php_http_env_get_response_status_for_code(code), 1);
676 }
677 RETURN_FALSE;
678 }
679
680 PHP_METHOD(HttpEnv, getResponseHeader)
681 {
682 char *header_name_str = NULL;
683 int header_name_len = 0;
684
685 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
686 if (header_name_str && header_name_len) {
687 char *header_value = php_http_env_get_response_header(header_name_str, header_name_len TSRMLS_CC);
688
689 if (header_value) {
690 RETURN_STRING(header_value, 0);
691 }
692 RETURN_NULL();
693 } else {
694 array_init(return_value);
695 php_http_env_get_response_headers(Z_ARRVAL_P(return_value) TSRMLS_CC);
696 return;
697 }
698 }
699 RETURN_FALSE;
700 }
701
702 PHP_METHOD(HttpEnv, getResponseCode)
703 {
704 if (SUCCESS == zend_parse_parameters_none()) {
705 RETURN_LONG(php_http_env_get_response_code(TSRMLS_C));
706 }
707 RETURN_FALSE;
708 }
709
710 PHP_METHOD(HttpEnv, setResponseHeader)
711 {
712 char *header_name_str;
713 int header_name_len;
714 zval *header_value = NULL;
715 long code = 0;
716 zend_bool replace_header = 1;
717
718 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!lb", &header_name_str, &header_name_len, &header_value, &code, &replace_header)) {
719 RETURN_SUCCESS(php_http_env_set_response_header_value(code, header_name_str, header_name_len, header_value, replace_header TSRMLS_CC));
720 }
721 RETURN_FALSE;
722 }
723
724 PHP_METHOD(HttpEnv, setResponseCode)
725 {
726 long code;
727
728 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
729 RETURN_SUCCESS(php_http_env_set_response_code(code TSRMLS_CC));
730 }
731 RETURN_FALSE;
732 }
733
734 PHP_METHOD(HttpEnv, negotiateLanguage)
735 {
736 HashTable *supported;
737 zval *rs_array = NULL;
738
739 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
740 if (rs_array) {
741 zval_dtor(rs_array);
742 array_init(rs_array);
743 }
744
745 PHP_HTTP_DO_NEGOTIATE(language, supported, rs_array);
746 } else {
747 RETURN_FALSE;
748 }
749 }
750
751 PHP_METHOD(HttpEnv, negotiateCharset)
752 {
753 HashTable *supported;
754 zval *rs_array = NULL;
755
756 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
757 if (rs_array) {
758 zval_dtor(rs_array);
759 array_init(rs_array);
760 }
761 PHP_HTTP_DO_NEGOTIATE(charset, supported, rs_array);
762 } else {
763 RETURN_FALSE;
764 }
765 }
766
767 PHP_METHOD(HttpEnv, negotiateEncoding)
768 {
769 HashTable *supported;
770 zval *rs_array = NULL;
771
772 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
773 if (rs_array) {
774 zval_dtor(rs_array);
775 array_init(rs_array);
776 }
777 PHP_HTTP_DO_NEGOTIATE(encoding, supported, rs_array);
778 } else {
779 RETURN_FALSE;
780 }
781 }
782
783 PHP_METHOD(HttpEnv, negotiateContentType)
784 {
785 HashTable *supported;
786 zval *rs_array = NULL;
787
788 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
789 if (rs_array) {
790 zval_dtor(rs_array);
791 array_init(rs_array);
792 }
793 PHP_HTTP_DO_NEGOTIATE(content_type, supported, rs_array);
794 } else {
795 RETURN_FALSE;
796 }
797 }
798
799 PHP_METHOD(HttpEnv, negotiate)
800 {
801 HashTable *supported;
802 zval *rs_array = NULL;
803 char *value_str, *sep_str = NULL;
804 int value_len, sep_len = 0;
805
806 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sH|s!z", &value_str, &value_len, &supported, &sep_str, &sep_len, &rs_array)) {
807 HashTable *rs;
808
809 if (rs_array) {
810 zval_dtor(rs_array);
811 array_init(rs_array);
812 }
813
814 if ((rs = php_http_negotiate(value_str, value_len, supported, sep_str, sep_len TSRMLS_CC))) {
815 PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs, supported, rs_array);
816 } else {
817 PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array);
818 }
819 } else {
820 RETURN_FALSE;
821 }
822 }
823
824 PHP_METHOD(HttpEnv, statPersistentHandles)
825 {
826 if (SUCCESS == zend_parse_parameters_none()) {
827 object_init(return_value);
828 if (php_http_persistent_handle_statall(HASH_OF(return_value) TSRMLS_CC)) {
829 return;
830 }
831 zval_dtor(return_value);
832 }
833 RETURN_FALSE;
834 }
835
836 PHP_METHOD(HttpEnv, cleanPersistentHandles)
837 {
838 char *name_str = NULL, *ident_str = NULL;
839 int name_len = 0, ident_len = 0;
840
841 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!", &name_str, &name_len, &ident_str, &ident_len)) {
842 php_http_persistent_handle_cleanup(name_str, name_len, ident_str, ident_len TSRMLS_CC);
843 }
844 }
845
846 PHP_MINIT_FUNCTION(http_env)
847 {
848 PHP_HTTP_REGISTER_CLASS(http, Env, http_env, NULL, 0);
849
850 return SUCCESS;
851 }
852
853
854 /*
855 * Local variables:
856 * tab-width: 4
857 * c-basic-offset: 4
858 * End:
859 * vim600: noet sw=4 ts=4 fdm=marker
860 * vim<600: noet sw=4 ts=4
861 */