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