flush wip
[m6w6/ext-http] / php_http_env.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14 #include "php_variables.h"
15
16
17 PHP_RSHUTDOWN_FUNCTION(http_env)
18 {
19 if (PHP_HTTP_G->env.request.headers) {
20 zend_hash_destroy(PHP_HTTP_G->env.request.headers);
21 FREE_HASHTABLE(PHP_HTTP_G->env.request.headers);
22 PHP_HTTP_G->env.request.headers = NULL;
23 }
24 if (PHP_HTTP_G->env.request.body) {
25 php_http_message_body_free(&PHP_HTTP_G->env.request.body);
26 }
27
28 if (PHP_HTTP_G->env.server_var) {
29 zval_ptr_dtor(PHP_HTTP_G->env.server_var);
30 PHP_HTTP_G->env.server_var = NULL;
31 }
32
33 return SUCCESS;
34 }
35
36 void php_http_env_get_request_headers(HashTable *headers TSRMLS_DC)
37 {
38 php_http_arrkey_t key;
39 zval *hsv, *header;
40
41 if (!PHP_HTTP_G->env.request.headers) {
42 ALLOC_HASHTABLE(PHP_HTTP_G->env.request.headers);
43 ZEND_INIT_SYMTABLE(PHP_HTTP_G->env.request.headers);
44
45 if ((hsv = php_http_env_get_superglobal(ZEND_STRL("_SERVER")))) {
46 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(hsv), key.h, key.key, header)
47 {
48 if (key.key && key.key->len > 5 && *key.key->val == 'H' && !strncmp(key.key->val, "HTTP_", 5)) {
49 size_t key_len = key.key->len - 5;
50 char *key_str = php_http_pretty_key(estrndup(&key.key->val[5], key_len), key_len, 1, 1);
51
52 Z_TRY_ADDREF_P(header);
53 zend_symtable_str_update(PHP_HTTP_G->env.request.headers, key_str, key_len, header);
54
55 efree(key_str);
56 } else if (key.key && key.key->len > 8 && *key.key->val == 'C' && !strncmp(key.key->val, "CONTENT_", 8)) {
57 char *key_str = php_http_pretty_key(estrndup(key.key->val, key.key->len), key.key->len, 1, 1);
58
59 Z_TRY_ADDREF_P(header);
60 zend_symtable_str_update(PHP_HTTP_G->env.request.headers, key_str, key.key->len, header);
61
62 efree(key_str);
63 }
64 }
65 ZEND_HASH_FOREACH_END();
66 }
67 }
68
69 if (headers) {
70 array_copy(PHP_HTTP_G->env.request.headers, headers);
71 }
72 }
73
74 char *php_http_env_get_request_header(const char *name_str, size_t name_len, size_t *len, php_http_message_t *request)
75 {
76 HashTable *request_headers;
77 zval *zvalue = NULL;
78 char *val = NULL, *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
79
80 if (request) {
81 request_headers = &request->hdrs;
82 } else {
83 php_http_env_get_request_headers(NULL);
84 request_headers = PHP_HTTP_G->env.request.headers;
85 }
86
87 if ((zvalue = zend_symtable_str_find(request_headers, key, name_len))) {
88 zend_string *zs = zval_get_string(zvalue);
89
90 val = estrndup(zs->val, zs->len);
91 if (len) {
92 *len = zs->len;
93 }
94 zend_string_release(zs);
95 }
96
97 efree(key);
98
99 return val;
100 }
101
102 zend_bool php_http_env_got_request_header(const char *name_str, size_t name_len, php_http_message_t *request)
103 {
104 HashTable *request_headers;
105 char *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
106 zend_bool got;
107
108 if (request) {
109 request_headers = &request->hdrs;
110 } else {
111 php_http_env_get_request_headers(NULL);
112 request_headers = PHP_HTTP_G->env.request.headers;
113 }
114 got = zend_symtable_str_exists(request_headers, key, name_len);
115 efree(key);
116
117 return got;
118 }
119
120 zval *php_http_env_get_superglobal(const char *key, size_t key_len)
121 {
122 zval *hsv;
123 zend_string *key_str = zend_string_init(key, key_len, 0);
124
125 zend_is_auto_global(key_str);
126 hsv = zend_hash_find(&EG(symbol_table), key_str);
127 zend_string_release(key_str);
128
129 if (Z_TYPE_P(hsv) != IS_ARRAY) {
130 return NULL;
131 }
132
133 return hsv;
134 }
135
136 zval *php_http_env_get_server_var(const char *key, size_t key_len, zend_bool check)
137 {
138 zval *hsv, *var;
139
140 /* if available, this is a lot faster than accessing $_SERVER * /
141 if (sapi_module.getenv) {
142 char *env;
143
144 if ((!(env = sapi_module.getenv((char *) key, key_len))) || (check && !*env)) {
145 return NULL;
146 }
147 if (PHP_HTTP_G->env.server_var) {
148 zval_ptr_dtor(&PHP_HTTP_G->env.server_var);
149 }
150 MAKE_STD_ZVAL(PHP_HTTP_G->env.server_var);
151 ZVAL_STRING(PHP_HTTP_G->env.server_var, env, 1);
152 return PHP_HTTP_G->env.server_var;
153 }
154 / * */
155
156 if (!(hsv = php_http_env_get_superglobal(ZEND_STRL("_SERVER")))) {
157 return NULL;
158 }
159 if (!(var = zend_symtable_str_find(Z_ARRVAL_P(hsv), key, key_len))) {
160 return NULL;
161 }
162 if (check && !((Z_TYPE_P(var) == IS_STRING) && Z_STRVAL_P(var) && Z_STRLEN_P(var))) {
163 return NULL;
164 }
165 return var;
166 }
167
168 php_http_message_body_t *php_http_env_get_request_body(void)
169 {
170 if (!PHP_HTTP_G->env.request.body) {
171 php_stream *s = php_stream_temp_new();
172 php_stream *input = php_stream_open_wrapper("php://input", "r", 0, NULL);
173
174 /* php://input does not support stat */
175 php_stream_copy_to_stream_ex(input, s, -1, NULL);
176 php_stream_close(input);
177
178 php_stream_rewind(s);
179 PHP_HTTP_G->env.request.body = php_http_message_body_init(NULL, s);
180 }
181
182 return PHP_HTTP_G->env.request.body;
183 }
184
185 const char *php_http_env_get_request_method(php_http_message_t *request)
186 {
187 const char *m;
188
189 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, request)) {
190 m = request->http.info.request.method;
191 } else {
192 m = SG(request_info).request_method;
193 }
194
195 return m ? m : "GET";
196 }
197
198 php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_t length, php_http_message_t *request)
199 {
200 zval zentry;
201 char *range, *rp, c;
202 long begin = -1, end = -1, *ptr;
203
204 if (!(range = php_http_env_get_request_header(ZEND_STRL("Range"), NULL, request))) {
205 return PHP_HTTP_RANGE_NO;
206 }
207 if (strncmp(range, "bytes=", lenof("bytes="))) {
208 PTR_FREE(range);
209 return PHP_HTTP_RANGE_NO;
210 }
211
212 rp = range + lenof("bytes=");
213 ptr = &begin;
214
215 do {
216 switch (c = *(rp++)) {
217 case '0':
218 /* allow 000... - shall we? */
219 if (*ptr != -10) {
220 *ptr *= 10;
221 }
222 break;
223
224 case '1': case '2': case '3':
225 case '4': case '5': case '6':
226 case '7': case '8': case '9':
227 /*
228 * If the value of the pointer is already set (non-negative)
229 * then multiply its value by ten and add the current value,
230 * else initialise the pointers value with the current value
231 * --
232 * This let us recognize empty fields when validating the
233 * ranges, i.e. a "-10" for begin and "12345" for the end
234 * was the following range request: "Range: bytes=0-12345";
235 * While a "-1" for begin and "12345" for the end would
236 * have been: "Range: bytes=-12345".
237 */
238 if (*ptr > 0) {
239 *ptr *= 10;
240 *ptr += c - '0';
241 } else {
242 *ptr = c - '0';
243 }
244 break;
245
246 case '-':
247 ptr = &end;
248 break;
249
250 case ' ':
251 break;
252
253 case 0:
254 case ',':
255
256 if (length) {
257 /* validate ranges */
258 switch (begin) {
259 /* "0-12345" */
260 case -10:
261 switch (end) {
262 /* "0-" */
263 case -1:
264 PTR_FREE(range);
265 return PHP_HTTP_RANGE_NO;
266
267 /* "0-0" */
268 case -10:
269 end = 0;
270 break;
271
272 default:
273 if (length <= (size_t) end) {
274 end = length - 1;
275 }
276 break;
277 }
278 begin = 0;
279 break;
280
281 /* "-12345" */
282 case -1:
283 /* "-", "-0" */
284 if (end == -1 || end == -10) {
285 PTR_FREE(range);
286 return PHP_HTTP_RANGE_ERR;
287 }
288 begin = length - end;
289 end = length - 1;
290 break;
291
292 /* "12345-(NNN)" */
293 default:
294 if (length <= (size_t) begin) {
295 PTR_FREE(range);
296 return PHP_HTTP_RANGE_ERR;
297 }
298 switch (end) {
299 /* "12345-0" */
300 case -10:
301 PTR_FREE(range);
302 return PHP_HTTP_RANGE_ERR;
303
304 /* "12345-" */
305 case -1:
306 end = length - 1;
307 break;
308
309 /* "12345-67890" */
310 default:
311 if (length <= (size_t) end) {
312 end = length - 1;
313 } else if (end < begin) {
314 PTR_FREE(range);
315 return PHP_HTTP_RANGE_ERR;
316 }
317 break;
318 }
319 break;
320 }
321 }
322
323 array_init(&zentry);
324 add_index_long(&zentry, 0, begin);
325 add_index_long(&zentry, 1, end);
326 zend_hash_next_index_insert(ranges, &zentry);
327
328 begin = -1;
329 end = -1;
330 ptr = &begin;
331
332 break;
333
334 default:
335 PTR_FREE(range);
336 return PHP_HTTP_RANGE_NO;
337 }
338 } while (c != 0);
339
340 PTR_FREE(range);
341 return PHP_HTTP_RANGE_OK;
342 }
343
344 static void grab_headers(void *data, void *arg)
345 {
346 php_http_buffer_appendl(PHP_HTTP_BUFFER(arg), ((sapi_header_struct *)data)->header);
347 php_http_buffer_appends(PHP_HTTP_BUFFER(arg), PHP_HTTP_CRLF);
348 }
349
350 static void grab_header(void *data, void *arg)
351 {
352 struct {
353 char *name_str;
354 size_t name_len;
355 char *value_ptr;
356 } *args = arg;
357 sapi_header_struct *header = data;
358
359 if ( header->header_len > args->name_len
360 && header->header[args->name_len] == ':'
361 && !strncmp(header->header, args->name_str, args->name_len)
362 ) {
363 args->value_ptr = &header->header[args->name_len + 1];
364 while (PHP_HTTP_IS_CTYPE(space, *args->value_ptr)) {
365 ++args->value_ptr;
366 }
367 }
368 }
369
370 ZEND_RESULT_CODE php_http_env_get_response_headers(HashTable *headers_ht)
371 {
372 ZEND_RESULT_CODE status;
373 php_http_buffer_t headers;
374
375 php_http_buffer_init(&headers);
376 zend_llist_apply_with_argument(&SG(sapi_headers).headers, grab_headers, &headers);
377 php_http_buffer_fix(&headers);
378
379 status = php_http_header_parse(headers.data, headers.used, headers_ht, NULL, NULL);
380 php_http_buffer_dtor(&headers);
381
382 return status;
383 }
384
385 char *php_http_env_get_response_header(const char *name_str, size_t name_len)
386 {
387 struct {
388 char *name_str;
389 size_t name_len;
390 char *value_ptr;
391 } args;
392
393 args.name_str = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
394 args.name_len = name_len;
395 args.value_ptr = NULL;
396 zend_llist_apply_with_argument(&SG(sapi_headers).headers, grab_header, &args);
397 efree(args.name_str);
398
399 return args.value_ptr ? estrdup(args.value_ptr) : NULL;
400 }
401
402 long php_http_env_get_response_code(void)
403 {
404 long code = SG(sapi_headers).http_response_code;
405 return code ? code : 200;
406 }
407
408 ZEND_RESULT_CODE php_http_env_set_response_code(long http_code)
409 {
410 return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) (zend_intptr_t) http_code);
411 }
412
413 ZEND_RESULT_CODE php_http_env_set_response_status_line(long code, php_http_version_t *v)
414 {
415 sapi_header_line h = {NULL, 0, 0};
416 ZEND_RESULT_CODE ret;
417
418 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));
419 ret = sapi_header_op(SAPI_HEADER_REPLACE, (void *) &h);
420 efree(h.line);
421
422 return ret;
423 }
424
425 ZEND_RESULT_CODE php_http_env_set_response_protocol_version(php_http_version_t *v)
426 {
427 return php_http_env_set_response_status_line(php_http_env_get_response_code(), v);
428 }
429
430 ZEND_RESULT_CODE php_http_env_set_response_header(long http_code, const char *header_str, size_t header_len, zend_bool replace)
431 {
432 sapi_header_line h = {estrndup(header_str, header_len), header_len, http_code};
433 ZEND_RESULT_CODE ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h);
434
435 efree(h.line);
436 return ret;
437 }
438
439 ZEND_RESULT_CODE php_http_env_set_response_header_va(long http_code, zend_bool replace, const char *fmt, va_list argv)
440 {
441 ZEND_RESULT_CODE ret = FAILURE;
442 sapi_header_line h = {NULL, 0, http_code};
443
444 h.line_len = vspprintf(&h.line, 0, fmt, argv);
445
446 if (h.line) {
447 if (h.line_len) {
448 ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h);
449 }
450 efree(h.line);
451 }
452 return ret;
453 }
454
455 ZEND_RESULT_CODE php_http_env_set_response_header_format(long http_code, zend_bool replace, const char *fmt, ...)
456 {
457 ZEND_RESULT_CODE ret;
458 va_list args;
459
460 va_start(args, fmt);
461 ret = php_http_env_set_response_header_va(http_code, replace, fmt, args);
462 va_end(args);
463
464 return ret;
465 }
466
467 ZEND_RESULT_CODE php_http_env_set_response_header_value(long http_code, const char *name_str, size_t name_len, zval *value, zend_bool replace)
468 {
469 if (!value) {
470 sapi_header_line h = {(char *) name_str, name_len, http_code};
471
472 return sapi_header_op(SAPI_HEADER_DELETE, (void *) &h);
473 }
474
475 if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
476 int first = replace;
477 zval *data_ptr;
478 HashTable *ht = HASH_OF(value);
479
480 ZEND_HASH_FOREACH_VAL_IND(ht, data_ptr)
481 {
482 if (SUCCESS != php_http_env_set_response_header_value(http_code, name_str, name_len, data_ptr, first)) {
483 return FAILURE;
484 }
485 first = 0;
486 }
487 ZEND_HASH_FOREACH_END();
488
489 return SUCCESS;
490 } else {
491 zend_string *data = zval_get_string(value);
492
493 if (!data->len) {
494 zend_string_release(data);
495 return php_http_env_set_response_header_value(http_code, name_str, name_len, NULL, replace);
496 } else {
497 sapi_header_line h;
498 ZEND_RESULT_CODE ret;
499
500 if (name_len > INT_MAX) {
501 return FAILURE;
502 }
503 h.response_code = http_code;
504 h.line_len = spprintf(&h.line, 0, "%.*s: %.*s", (int) name_len, name_str, data->len, data->val);
505
506 ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h);
507
508 zend_string_release(data);
509 PTR_FREE(h.line);
510
511 return ret;
512 }
513 }
514 }
515
516 const char *php_http_env_get_response_status_for_code(unsigned code)
517 {
518 switch (code) {
519 #define PHP_HTTP_RESPONSE_CODE(c, s) case c: return s;
520 #include "php_http_response_codes.h"
521 #undef PHP_HTTP_RESPONSE_CODE
522 default:
523 return NULL;
524 }
525 }
526
527 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getRequestHeader, 0, 0, 0)
528 ZEND_ARG_INFO(0, header_name)
529 ZEND_END_ARG_INFO();
530 static PHP_METHOD(HttpEnv, getRequestHeader)
531 {
532 char *header_name_str = NULL;
533 size_t header_name_len = 0;
534
535 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str, &header_name_len)) {
536 return;
537 }
538 if (header_name_str && header_name_len) {
539 size_t header_length;
540 char *header_value = php_http_env_get_request_header(header_name_str, header_name_len, &header_length, NULL);
541
542 if (header_value) {
543 RETURN_STR(php_http_cs2zs(header_value, header_length));
544 }
545 } else {
546 array_init(return_value);
547 php_http_env_get_request_headers(Z_ARRVAL_P(return_value));
548 }
549 }
550
551 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getRequestBody, 0, 0, 0)
552 ZEND_ARG_INFO(0, body_class_name)
553 ZEND_END_ARG_INFO();
554 static PHP_METHOD(HttpEnv, getRequestBody)
555 {
556 php_http_message_body_t *body;
557 php_http_message_body_object_t *body_obj;
558 zend_class_entry *class_entry = php_http_message_body_class_entry;
559
560 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &class_entry), invalid_arg, return);
561
562 body = php_http_env_get_request_body();
563 if (SUCCESS == php_http_new((void *) &body_obj, class_entry, (php_http_new_t) php_http_message_body_object_new_ex, php_http_message_body_class_entry, body)) {
564 php_http_message_body_addref(body);
565 RETVAL_OBJ(&body_obj->zo);
566 }
567 }
568
569 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseStatusForCode, 0, 0, 1)
570 ZEND_ARG_INFO(0, code)
571 ZEND_END_ARG_INFO();
572 static PHP_METHOD(HttpEnv, getResponseStatusForCode)
573 {
574 zend_long code;
575 const char *status;
576
577 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
578 return;
579 }
580
581 if ((status = php_http_env_get_response_status_for_code(code))) {
582 RETURN_STRING(status);
583 }
584 }
585
586 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseStatusForAllCodes, 0, 0, 0)
587 ZEND_END_ARG_INFO();
588 static PHP_METHOD(HttpEnv, getResponseStatusForAllCodes)
589 {
590 if (SUCCESS != zend_parse_parameters_none()) {
591 return;
592 }
593
594 array_init(return_value);
595 #define PHP_HTTP_RESPONSE_CODE(code, status) add_index_string(return_value, code, status);
596 #include "php_http_response_codes.h"
597 #undef PHP_HTTP_RESPONSE_CODE
598 }
599
600 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseHeader, 0, 0, 0)
601 ZEND_ARG_INFO(0, header_name)
602 ZEND_END_ARG_INFO();
603 static PHP_METHOD(HttpEnv, getResponseHeader)
604 {
605 char *header_name_str = NULL;
606 size_t header_name_len = 0;
607
608 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str, &header_name_len)) {
609 return;
610 }
611 if (header_name_str && header_name_len) {
612 char *header_value = php_http_env_get_response_header(header_name_str, header_name_len);
613
614 if (header_value) {
615 RETURN_STR(php_http_cs2zs(header_value, strlen(header_value)));
616 }
617 } else {
618 array_init(return_value);
619 php_http_env_get_response_headers(Z_ARRVAL_P(return_value));
620 }
621 }
622
623 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseCode, 0, 0, 0)
624 ZEND_END_ARG_INFO();
625 static PHP_METHOD(HttpEnv, getResponseCode)
626 {
627 if (SUCCESS != zend_parse_parameters_none()) {
628 return;
629 }
630 RETURN_LONG(php_http_env_get_response_code());
631 }
632
633 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_setResponseHeader, 0, 0, 1)
634 ZEND_ARG_INFO(0, header_name)
635 ZEND_ARG_INFO(0, header_value)
636 ZEND_ARG_INFO(0, response_code)
637 ZEND_ARG_INFO(0, replace_header)
638 ZEND_END_ARG_INFO();
639 static PHP_METHOD(HttpEnv, setResponseHeader)
640 {
641 char *header_name_str;
642 size_t header_name_len;
643 zval *header_value = NULL;
644 zend_long code = 0;
645 zend_bool replace_header = 1;
646
647 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!lb", &header_name_str, &header_name_len, &header_value, &code, &replace_header)) {
648 return;
649 }
650 RETURN_BOOL(SUCCESS == php_http_env_set_response_header_value(code, header_name_str, header_name_len, header_value, replace_header));
651 }
652
653 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_setResponseCode, 0, 0, 1)
654 ZEND_ARG_INFO(0, code)
655 ZEND_END_ARG_INFO();
656 static PHP_METHOD(HttpEnv, setResponseCode)
657 {
658 zend_long code;
659
660 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code)) {
661 return;
662 }
663 RETURN_BOOL(SUCCESS == php_http_env_set_response_code(code));
664 }
665
666 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateLanguage, 0, 0, 1)
667 ZEND_ARG_INFO(0, supported)
668 ZEND_ARG_INFO(1, result_array)
669 ZEND_END_ARG_INFO();
670 static PHP_METHOD(HttpEnv, negotiateLanguage)
671 {
672 HashTable *supported;
673 zval *rs_array = NULL;
674
675 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
676 return;
677 }
678 if (rs_array) {
679 ZVAL_DEREF(rs_array);
680 zval_dtor(rs_array);
681 array_init(rs_array);
682 }
683
684 PHP_HTTP_DO_NEGOTIATE(language, supported, rs_array);
685 }
686
687 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateCharset, 0, 0, 1)
688 ZEND_ARG_INFO(0, supported)
689 ZEND_ARG_INFO(1, result_array)
690 ZEND_END_ARG_INFO();
691 static PHP_METHOD(HttpEnv, negotiateCharset)
692 {
693 HashTable *supported;
694 zval *rs_array = NULL;
695
696 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported, &rs_array)) {
697 return;
698 }
699 if (rs_array) {
700 ZVAL_DEREF(rs_array);
701 zval_dtor(rs_array);
702 array_init(rs_array);
703 }
704 PHP_HTTP_DO_NEGOTIATE(charset, supported, rs_array);
705 }
706
707 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateEncoding, 0, 0, 1)
708 ZEND_ARG_INFO(0, supported)
709 ZEND_ARG_INFO(1, result_array)
710 ZEND_END_ARG_INFO();
711 static PHP_METHOD(HttpEnv, negotiateEncoding)
712 {
713 HashTable *supported;
714 zval *rs_array = NULL;
715
716 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported, &rs_array)) {
717 return;
718 }
719 if (rs_array) {
720 ZVAL_DEREF(rs_array);
721 zval_dtor(rs_array);
722 array_init(rs_array);
723 }
724 PHP_HTTP_DO_NEGOTIATE(encoding, supported, rs_array);
725 }
726
727 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateContentType, 0, 0, 1)
728 ZEND_ARG_INFO(0, supported)
729 ZEND_ARG_INFO(1, result_array)
730 ZEND_END_ARG_INFO();
731 static PHP_METHOD(HttpEnv, negotiateContentType)
732 {
733 HashTable *supported;
734 zval *rs_array = NULL;
735
736 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported, &rs_array)) {
737 return;
738 }
739 if (rs_array) {
740 ZVAL_DEREF(rs_array);
741 zval_dtor(rs_array);
742 array_init(rs_array);
743 }
744 PHP_HTTP_DO_NEGOTIATE(content_type, supported, rs_array);
745 }
746
747 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiate, 0, 0, 2)
748 ZEND_ARG_INFO(0, params)
749 ZEND_ARG_INFO(0, supported)
750 ZEND_ARG_INFO(0, primary_type_separator)
751 ZEND_ARG_INFO(1, result_array)
752 ZEND_END_ARG_INFO();
753 static PHP_METHOD(HttpEnv, negotiate)
754 {
755 HashTable *supported, *rs;
756 zval *rs_array = NULL;
757 char *value_str, *sep_str = NULL;
758 size_t value_len, sep_len = 0;
759
760 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sH|s!z", &value_str, &value_len, &supported, &sep_str, &sep_len, &rs_array)) {
761 return;
762 }
763
764 if (rs_array) {
765 ZVAL_DEREF(rs_array);
766 zval_dtor(rs_array);
767 array_init(rs_array);
768 }
769
770 if ((rs = php_http_negotiate(value_str, value_len, supported, sep_str, sep_len))) {
771 PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs, supported, rs_array);
772 } else {
773 PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array);
774 }
775 }
776
777 static zend_function_entry php_http_env_methods[] = {
778 PHP_ME(HttpEnv, getRequestHeader, ai_HttpEnv_getRequestHeader, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
779 PHP_ME(HttpEnv, getRequestBody, ai_HttpEnv_getRequestBody, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
780
781 PHP_ME(HttpEnv, getResponseStatusForCode, ai_HttpEnv_getResponseStatusForCode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
782 PHP_ME(HttpEnv, getResponseStatusForAllCodes, ai_HttpEnv_getResponseStatusForAllCodes, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
783
784 PHP_ME(HttpEnv, getResponseHeader, ai_HttpEnv_getResponseHeader, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
785 PHP_ME(HttpEnv, getResponseCode, ai_HttpEnv_getResponseCode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
786 PHP_ME(HttpEnv, setResponseHeader, ai_HttpEnv_setResponseHeader, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
787 PHP_ME(HttpEnv, setResponseCode, ai_HttpEnv_setResponseCode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
788
789 PHP_ME(HttpEnv, negotiateLanguage, ai_HttpEnv_negotiateLanguage, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
790 PHP_ME(HttpEnv, negotiateContentType, ai_HttpEnv_negotiateContentType, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
791 PHP_ME(HttpEnv, negotiateEncoding, ai_HttpEnv_negotiateEncoding, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
792 PHP_ME(HttpEnv, negotiateCharset, ai_HttpEnv_negotiateCharset, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
793 PHP_ME(HttpEnv, negotiate, ai_HttpEnv_negotiate, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
794
795 EMPTY_FUNCTION_ENTRY
796 };
797
798 zend_class_entry *php_http_env_class_entry;
799
800 PHP_MINIT_FUNCTION(http_env)
801 {
802 zend_class_entry ce = {0};
803
804 INIT_NS_CLASS_ENTRY(ce, "http", "Env", php_http_env_methods);
805 php_http_env_class_entry = zend_register_internal_class(&ce);
806
807 return SUCCESS;
808 }
809
810
811 /*
812 * Local variables:
813 * tab-width: 4
814 * c-basic-offset: 4
815 * End:
816 * vim600: noet sw=4 ts=4 fdm=marker
817 * vim<600: noet sw=4 ts=4
818 */