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