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