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