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