etag test & fixes; set default etag mode for temp streams to crc32(b)
[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
15 PHP_RINIT_FUNCTION(http_env)
16 {
17 PHP_HTTP_G->env.request.time = sapi_get_request_time(TSRMLS_C);
18
19 return SUCCESS;
20 }
21
22 PHP_RSHUTDOWN_FUNCTION(http_env)
23 {
24 if (PHP_HTTP_G->env.request.headers) {
25 zend_hash_destroy(PHP_HTTP_G->env.request.headers);
26 FREE_HASHTABLE(PHP_HTTP_G->env.request.headers);
27 PHP_HTTP_G->env.request.headers = NULL;
28 }
29 if (PHP_HTTP_G->env.request.body) {
30 php_http_message_body_free(&PHP_HTTP_G->env.request.body);
31 }
32
33 if (PHP_HTTP_G->env.server_var) {
34 zval_ptr_dtor(&PHP_HTTP_G->env.server_var);
35 PHP_HTTP_G->env.server_var = NULL;
36 }
37
38 return SUCCESS;
39 }
40
41 PHP_HTTP_API void php_http_env_get_request_headers(HashTable *headers TSRMLS_DC)
42 {
43 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
44 zval **hsv, **header;
45 HashPosition pos;
46
47 if (!PHP_HTTP_G->env.request.headers) {
48 ALLOC_HASHTABLE(PHP_HTTP_G->env.request.headers);
49 zend_hash_init(PHP_HTTP_G->env.request.headers, 0, NULL, ZVAL_PTR_DTOR, 0);
50
51 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
52
53 if (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv) && Z_TYPE_PP(hsv) == IS_ARRAY) {
54 FOREACH_KEY(pos, *hsv, key) {
55 if (key.type == HASH_KEY_IS_STRING && key.len > 6 && !strncmp(key.str, "HTTP_", 5)) {
56 key.len -= 5;
57 key.str = php_http_pretty_key(estrndup(key.str + 5, key.len - 1), key.len - 1, 1, 1);
58
59 zend_hash_get_current_data_ex(Z_ARRVAL_PP(hsv), (void *) &header, &pos);
60 Z_ADDREF_P(*header);
61 zend_symtable_update(PHP_HTTP_G->env.request.headers, key.str, key.len, (void *) header, sizeof(zval *), NULL);
62
63 efree(key.str);
64 }
65 }
66 }
67 }
68
69 if (headers) {
70 zend_hash_copy(headers, PHP_HTTP_G->env.request.headers, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
71 }
72 }
73
74 PHP_HTTP_API char *php_http_env_get_request_header(const char *name_str, size_t name_len, size_t *len TSRMLS_DC)
75 {
76 zval **zvalue;
77 char *val = NULL, *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
78
79 php_http_env_get_request_headers(NULL TSRMLS_CC);
80
81 if (SUCCESS == zend_symtable_find(PHP_HTTP_G->env.request.headers, key, name_len + 1, (void *) &zvalue)) {
82 zval *zcopy = php_http_ztyp(IS_STRING, *zvalue);
83
84 val = estrndup(Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy));
85 if (len) {
86 *len = Z_STRLEN_P(zcopy);
87 }
88 zval_ptr_dtor(&zcopy);
89 }
90
91 efree(key);
92
93 return val;
94 }
95
96 PHP_HTTP_API int php_http_env_got_request_header(const char *name_str, size_t name_len TSRMLS_DC)
97 {
98 char *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
99 int got;
100
101 php_http_env_get_request_headers(NULL TSRMLS_CC);
102 got = zend_symtable_exists(PHP_HTTP_G->env.request.headers, key, name_len + 1);
103 efree(key);
104
105 return got;
106 }
107
108 PHP_HTTP_API zval *php_http_env_get_server_var(const char *key, size_t key_len, zend_bool check TSRMLS_DC)
109 {
110 zval **hsv, **var;
111 char *env;
112
113 /* if available, this is a lot faster than accessing $_SERVER */
114 if (sapi_module.getenv) {
115 if ((!(env = sapi_module.getenv((char *) key, key_len TSRMLS_CC))) || (check && !*env)) {
116 return NULL;
117 }
118 if (PHP_HTTP_G->env.server_var) {
119 zval_ptr_dtor(&PHP_HTTP_G->env.server_var);
120 }
121 MAKE_STD_ZVAL(PHP_HTTP_G->env.server_var);
122 ZVAL_STRING(PHP_HTTP_G->env.server_var, env, 1);
123 return PHP_HTTP_G->env.server_var;
124 }
125
126 zend_is_auto_global(ZEND_STRL("_SERVER") TSRMLS_CC);
127
128 if ((SUCCESS != zend_hash_find(&EG(symbol_table), ZEND_STRS("_SERVER"), (void *) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
129 return NULL;
130 }
131 if ((SUCCESS != zend_symtable_find(Z_ARRVAL_PP(hsv), key, key_len + 1, (void *) &var))) {
132 return NULL;
133 }
134 if (check && !((Z_TYPE_PP(var) == IS_STRING) && Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
135 return NULL;
136 }
137 return *var;
138 }
139
140 PHP_HTTP_API php_http_message_body_t *php_http_env_get_request_body(TSRMLS_D)
141 {
142 if (!PHP_HTTP_G->env.request.body) {
143 php_stream *s = NULL;
144
145 if (SG(request_info).post_data || SG(request_info).raw_post_data) {
146 if ((s = php_stream_temp_new())) {
147 /* php://input does not support seek() */
148 if (SG(request_info).raw_post_data) {
149 php_stream_write(s, SG(request_info).raw_post_data, SG(request_info).raw_post_data_length);
150 } else {
151 php_stream_write(s, SG(request_info).post_data, SG(request_info).post_data_length);
152 }
153 php_stream_rewind(s);
154 }
155 } else if (sapi_module.read_post && !SG(read_post_bytes)) {
156 if ((s = php_stream_temp_new())) {
157 char *buf = emalloc(4096);
158 int len;
159
160 while (0 < (len = sapi_module.read_post(buf, 4096 TSRMLS_CC))) {
161 SG(read_post_bytes) += len;
162 php_stream_write(s, buf, len);
163
164 if (len < 4096) {
165 break;
166 }
167 }
168 efree(buf);
169
170 php_stream_rewind(s);
171 }
172 }
173 PHP_HTTP_G->env.request.body = php_http_message_body_init(NULL, s TSRMLS_CC);
174 }
175
176 return PHP_HTTP_G->env.request.body;
177 }
178
179 PHP_HTTP_API php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_t length TSRMLS_DC)
180 {
181 zval *zentry;
182 char *range, *rp, c;
183 long begin = -1, end = -1, *ptr;
184
185 if (!(range = php_http_env_get_request_header(ZEND_STRL("Range"), NULL TSRMLS_CC))) {
186 return PHP_HTTP_RANGE_NO;
187 }
188 if (strncmp(range, "bytes=", lenof("bytes="))) {
189 STR_FREE(range);
190 return PHP_HTTP_RANGE_NO;
191 }
192
193 rp = range + lenof("bytes=");
194 ptr = &begin;
195
196 do {
197 switch (c = *(rp++)) {
198 case '0':
199 /* allow 000... - shall we? */
200 if (*ptr != -10) {
201 *ptr *= 10;
202 }
203 break;
204
205 case '1': case '2': case '3':
206 case '4': case '5': case '6':
207 case '7': case '8': case '9':
208 /*
209 * If the value of the pointer is already set (non-negative)
210 * then multiply its value by ten and add the current value,
211 * else initialise the pointers value with the current value
212 * --
213 * This let us recognize empty fields when validating the
214 * ranges, i.e. a "-10" for begin and "12345" for the end
215 * was the following range request: "Range: bytes=0-12345";
216 * While a "-1" for begin and "12345" for the end would
217 * have been: "Range: bytes=-12345".
218 */
219 if (*ptr > 0) {
220 *ptr *= 10;
221 *ptr += c - '0';
222 } else {
223 *ptr = c - '0';
224 }
225 break;
226
227 case '-':
228 ptr = &end;
229 break;
230
231 case ' ':
232 break;
233
234 case 0:
235 case ',':
236
237 if (length) {
238 /* validate ranges */
239 switch (begin) {
240 /* "0-12345" */
241 case -10:
242 switch (end) {
243 /* "0-" */
244 case -1:
245 STR_FREE(range);
246 return PHP_HTTP_RANGE_NO;
247
248 /* "0-0" */
249 case -10:
250 end = 0;
251 break;
252
253 default:
254 if (length <= (size_t) end) {
255 end = length - 1;
256 }
257 break;
258 }
259 begin = 0;
260 break;
261
262 /* "-12345" */
263 case -1:
264 /* "-", "-0" */
265 if (end == -1 || end == -10) {
266 STR_FREE(range);
267 return PHP_HTTP_RANGE_ERR;
268 }
269 begin = length - end;
270 end = length - 1;
271 break;
272
273 /* "12345-(NNN)" */
274 default:
275 if (length <= (size_t) begin) {
276 STR_FREE(range);
277 return PHP_HTTP_RANGE_ERR;
278 }
279 switch (end) {
280 /* "12345-0" */
281 case -10:
282 STR_FREE(range);
283 return PHP_HTTP_RANGE_ERR;
284
285 /* "12345-" */
286 case -1:
287 end = length - 1;
288 break;
289
290 /* "12345-67890" */
291 default:
292 if (length <= (size_t) end) {
293 end = length - 1;
294 } else if (end < begin) {
295 STR_FREE(range);
296 return PHP_HTTP_RANGE_ERR;
297 }
298 break;
299 }
300 break;
301 }
302 }
303
304 MAKE_STD_ZVAL(zentry);
305 array_init(zentry);
306 add_index_long(zentry, 0, begin);
307 add_index_long(zentry, 1, end);
308 zend_hash_next_index_insert(ranges, &zentry, sizeof(zval *), NULL);
309
310 begin = -1;
311 end = -1;
312 ptr = &begin;
313
314 break;
315
316 default:
317 STR_FREE(range);
318 return PHP_HTTP_RANGE_NO;
319 }
320 } while (c != 0);
321
322 STR_FREE(range);
323 return PHP_HTTP_RANGE_OK;
324 }
325
326 static void grab_headers(void *data, void *arg TSRMLS_DC)
327 {
328 php_http_buffer_appendl(PHP_HTTP_BUFFER(arg), ((sapi_header_struct *)data)->header);
329 php_http_buffer_appends(PHP_HTTP_BUFFER(arg), PHP_HTTP_CRLF);
330 }
331
332 PHP_HTTP_API STATUS php_http_env_get_response_headers(HashTable *headers_ht TSRMLS_DC)
333 {
334 STATUS status;
335 php_http_buffer_t headers;
336
337 php_http_buffer_init(&headers);
338 zend_llist_apply_with_argument(&SG(sapi_headers).headers, grab_headers, &headers TSRMLS_CC);
339 php_http_buffer_fix(&headers);
340
341 status = php_http_headers_parse(PHP_HTTP_BUFFER_VAL(&headers), PHP_HTTP_BUFFER_LEN(&headers), headers_ht, NULL, NULL TSRMLS_CC);
342 php_http_buffer_dtor(&headers);
343
344 return status;
345 }
346
347 PHP_HTTP_API char *php_http_env_get_response_header(const char *name_str, size_t name_len TSRMLS_DC)
348 {
349 char *val = NULL;
350 HashTable headers;
351
352 zend_hash_init(&headers, 0, NULL, NULL, 0);
353 if (SUCCESS == php_http_env_get_response_headers(&headers TSRMLS_CC)) {
354 zval **zvalue;
355 char *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
356
357 if (SUCCESS == zend_symtable_find(&headers, key, name_len + 1, (void *) &zvalue)) {
358 zval *zcopy = php_http_ztyp(IS_STRING, *zvalue);
359
360 val = estrndup(Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy));
361 zval_ptr_dtor(&zcopy);
362 }
363
364 efree(key);
365 }
366 zend_hash_destroy(&headers);
367
368 return val;
369 }
370
371 PHP_HTTP_API long php_http_env_get_response_code(TSRMLS_D)
372 {
373 long code = SG(sapi_headers).http_response_code;
374 return code ? code : 200;
375 }
376
377 PHP_HTTP_API STATUS php_http_env_set_response_code(long http_code TSRMLS_DC)
378 {
379 return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) http_code TSRMLS_CC);
380 }
381
382 PHP_HTTP_API STATUS php_http_env_set_response_status_line(long code, php_http_version_t *v TSRMLS_DC)
383 {
384 sapi_header_line h = {0};
385 STATUS ret;
386
387 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));
388 ret = sapi_header_op(SAPI_HEADER_REPLACE, (void *) &h TSRMLS_CC);
389 efree(h.line);
390
391 return ret;
392 }
393
394 PHP_HTTP_API STATUS php_http_env_set_response_protocol_version(php_http_version_t *v TSRMLS_DC)
395 {
396 return php_http_env_set_response_status_line(php_http_env_get_response_code(TSRMLS_C), v TSRMLS_CC);
397 }
398
399 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)
400 {
401 sapi_header_line h = {estrndup(header_str, header_len), header_len, http_code};
402 STATUS ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC);
403 efree(h.line);
404 return ret;
405 }
406
407 PHP_HTTP_API STATUS php_http_env_set_response_header_format(long http_code, zend_bool replace TSRMLS_DC, const char *fmt, ...)
408 {
409 va_list args;
410 STATUS ret = FAILURE;
411 sapi_header_line h = {NULL, 0, http_code};
412
413 va_start(args, fmt);
414 h.line_len = vspprintf(&h.line, 0, fmt, args);
415 va_end(args);
416
417 if (h.line) {
418 if (h.line_len) {
419 ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC);
420 }
421 efree(h.line);
422 }
423 return ret;
424 }
425
426 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)
427 {
428 if (!value) {
429 sapi_header_line h = {(char *) name_str, name_len, http_code};
430
431 return sapi_header_op(SAPI_HEADER_DELETE, (void *) &h TSRMLS_CC);
432 }
433
434 if(Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
435 HashPosition pos;
436 int first = replace;
437 zval **data_ptr;
438
439 FOREACH_HASH_VAL(pos, HASH_OF(value), data_ptr) {
440 if (SUCCESS != php_http_env_set_response_header_value(http_code, name_str, name_len, *data_ptr, first TSRMLS_CC)) {
441 return FAILURE;
442 }
443 first = 0;
444 }
445
446 return SUCCESS;
447 } else {
448 zval *data = php_http_ztyp(IS_STRING, value);
449
450 if (!Z_STRLEN_P(data)) {
451 zval_ptr_dtor(&data);
452 return php_http_env_set_response_header_value(http_code, name_str, name_len, NULL, replace TSRMLS_CC);
453 } else {
454 sapi_header_line h;
455 STATUS ret;
456
457 if (name_len > INT_MAX) {
458 name_len = INT_MAX;
459 }
460 h.response_code = http_code;
461 h.line_len = spprintf(&h.line, 0, "%.*s: %.*s", (int) name_len, name_str, Z_STRLEN_P(data), Z_STRVAL_P(data));
462
463 ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC);
464
465 zval_ptr_dtor(&data);
466 STR_FREE(h.line);
467
468 return ret;
469 }
470 }
471 }
472
473
474 static PHP_HTTP_STRLIST(php_http_env_response_status) =
475 PHP_HTTP_STRLIST_ITEM("Continue")
476 PHP_HTTP_STRLIST_ITEM("Switching Protocols")
477 PHP_HTTP_STRLIST_NEXT
478 PHP_HTTP_STRLIST_ITEM("OK")
479 PHP_HTTP_STRLIST_ITEM("Created")
480 PHP_HTTP_STRLIST_ITEM("Accepted")
481 PHP_HTTP_STRLIST_ITEM("Non-Authoritative Information")
482 PHP_HTTP_STRLIST_ITEM("No Content")
483 PHP_HTTP_STRLIST_ITEM("Reset Content")
484 PHP_HTTP_STRLIST_ITEM("Partial Content")
485 PHP_HTTP_STRLIST_NEXT
486 PHP_HTTP_STRLIST_ITEM("Multiple Choices")
487 PHP_HTTP_STRLIST_ITEM("Moved Permanently")
488 PHP_HTTP_STRLIST_ITEM("Found")
489 PHP_HTTP_STRLIST_ITEM("See Other")
490 PHP_HTTP_STRLIST_ITEM("Not Modified")
491 PHP_HTTP_STRLIST_ITEM("Use Proxy")
492 PHP_HTTP_STRLIST_ITEM("(Unused)")
493 PHP_HTTP_STRLIST_ITEM("Temporary Redirect")
494 PHP_HTTP_STRLIST_NEXT
495 PHP_HTTP_STRLIST_ITEM("Bad Request")
496 PHP_HTTP_STRLIST_ITEM("Unauthorized")
497 PHP_HTTP_STRLIST_ITEM("Payment Required")
498 PHP_HTTP_STRLIST_ITEM("Forbidden")
499 PHP_HTTP_STRLIST_ITEM("Not Found")
500 PHP_HTTP_STRLIST_ITEM("Method Not Allowed")
501 PHP_HTTP_STRLIST_ITEM("Not Acceptable")
502 PHP_HTTP_STRLIST_ITEM("Proxy Authentication Required")
503 PHP_HTTP_STRLIST_ITEM("Request Timeout")
504 PHP_HTTP_STRLIST_ITEM("Conflict")
505 PHP_HTTP_STRLIST_ITEM("Gone")
506 PHP_HTTP_STRLIST_ITEM("Length Required")
507 PHP_HTTP_STRLIST_ITEM("Precondition Failed")
508 PHP_HTTP_STRLIST_ITEM("Request Entity Too Large")
509 PHP_HTTP_STRLIST_ITEM("Request URI Too Long")
510 PHP_HTTP_STRLIST_ITEM("Unsupported Media Type")
511 PHP_HTTP_STRLIST_ITEM("Requested Range Not Satisfiable")
512 PHP_HTTP_STRLIST_ITEM("Expectation Failed")
513 PHP_HTTP_STRLIST_NEXT
514 PHP_HTTP_STRLIST_ITEM("Internal Server Error")
515 PHP_HTTP_STRLIST_ITEM("Not Implemented")
516 PHP_HTTP_STRLIST_ITEM("Bad Gateway")
517 PHP_HTTP_STRLIST_ITEM("Service Unavailable")
518 PHP_HTTP_STRLIST_ITEM("Gateway Timeout")
519 PHP_HTTP_STRLIST_ITEM("HTTP Version Not Supported")
520 PHP_HTTP_STRLIST_STOP
521 ;
522
523 PHP_HTTP_API const char *php_http_env_get_response_status_for_code(unsigned code)
524 {
525 return php_http_strlist_find(php_http_env_response_status, 100, code);
526 }
527
528 zend_class_entry *php_http_env_class_entry;
529
530 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpEnv, method, 0, req_args)
531 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpEnv, method, 0)
532 #define PHP_HTTP_ENV_ME(method) PHP_ME(HttpEnv, method, PHP_HTTP_ARGS(HttpEnv, method), ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
533
534 PHP_HTTP_BEGIN_ARGS(getRequestHeader, 0)
535 PHP_HTTP_ARG_VAL(header_name, 0)
536 PHP_HTTP_END_ARGS;
537
538 PHP_HTTP_BEGIN_ARGS(getRequestBody, 0)
539 PHP_HTTP_ARG_VAL(body_class_name, 0)
540 PHP_HTTP_END_ARGS;
541
542 PHP_HTTP_BEGIN_ARGS(getResponseStatusForCode, 1)
543 PHP_HTTP_ARG_VAL(code, 0)
544 PHP_HTTP_END_ARGS;
545
546 PHP_HTTP_BEGIN_ARGS(getResponseHeader, 0)
547 PHP_HTTP_ARG_VAL(header_name, 0)
548 PHP_HTTP_END_ARGS;
549
550 PHP_HTTP_EMPTY_ARGS(getResponseCode);
551
552 PHP_HTTP_BEGIN_ARGS(setResponseHeader, 1)
553 PHP_HTTP_ARG_VAL(header_name, 0)
554 PHP_HTTP_ARG_VAL(header_value, 0)
555 PHP_HTTP_ARG_VAL(response_code, 0)
556 PHP_HTTP_ARG_VAL(replace_header, 0)
557 PHP_HTTP_END_ARGS;
558
559 PHP_HTTP_BEGIN_ARGS(setResponseCode, 1)
560 PHP_HTTP_ARG_VAL(code, 0)
561 PHP_HTTP_END_ARGS;
562
563 PHP_HTTP_BEGIN_ARGS(negotiateLanguage, 1)
564 PHP_HTTP_ARG_VAL(supported, 0)
565 PHP_HTTP_ARG_VAL(result_array, 1)
566 PHP_HTTP_END_ARGS;
567
568 PHP_HTTP_BEGIN_ARGS(negotiateContentType, 1)
569 PHP_HTTP_ARG_VAL(supported, 0)
570 PHP_HTTP_ARG_VAL(result_array, 1)
571 PHP_HTTP_END_ARGS;
572
573 PHP_HTTP_BEGIN_ARGS(negotiateCharset, 1)
574 PHP_HTTP_ARG_VAL(supported, 0)
575 PHP_HTTP_ARG_VAL(result_array, 1)
576 PHP_HTTP_END_ARGS;
577
578 PHP_HTTP_BEGIN_ARGS(negotiateEncoding, 1)
579 PHP_HTTP_ARG_VAL(supported, 0)
580 PHP_HTTP_ARG_VAL(result_array, 1)
581 PHP_HTTP_END_ARGS;
582
583 PHP_HTTP_BEGIN_ARGS(negotiate, 2)
584 PHP_HTTP_ARG_VAL(value, 0)
585 PHP_HTTP_ARG_VAL(supported, 0)
586 PHP_HTTP_ARG_VAL(primary_type_separator, 0)
587 PHP_HTTP_ARG_VAL(result_array, 1)
588 PHP_HTTP_END_ARGS;
589
590 PHP_HTTP_EMPTY_ARGS(persistentHandlesStat);
591
592 PHP_HTTP_BEGIN_ARGS(persistentHandlesClean, 0)
593 PHP_HTTP_ARG_VAL(name, 0)
594 PHP_HTTP_ARG_VAL(ident, 0)
595 PHP_HTTP_END_ARGS;
596
597 zend_function_entry php_http_env_method_entry[] = {
598 PHP_HTTP_ENV_ME(getRequestHeader)
599 PHP_HTTP_ENV_ME(getRequestBody)
600
601 PHP_HTTP_ENV_ME(getResponseStatusForCode)
602
603 PHP_HTTP_ENV_ME(getResponseHeader)
604 PHP_HTTP_ENV_ME(getResponseCode)
605 PHP_HTTP_ENV_ME(setResponseHeader)
606 PHP_HTTP_ENV_ME(setResponseCode)
607
608 PHP_HTTP_ENV_ME(negotiateLanguage)
609 PHP_HTTP_ENV_ME(negotiateContentType)
610 PHP_HTTP_ENV_ME(negotiateEncoding)
611 PHP_HTTP_ENV_ME(negotiateCharset)
612 PHP_HTTP_ENV_ME(negotiate)
613
614 PHP_HTTP_ENV_ME(persistentHandlesStat)
615 PHP_HTTP_ENV_ME(persistentHandlesClean)
616
617 EMPTY_FUNCTION_ENTRY
618 };
619
620 PHP_METHOD(HttpEnv, getRequestHeader)
621 {
622 char *header_name_str;
623 int header_name_len;
624
625 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
626 if (header_name_str && header_name_len) {
627 size_t header_length;
628 char *header_value = php_http_env_get_request_header(header_name_str, header_name_len, &header_length TSRMLS_CC);
629
630 if (header_value) {
631 RETURN_STRINGL(header_value, header_length, 0);
632 }
633 RETURN_NULL();
634 } else {
635 array_init(return_value);
636 php_http_env_get_request_headers(Z_ARRVAL_P(return_value) TSRMLS_CC);
637 return;
638 }
639 }
640 RETURN_FALSE;
641 }
642
643 PHP_METHOD(HttpEnv, getRequestBody)
644 {
645 with_error_handling(EH_THROW, php_http_exception_class_entry) {
646 zend_class_entry *class_entry = php_http_message_body_class_entry;
647
648 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|C", &class_entry)) {
649 zend_object_value ov;
650 php_http_message_body_t *body = php_http_env_get_request_body(TSRMLS_C);
651
652 if (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_message_body_object_new_ex, php_http_message_body_class_entry, body, NULL TSRMLS_CC)) {
653 RETURN_OBJVAL(ov, 0);
654 }
655 }
656 } end_error_handling();
657 }
658
659 PHP_METHOD(HttpEnv, getResponseStatusForCode)
660 {
661 long code;
662
663 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
664 RETURN_STRING(php_http_env_get_response_status_for_code(code), 1);
665 }
666 RETURN_FALSE;
667 }
668
669 PHP_METHOD(HttpEnv, getResponseHeader)
670 {
671 char *header_name_str = NULL;
672 int header_name_len = 0;
673
674 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
675 if (header_name_str && header_name_len) {
676 char *header_value = php_http_env_get_response_header(header_name_str, header_name_len TSRMLS_CC);
677
678 if (header_value) {
679 RETURN_STRING(header_value, 0);
680 }
681 RETURN_NULL();
682 } else {
683 array_init(return_value);
684 php_http_env_get_response_headers(Z_ARRVAL_P(return_value) TSRMLS_CC);
685 return;
686 }
687 }
688 RETURN_FALSE;
689 }
690
691 PHP_METHOD(HttpEnv, getResponseCode)
692 {
693 if (SUCCESS == zend_parse_parameters_none()) {
694 RETURN_LONG(php_http_env_get_response_code(TSRMLS_C));
695 }
696 RETURN_FALSE;
697 }
698
699 PHP_METHOD(HttpEnv, setResponseHeader)
700 {
701 char *header_name_str;
702 int header_name_len;
703 zval *header_value = NULL;
704 long code = 0;
705 zend_bool replace_header = 1;
706
707 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!lb", &header_name_str, &header_name_len, &header_value, &code, &replace_header)) {
708 RETURN_SUCCESS(php_http_env_set_response_header_value(code, header_name_str, header_name_len, header_value, replace_header TSRMLS_CC));
709 }
710 RETURN_FALSE;
711 }
712
713 PHP_METHOD(HttpEnv, setResponseCode)
714 {
715 long code;
716
717 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
718 RETURN_SUCCESS(php_http_env_set_response_code(code TSRMLS_CC));
719 }
720 RETURN_FALSE;
721 }
722
723 PHP_METHOD(HttpEnv, negotiateLanguage)
724 {
725 HashTable *supported;
726 zval *rs_array = NULL;
727
728 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
729 if (rs_array) {
730 zval_dtor(rs_array);
731 array_init(rs_array);
732 }
733
734 PHP_HTTP_DO_NEGOTIATE(language, supported, rs_array);
735 } else {
736 RETURN_FALSE;
737 }
738 }
739
740 PHP_METHOD(HttpEnv, negotiateCharset)
741 {
742 HashTable *supported;
743 zval *rs_array = NULL;
744
745 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
746 if (rs_array) {
747 zval_dtor(rs_array);
748 array_init(rs_array);
749 }
750 PHP_HTTP_DO_NEGOTIATE(charset, supported, rs_array);
751 } else {
752 RETURN_FALSE;
753 }
754 }
755
756 PHP_METHOD(HttpEnv, negotiateEncoding)
757 {
758 HashTable *supported;
759 zval *rs_array = NULL;
760
761 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
762 if (rs_array) {
763 zval_dtor(rs_array);
764 array_init(rs_array);
765 }
766 PHP_HTTP_DO_NEGOTIATE(encoding, supported, rs_array);
767 } else {
768 RETURN_FALSE;
769 }
770 }
771
772 PHP_METHOD(HttpEnv, negotiateContentType)
773 {
774 HashTable *supported;
775 zval *rs_array = NULL;
776
777 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
778 if (rs_array) {
779 zval_dtor(rs_array);
780 array_init(rs_array);
781 }
782 PHP_HTTP_DO_NEGOTIATE(content_type, supported, rs_array);
783 } else {
784 RETURN_FALSE;
785 }
786 }
787
788 PHP_METHOD(HttpEnv, negotiate)
789 {
790 HashTable *supported;
791 zval *rs_array = NULL;
792 char *value_str, *sep_str = NULL;
793 int value_len, sep_len = 0;
794
795 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sH|s!z", &value_str, &value_len, &supported, &sep_str, &sep_len, &rs_array)) {
796 HashTable *rs;
797
798 if (rs_array) {
799 zval_dtor(rs_array);
800 array_init(rs_array);
801 }
802
803 if ((rs = php_http_negotiate(value_str, value_len, supported, sep_str, sep_len TSRMLS_CC))) {
804 PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs, supported, rs_array);
805 } else {
806 PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array);
807 }
808 } else {
809 RETURN_FALSE;
810 }
811 }
812
813 PHP_METHOD(HttpEnv, persistentHandlesStat)
814 {
815 if (SUCCESS == zend_parse_parameters_none()) {
816 object_init(return_value);
817 if (php_http_persistent_handle_statall(HASH_OF(return_value) TSRMLS_CC)) {
818 return;
819 }
820 zval_dtor(return_value);
821 }
822 RETURN_FALSE;
823 }
824
825 PHP_METHOD(HttpEnv, persistentHandlesClean)
826 {
827 char *name_str = NULL, *ident_str = NULL;
828 int name_len = 0, ident_len = 0;
829
830 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &name_str, &name_len, &ident_str, &ident_len)) {
831 php_http_persistent_handle_cleanup(name_str, name_len, ident_str, ident_len TSRMLS_CC);
832 }
833 }
834
835 zend_class_entry *php_http_env_request_class_entry;
836
837 #undef PHP_HTTP_BEGIN_ARGS
838 #undef PHP_HTTP_EMPTY_ARGS
839 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpEnvRequest, method, 0, req_args)
840 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpEnvRequest, method, 0)
841 #define PHP_HTTP_ENV_REQUEST_ME(method, visibility) PHP_ME(HttpEnvRequest, method, PHP_HTTP_ARGS(HttpEnvRequest, method), visibility)
842
843 PHP_HTTP_EMPTY_ARGS(__construct);
844
845 zend_function_entry php_http_env_request_method_entry[] = {
846 PHP_HTTP_ENV_REQUEST_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
847
848 EMPTY_FUNCTION_ENTRY
849 };
850
851 PHP_METHOD(HttpEnvRequest, __construct)
852 {
853 with_error_handling(EH_THROW, php_http_exception_class_entry) {
854 if (SUCCESS == zend_parse_parameters_none()) {
855 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
856
857 with_error_handling(EH_THROW, php_http_exception_class_entry) {
858 obj->message = php_http_message_init_env(obj->message, PHP_HTTP_REQUEST TSRMLS_CC);
859 } end_error_handling();
860 }
861 } end_error_handling();
862 }
863
864 PHP_MINIT_FUNCTION(http_env)
865 {
866 PHP_HTTP_REGISTER_CLASS(http, Env, http_env, NULL, 0);
867
868 PHP_HTTP_REGISTER_CLASS(http\\Env, Request, http_env_request, php_http_message_class_entry, 0);
869
870 return SUCCESS;
871 }
872
873
874 /*
875 * Local variables:
876 * tab-width: 4
877 * c-basic-offset: 4
878 * End:
879 * vim600: noet sw=4 ts=4 fdm=marker
880 * vim<600: noet sw=4 ts=4
881 */