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