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