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