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