04c8a980fc88946a5f41a94a484b729ad858b5bc
[m6w6/ext-http] / php_http_env_response.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
15 static void set_option(zval *options, const char *name_str, size_t name_len, int type, void *value_ptr, size_t value_len TSRMLS_DC)
16 {
17 if (Z_TYPE_P(options) == IS_OBJECT) {
18 if (value_ptr) {
19 switch (type) {
20 case IS_DOUBLE:
21 zend_update_property_double(Z_OBJCE_P(options), options, name_str, name_len, *(double *)value_ptr TSRMLS_CC);
22 break;
23 case IS_LONG:
24 zend_update_property_long(Z_OBJCE_P(options), options, name_str, name_len, *(long *)value_ptr TSRMLS_CC);
25 break;
26 case IS_STRING:
27 zend_update_property_stringl(Z_OBJCE_P(options), options, name_str, name_len, value_ptr, value_len TSRMLS_CC);
28 break;
29 case IS_ARRAY:
30 case IS_OBJECT:
31 zend_update_property(Z_OBJCE_P(options), options, name_str, name_len, value_ptr TSRMLS_CC);
32 break;
33 }
34 } else {
35 zend_update_property_null(Z_OBJCE_P(options), options, name_str, name_len TSRMLS_CC);
36 }
37 } else {
38 convert_to_array(options);
39 if (value_ptr) {
40 switch (type) {
41 case IS_DOUBLE:
42 add_assoc_double_ex(options, name_str, name_len + 1, *(double *)value_ptr);
43 break;
44 case IS_LONG:
45 add_assoc_long_ex(options, name_str, name_len + 1, *(long *)value_ptr);
46 break;
47 case IS_STRING: {
48 char *value = estrndup(value_ptr, value_len);
49 add_assoc_stringl_ex(options, name_str, name_len + 1, value, value_len, 0);
50 break;
51 case IS_ARRAY:
52 case IS_OBJECT:
53 Z_ADDREF_P(value_ptr);
54 add_assoc_zval_ex(options, name_str, name_len + 1, value_ptr);
55 break;
56 }
57 }
58 } else {
59 add_assoc_null_ex(options, name_str, name_len + 1);
60 }
61 }
62 }
63 static zval *get_option(zval *options, const char *name_str, size_t name_len TSRMLS_DC)
64 {
65 zval *val, **valptr;
66
67 if (Z_TYPE_P(options) == IS_OBJECT) {
68 val = zend_read_property(Z_OBJCE_P(options), options, name_str, name_len, 0 TSRMLS_CC);
69 } else {
70 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(options), name_str, name_len + 1, (void *) &valptr)) {
71 val = *valptr;
72 } else {
73 val = NULL;
74 }
75 }
76 if (val) {
77 Z_ADDREF_P(val);
78 }
79 return val;
80 }
81 static php_http_message_body_t *get_body(zval *options TSRMLS_DC)
82 {
83 zval *zbody;
84 php_http_message_body_t *body = NULL;
85
86 if ((zbody = get_option(options, ZEND_STRL("body") TSRMLS_CC))) {
87 if ((Z_TYPE_P(zbody) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)) {
88 php_http_message_body_object_t *body_obj = zend_object_store_get_object(zbody TSRMLS_CC);
89
90 body = body_obj->body;
91 }
92 zval_ptr_dtor(&zbody);
93 }
94
95 return body;
96 }
97 static php_http_message_t *get_request(zval *options TSRMLS_DC)
98 {
99 zval *zrequest;
100 php_http_message_t *request = NULL;
101
102 if ((zrequest = get_option(options, ZEND_STRL("request") TSRMLS_CC))) {
103 if (Z_TYPE_P(zrequest) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zrequest), php_http_message_class_entry TSRMLS_CC)) {
104 php_http_message_object_t *request_obj = zend_object_store_get_object(zrequest TSRMLS_CC);
105
106 request = request_obj->message;
107 }
108 zval_ptr_dtor(&zrequest);
109 }
110
111 return request;
112 }
113 static void set_cookie(zval *options, zval *zcookie_new TSRMLS_DC)
114 {
115 HashPosition pos;
116 zval *zcookies_set;
117 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
118 php_http_cookie_object_t *obj = zend_object_store_get_object(zcookie_new TSRMLS_CC);
119
120 zcookies_set = get_option(options, ZEND_STRL("cookies") TSRMLS_CC);
121 if (!zcookies_set || Z_TYPE_P(zcookies_set) != IS_ARRAY) {
122 if (zcookies_set) {
123 zval_ptr_dtor(&zcookies_set);
124 }
125 MAKE_STD_ZVAL(zcookies_set);
126 array_init_size(zcookies_set, zend_hash_num_elements(&obj->list->cookies));
127 } else {
128 SEPARATE_ZVAL(&zcookies_set);
129 }
130
131 FOREACH_HASH_KEY(pos, &obj->list->cookies, key) {
132 Z_ADDREF_P(zcookie_new);
133 if (key.type == HASH_KEY_IS_STRING) {
134 add_assoc_zval_ex(zcookies_set, key.str, key.len, zcookie_new);
135 } else {
136 add_index_zval(zcookies_set, key.num, zcookie_new);
137 }
138 }
139
140 set_option(options, ZEND_STRL("cookies"), IS_ARRAY, zcookies_set, 0 TSRMLS_CC);
141 zval_ptr_dtor(&zcookies_set);
142 }
143
144 php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *options, const char *header_str, size_t header_len, php_http_message_t *request TSRMLS_DC)
145 {
146 php_http_cache_status_t ret = PHP_HTTP_CACHE_NO;
147 int free_etag = 0;
148 char *header = NULL, *etag;
149 php_http_message_body_t *body;
150 zval *zetag;
151
152
153 if (!(body = get_body(options TSRMLS_CC))) {
154 return ret;
155 }
156
157 if ((zetag = get_option(options, ZEND_STRL("etag") TSRMLS_CC))) {
158 zval *zetag_copy = php_http_ztyp(IS_STRING, zetag);
159 zval_ptr_dtor(&zetag);
160 zetag = zetag_copy;
161 }
162
163 if (zetag && Z_STRLEN_P(zetag)) {
164 etag = Z_STRVAL_P(zetag);
165 } else if ((etag = php_http_message_body_etag(body))) {
166 set_option(options, ZEND_STRL("etag"), IS_STRING, etag, strlen(etag) TSRMLS_CC);
167 free_etag = 1;
168 }
169
170 if (zetag) {
171 zval_ptr_dtor(&zetag);
172 }
173
174 if (etag && (header = php_http_env_get_request_header(header_str, header_len, NULL, request TSRMLS_CC))) {
175 ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD) ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS;
176 }
177
178 if (free_etag) {
179 efree(etag);
180 }
181
182 PTR_FREE(header);
183 return ret;
184 }
185
186 php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len, php_http_message_t *request TSRMLS_DC)
187 {
188 php_http_cache_status_t ret = PHP_HTTP_CACHE_NO;
189 char *header;
190 time_t ums, lm = 0;
191 php_http_message_body_t *body;
192 zval *zlm;
193
194 if (!(body = get_body(options TSRMLS_CC))) {
195 return ret;
196 }
197
198 if ((zlm = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) {
199 zval *zlm_copy = php_http_ztyp(IS_LONG, zlm);
200 zval_ptr_dtor(&zlm);
201 zlm = zlm_copy;
202 }
203
204 if (zlm && Z_LVAL_P(zlm) > 0) {
205 lm = Z_LVAL_P(zlm);
206 } else {
207 lm = php_http_message_body_mtime(body);
208 set_option(options, ZEND_STRL("lastModified"), IS_LONG, &lm, 0 TSRMLS_CC);
209 }
210
211 if (zlm) {
212 zval_ptr_dtor(&zlm);
213 }
214
215 if ((header = php_http_env_get_request_header(header_str, header_len, NULL, request TSRMLS_CC))) {
216 ums = php_parse_date(header, NULL);
217
218 if (ums > 0 && ums >= lm) {
219 ret = PHP_HTTP_CACHE_HIT;
220 } else {
221 ret = PHP_HTTP_CACHE_MISS;
222 }
223 }
224
225 PTR_FREE(header);
226 return ret;
227 }
228
229 static zend_bool php_http_env_response_is_cacheable(php_http_env_response_t *r, php_http_message_t *request)
230 {
231 TSRMLS_FETCH_FROM_CTX(r->ts);
232
233 if (r->ops->get_status(r) >= 400) {
234 return 0;
235 }
236
237 if (php_http_env_got_request_header(ZEND_STRL("Authorization"), request TSRMLS_CC)) {
238 return 0;
239 }
240
241 if (-1 == php_http_select_str(php_http_env_get_request_method(request TSRMLS_CC), 2, "HEAD", "GET")) {
242 return 0;
243 }
244
245 return 1;
246 }
247
248 static size_t output(void *context, char *buf, size_t len TSRMLS_DC)
249 {
250 php_http_env_response_t *r = context;
251
252 if (SUCCESS != r->ops->write(r, buf, len)) {
253 return (size_t) -1;
254 }
255
256 /* we really only need to flush when throttling is enabled,
257 because we push the data as fast as possible anyway if not */
258 if (r->throttle.delay >= PHP_HTTP_DIFFSEC) {
259 r->ops->flush(r);
260 php_http_sleep(r->throttle.delay);
261 }
262 return len;
263 }
264
265 #define php_http_env_response_send_done(r) php_http_env_response_send_data((r), NULL, 0)
266 static STATUS php_http_env_response_send_data(php_http_env_response_t *r, const char *buf, size_t len)
267 {
268 size_t chunks_sent, chunk = r->throttle.chunk ? r->throttle.chunk : PHP_HTTP_SENDBUF_SIZE;
269 TSRMLS_FETCH_FROM_CTX(r->ts);
270
271 if (r->content.encoder) {
272 char *enc_str = NULL;
273 size_t enc_len = 0;
274
275 if (buf) {
276 if (SUCCESS != php_http_encoding_stream_update(r->content.encoder, buf, len, &enc_str, &enc_len)) {
277 return FAILURE;
278 }
279 } else {
280 if (SUCCESS != php_http_encoding_stream_finish(r->content.encoder, &enc_str, &enc_len)) {
281 return FAILURE;
282 }
283 }
284
285 if (!enc_str) {
286 return SUCCESS;
287 }
288 chunks_sent = php_http_buffer_chunked_output(&r->buffer, enc_str, enc_len, buf ? chunk : 0, output, r TSRMLS_CC);
289 PTR_FREE(enc_str);
290 } else {
291 chunks_sent = php_http_buffer_chunked_output(&r->buffer, buf, len, buf ? chunk : 0, output, r TSRMLS_CC);
292 }
293
294 return chunks_sent != (size_t) -1 ? SUCCESS : FAILURE;
295 }
296
297 php_http_env_response_t *php_http_env_response_init(php_http_env_response_t *r, zval *options, php_http_env_response_ops_t *ops, void *init_arg TSRMLS_DC)
298 {
299 zend_bool free_r;
300
301 if ((free_r = !r)) {
302 r = emalloc(sizeof(*r));
303 }
304 memset(r, 0, sizeof(*r));
305
306 if (ops) {
307 r->ops = ops;
308 } else {
309 r->ops = php_http_env_response_get_sapi_ops();
310 }
311
312 r->buffer = php_http_buffer_init(NULL);
313
314 Z_ADDREF_P(options);
315 r->options = options;
316
317 TSRMLS_SET_CTX(r->ts);
318
319 if (r->ops->init && (SUCCESS != r->ops->init(r, init_arg))) {
320 if (free_r) {
321 php_http_env_response_free(&r);
322 } else {
323 php_http_env_response_dtor(r);
324 r = NULL;
325 }
326 }
327
328 return r;
329 }
330
331 void php_http_env_response_dtor(php_http_env_response_t *r)
332 {
333 if (r->ops->dtor) {
334 r->ops->dtor(r);
335 }
336 php_http_buffer_free(&r->buffer);
337 zval_ptr_dtor(&r->options);
338 PTR_FREE(r->content.type);
339 PTR_FREE(r->content.encoding);
340 if (r->content.encoder) {
341 php_http_encoding_stream_free(&r->content.encoder);
342 }
343 }
344
345 void php_http_env_response_free(php_http_env_response_t **r)
346 {
347 if (*r) {
348 php_http_env_response_dtor(*r);
349 efree(*r);
350 *r = NULL;
351 }
352 }
353
354 static STATUS php_http_env_response_send_head(php_http_env_response_t *r, php_http_message_t *request)
355 {
356 STATUS ret = SUCCESS;
357 zval *zoption, *options = r->options;
358 TSRMLS_FETCH_FROM_CTX(r->ts);
359
360 if (r->done) {
361 return ret;
362 }
363
364 if ((zoption = get_option(options, ZEND_STRL("headers") TSRMLS_CC))) {
365 if (Z_TYPE_P(zoption) == IS_ARRAY) {
366 php_http_header_to_callback(Z_ARRVAL_P(zoption), 0, (php_http_pass_format_callback_t) r->ops->set_header, r TSRMLS_CC);
367 }
368 zval_ptr_dtor(&zoption);
369 }
370
371 if (ret != SUCCESS) {
372 return ret;
373 }
374
375 if ((zoption = get_option(options, ZEND_STRL("responseCode") TSRMLS_CC))) {
376 zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
377
378 zval_ptr_dtor(&zoption);
379 if (Z_LVAL_P(zoption_copy) > 0) {
380 ret = r->ops->set_status(r, Z_LVAL_P(zoption_copy));
381 }
382 zval_ptr_dtor(&zoption_copy);
383 }
384
385 if (ret != SUCCESS) {
386 return ret;
387 }
388
389 if ((zoption = get_option(options, ZEND_STRL("httpVersion") TSRMLS_CC))) {
390 php_http_version_t v;
391 zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
392
393 zval_ptr_dtor(&zoption);
394 if (Z_STRLEN_P(zoption_copy) && php_http_version_parse(&v, Z_STRVAL_P(zoption_copy) TSRMLS_CC)) {
395 ret = r->ops->set_protocol_version(r, &v);
396 php_http_version_dtor(&v);
397 }
398 zval_ptr_dtor(&zoption_copy);
399 }
400
401 if (ret != SUCCESS) {
402 return ret;
403 }
404
405 if ((zoption = get_option(options, ZEND_STRL("cookies") TSRMLS_CC))) {
406 if (Z_TYPE_P(zoption) == IS_ARRAY) {
407 HashPosition pos;
408 zval **zcookie;
409
410 FOREACH_VAL(pos, zoption, zcookie) {
411 if (Z_TYPE_PP(zcookie) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(zcookie), php_http_cookie_class_entry TSRMLS_CC)) {
412 php_http_cookie_object_t *obj = zend_object_store_get_object(*zcookie TSRMLS_CC);
413 char *str;
414 size_t len;
415
416 php_http_cookie_list_to_string(obj->list, &str, &len);
417 if (SUCCESS != (ret = r->ops->add_header(r, "Set-Cookie: %s", str))) {
418 efree(str);
419 break;
420 }
421 efree(str);
422 }
423 }
424 }
425 zval_ptr_dtor(&zoption);
426 }
427
428 if (ret != SUCCESS) {
429 return ret;
430 }
431
432 if ((zoption = get_option(options, ZEND_STRL("contentType") TSRMLS_CC))) {
433 zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
434
435 zval_ptr_dtor(&zoption);
436 if (Z_STRLEN_P(zoption_copy) && strchr(Z_STRVAL_P(zoption_copy), '/')) {
437 if (SUCCESS == (ret = r->ops->set_header(r, "Content-Type: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy)))) {
438 r->content.type = estrndup(Z_STRVAL_P(zoption_copy), Z_STRLEN_P(zoption_copy));
439 }
440 }
441 zval_ptr_dtor(&zoption_copy);
442 }
443
444 if (ret != SUCCESS) {
445 return ret;
446 }
447
448 if (r->range.status == PHP_HTTP_RANGE_OK) {
449 if (zend_hash_num_elements(&r->range.values) == 1) {
450 zval **range, **begin, **end;
451
452 if ( 1 == php_http_array_list(&r->range.values TSRMLS_CC, 1, &range)
453 && 2 == php_http_array_list(Z_ARRVAL_PP(range) TSRMLS_CC, 2, &begin, &end)
454 ) {
455 if (SUCCESS == (ret = r->ops->set_status(r, 206))) {
456 ret = r->ops->set_header(r, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin), Z_LVAL_PP(end), r->content.length);
457 }
458 } else {
459 /* this should never happen */
460 zend_hash_destroy(&r->range.values);
461 ret = FAILURE;
462 }
463 } else {
464 php_http_boundary(r->range.boundary, sizeof(r->range.boundary) TSRMLS_CC);
465 if (SUCCESS == (ret = r->ops->set_status(r, 206))) {
466 ret = r->ops->set_header(r, "Content-Type: multipart/byteranges; boundary=%s", r->range.boundary);
467 }
468 }
469 } else {
470 if ((zoption = get_option(options, ZEND_STRL("cacheControl") TSRMLS_CC))) {
471 zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
472
473 zval_ptr_dtor(&zoption);
474 if (Z_STRLEN_P(zoption_copy)) {
475 ret = r->ops->set_header(r, "Cache-Control: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy));
476 }
477 zval_ptr_dtor(&zoption_copy);
478 }
479
480 if (ret != SUCCESS) {
481 return ret;
482 }
483
484 if ((zoption = get_option(options, ZEND_STRL("contentDisposition") TSRMLS_CC))) {
485 zval *zoption_copy = php_http_ztyp(IS_ARRAY, zoption);
486 php_http_buffer_t buf;
487
488 php_http_buffer_init(&buf);
489 if (php_http_params_to_string(&buf, Z_ARRVAL_P(zoption_copy), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC)) {
490 if (buf.used) {
491 ret = r->ops->set_header(r, "Content-Disposition: %.*s", buf.used, buf.data);
492 }
493 }
494
495 php_http_buffer_dtor(&buf);
496 zval_ptr_dtor(&zoption_copy);
497 zval_ptr_dtor(&zoption);
498 }
499
500 if (ret != SUCCESS) {
501 return ret;
502 }
503
504 if ((zoption = get_option(options, ZEND_STRL("contentEncoding") TSRMLS_CC))) {
505 zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
506 zval zsupported;
507 HashTable *result = NULL;
508
509 zval_ptr_dtor(&zoption);
510 switch (Z_LVAL_P(zoption_copy)) {
511 case PHP_HTTP_CONTENT_ENCODING_GZIP:
512 INIT_PZVAL(&zsupported);
513 array_init(&zsupported);
514 add_next_index_stringl(&zsupported, ZEND_STRL("none"), 1);
515 add_next_index_stringl(&zsupported, ZEND_STRL("gzip"), 1);
516 add_next_index_stringl(&zsupported, ZEND_STRL("deflate"), 1);
517
518 if ((result = php_http_negotiate_encoding(Z_ARRVAL(zsupported), request TSRMLS_CC))) {
519 char *key_str = NULL;
520 uint key_len = 0;
521
522 zend_hash_internal_pointer_reset(result);
523 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key_str, &key_len, NULL, 0, NULL)) {
524 if (!strcmp(key_str, "gzip")) {
525 if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP TSRMLS_CC))) {
526 ret = FAILURE;
527 } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: gzip"))) {
528 r->content.encoding = estrndup(key_str, key_len - 1);
529 }
530 } else if (!strcmp(key_str, "deflate")) {
531 if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_ZLIB TSRMLS_CC))) {
532 ret = FAILURE;
533 } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: deflate"))) {
534 r->content.encoding = estrndup(key_str, key_len - 1);
535 }
536 } else {
537 ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding"));
538 }
539
540 if (SUCCESS == ret) {
541 ret = r->ops->add_header(r, "Vary: Accept-Encoding");
542 }
543 }
544
545 zend_hash_destroy(result);
546 FREE_HASHTABLE(result);
547 }
548
549 zval_dtor(&zsupported);
550 break;
551
552 case PHP_HTTP_CONTENT_ENCODING_NONE:
553 default:
554 ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding"));
555 break;
556 }
557 zval_ptr_dtor(&zoption_copy);
558 }
559
560 if (SUCCESS != ret) {
561 return ret;
562 }
563
564 if (php_http_env_response_is_cacheable(r, request)) {
565 switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match"), request TSRMLS_CC)) {
566 case PHP_HTTP_CACHE_MISS:
567 break;
568
569 case PHP_HTTP_CACHE_NO:
570 if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(options, ZEND_STRL("If-Modified-Since"), request TSRMLS_CC)) {
571 break;
572 }
573 /* no break */
574
575 case PHP_HTTP_CACHE_HIT:
576 ret = r->ops->set_status(r, 304);
577 r->done = 1;
578 break;
579 }
580
581 if ((zoption = get_option(options, ZEND_STRL("etag") TSRMLS_CC))) {
582 zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
583
584 zval_ptr_dtor(&zoption);
585 if (*Z_STRVAL_P(zoption_copy) != '"' && strncmp(Z_STRVAL_P(zoption_copy), "W/\"", 3)) {
586 ret = r->ops->set_header(r, "ETag: \"%s\"", Z_STRVAL_P(zoption_copy));
587 } else {
588 ret = r->ops->set_header(r, "ETag: %s", Z_STRVAL_P(zoption_copy));
589 }
590 zval_ptr_dtor(&zoption_copy);
591 }
592 if ((zoption = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) {
593 zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
594
595 zval_ptr_dtor(&zoption);
596 if (Z_LVAL_P(zoption_copy)) {
597 char *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), Z_LVAL_P(zoption_copy), 0 TSRMLS_CC);
598 if (date) {
599 ret = r->ops->set_header(r, "Last-Modified: %s", date);
600 efree(date);
601 }
602 }
603 zval_ptr_dtor(&zoption_copy);
604 }
605 }
606 }
607
608 return ret;
609 }
610
611 static STATUS php_http_env_response_send_body(php_http_env_response_t *r)
612 {
613 STATUS ret = SUCCESS;
614 zval *zoption;
615 php_http_message_body_t *body;
616 TSRMLS_FETCH_FROM_CTX(r->ts);
617
618 if (r->done) {
619 return ret;
620 }
621
622 if ((body = get_body(r->options TSRMLS_CC))) {
623 if ((zoption = get_option(r->options, ZEND_STRL("throttleDelay") TSRMLS_CC))) {
624 if (Z_TYPE_P(zoption) == IS_DOUBLE) {
625 r->throttle.delay = Z_DVAL_P(zoption);
626 }
627 zval_ptr_dtor(&zoption);
628 }
629 if ((zoption = get_option(r->options, ZEND_STRL("throttleChunk") TSRMLS_CC))) {
630 if (Z_TYPE_P(zoption) == IS_LONG) {
631 r->throttle.chunk = Z_LVAL_P(zoption);
632 }
633 zval_ptr_dtor(&zoption);
634 }
635
636 if (r->range.status == PHP_HTTP_RANGE_OK) {
637 if (zend_hash_num_elements(&r->range.values) == 1) {
638 /* single range */
639 zval **range, **begin, **end;
640
641 if ( 1 == php_http_array_list(&r->range.values TSRMLS_CC, 1, &range)
642 && 2 == php_http_array_list(Z_ARRVAL_PP(range) TSRMLS_CC, 2, &begin, &end)
643 ) {
644 /* send chunk */
645 ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1);
646 if (ret == SUCCESS) {
647 ret = php_http_env_response_send_done(r);
648 }
649 zend_hash_destroy(&r->range.values);
650 } else {
651 /* this should never happen */
652 zend_hash_destroy(&r->range.values);
653 r->ops->set_status(r, 500);
654 ret = FAILURE;
655 }
656
657 } else {
658 /* send multipart/byte-ranges message */
659 HashPosition pos;
660 zval **chunk;
661
662 FOREACH_HASH_VAL(pos, &r->range.values, chunk) {
663 zval **begin, **end;
664
665 if (2 == php_http_array_list(Z_ARRVAL_PP(chunk) TSRMLS_CC, 2, &begin, &end)) {
666 php_http_buffer_appendf(r->buffer,
667 PHP_HTTP_CRLF
668 "--%s" PHP_HTTP_CRLF
669 "Content-Type: %s" PHP_HTTP_CRLF
670 "Content-Range: bytes %ld-%ld/%zu" PHP_HTTP_CRLF PHP_HTTP_CRLF,
671 /* - */
672 r->range.boundary,
673 r->content.type ? r->content.type : "application/octet-stream",
674 Z_LVAL_PP(begin),
675 Z_LVAL_PP(end),
676 r->content.length
677 );
678 ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1);
679 }
680 }
681
682 if (ret == SUCCESS) {
683 php_http_buffer_appendf(r->buffer, PHP_HTTP_CRLF "--%s--", r->range.boundary);
684 ret = php_http_env_response_send_done(r);
685 }
686 zend_hash_destroy(&r->range.values);
687 }
688
689 } else {
690 ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, 0, 0);
691 if (ret == SUCCESS) {
692 ret = php_http_env_response_send_done(r);
693 }
694 }
695 }
696 return ret;
697 }
698
699 STATUS php_http_env_response_send(php_http_env_response_t *r)
700 {
701 php_http_message_t *request;
702 php_http_message_body_t *body;
703 TSRMLS_FETCH_FROM_CTX(r->ts);
704
705 request = get_request(r->options TSRMLS_CC);
706
707 /* check for ranges */
708 if ((body = get_body(r->options TSRMLS_CC))) {
709 r->content.length = php_http_message_body_size(body);
710
711 if (SUCCESS != r->ops->set_header(r, "Accept-Ranges: bytes")) {
712 return FAILURE;
713 } else {
714 zend_hash_init(&r->range.values, 0, NULL, ZVAL_PTR_DTOR, 0);
715 r->range.status = php_http_env_get_request_ranges(&r->range.values, r->content.length, request TSRMLS_CC);
716
717 switch (r->range.status) {
718 case PHP_HTTP_RANGE_NO:
719 zend_hash_destroy(&r->range.values);
720 break;
721
722 case PHP_HTTP_RANGE_ERR:
723 if (php_http_env_got_request_header(ZEND_STRL("If-Range"), request TSRMLS_CC)) {
724 r->range.status = PHP_HTTP_RANGE_NO;
725 zend_hash_destroy(&r->range.values);
726 } else {
727 r->done = 1;
728 zend_hash_destroy(&r->range.values);
729 if (SUCCESS != r->ops->set_status(r, 416)) {
730 return FAILURE;
731 }
732 if (SUCCESS != r->ops->set_header(r, "Content-Range: bytes */%zu", r->content.length)) {
733 return FAILURE;
734 }
735 }
736 break;
737
738 case PHP_HTTP_RANGE_OK:
739 if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Range"), request TSRMLS_CC)
740 || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Range"), request TSRMLS_CC)
741 ) {
742 r->range.status = PHP_HTTP_RANGE_NO;
743 zend_hash_destroy(&r->range.values);
744 break;
745 }
746 if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Match"), request TSRMLS_CC)
747 || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Unmodified-Since"), request TSRMLS_CC)
748 || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("Unless-Modified-Since"), request TSRMLS_CC)
749 ) {
750 r->done = 1;
751 zend_hash_destroy(&r->range.values);
752 if (SUCCESS != r->ops->set_status(r, 412)) {
753 return FAILURE;
754 }
755 break;
756 }
757
758 break;
759 }
760 }
761 }
762
763 if (SUCCESS != php_http_env_response_send_head(r, request)) {
764 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to send response headers");
765 return FAILURE;
766 }
767
768 if (SUCCESS != php_http_env_response_send_body(r)) {
769 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to send response body");
770 return FAILURE;
771 }
772
773 if (SUCCESS != r->ops->finish(r)) {
774 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to finish response");
775 return FAILURE;
776 }
777
778 return SUCCESS;
779 }
780
781 static long php_http_env_response_sapi_get_status(php_http_env_response_t *r)
782 {
783 TSRMLS_FETCH_FROM_CTX(r->ts);
784
785 return php_http_env_get_response_code(TSRMLS_C);
786 }
787 static STATUS php_http_env_response_sapi_set_status(php_http_env_response_t *r, long http_code)
788 {
789 TSRMLS_FETCH_FROM_CTX(r->ts);
790
791 return php_http_env_set_response_code(http_code TSRMLS_CC);
792 }
793 static STATUS php_http_env_response_sapi_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v)
794 {
795 TSRMLS_FETCH_FROM_CTX(r->ts);
796
797 return php_http_env_set_response_protocol_version(v TSRMLS_CC);
798 }
799 static STATUS php_http_env_response_sapi_set_header(php_http_env_response_t *r, const char *fmt, ...)
800 {
801 STATUS ret;
802 va_list args;
803 TSRMLS_FETCH_FROM_CTX(r->ts);
804
805 va_start(args, fmt);
806 ret = php_http_env_set_response_header_va(0, 1, fmt, args TSRMLS_CC);
807 va_end(args);
808
809 return ret;
810 }
811 static STATUS php_http_env_response_sapi_add_header(php_http_env_response_t *r, const char *fmt, ...)
812 {
813 STATUS ret;
814 va_list args;
815 TSRMLS_FETCH_FROM_CTX(r->ts);
816
817 va_start(args, fmt);
818 ret = php_http_env_set_response_header_va(0, 0, fmt, args TSRMLS_CC);
819 va_end(args);
820
821 return ret;
822 }
823 static STATUS php_http_env_response_sapi_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len)
824 {
825 TSRMLS_FETCH_FROM_CTX(r->ts);
826
827 return php_http_env_set_response_header_value(0, header_str, header_len, NULL, 1 TSRMLS_CC);
828 }
829 static STATUS php_http_env_response_sapi_write(php_http_env_response_t *r, const char *data_str, size_t data_len)
830 {
831 TSRMLS_FETCH_FROM_CTX(r->ts);
832
833 if (0 < PHPWRITE(data_str, data_len)) {
834 return SUCCESS;
835 }
836 return FAILURE;
837 }
838 static STATUS php_http_env_response_sapi_flush(php_http_env_response_t *r)
839 {
840 TSRMLS_FETCH_FROM_CTX(r->ts);
841
842 #if PHP_VERSION_ID >= 50400
843 if (php_output_get_level(TSRMLS_C)) {
844 php_output_flush_all(TSRMLS_C);
845 }
846 if (!(php_output_get_status(TSRMLS_C) & PHP_OUTPUT_IMPLICITFLUSH)) {
847 sapi_flush(TSRMLS_C);
848 }
849 #else
850 php_end_ob_buffer(1, 1 TSRMLS_CC);
851 sapi_flush(TSRMLS_C);
852 #endif
853
854 return SUCCESS;
855 }
856 static STATUS php_http_env_response_sapi_finish(php_http_env_response_t *r)
857 {
858 return SUCCESS;
859 }
860
861 static php_http_env_response_ops_t php_http_env_response_sapi_ops = {
862 NULL,
863 NULL,
864 php_http_env_response_sapi_get_status,
865 php_http_env_response_sapi_set_status,
866 php_http_env_response_sapi_set_protocol_version,
867 php_http_env_response_sapi_set_header,
868 php_http_env_response_sapi_add_header,
869 php_http_env_response_sapi_del_header,
870 php_http_env_response_sapi_write,
871 php_http_env_response_sapi_flush,
872 php_http_env_response_sapi_finish
873 };
874
875 php_http_env_response_ops_t *php_http_env_response_get_sapi_ops(void)
876 {
877 return &php_http_env_response_sapi_ops;
878 }
879
880 typedef struct php_http_env_response_stream_ctx {
881 HashTable header;
882 php_http_version_t version;
883 long status_code;
884
885 php_stream *stream;
886
887 unsigned started:1;
888 unsigned finished:1;
889 unsigned chunked:1;
890 } php_http_env_response_stream_ctx_t;
891
892 static STATUS php_http_env_response_stream_init(php_http_env_response_t *r, void *init_arg)
893 {
894 php_http_env_response_stream_ctx_t *ctx;
895 TSRMLS_FETCH_FROM_CTX(r->ts);
896
897 ctx = ecalloc(1, sizeof(*ctx));
898
899 ctx->stream = init_arg;
900 if (SUCCESS != zend_list_addref(ctx->stream->rsrc_id)) {
901 efree(ctx);
902 return FAILURE;
903 }
904 zend_hash_init(&ctx->header, 0, NULL, ZVAL_PTR_DTOR, 0);
905 php_http_version_init(&ctx->version, 1, 1 TSRMLS_CC);
906 ctx->status_code = 200;
907 ctx->chunked = 1;
908
909 r->ctx = ctx;
910
911 return SUCCESS;
912 }
913 static void php_http_env_response_stream_dtor(php_http_env_response_t *r)
914 {
915 php_http_env_response_stream_ctx_t *ctx = r->ctx;
916 TSRMLS_FETCH_FROM_CTX(r->ts);
917
918 zend_hash_destroy(&ctx->header);
919 zend_list_delete(ctx->stream->rsrc_id);
920 efree(ctx);
921 r->ctx = NULL;
922 }
923 static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t *ctx, HashTable *header TSRMLS_DC)
924 {
925 HashPosition pos;
926 zval **val;
927
928 FOREACH_HASH_VAL(pos, header, val) {
929 if (Z_TYPE_PP(val) == IS_ARRAY) {
930 php_http_env_response_stream_header(ctx, Z_ARRVAL_PP(val) TSRMLS_CC);
931 } else {
932 zval *tmp = php_http_ztyp(IS_STRING, *val);
933
934 if (ctx->chunked) {
935 /* disable chunked transfer encoding if we've got an explicit content-length */
936 if (!strncasecmp(Z_STRVAL_P(tmp), "Content-Length:", lenof("Content-Length:"))) {
937 ctx->chunked = 0;
938 }
939 }
940 php_stream_write(ctx->stream, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
941 php_stream_write_string(ctx->stream, PHP_HTTP_CRLF);
942 zval_ptr_dtor(&tmp);
943 }
944 }
945 }
946 static STATUS php_http_env_response_stream_start(php_http_env_response_stream_ctx_t *ctx TSRMLS_DC)
947 {
948 if (ctx->started || ctx->finished) {
949 return FAILURE;
950 }
951
952 php_stream_printf(ctx->stream TSRMLS_CC, "HTTP/%u.%u %ld %s" PHP_HTTP_CRLF, ctx->version.major, ctx->version.minor, ctx->status_code, php_http_env_get_response_status_for_code(ctx->status_code));
953 php_http_env_response_stream_header(ctx, &ctx->header TSRMLS_CC);
954 /* enable chunked transfer encoding */
955 if (ctx->chunked) {
956 php_stream_write_string(ctx->stream, "Transfer-Encoding: chunked" PHP_HTTP_CRLF PHP_HTTP_CRLF);
957 } else {
958 php_stream_write_string(ctx->stream, PHP_HTTP_CRLF);
959 }
960 ctx->started = 1;
961 return SUCCESS;
962 }
963 static long php_http_env_response_stream_get_status(php_http_env_response_t *r)
964 {
965 php_http_env_response_stream_ctx_t *ctx = r->ctx;
966
967 return ctx->status_code;
968 }
969 static STATUS php_http_env_response_stream_set_status(php_http_env_response_t *r, long http_code)
970 {
971 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
972
973 if (stream_ctx->started || stream_ctx->finished) {
974 return FAILURE;
975 }
976
977 stream_ctx->status_code = http_code;
978
979 return SUCCESS;
980 }
981 static STATUS php_http_env_response_stream_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v)
982 {
983 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
984
985 if (stream_ctx->started || stream_ctx->finished) {
986 return FAILURE;
987 }
988
989 memcpy(&stream_ctx->version, v, sizeof(stream_ctx->version));
990
991 return SUCCESS;
992 }
993 static STATUS php_http_env_response_stream_set_header_ex(php_http_env_response_t *r, zend_bool replace, const char *fmt, va_list argv)
994 {
995 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
996 char *header_end, *header_str = NULL;
997 size_t header_len = 0;
998 zval *zheader, **zheader_ptr;
999
1000 if (stream_ctx->started || stream_ctx->finished) {
1001 return FAILURE;
1002 }
1003
1004 header_len = vspprintf(&header_str, 0, fmt, argv);
1005
1006 if (!(header_end = strchr(header_str, ':'))) {
1007 efree(header_str);
1008 return FAILURE;
1009 }
1010
1011 *header_end = '\0';
1012
1013 if (!replace && (SUCCESS == zend_hash_find(&stream_ctx->header, header_str, header_end - header_str + 1, (void *) &zheader_ptr))) {
1014 convert_to_array(*zheader_ptr);
1015 *header_end = ':';
1016 return add_next_index_stringl(*zheader_ptr, header_str, header_len, 0);
1017 } else {
1018 MAKE_STD_ZVAL(zheader);
1019 ZVAL_STRINGL(zheader, header_str, header_len, 0);
1020
1021 if (SUCCESS != zend_hash_update(&stream_ctx->header, header_str, header_end - header_str + 1, (void *) &zheader, sizeof(zval *), NULL)) {
1022 zval_ptr_dtor(&zheader);
1023 return FAILURE;
1024 }
1025
1026 *header_end = ':';
1027 return SUCCESS;
1028 }
1029 }
1030 static STATUS php_http_env_response_stream_set_header(php_http_env_response_t *r, const char *fmt, ...)
1031 {
1032 STATUS ret;
1033 va_list argv;
1034
1035 va_start(argv, fmt);
1036 ret = php_http_env_response_stream_set_header_ex(r, 1, fmt, argv);
1037 va_end(argv);
1038
1039 return ret;
1040 }
1041 static STATUS php_http_env_response_stream_add_header(php_http_env_response_t *r, const char *fmt, ...)
1042 {
1043 STATUS ret;
1044 va_list argv;
1045
1046 va_start(argv, fmt);
1047 ret = php_http_env_response_stream_set_header_ex(r, 0, fmt, argv);
1048 va_end(argv);
1049
1050 return ret;
1051 }
1052 static STATUS php_http_env_response_stream_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len)
1053 {
1054 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
1055
1056 if (stream_ctx->started || stream_ctx->finished) {
1057 return FAILURE;
1058 }
1059
1060 zend_hash_del(&stream_ctx->header, header_str, header_len + 1);
1061 return SUCCESS;
1062 }
1063 static STATUS php_http_env_response_stream_write(php_http_env_response_t *r, const char *data_str, size_t data_len)
1064 {
1065 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
1066 TSRMLS_FETCH_FROM_CTX(r->ts);
1067
1068 if (stream_ctx->finished) {
1069 return FAILURE;
1070 }
1071 if (!stream_ctx->started) {
1072 if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) {
1073 return FAILURE;
1074 }
1075 }
1076
1077 if (stream_ctx->chunked && 0 == php_stream_printf(stream_ctx->stream TSRMLS_CC, "%lx" PHP_HTTP_CRLF, (unsigned long) data_len)) {
1078 return FAILURE;
1079 }
1080
1081 if (data_len != php_stream_write(stream_ctx->stream, data_str, data_len)) {
1082 return FAILURE;
1083 }
1084
1085 if (stream_ctx->chunked && 2 != php_stream_write_string(stream_ctx->stream, PHP_HTTP_CRLF)) {
1086 return FAILURE;
1087 }
1088
1089 return SUCCESS;
1090 }
1091 static STATUS php_http_env_response_stream_flush(php_http_env_response_t *r)
1092 {
1093 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
1094 TSRMLS_FETCH_FROM_CTX(r->ts);
1095
1096 if (stream_ctx->finished) {
1097 return FAILURE;
1098 }
1099 if (!stream_ctx->started) {
1100 if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) {
1101 return FAILURE;
1102 }
1103 }
1104
1105 return php_stream_flush(stream_ctx->stream);
1106 }
1107 static STATUS php_http_env_response_stream_finish(php_http_env_response_t *r)
1108 {
1109 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
1110 TSRMLS_FETCH_FROM_CTX(r->ts);
1111
1112 if (stream_ctx->finished) {
1113 return FAILURE;
1114 }
1115 if (!stream_ctx->started) {
1116 if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) {
1117 return FAILURE;
1118 }
1119 }
1120
1121 if (stream_ctx->chunked && 5 != php_stream_write_string(stream_ctx->stream, "0" PHP_HTTP_CRLF PHP_HTTP_CRLF)) {
1122 return FAILURE;
1123 }
1124
1125 stream_ctx->finished = 1;
1126
1127 return SUCCESS;
1128 }
1129
1130 static php_http_env_response_ops_t php_http_env_response_stream_ops = {
1131 php_http_env_response_stream_init,
1132 php_http_env_response_stream_dtor,
1133 php_http_env_response_stream_get_status,
1134 php_http_env_response_stream_set_status,
1135 php_http_env_response_stream_set_protocol_version,
1136 php_http_env_response_stream_set_header,
1137 php_http_env_response_stream_add_header,
1138 php_http_env_response_stream_del_header,
1139 php_http_env_response_stream_write,
1140 php_http_env_response_stream_flush,
1141 php_http_env_response_stream_finish
1142 };
1143
1144 php_http_env_response_ops_t *php_http_env_response_get_stream_ops(void)
1145 {
1146 return &php_http_env_response_stream_ops;
1147 }
1148
1149 #define PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj) \
1150 do { \
1151 if (!obj->message) { \
1152 obj->message = php_http_message_init_env(NULL, PHP_HTTP_RESPONSE TSRMLS_CC); \
1153 } \
1154 } while (0)
1155
1156 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___construct, 0, 0, 0)
1157 ZEND_END_ARG_INFO();
1158 static PHP_METHOD(HttpEnvResponse, __construct)
1159 {
1160 php_http_message_object_t *obj;
1161
1162 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1163
1164 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1165
1166 php_http_expect(obj->message = php_http_message_init_env(obj->message, PHP_HTTP_RESPONSE TSRMLS_CC), unexpected_val, return);
1167 }
1168
1169 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___invoke, 0, 0, 1)
1170 ZEND_ARG_INFO(0, ob_string)
1171 ZEND_ARG_INFO(0, ob_flags)
1172 ZEND_END_ARG_INFO();
1173 static PHP_METHOD(HttpEnvResponse, __invoke)
1174 {
1175 char *ob_str;
1176 int ob_len;
1177 long ob_flags = 0;
1178
1179 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &ob_str, &ob_len, &ob_flags)) {
1180 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1181
1182 PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj);
1183
1184 php_http_message_object_init_body_object(obj);
1185 php_http_message_body_append(obj->message->body, ob_str, ob_len);
1186 #if PHP_VERSION_ID >= 50400
1187 RETURN_TRUE;
1188 #else
1189 RETURN_EMPTY_STRING();
1190 #endif
1191 }
1192 }
1193
1194 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEnvRequest, 0, 0, 1)
1195 ZEND_ARG_OBJ_INFO(0, env_request, http\\Message, 1)
1196 ZEND_END_ARG_INFO();
1197 static PHP_METHOD(HttpEnvResponse, setEnvRequest)
1198 {
1199 zval *env_req = NULL;
1200
1201 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &env_req, php_http_message_class_entry), invalid_arg, return);
1202
1203 set_option(getThis(), ZEND_STRL("request"), IS_OBJECT, env_req, 0 TSRMLS_CC);
1204 RETVAL_ZVAL(getThis(), 1, 0);
1205 }
1206
1207 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentType, 0, 0, 1)
1208 ZEND_ARG_INFO(0, content_type)
1209 ZEND_END_ARG_INFO();
1210 static PHP_METHOD(HttpEnvResponse, setContentType)
1211 {
1212 char *ct_str = NULL;
1213 int ct_len = 0;
1214
1215 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &ct_str, &ct_len), invalid_arg, return);
1216
1217 set_option(getThis(), ZEND_STRL("contentType"), IS_STRING, ct_str, ct_len TSRMLS_CC);
1218 RETVAL_ZVAL(getThis(), 1, 0);
1219 }
1220
1221 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentDisposition, 0, 0, 1)
1222 ZEND_ARG_ARRAY_INFO(0, disposition_params, 1)
1223 ZEND_END_ARG_INFO();
1224 static PHP_METHOD(HttpEnvResponse, setContentDisposition)
1225 {
1226 zval *zdisposition;
1227
1228 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &zdisposition), invalid_arg, return);
1229
1230 zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), zdisposition TSRMLS_CC);
1231 RETVAL_ZVAL(getThis(), 1, 0);
1232 }
1233
1234 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentEncoding, 0, 0, 1)
1235 ZEND_ARG_INFO(0, content_encoding)
1236 ZEND_END_ARG_INFO();
1237 static PHP_METHOD(HttpEnvResponse, setContentEncoding)
1238 {
1239 long ce;
1240
1241 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ce), invalid_arg, return);
1242
1243 set_option(getThis(), ZEND_STRL("contentEncoding"), IS_LONG, &ce, 0 TSRMLS_CC);
1244 RETVAL_ZVAL(getThis(), 1, 0);
1245 }
1246
1247 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCacheControl, 0, 0, 1)
1248 ZEND_ARG_INFO(0, cache_control)
1249 ZEND_END_ARG_INFO();
1250 static PHP_METHOD(HttpEnvResponse, setCacheControl)
1251 {
1252 char *cc_str = NULL;
1253 int cc_len = 0;
1254
1255 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &cc_str, &cc_len), invalid_arg, return);
1256
1257 set_option(getThis(), ZEND_STRL("cacheControl"), IS_STRING, cc_str, cc_len TSRMLS_CC);
1258 RETVAL_ZVAL(getThis(), 1, 0);
1259 }
1260
1261 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setLastModified, 0, 0, 1)
1262 ZEND_ARG_INFO(0, last_modified)
1263 ZEND_END_ARG_INFO();
1264 static PHP_METHOD(HttpEnvResponse, setLastModified)
1265 {
1266 long last_modified;
1267
1268 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &last_modified), invalid_arg, return);
1269
1270 set_option(getThis(), ZEND_STRL("lastModified"), IS_LONG, &last_modified, 0 TSRMLS_CC);
1271 RETVAL_ZVAL(getThis(), 1, 0);
1272 }
1273
1274 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByLastModified, 0, 0, 0)
1275 ZEND_ARG_INFO(0, header_name)
1276 ZEND_END_ARG_INFO();
1277 static PHP_METHOD(HttpEnvResponse, isCachedByLastModified)
1278 {
1279 char *header_name_str = NULL;
1280 int header_name_len = 0;
1281
1282 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
1283 if (!header_name_str || !header_name_len) {
1284 header_name_str = "If-Modified-Since";
1285 header_name_len = lenof("If-Modified-Since");
1286 }
1287
1288 RETURN_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str, header_name_len, get_request(getThis() TSRMLS_CC) TSRMLS_CC));
1289 }
1290 }
1291
1292 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEtag, 0, 0, 1)
1293 ZEND_ARG_INFO(0, etag)
1294 ZEND_END_ARG_INFO();
1295 static PHP_METHOD(HttpEnvResponse, setEtag)
1296 {
1297 char *etag_str = NULL;
1298 int etag_len = 0;
1299
1300 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &etag_str, &etag_len), invalid_arg, return);
1301
1302 set_option(getThis(), ZEND_STRL("etag"), IS_STRING, etag_str, etag_len TSRMLS_CC);
1303 RETVAL_ZVAL(getThis(), 1, 0);
1304 }
1305
1306 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByEtag, 0, 0, 0)
1307 ZEND_ARG_INFO(0, header_name)
1308 ZEND_END_ARG_INFO();
1309 static PHP_METHOD(HttpEnvResponse, isCachedByEtag)
1310 {
1311 char *header_name_str = NULL;
1312 int header_name_len = 0;
1313
1314 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
1315 if (!header_name_str || !header_name_len) {
1316 header_name_str = "If-None-Match";
1317 header_name_len = lenof("If-None-Match");
1318 }
1319 RETURN_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str, header_name_len, get_request(getThis() TSRMLS_CC) TSRMLS_CC));
1320 }
1321 }
1322
1323 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setThrottleRate, 0, 0, 1)
1324 ZEND_ARG_INFO(0, chunk_size)
1325 ZEND_ARG_INFO(0, delay)
1326 ZEND_END_ARG_INFO();
1327 static PHP_METHOD(HttpEnvResponse, setThrottleRate)
1328 {
1329 long chunk_size;
1330 double delay = 1;
1331
1332 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|d", &chunk_size, &delay), invalid_arg, return);
1333
1334 set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE, &delay, 0 TSRMLS_CC);
1335 set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG, &chunk_size, 0 TSRMLS_CC);
1336 RETVAL_ZVAL(getThis(), 1, 0);
1337 }
1338
1339 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCookie, 0, 0, 1)
1340 ZEND_ARG_INFO(0, cookie)
1341 ZEND_END_ARG_INFO();
1342 static PHP_METHOD(HttpEnvResponse, setCookie)
1343 {
1344 zval *zcookie_new;
1345 zend_error_handling zeh;
1346 php_http_cookie_list_t *list = NULL;
1347
1348 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zcookie_new), invalid_arg, return);
1349
1350 zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh TSRMLS_CC);
1351 switch (Z_TYPE_P(zcookie_new)) {
1352 case IS_OBJECT:
1353 if (instanceof_function(Z_OBJCE_P(zcookie_new), php_http_cookie_class_entry TSRMLS_CC)) {
1354 Z_ADDREF_P(zcookie_new);
1355 break;
1356 }
1357 /* no break */
1358 case IS_ARRAY:
1359 list = php_http_cookie_list_from_struct(NULL, zcookie_new TSRMLS_CC);
1360 MAKE_STD_ZVAL(zcookie_new);
1361 ZVAL_OBJVAL(zcookie_new, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0);
1362 break;
1363
1364 default:
1365 zcookie_new = php_http_ztyp(IS_STRING, zcookie_new);
1366 list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(zcookie_new), Z_STRLEN_P(zcookie_new), 0, NULL TSRMLS_CC);
1367 zval_ptr_dtor(&zcookie_new);
1368 MAKE_STD_ZVAL(zcookie_new);
1369 ZVAL_OBJVAL(zcookie_new, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0);
1370 }
1371 zend_restore_error_handling(&zeh TSRMLS_CC);
1372
1373 set_cookie(getThis(), zcookie_new TSRMLS_CC);
1374 zval_ptr_dtor(&zcookie_new);
1375
1376 RETVAL_ZVAL(getThis(), 1, 0);
1377 }
1378
1379 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_send, 0, 0, 0)
1380 ZEND_ARG_INFO(0, stream)
1381 ZEND_END_ARG_INFO();
1382 static PHP_METHOD(HttpEnvResponse, send)
1383 {
1384 zval *zstream = NULL;
1385 php_stream *s = NULL;
1386
1387 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &zstream)) {
1388 /* first flush the output layer to avoid conflicting headers and output;
1389 * also, ob_start($thisEnvResponse) might have been called */
1390 #if PHP_VERSION_ID >= 50400
1391 php_output_end_all(TSRMLS_C);
1392 #else
1393 php_end_ob_buffers(1 TSRMLS_CC);
1394 #endif
1395
1396 if (zstream) {
1397 php_http_env_response_t *r;
1398
1399 php_stream_from_zval(s, &zstream);
1400 r = php_http_env_response_init(NULL, getThis(), php_http_env_response_get_stream_ops(), s TSRMLS_CC);
1401 if (!r) {
1402 RETURN_FALSE;
1403 }
1404
1405 RETVAL_BOOL(SUCCESS == php_http_env_response_send(r));
1406 php_http_env_response_free(&r);
1407 } else {
1408 php_http_env_response_t r;
1409
1410 if (!php_http_env_response_init(&r, getThis(), NULL, NULL TSRMLS_CC)) {
1411 RETURN_FALSE;
1412 }
1413
1414 RETVAL_BOOL(SUCCESS == php_http_env_response_send(&r));
1415 php_http_env_response_dtor(&r);
1416 }
1417 }
1418 }
1419
1420 static zend_function_entry php_http_env_response_methods[] = {
1421 PHP_ME(HttpEnvResponse, __construct, ai_HttpEnvResponse___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1422 PHP_ME(HttpEnvResponse, __invoke, ai_HttpEnvResponse___invoke, ZEND_ACC_PUBLIC)
1423 PHP_ME(HttpEnvResponse, setEnvRequest, ai_HttpEnvResponse_setEnvRequest, ZEND_ACC_PUBLIC)
1424 PHP_ME(HttpEnvResponse, setCookie, ai_HttpEnvResponse_setCookie, ZEND_ACC_PUBLIC)
1425 PHP_ME(HttpEnvResponse, setContentType, ai_HttpEnvResponse_setContentType, ZEND_ACC_PUBLIC)
1426 PHP_ME(HttpEnvResponse, setContentDisposition, ai_HttpEnvResponse_setContentDisposition, ZEND_ACC_PUBLIC)
1427 PHP_ME(HttpEnvResponse, setContentEncoding, ai_HttpEnvResponse_setContentEncoding, ZEND_ACC_PUBLIC)
1428 PHP_ME(HttpEnvResponse, setCacheControl, ai_HttpEnvResponse_setCacheControl, ZEND_ACC_PUBLIC)
1429 PHP_ME(HttpEnvResponse, setLastModified, ai_HttpEnvResponse_setLastModified, ZEND_ACC_PUBLIC)
1430 PHP_ME(HttpEnvResponse, isCachedByLastModified, ai_HttpEnvResponse_isCachedByLastModified, ZEND_ACC_PUBLIC)
1431 PHP_ME(HttpEnvResponse, setEtag, ai_HttpEnvResponse_setEtag, ZEND_ACC_PUBLIC)
1432 PHP_ME(HttpEnvResponse, isCachedByEtag, ai_HttpEnvResponse_isCachedByEtag, ZEND_ACC_PUBLIC)
1433 PHP_ME(HttpEnvResponse, setThrottleRate, ai_HttpEnvResponse_setThrottleRate, ZEND_ACC_PUBLIC)
1434 PHP_ME(HttpEnvResponse, send, ai_HttpEnvResponse_send, ZEND_ACC_PUBLIC)
1435 EMPTY_FUNCTION_ENTRY
1436 };
1437
1438 zend_class_entry *php_http_env_response_class_entry;
1439
1440 PHP_MINIT_FUNCTION(http_env_response)
1441 {
1442 zend_class_entry ce = {0};
1443
1444 INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Response", php_http_env_response_methods);
1445 php_http_env_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL TSRMLS_CC);
1446
1447 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_NONE"), PHP_HTTP_CONTENT_ENCODING_NONE TSRMLS_CC);
1448 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_GZIP"), PHP_HTTP_CONTENT_ENCODING_GZIP TSRMLS_CC);
1449
1450 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_NO"), PHP_HTTP_CACHE_NO TSRMLS_CC);
1451 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_HIT"), PHP_HTTP_CACHE_HIT TSRMLS_CC);
1452 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_MISS"), PHP_HTTP_CACHE_MISS TSRMLS_CC);
1453
1454 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("request"), ZEND_ACC_PROTECTED TSRMLS_CC);
1455 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cookies"), ZEND_ACC_PROTECTED TSRMLS_CC);
1456 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentType"), ZEND_ACC_PROTECTED TSRMLS_CC);
1457 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentDisposition"), ZEND_ACC_PROTECTED TSRMLS_CC);
1458 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentEncoding"), ZEND_ACC_PROTECTED TSRMLS_CC);
1459 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cacheControl"), ZEND_ACC_PROTECTED TSRMLS_CC);
1460 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("etag"), ZEND_ACC_PROTECTED TSRMLS_CC);
1461 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("lastModified"), ZEND_ACC_PROTECTED TSRMLS_CC);
1462 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleDelay"), ZEND_ACC_PROTECTED TSRMLS_CC);
1463 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleChunk"), ZEND_ACC_PROTECTED TSRMLS_CC);
1464
1465 return SUCCESS;
1466 }
1467
1468
1469 /*
1470 * Local variables:
1471 * tab-width: 4
1472 * c-basic-offset: 4
1473 * End:
1474 * vim600: noet sw=4 ts=4 fdm=marker
1475 * vim<600: noet sw=4 ts=4
1476 */