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