zend_hash_get_current_key_ex does not accept NULL for position anymore
[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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14 #include "php_variables.h"
15
16 PHP_RINIT_FUNCTION(http_env)
17 {
18 /* populate form data on non-POST requests */
19 if (SG(request_info).request_method && strcasecmp(SG(request_info).request_method, "POST") && SG(request_info).content_type && *SG(request_info).content_type) {
20 uint ct_len = strlen(SG(request_info).content_type);
21 char *ct_str = estrndup(SG(request_info).content_type, ct_len);
22 php_http_params_opts_t opts;
23 HashTable params;
24
25 php_http_params_opts_default_get(&opts);
26 opts.input.str = ct_str;
27 opts.input.len = ct_len;
28
29 SG(request_info).content_type_dup = ct_str;
30
31 ZEND_INIT_SYMTABLE(&params);
32 if (php_http_params_parse(&params, &opts)) {
33 zend_string *key_str;
34 zend_ulong key_num;
35
36 if (HASH_KEY_IS_STRING == zend_hash_get_current_key(&params, &key_str, &key_num)) {
37 sapi_post_entry *post_entry = NULL;
38
39 if ((post_entry = zend_hash_find_ptr(&SG(known_post_content_types), key_str))) {
40 if (post_entry) {
41 SG(request_info).post_entry = post_entry;
42
43 if (post_entry->post_reader) {
44 post_entry->post_reader();
45 }
46 }
47
48 if (sapi_module.default_post_reader) {
49 sapi_module.default_post_reader();
50 }
51
52 sapi_handle_post(&PG(http_globals)[TRACK_VARS_POST]);
53
54 /*
55 * the rfc1867 handler is an awkward buddy
56 */
57 Z_TRY_ADDREF(PG(http_globals)[TRACK_VARS_FILES]);
58 zend_hash_str_update(&EG(symbol_table).ht, "_FILES", sizeof("_FILES"), &PG(http_globals)[TRACK_VARS_FILES]);
59 }
60 }
61 zend_hash_destroy(&params);
62 }
63 }
64
65 PTR_SET(SG(request_info).content_type_dup, NULL);
66
67 return SUCCESS;
68 }
69
70 PHP_RSHUTDOWN_FUNCTION(http_env)
71 {
72 if (PHP_HTTP_G->env.request.headers) {
73 zend_hash_destroy(PHP_HTTP_G->env.request.headers);
74 FREE_HASHTABLE(PHP_HTTP_G->env.request.headers);
75 PHP_HTTP_G->env.request.headers = NULL;
76 }
77 if (PHP_HTTP_G->env.request.body) {
78 php_http_message_body_free(&PHP_HTTP_G->env.request.body);
79 }
80
81 if (PHP_HTTP_G->env.server_var) {
82 zval_ptr_dtor(PHP_HTTP_G->env.server_var);
83 PHP_HTTP_G->env.server_var = NULL;
84 }
85
86 return SUCCESS;
87 }
88
89 void php_http_env_get_request_headers(HashTable *headers TSRMLS_DC)
90 {
91 php_http_arrkey_t key;
92 zval *hsv, *header;
93
94 if (!PHP_HTTP_G->env.request.headers) {
95 ALLOC_HASHTABLE(PHP_HTTP_G->env.request.headers);
96 ZEND_INIT_SYMTABLE(PHP_HTTP_G->env.request.headers);
97
98 if ((hsv = php_http_env_get_superglobal(ZEND_STRL("_SERVER")))) {
99 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(hsv), key.h, key.key, header)
100 {
101 if (key.key && key.key->len > 5 && *key.key->val == 'H' && !strncmp(key.key->val, "HTTP_", 5)) {
102 size_t key_len = key.key->len - 5;
103 char *key_str = php_http_pretty_key(estrndup(&key.key->val[5], key_len), key_len, 1, 1);
104
105 Z_TRY_ADDREF_P(header);
106 zend_symtable_str_update(PHP_HTTP_G->env.request.headers, key_str, key_len, header);
107
108 efree(key_str);
109 } else if (key.key && key.key->len > 8 && *key.key->val == 'C' && !strncmp(key.key->val, "CONTENT_", 8)) {
110 char *key_str = php_http_pretty_key(estrndup(key.key->val, key.key->len), key.key->len, 1, 1);
111
112 Z_TRY_ADDREF_P(header);
113 zend_symtable_str_update(PHP_HTTP_G->env.request.headers, key_str, key.key->len, header);
114
115 efree(key_str);
116 }
117 }
118 ZEND_HASH_FOREACH_END();
119 }
120 }
121
122 if (headers) {
123 array_copy(PHP_HTTP_G->env.request.headers, headers);
124 }
125 }
126
127 char *php_http_env_get_request_header(const char *name_str, size_t name_len, size_t *len, php_http_message_t *request)
128 {
129 HashTable *request_headers;
130 zval *zvalue = NULL;
131 char *val = NULL, *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
132
133 if (request) {
134 request_headers = &request->hdrs;
135 } else {
136 php_http_env_get_request_headers(NULL);
137 request_headers = PHP_HTTP_G->env.request.headers;
138 }
139
140 if ((zvalue == zend_symtable_str_find(request_headers, key, name_len))) {
141 zend_string *zs = zval_get_string(zvalue);
142
143 val = estrndup(zs->val, zs->len);
144 if (len) {
145 *len = zs->len;
146 }
147 zend_string_release(zs);
148 }
149
150 efree(key);
151
152 return val;
153 }
154
155 zend_bool php_http_env_got_request_header(const char *name_str, size_t name_len, php_http_message_t *request)
156 {
157 HashTable *request_headers;
158 char *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
159 zend_bool got;
160
161 if (request) {
162 request_headers = &request->hdrs;
163 } else {
164 php_http_env_get_request_headers(NULL);
165 request_headers = PHP_HTTP_G->env.request.headers;
166 }
167 got = zend_symtable_str_exists(request_headers, key, name_len);
168 efree(key);
169
170 return got;
171 }
172
173 zval *php_http_env_get_superglobal(const char *key, size_t key_len)
174 {
175 zval *hsv;
176 zend_string *key_str = zend_string_init(key, key_len, 0);
177
178 zend_is_auto_global(key_str);
179 hsv = zend_hash_find(&EG(symbol_table).ht, key_str);
180 zend_string_release(key_str);
181
182 if (Z_TYPE_P(hsv) != IS_ARRAY) {
183 return NULL;
184 }
185
186 return hsv;
187 }
188
189 zval *php_http_env_get_server_var(const char *key, size_t key_len, zend_bool check)
190 {
191 zval *hsv, *var;
192
193 /* if available, this is a lot faster than accessing $_SERVER * /
194 if (sapi_module.getenv) {
195 char *env;
196
197 if ((!(env = sapi_module.getenv((char *) key, key_len))) || (check && !*env)) {
198 return NULL;
199 }
200 if (PHP_HTTP_G->env.server_var) {
201 zval_ptr_dtor(&PHP_HTTP_G->env.server_var);
202 }
203 MAKE_STD_ZVAL(PHP_HTTP_G->env.server_var);
204 ZVAL_STRING(PHP_HTTP_G->env.server_var, env, 1);
205 return PHP_HTTP_G->env.server_var;
206 }
207 / * */
208
209 if (!(hsv = php_http_env_get_superglobal(ZEND_STRL("_SERVER")))) {
210 return NULL;
211 }
212 if (!(var = zend_symtable_str_find(Z_ARRVAL_P(hsv), key, key_len))) {
213 return NULL;
214 }
215 if (check && !((Z_TYPE_P(var) == IS_STRING) && Z_STRVAL_P(var) && Z_STRLEN_P(var))) {
216 return NULL;
217 }
218 return var;
219 }
220
221 php_http_message_body_t *php_http_env_get_request_body(void)
222 {
223 if (!PHP_HTTP_G->env.request.body) {
224 php_stream *s = php_stream_temp_new();
225 php_stream *input = php_stream_open_wrapper("php://input", "r", 0, NULL);
226
227 /* php://input does not support stat */
228 php_stream_copy_to_stream_ex(input, s, -1, NULL);
229 php_stream_close(input);
230
231 php_stream_rewind(s);
232 PHP_HTTP_G->env.request.body = php_http_message_body_init(NULL, s);
233 }
234
235 return PHP_HTTP_G->env.request.body;
236 }
237
238 const char *php_http_env_get_request_method(php_http_message_t *request)
239 {
240 const char *m;
241
242 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, request)) {
243 m = request->http.info.request.method;
244 } else {
245 m = SG(request_info).request_method;
246 }
247
248 return m ? m : "GET";
249 }
250
251 php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_t length, php_http_message_t *request)
252 {
253 zval zentry;
254 char *range, *rp, c;
255 long begin = -1, end = -1, *ptr;
256
257 if (!(range = php_http_env_get_request_header(ZEND_STRL("Range"), NULL, request))) {
258 return PHP_HTTP_RANGE_NO;
259 }
260 if (strncmp(range, "bytes=", lenof("bytes="))) {
261 PTR_FREE(range);
262 return PHP_HTTP_RANGE_NO;
263 }
264
265 rp = range + lenof("bytes=");
266 ptr = &begin;
267
268 do {
269 switch (c = *(rp++)) {
270 case '0':
271 /* allow 000... - shall we? */
272 if (*ptr != -10) {
273 *ptr *= 10;
274 }
275 break;
276
277 case '1': case '2': case '3':
278 case '4': case '5': case '6':
279 case '7': case '8': case '9':
280 /*
281 * If the value of the pointer is already set (non-negative)
282 * then multiply its value by ten and add the current value,
283 * else initialise the pointers value with the current value
284 * --
285 * This let us recognize empty fields when validating the
286 * ranges, i.e. a "-10" for begin and "12345" for the end
287 * was the following range request: "Range: bytes=0-12345";
288 * While a "-1" for begin and "12345" for the end would
289 * have been: "Range: bytes=-12345".
290 */
291 if (*ptr > 0) {
292 *ptr *= 10;
293 *ptr += c - '0';
294 } else {
295 *ptr = c - '0';
296 }
297 break;
298
299 case '-':
300 ptr = &end;
301 break;
302
303 case ' ':
304 break;
305
306 case 0:
307 case ',':
308
309 if (length) {
310 /* validate ranges */
311 switch (begin) {
312 /* "0-12345" */
313 case -10:
314 switch (end) {
315 /* "0-" */
316 case -1:
317 PTR_FREE(range);
318 return PHP_HTTP_RANGE_NO;
319
320 /* "0-0" */
321 case -10:
322 end = 0;
323 break;
324
325 default:
326 if (length <= (size_t) end) {
327 end = length - 1;
328 }
329 break;
330 }
331 begin = 0;
332 break;
333
334 /* "-12345" */
335 case -1:
336 /* "-", "-0" */
337 if (end == -1 || end == -10) {
338 PTR_FREE(range);
339 return PHP_HTTP_RANGE_ERR;
340 }
341 begin = length - end;
342 end = length - 1;
343 break;
344
345 /* "12345-(NNN)" */
346 default:
347 if (length <= (size_t) begin) {
348 PTR_FREE(range);
349 return PHP_HTTP_RANGE_ERR;
350 }
351 switch (end) {
352 /* "12345-0" */
353 case -10:
354 PTR_FREE(range);
355 return PHP_HTTP_RANGE_ERR;
356
357 /* "12345-" */
358 case -1:
359 end = length - 1;
360 break;
361
362 /* "12345-67890" */
363 default:
364 if (length <= (size_t) end) {
365 end = length - 1;
366 } else if (end < begin) {
367 PTR_FREE(range);
368 return PHP_HTTP_RANGE_ERR;
369 }
370 break;
371 }
372 break;
373 }
374 }
375
376 array_init(&zentry);
377 add_index_long(&zentry, 0, begin);
378 add_index_long(&zentry, 1, end);
379 zend_hash_next_index_insert(ranges, &zentry);
380
381 begin = -1;
382 end = -1;
383 ptr = &begin;
384
385 break;
386
387 default:
388 PTR_FREE(range);
389 return PHP_HTTP_RANGE_NO;
390 }
391 } while (c != 0);
392
393 PTR_FREE(range);
394 return PHP_HTTP_RANGE_OK;
395 }
396
397 static void grab_headers(void *data, void *arg)
398 {
399 php_http_buffer_appendl(PHP_HTTP_BUFFER(arg), ((sapi_header_struct *)data)->header);
400 php_http_buffer_appends(PHP_HTTP_BUFFER(arg), PHP_HTTP_CRLF);
401 }
402
403 static void grab_header(void *data, void *arg)
404 {
405 struct {
406 char *name_str;
407 size_t name_len;
408 char *value_ptr;
409 } *args = arg;
410 sapi_header_struct *header = data;
411
412 if ( header->header_len > args->name_len
413 && header->header[args->name_len] == ':'
414 && !strncmp(header->header, args->name_str, args->name_len)
415 ) {
416 args->value_ptr = &header->header[args->name_len + 1];
417 }
418 }
419
420 ZEND_RESULT_CODE php_http_env_get_response_headers(HashTable *headers_ht)
421 {
422 ZEND_RESULT_CODE status;
423 php_http_buffer_t headers;
424
425 php_http_buffer_init(&headers);
426 zend_llist_apply_with_argument(&SG(sapi_headers).headers, grab_headers, &headers);
427 php_http_buffer_fix(&headers);
428
429 status = php_http_header_parse(headers.data, headers.used, headers_ht, NULL, NULL);
430 php_http_buffer_dtor(&headers);
431
432 return status;
433 }
434
435 char *php_http_env_get_response_header(const char *name_str, size_t name_len)
436 {
437 struct {
438 char *name_str;
439 size_t name_len;
440 char *value_ptr;
441 } args;
442
443 args.name_str = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
444 args.name_len = name_len;
445 args.value_ptr = NULL;
446 zend_llist_apply_with_argument(&SG(sapi_headers).headers, grab_header, &args);
447 efree(args.name_str);
448
449 return args.value_ptr ? estrdup(args.value_ptr) : NULL;
450 }
451
452 long php_http_env_get_response_code(void)
453 {
454 long code = SG(sapi_headers).http_response_code;
455 return code ? code : 200;
456 }
457
458 ZEND_RESULT_CODE php_http_env_set_response_code(long http_code)
459 {
460 return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) (zend_intptr_t) http_code);
461 }
462
463 ZEND_RESULT_CODE php_http_env_set_response_status_line(long code, php_http_version_t *v)
464 {
465 sapi_header_line h = {NULL, 0, 0};
466 ZEND_RESULT_CODE ret;
467
468 h.line_len = spprintf(&h.line, 0, "HTTP/%u.%u %ld %s", v->major, v->minor, code, php_http_env_get_response_status_for_code(code));
469 ret = sapi_header_op(SAPI_HEADER_REPLACE, (void *) &h);
470 efree(h.line);
471
472 return ret;
473 }
474
475 ZEND_RESULT_CODE php_http_env_set_response_protocol_version(php_http_version_t *v)
476 {
477 return php_http_env_set_response_status_line(php_http_env_get_response_code(), v);
478 }
479
480 ZEND_RESULT_CODE php_http_env_set_response_header(long http_code, const char *header_str, size_t header_len, zend_bool replace)
481 {
482 sapi_header_line h = {estrndup(header_str, header_len), header_len, http_code};
483 ZEND_RESULT_CODE ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h);
484 efree(h.line);
485 return ret;
486 }
487
488 ZEND_RESULT_CODE php_http_env_set_response_header_va(long http_code, zend_bool replace, const char *fmt, va_list argv)
489 {
490 ZEND_RESULT_CODE ret = FAILURE;
491 sapi_header_line h = {NULL, 0, http_code};
492
493 h.line_len = vspprintf(&h.line, 0, fmt, argv);
494
495 if (h.line) {
496 if (h.line_len) {
497 ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h);
498 }
499 efree(h.line);
500 }
501 return ret;
502 }
503
504 ZEND_RESULT_CODE php_http_env_set_response_header_format(long http_code, zend_bool replace, const char *fmt, ...)
505 {
506 ZEND_RESULT_CODE ret;
507 va_list args;
508
509 va_start(args, fmt);
510 ret = php_http_env_set_response_header_va(http_code, replace, fmt, args);
511 va_end(args);
512
513 return ret;
514 }
515
516 ZEND_RESULT_CODE php_http_env_set_response_header_value(long http_code, const char *name_str, size_t name_len, zval *value, zend_bool replace)
517 {
518 if (!value) {
519 sapi_header_line h = {(char *) name_str, name_len, http_code};
520
521 return sapi_header_op(SAPI_HEADER_DELETE, (void *) &h);
522 }
523
524 if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
525 int first = replace;
526 zval *data_ptr;
527 HashTable *ht = HASH_OF(value);
528
529 ZEND_HASH_FOREACH_VAL(ht, data_ptr)
530 {
531 if (SUCCESS != php_http_env_set_response_header_value(http_code, name_str, name_len, data_ptr, first)) {
532 return FAILURE;
533 }
534 first = 0;
535 }
536 ZEND_HASH_FOREACH_END();
537
538 return SUCCESS;
539 } else {
540 zend_string *data = zval_get_string(value);
541
542 if (!data->len) {
543 zend_string_release(data);
544 return php_http_env_set_response_header_value(http_code, name_str, name_len, NULL, replace);
545 } else {
546 sapi_header_line h;
547 ZEND_RESULT_CODE ret;
548
549 if (name_len > INT_MAX) {
550 return FAILURE;
551 }
552 h.response_code = http_code;
553 h.line_len = spprintf(&h.line, 0, "%.*s: %.*s", (int) name_len, name_str, data->len, data->val);
554
555 ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h);
556
557 zend_string_release(data);
558 PTR_FREE(h.line);
559
560 return ret;
561 }
562 }
563 }
564
565 static PHP_HTTP_STRLIST(php_http_env_response_status) =
566 PHP_HTTP_STRLIST_ITEM("Continue")
567 PHP_HTTP_STRLIST_ITEM("Switching Protocols")
568 PHP_HTTP_STRLIST_ITEM("Processing")
569 PHP_HTTP_STRLIST_NEXT
570 PHP_HTTP_STRLIST_ITEM("OK")
571 PHP_HTTP_STRLIST_ITEM("Created")
572 PHP_HTTP_STRLIST_ITEM("Accepted")
573 PHP_HTTP_STRLIST_ITEM("Non-Authoritative Information")
574 PHP_HTTP_STRLIST_ITEM("No Content")
575 PHP_HTTP_STRLIST_ITEM("Reset Content")
576 PHP_HTTP_STRLIST_ITEM("Partial Content")
577 PHP_HTTP_STRLIST_ITEM("Multi-Status")
578 PHP_HTTP_STRLIST_ITEM("Already Reported")
579 PHP_HTTP_STRLIST_ITEM("(Unused)")
580 PHP_HTTP_STRLIST_ITEM("(Unused)")
581 PHP_HTTP_STRLIST_ITEM("(Unused)")
582 PHP_HTTP_STRLIST_ITEM("(Unused)")
583 PHP_HTTP_STRLIST_ITEM("(Unused)")
584 PHP_HTTP_STRLIST_ITEM("(Unused)")
585 PHP_HTTP_STRLIST_ITEM("(Unused)")
586 PHP_HTTP_STRLIST_ITEM("(Unused)")
587 PHP_HTTP_STRLIST_ITEM("(Unused)")
588 PHP_HTTP_STRLIST_ITEM("(Unused)")
589 PHP_HTTP_STRLIST_ITEM("(Unused)")
590 PHP_HTTP_STRLIST_ITEM("(Unused)")
591 PHP_HTTP_STRLIST_ITEM("(Unused)")
592 PHP_HTTP_STRLIST_ITEM("(Unused)")
593 PHP_HTTP_STRLIST_ITEM("(Unused)")
594 PHP_HTTP_STRLIST_ITEM("(Unused)")
595 PHP_HTTP_STRLIST_ITEM("(Unused)")
596 PHP_HTTP_STRLIST_ITEM("IM Used")
597 PHP_HTTP_STRLIST_NEXT
598 PHP_HTTP_STRLIST_ITEM("Multiple Choices")
599 PHP_HTTP_STRLIST_ITEM("Moved Permanently")
600 PHP_HTTP_STRLIST_ITEM("Found")
601 PHP_HTTP_STRLIST_ITEM("See Other")
602 PHP_HTTP_STRLIST_ITEM("Not Modified")
603 PHP_HTTP_STRLIST_ITEM("Use Proxy")
604 PHP_HTTP_STRLIST_ITEM("(Unused)")
605 PHP_HTTP_STRLIST_ITEM("Temporary Redirect")
606 PHP_HTTP_STRLIST_ITEM("Permanent Redirect")
607 PHP_HTTP_STRLIST_NEXT
608 PHP_HTTP_STRLIST_ITEM("Bad Request")
609 PHP_HTTP_STRLIST_ITEM("Unauthorized")
610 PHP_HTTP_STRLIST_ITEM("Payment Required")
611 PHP_HTTP_STRLIST_ITEM("Forbidden")
612 PHP_HTTP_STRLIST_ITEM("Not Found")
613 PHP_HTTP_STRLIST_ITEM("Method Not Allowed")
614 PHP_HTTP_STRLIST_ITEM("Not Acceptable")
615 PHP_HTTP_STRLIST_ITEM("Proxy Authentication Required")
616 PHP_HTTP_STRLIST_ITEM("Request Timeout")
617 PHP_HTTP_STRLIST_ITEM("Conflict")
618 PHP_HTTP_STRLIST_ITEM("Gone")
619 PHP_HTTP_STRLIST_ITEM("Length Required")
620 PHP_HTTP_STRLIST_ITEM("Precondition Failed")
621 PHP_HTTP_STRLIST_ITEM("Request Entity Too Large")
622 PHP_HTTP_STRLIST_ITEM("Request URI Too Long")
623 PHP_HTTP_STRLIST_ITEM("Unsupported Media Type")
624 PHP_HTTP_STRLIST_ITEM("Requested Range Not Satisfiable")
625 PHP_HTTP_STRLIST_ITEM("Expectation Failed")
626 PHP_HTTP_STRLIST_ITEM("(Unused)")
627 PHP_HTTP_STRLIST_ITEM("(Unused)")
628 PHP_HTTP_STRLIST_ITEM("(Unused)")
629 PHP_HTTP_STRLIST_ITEM("(Unused)")
630 PHP_HTTP_STRLIST_ITEM("Unprocessible Entity")
631 PHP_HTTP_STRLIST_ITEM("Locked")
632 PHP_HTTP_STRLIST_ITEM("Failed Dependency")
633 PHP_HTTP_STRLIST_ITEM("(Reserved)")
634 PHP_HTTP_STRLIST_ITEM("Upgrade Required")
635 PHP_HTTP_STRLIST_ITEM("(Unused)")
636 PHP_HTTP_STRLIST_ITEM("Precondition Required")
637 PHP_HTTP_STRLIST_ITEM("Too Many Requests")
638 PHP_HTTP_STRLIST_ITEM("(Unused)")
639 PHP_HTTP_STRLIST_ITEM("Request Header Fields Too Large")
640 PHP_HTTP_STRLIST_NEXT
641 PHP_HTTP_STRLIST_ITEM("Internal Server Error")
642 PHP_HTTP_STRLIST_ITEM("Not Implemented")
643 PHP_HTTP_STRLIST_ITEM("Bad Gateway")
644 PHP_HTTP_STRLIST_ITEM("Service Unavailable")
645 PHP_HTTP_STRLIST_ITEM("Gateway Timeout")
646 PHP_HTTP_STRLIST_ITEM("HTTP Version Not Supported")
647 PHP_HTTP_STRLIST_ITEM("Variant Also Negotiates")
648 PHP_HTTP_STRLIST_ITEM("Insufficient Storage")
649 PHP_HTTP_STRLIST_ITEM("Loop Detected")
650 PHP_HTTP_STRLIST_ITEM("(Unused)")
651 PHP_HTTP_STRLIST_ITEM("Not Extended")
652 PHP_HTTP_STRLIST_ITEM("Network Authentication Required")
653 PHP_HTTP_STRLIST_STOP
654 ;
655
656 const char *php_http_env_get_response_status_for_code(unsigned code)
657 {
658 return php_http_strlist_find(php_http_env_response_status, 100, code);
659 }
660
661 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getRequestHeader, 0, 0, 0)
662 ZEND_ARG_INFO(0, header_name)
663 ZEND_END_ARG_INFO();
664 static PHP_METHOD(HttpEnv, getRequestHeader)
665 {
666 char *header_name_str = NULL;
667 size_t header_name_len = 0;
668
669 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str, &header_name_len)) {
670 return;
671 }
672 if (header_name_str && header_name_len) {
673 size_t header_length;
674 char *header_value = php_http_env_get_request_header(header_name_str, header_name_len, &header_length, NULL);
675
676 if (header_value) {
677 RETURN_STR(php_http_cs2zs(header_value, header_length));
678 }
679 } else {
680 array_init(return_value);
681 php_http_env_get_request_headers(Z_ARRVAL_P(return_value));
682 }
683 }
684
685 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getRequestBody, 0, 0, 0)
686 ZEND_ARG_INFO(0, body_class_name)
687 ZEND_END_ARG_INFO();
688 static PHP_METHOD(HttpEnv, getRequestBody)
689 {
690 php_http_message_body_t *body;
691 php_http_message_body_object_t *body_obj;
692 zend_class_entry *class_entry = php_http_message_body_class_entry;
693
694 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &class_entry), invalid_arg, return);
695
696 body = php_http_env_get_request_body();
697 if (SUCCESS == php_http_new((void *) &body_obj, class_entry, (php_http_new_t) php_http_message_body_object_new_ex, php_http_message_body_class_entry, body)) {
698 php_http_message_body_addref(body);
699 RETVAL_OBJ(&body_obj->zo);
700 }
701 }
702
703 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseStatusForCode, 0, 0, 1)
704 ZEND_ARG_INFO(0, code)
705 ZEND_END_ARG_INFO();
706 static PHP_METHOD(HttpEnv, getResponseStatusForCode)
707 {
708 zend_long code;
709
710 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
711 return;
712 }
713 RETURN_STRING(php_http_env_get_response_status_for_code(code));
714 }
715
716 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseStatusForAllCodes, 0, 0, 0)
717 ZEND_END_ARG_INFO();
718 static PHP_METHOD(HttpEnv, getResponseStatusForAllCodes)
719 {
720 const char *s;
721 unsigned c;
722 php_http_strlist_iterator_t i;
723
724 if (SUCCESS != zend_parse_parameters_none()) {
725 return;
726 }
727
728 array_init(return_value);
729 for ( php_http_strlist_iterator_init(&i, php_http_env_response_status, 100);
730 *(s = php_http_strlist_iterator_this(&i, &c));
731 php_http_strlist_iterator_next(&i)
732 ) {
733 add_index_string(return_value, c, s);
734 }
735 }
736
737 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseHeader, 0, 0, 0)
738 ZEND_ARG_INFO(0, header_name)
739 ZEND_END_ARG_INFO();
740 static PHP_METHOD(HttpEnv, getResponseHeader)
741 {
742 char *header_name_str = NULL;
743 size_t header_name_len = 0;
744
745 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str, &header_name_len)) {
746 return;
747 }
748 if (header_name_str && header_name_len) {
749 char *header_value = php_http_env_get_response_header(header_name_str, header_name_len);
750
751 if (header_value) {
752 RETURN_STR(php_http_cs2zs(header_value, strlen(header_value)));
753 }
754 } else {
755 array_init(return_value);
756 php_http_env_get_response_headers(Z_ARRVAL_P(return_value));
757 }
758 }
759
760 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseCode, 0, 0, 0)
761 ZEND_END_ARG_INFO();
762 static PHP_METHOD(HttpEnv, getResponseCode)
763 {
764 if (SUCCESS != zend_parse_parameters_none()) {
765 return;
766 }
767 RETURN_LONG(php_http_env_get_response_code());
768 }
769
770 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_setResponseHeader, 0, 0, 1)
771 ZEND_ARG_INFO(0, header_name)
772 ZEND_ARG_INFO(0, header_value)
773 ZEND_ARG_INFO(0, response_code)
774 ZEND_ARG_INFO(0, replace_header)
775 ZEND_END_ARG_INFO();
776 static PHP_METHOD(HttpEnv, setResponseHeader)
777 {
778 char *header_name_str;
779 int header_name_len;
780 zval *header_value = NULL;
781 long code = 0;
782 zend_bool replace_header = 1;
783
784 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!lb", &header_name_str, &header_name_len, &header_value, &code, &replace_header)) {
785 return;
786 }
787 RETURN_BOOL(SUCCESS == php_http_env_set_response_header_value(code, header_name_str, header_name_len, header_value, replace_header TSRMLS_CC));
788 }
789
790 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_setResponseCode, 0, 0, 1)
791 ZEND_ARG_INFO(0, code)
792 ZEND_END_ARG_INFO();
793 static PHP_METHOD(HttpEnv, setResponseCode)
794 {
795 zend_long code;
796
797 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code)) {
798 return;
799 }
800 RETURN_BOOL(SUCCESS == php_http_env_set_response_code(code));
801 }
802
803 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateLanguage, 0, 0, 1)
804 ZEND_ARG_INFO(0, supported)
805 ZEND_ARG_INFO(1, result_array)
806 ZEND_END_ARG_INFO();
807 static PHP_METHOD(HttpEnv, negotiateLanguage)
808 {
809 HashTable *supported;
810 zval *rs_array = NULL;
811
812 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
813 return;
814 }
815 if (rs_array) {
816 zval_dtor(rs_array);
817 array_init(rs_array);
818 }
819
820 PHP_HTTP_DO_NEGOTIATE(language, supported, rs_array);
821 }
822
823 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateCharset, 0, 0, 1)
824 ZEND_ARG_INFO(0, supported)
825 ZEND_ARG_INFO(1, result_array)
826 ZEND_END_ARG_INFO();
827 static PHP_METHOD(HttpEnv, negotiateCharset)
828 {
829 HashTable *supported;
830 zval *rs_array = NULL;
831
832 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported, &rs_array)) {
833 return;
834 }
835 if (rs_array) {
836 zval_dtor(rs_array);
837 array_init(rs_array);
838 }
839 PHP_HTTP_DO_NEGOTIATE(charset, supported, rs_array);
840 }
841
842 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateEncoding, 0, 0, 1)
843 ZEND_ARG_INFO(0, supported)
844 ZEND_ARG_INFO(1, result_array)
845 ZEND_END_ARG_INFO();
846 static PHP_METHOD(HttpEnv, negotiateEncoding)
847 {
848 HashTable *supported;
849 zval *rs_array = NULL;
850
851 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported, &rs_array)) {
852 return;
853 }
854 if (rs_array) {
855 zval_dtor(rs_array);
856 array_init(rs_array);
857 }
858 PHP_HTTP_DO_NEGOTIATE(encoding, supported, rs_array);
859 }
860
861 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateContentType, 0, 0, 1)
862 ZEND_ARG_INFO(0, supported)
863 ZEND_ARG_INFO(1, result_array)
864 ZEND_END_ARG_INFO();
865 static PHP_METHOD(HttpEnv, negotiateContentType)
866 {
867 HashTable *supported;
868 zval *rs_array = NULL;
869
870 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported, &rs_array)) {
871 return;
872 }
873 if (rs_array) {
874 zval_dtor(rs_array);
875 array_init(rs_array);
876 }
877 PHP_HTTP_DO_NEGOTIATE(content_type, supported, rs_array);
878 }
879
880 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiate, 0, 0, 2)
881 ZEND_ARG_INFO(0, params)
882 ZEND_ARG_INFO(0, supported)
883 ZEND_ARG_INFO(0, primary_type_separator)
884 ZEND_ARG_INFO(1, result_array)
885 ZEND_END_ARG_INFO();
886 static PHP_METHOD(HttpEnv, negotiate)
887 {
888 HashTable *supported, *rs;
889 zval *rs_array = NULL;
890 char *value_str, *sep_str = NULL;
891 size_t value_len, sep_len = 0;
892
893 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sH|s!z", &value_str, &value_len, &supported, &sep_str, &sep_len, &rs_array)) {
894 return;
895 }
896
897
898 if (rs_array) {
899 zval_dtor(rs_array);
900 array_init(rs_array);
901 }
902
903 if ((rs = php_http_negotiate(value_str, value_len, supported, sep_str, sep_len))) {
904 PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs, supported, rs_array);
905 } else {
906 PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array);
907 }
908 }
909
910 static zend_function_entry php_http_env_methods[] = {
911 PHP_ME(HttpEnv, getRequestHeader, ai_HttpEnv_getRequestHeader, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
912 PHP_ME(HttpEnv, getRequestBody, ai_HttpEnv_getRequestBody, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
913
914 PHP_ME(HttpEnv, getResponseStatusForCode, ai_HttpEnv_getResponseStatusForCode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
915 PHP_ME(HttpEnv, getResponseStatusForAllCodes, ai_HttpEnv_getResponseStatusForAllCodes, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
916
917 PHP_ME(HttpEnv, getResponseHeader, ai_HttpEnv_getResponseHeader, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
918 PHP_ME(HttpEnv, getResponseCode, ai_HttpEnv_getResponseCode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
919 PHP_ME(HttpEnv, setResponseHeader, ai_HttpEnv_setResponseHeader, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
920 PHP_ME(HttpEnv, setResponseCode, ai_HttpEnv_setResponseCode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
921
922 PHP_ME(HttpEnv, negotiateLanguage, ai_HttpEnv_negotiateLanguage, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
923 PHP_ME(HttpEnv, negotiateContentType, ai_HttpEnv_negotiateContentType, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
924 PHP_ME(HttpEnv, negotiateEncoding, ai_HttpEnv_negotiateEncoding, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
925 PHP_ME(HttpEnv, negotiateCharset, ai_HttpEnv_negotiateCharset, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
926 PHP_ME(HttpEnv, negotiate, ai_HttpEnv_negotiate, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
927
928 EMPTY_FUNCTION_ENTRY
929 };
930
931 #ifdef PHP_HTTP_HAVE_JSON
932 #include "ext/json/php_json.h"
933
934 static SAPI_POST_HANDLER_FUNC(php_http_json_post_handler)
935 {
936 zval *zarg = arg;
937 zend_string *json = NULL;
938
939 if (SG(request_info).request_body) {
940 /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
941 php_stream_rewind(SG(request_info).request_body);
942 json = php_stream_copy_to_mem(SG(request_info).request_body, PHP_STREAM_COPY_ALL, 0);
943 }
944
945 if (json) {
946 if (json->len) {
947 zval zjson;
948
949 ZVAL_NULL(&zjson);
950 php_json_decode(&zjson, json->val, json->len, 1, PG(max_input_nesting_level));
951 if (Z_TYPE(zjson) != IS_NULL) {
952 zval_dtor(zarg);
953 ZVAL_COPY_VALUE(zarg, (&zjson));
954 }
955 }
956 zend_string_release(json);
957 }
958 }
959
960 static void php_http_env_register_json_handler(void)
961 {
962 sapi_post_entry entry = {NULL, 0, NULL, NULL};
963
964 entry.post_reader = sapi_read_standard_form_data;
965 entry.post_handler = php_http_json_post_handler;
966
967 entry.content_type = "text/json";
968 entry.content_type_len = lenof("text/json");
969 sapi_register_post_entry(&entry);
970
971 entry.content_type = "application/json";
972 entry.content_type_len = lenof("application/json");
973 sapi_register_post_entry(&entry);
974 }
975 #endif
976
977 zend_class_entry *php_http_env_class_entry;
978
979 PHP_MINIT_FUNCTION(http_env)
980 {
981 zend_class_entry ce = {0};
982
983 INIT_NS_CLASS_ENTRY(ce, "http", "Env", php_http_env_methods);
984 php_http_env_class_entry = zend_register_internal_class(&ce);
985
986 #ifdef PHP_HTTP_HAVE_JSON
987 php_http_env_register_json_handler();
988 #endif
989
990 return SUCCESS;
991 }
992
993
994 /*
995 * Local variables:
996 * tab-width: 4
997 * c-basic-offset: 4
998 * End:
999 * vim600: noet sw=4 ts=4 fdm=marker
1000 * vim<600: noet sw=4 ts=4
1001 */