fix cookies
[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)
64 {
65 zval *val;
66
67 if (Z_TYPE_P(options) == IS_OBJECT) {
68 val = zend_read_property(Z_OBJCE_P(options), options, name_str, name_len, 0);
69 } else {
70 val = zend_symtable_str_find(Z_ARRVAL_P(options), name_str, name_len);
71 }
72 if (val) {
73 Z_TRY_ADDREF_P(val);
74 }
75 return val;
76 }
77 static php_http_message_body_t *get_body(zval *options)
78 {
79 zval *zbody;
80 php_http_message_body_t *body = NULL;
81
82 if ((zbody = get_option(options, ZEND_STRL("body")))) {
83 if ((Z_TYPE_P(zbody) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry)) {
84 php_http_message_body_object_t *body_obj = PHP_HTTP_OBJ(NULL, zbody);
85
86 body = body_obj->body;
87 }
88 zval_ptr_dtor(zbody);
89 }
90
91 return body;
92 }
93 static php_http_message_t *get_request(zval *options)
94 {
95 zval *zrequest;
96 php_http_message_t *request = NULL;
97
98 if ((zrequest = get_option(options, ZEND_STRL("request")))) {
99 if (Z_TYPE_P(zrequest) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zrequest), php_http_message_class_entry)) {
100 php_http_message_object_t *request_obj = PHP_HTTP_OBJ(NULL, zrequest);
101
102 request = request_obj->message;
103 }
104 zval_ptr_dtor(zrequest);
105 }
106
107 return request;
108 }
109 static void set_cookie(zval *options, zval *zcookie_new TSRMLS_DC)
110 {
111 zval *zcookies_set;
112 php_http_arrkey_t key;
113 php_http_cookie_object_t *obj = PHP_HTTP_OBJ(NULL, zcookie_new);
114
115 zcookies_set = get_option(options, ZEND_STRL("cookies"));
116 if (!zcookies_set || Z_TYPE_P(zcookies_set) != IS_ARRAY) {
117 if (zcookies_set) {
118 zval_ptr_dtor(zcookies_set);
119 }
120 array_init_size(zcookies_set, zend_hash_num_elements(&obj->list->cookies));
121 } else {
122 Z_ADDREF_P(zcookies_set);
123 SEPARATE_ZVAL(zcookies_set);
124 }
125
126 ZEND_HASH_FOREACH_KEY(&obj->list->cookies, key.h, key.key)
127 {
128 Z_ADDREF_P(zcookie_new);
129 if (key.key) {
130 add_assoc_zval_ex(zcookies_set, key.key->val, key.key->len, zcookie_new);
131 } else {
132 add_index_zval(zcookies_set, key.h, zcookie_new);
133 }
134 }
135 ZEND_HASH_FOREACH_END();
136
137 set_option(options, ZEND_STRL("cookies"), IS_ARRAY, zcookies_set, 0);
138 zval_ptr_dtor(zcookies_set);
139 }
140
141 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)
142 {
143 php_http_cache_status_t ret = PHP_HTTP_CACHE_NO;
144 char *header = NULL, *etag = NULL;
145 php_http_message_body_t *body;
146 zval *zetag;
147
148
149 if (!(body = get_body(options))) {
150 return ret;
151 }
152
153 if ((zetag = get_option(options, ZEND_STRL("etag")))) {
154 zend_string *zs = zval_get_string(zetag);
155 etag = estrndup(zs->val, zs->len);
156 zend_string_release(zs);
157 zval_ptr_dtor(zetag);
158 }
159
160 if (!etag && (etag = php_http_message_body_etag(body))) {
161 set_option(options, ZEND_STRL("etag"), IS_STRING, etag, strlen(etag));
162 }
163
164 if (etag && (header = php_http_env_get_request_header(header_str, header_len, NULL, request))) {
165 ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD) ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS;
166 }
167
168 PTR_FREE(etag);
169 PTR_FREE(header);
170
171 return ret;
172 }
173
174 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)
175 {
176 php_http_cache_status_t ret = PHP_HTTP_CACHE_NO;
177 char *header;
178 time_t ums, lm = 0;
179 php_http_message_body_t *body;
180 zval *zlm;
181
182 if (!(body = get_body(options))) {
183 return ret;
184 }
185
186 if ((zlm = get_option(options, ZEND_STRL("lastModified")))) {
187 lm = zval_get_long(zlm);
188 zval_ptr_dtor(zlm);
189 }
190
191 if (lm <= 0) {
192 lm = php_http_message_body_mtime(body);
193 set_option(options, ZEND_STRL("lastModified"), IS_LONG, &lm, 0);
194 }
195
196 if ((header = php_http_env_get_request_header(header_str, header_len, NULL, request))) {
197 ums = php_parse_date(header, NULL);
198
199 if (ums > 0 && ums >= lm) {
200 ret = PHP_HTTP_CACHE_HIT;
201 } else {
202 ret = PHP_HTTP_CACHE_MISS;
203 }
204 }
205
206 PTR_FREE(header);
207 return ret;
208 }
209
210 static zend_bool php_http_env_response_is_cacheable(php_http_env_response_t *r, php_http_message_t *request)
211 {
212 if (r->ops->get_status(r) >= 400) {
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, *options = &r->options;
337
338 if (r->done) {
339 return ret;
340 }
341
342 if ((zoption = get_option(options, ZEND_STRL("headers")))) {
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")))) {
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")))) {
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")))) {
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")))) {
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")))) {
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")))) {
463 php_http_buffer_t buf;
464
465 if (Z_TYPE_P(zoption) != IS_ARRAY) {
466 zval *tmp = zoption;
467 SEPARATE_ZVAL(tmp);
468 convert_to_array(tmp);
469 zoption = tmp;
470 }
471
472 php_http_buffer_init(&buf);
473 if (php_http_params_to_string(&buf, Z_ARRVAL_P(zoption), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT)) {
474 if (buf.used) {
475 ret = r->ops->set_header(r, "Content-Disposition: %.*s", buf.used, buf.data);
476 }
477 }
478
479 php_http_buffer_dtor(&buf);
480 zval_ptr_dtor(zoption);
481 }
482
483 if (ret != SUCCESS) {
484 return ret;
485 }
486
487 if ((zoption = get_option(options, ZEND_STRL("contentEncoding")))) {
488 zend_long ce = zval_get_long(zoption);
489 zval zsupported;
490 HashTable *result = NULL;
491
492 zval_ptr_dtor(zoption);
493 switch (ce) {
494 case PHP_HTTP_CONTENT_ENCODING_GZIP:
495 array_init(&zsupported);
496 add_next_index_stringl(&zsupported, ZEND_STRL("none"));
497 add_next_index_stringl(&zsupported, ZEND_STRL("gzip"));
498 add_next_index_stringl(&zsupported, ZEND_STRL("deflate"));
499
500 if ((result = php_http_negotiate_encoding(Z_ARRVAL(zsupported), request))) {
501 zend_string *key_str = NULL;
502 zend_ulong index = 0;
503
504 zend_hash_internal_pointer_reset(result);
505 if (HASH_KEY_IS_STRING == zend_hash_get_current_key(result, &key_str, &index)) {
506 if (zend_string_equals_literal(key_str, "gzip")) {
507 if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP))) {
508 ret = FAILURE;
509 } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: gzip"))) {
510 r->content.encoding = estrndup(key_str->val, key_str->len);
511 }
512 } else if (zend_string_equals_literal(key_str, "deflate")) {
513 if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_ZLIB))) {
514 ret = FAILURE;
515 } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: deflate"))) {
516 r->content.encoding = estrndup(key_str->val, key_str->len);
517 }
518 } else {
519 ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding"));
520 }
521
522 if (SUCCESS == ret) {
523 ret = r->ops->add_header(r, "Vary: Accept-Encoding");
524 }
525 }
526
527 zend_hash_destroy(result);
528 FREE_HASHTABLE(result);
529 }
530
531 zval_dtor(&zsupported);
532 break;
533
534 case PHP_HTTP_CONTENT_ENCODING_NONE:
535 default:
536 ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding"));
537 break;
538 }
539 }
540
541 if (SUCCESS != ret) {
542 return ret;
543 }
544
545 if (php_http_env_response_is_cacheable(r, request)) {
546 switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match"), request)) {
547 case PHP_HTTP_CACHE_MISS:
548 break;
549
550 case PHP_HTTP_CACHE_NO:
551 if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(options, ZEND_STRL("If-Modified-Since"), request)) {
552 break;
553 }
554 /* no break */
555
556 case PHP_HTTP_CACHE_HIT:
557 ret = r->ops->set_status(r, 304);
558 r->done = 1;
559 break;
560 }
561
562 if ((zoption = get_option(options, ZEND_STRL("etag")))) {
563 zend_string *zs = zval_get_string(zoption);
564
565 zval_ptr_dtor(zoption);
566 if (*zs->val != '"' && strncmp(zs->val, "W/\"", 3)) {
567 ret = r->ops->set_header(r, "ETag: \"%s\"", zs->val);
568 } else {
569 ret = r->ops->set_header(r, "ETag: %s", zs->val);
570 }
571 zend_string_release(zs);
572 }
573 if ((zoption = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) {
574 zend_long lm = zval_get_long(zoption);
575
576 zval_ptr_dtor(zoption);
577 if (lm) {
578 zend_string *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), lm, 0);
579 if (date) {
580 ret = r->ops->set_header(r, "Last-Modified: %s", date->val);
581 zend_string_release(date);
582 }
583 }
584 }
585 }
586 }
587
588 return ret;
589 }
590
591 static ZEND_RESULT_CODE php_http_env_response_send_body(php_http_env_response_t *r)
592 {
593 ZEND_RESULT_CODE ret = SUCCESS;
594 zval *zoption;
595 php_http_message_body_t *body;
596
597 if (r->done) {
598 return ret;
599 }
600
601 if ((body = get_body(&r->options))) {
602 if ((zoption = get_option(&r->options, ZEND_STRL("throttleDelay")))) {
603 r->throttle.delay = zval_get_double(zoption);
604 zval_ptr_dtor(zoption);
605 }
606 if ((zoption = get_option(&r->options, ZEND_STRL("throttleChunk") TSRMLS_CC))) {
607 r->throttle.chunk = zval_get_long(zoption);
608 zval_ptr_dtor(zoption);
609 }
610
611 if (r->range.status == PHP_HTTP_RANGE_OK) {
612 if (zend_hash_num_elements(&r->range.values) == 1) {
613 /* single range */
614 zval *range, *begin, *end;
615
616 if ( 1 == php_http_array_list(&r->range.values, 1, &range)
617 && 2 == php_http_array_list(Z_ARRVAL_P(range), 2, &begin, &end)
618 ) {
619 /* send chunk */
620 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);
621 if (ret == SUCCESS) {
622 ret = php_http_env_response_send_done(r);
623 }
624 zend_hash_destroy(&r->range.values);
625 } else {
626 /* this should never happen */
627 zend_hash_destroy(&r->range.values);
628 r->ops->set_status(r, 500);
629 ret = FAILURE;
630 }
631
632 } else {
633 /* send multipart/byte-ranges message */
634 zval *chunk;
635
636 ZEND_HASH_FOREACH_VAL(&r->range.values, chunk)
637 {
638 zval *begin, *end;
639
640 if (2 == php_http_array_list(Z_ARRVAL_P(chunk), 2, &begin, &end)) {
641 php_http_buffer_appendf(r->buffer,
642 PHP_HTTP_CRLF
643 "--%s" PHP_HTTP_CRLF
644 "Content-Type: %s" PHP_HTTP_CRLF
645 "Content-Range: bytes %ld-%ld/%zu" PHP_HTTP_CRLF PHP_HTTP_CRLF,
646 /* - */
647 r->range.boundary,
648 r->content.type ? r->content.type : "application/octet-stream",
649 Z_LVAL_P(begin),
650 Z_LVAL_P(end),
651 r->content.length
652 );
653 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);
654 }
655 }
656 ZEND_HASH_FOREACH_END();
657
658 if (ret == SUCCESS) {
659 php_http_buffer_appendf(r->buffer, PHP_HTTP_CRLF "--%s--", r->range.boundary);
660 ret = php_http_env_response_send_done(r);
661 }
662 zend_hash_destroy(&r->range.values);
663 }
664
665 } else {
666 ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, 0, 0);
667 if (ret == SUCCESS) {
668 ret = php_http_env_response_send_done(r);
669 }
670 }
671 }
672 return ret;
673 }
674
675 ZEND_RESULT_CODE php_http_env_response_send(php_http_env_response_t *r)
676 {
677 php_http_message_t *request;
678 php_http_message_body_t *body;
679
680 request = get_request(&r->options);
681
682 /* check for ranges */
683 if ((body = get_body(&r->options))) {
684 r->content.length = php_http_message_body_size(body);
685
686 if (SUCCESS != r->ops->set_header(r, "Accept-Ranges: bytes")) {
687 return FAILURE;
688 } else {
689 ZEND_INIT_SYMTABLE_EX(&r->range.values, 0, 0);
690 r->range.status = php_http_env_get_request_ranges(&r->range.values, r->content.length, request);
691
692 switch (r->range.status) {
693 case PHP_HTTP_RANGE_NO:
694 zend_hash_destroy(&r->range.values);
695 break;
696
697 case PHP_HTTP_RANGE_ERR:
698 if (php_http_env_got_request_header(ZEND_STRL("If-Range"), request)) {
699 r->range.status = PHP_HTTP_RANGE_NO;
700 zend_hash_destroy(&r->range.values);
701 } else {
702 r->done = 1;
703 zend_hash_destroy(&r->range.values);
704 if (SUCCESS != r->ops->set_status(r, 416)) {
705 return FAILURE;
706 }
707 if (SUCCESS != r->ops->set_header(r, "Content-Range: bytes */%zu", r->content.length)) {
708 return FAILURE;
709 }
710 }
711 break;
712
713 case PHP_HTTP_RANGE_OK:
714 if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(&r->options, ZEND_STRL("If-Range"), request)
715 || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(&r->options, ZEND_STRL("If-Range"), request)
716 ) {
717 r->range.status = PHP_HTTP_RANGE_NO;
718 zend_hash_destroy(&r->range.values);
719 break;
720 }
721 if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(&r->options, ZEND_STRL("If-Match"), request)
722 || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(&r->options, ZEND_STRL("If-Unmodified-Since"), request)
723 || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(&r->options, ZEND_STRL("Unless-Modified-Since"), request)
724 ) {
725 r->done = 1;
726 zend_hash_destroy(&r->range.values);
727 if (SUCCESS != r->ops->set_status(r, 412)) {
728 return FAILURE;
729 }
730 break;
731 }
732
733 break;
734 }
735 }
736 }
737
738 if (SUCCESS != php_http_env_response_send_head(r, request)) {
739 php_error_docref(NULL, E_WARNING, "Failed to send response headers");
740 return FAILURE;
741 }
742
743 if (SUCCESS != php_http_env_response_send_body(r)) {
744 php_error_docref(NULL, E_WARNING, "Failed to send response body");
745 return FAILURE;
746 }
747
748 if (SUCCESS != r->ops->finish(r)) {
749 php_error_docref(NULL, E_WARNING, "Failed to finish response");
750 return FAILURE;
751 }
752
753 return SUCCESS;
754 }
755
756 static long php_http_env_response_sapi_get_status(php_http_env_response_t *r)
757 {
758 return php_http_env_get_response_code();
759 }
760 static ZEND_RESULT_CODE php_http_env_response_sapi_set_status(php_http_env_response_t *r, long http_code)
761 {
762 return php_http_env_set_response_code(http_code);
763 }
764 static ZEND_RESULT_CODE php_http_env_response_sapi_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v)
765 {
766
767 return php_http_env_set_response_protocol_version(v);
768 }
769 static ZEND_RESULT_CODE php_http_env_response_sapi_set_header(php_http_env_response_t *r, const char *fmt, ...)
770 {
771 ZEND_RESULT_CODE ret;
772 va_list args;
773
774 va_start(args, fmt);
775 ret = php_http_env_set_response_header_va(0, 1, fmt, args);
776 va_end(args);
777
778 return ret;
779 }
780 static ZEND_RESULT_CODE php_http_env_response_sapi_add_header(php_http_env_response_t *r, const char *fmt, ...)
781 {
782 ZEND_RESULT_CODE ret;
783 va_list args;
784
785 va_start(args, fmt);
786 ret = php_http_env_set_response_header_va(0, 0, fmt, args);
787 va_end(args);
788
789 return ret;
790 }
791 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)
792 {
793 return php_http_env_set_response_header_value(0, header_str, header_len, NULL, 1);
794 }
795 static ZEND_RESULT_CODE php_http_env_response_sapi_write(php_http_env_response_t *r, const char *data_str, size_t data_len)
796 {
797 if (0 < PHPWRITE(data_str, data_len)) {
798 return SUCCESS;
799 }
800 return FAILURE;
801 }
802 static ZEND_RESULT_CODE php_http_env_response_sapi_flush(php_http_env_response_t *r)
803 {
804 if (php_output_get_level()) {
805 php_output_flush_all();
806 }
807 if (!(php_output_get_status() & PHP_OUTPUT_IMPLICITFLUSH)) {
808 sapi_flush();
809 }
810
811 return SUCCESS;
812 }
813 static ZEND_RESULT_CODE php_http_env_response_sapi_finish(php_http_env_response_t *r)
814 {
815 return SUCCESS;
816 }
817
818 static php_http_env_response_ops_t php_http_env_response_sapi_ops = {
819 NULL,
820 NULL,
821 php_http_env_response_sapi_get_status,
822 php_http_env_response_sapi_set_status,
823 php_http_env_response_sapi_set_protocol_version,
824 php_http_env_response_sapi_set_header,
825 php_http_env_response_sapi_add_header,
826 php_http_env_response_sapi_del_header,
827 php_http_env_response_sapi_write,
828 php_http_env_response_sapi_flush,
829 php_http_env_response_sapi_finish
830 };
831
832 php_http_env_response_ops_t *php_http_env_response_get_sapi_ops(void)
833 {
834 return &php_http_env_response_sapi_ops;
835 }
836
837 typedef struct php_http_env_response_stream_ctx {
838 HashTable header;
839 php_http_version_t version;
840 long status_code;
841
842 php_stream *stream;
843
844 unsigned started:1;
845 unsigned finished:1;
846 } php_http_env_response_stream_ctx_t;
847
848 static ZEND_RESULT_CODE php_http_env_response_stream_init(php_http_env_response_t *r, void *init_arg)
849 {
850 php_http_env_response_stream_ctx_t *ctx;
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 ctx->status_code = 200;
859
860 r->ctx = ctx;
861
862 return SUCCESS;
863 }
864 static void php_http_env_response_stream_dtor(php_http_env_response_t *r)
865 {
866 php_http_env_response_stream_ctx_t *ctx = r->ctx;
867
868 zend_hash_destroy(&ctx->header);
869 zend_list_delete(ctx->stream->res);
870 efree(ctx);
871 r->ctx = NULL;
872 }
873 static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t *ctx, HashTable *header)
874 {
875 zval *val;
876
877 ZEND_HASH_FOREACH_VAL(header, val)
878 {
879 if (Z_TYPE_P(val) == IS_ARRAY) {
880 php_http_env_response_stream_header(ctx, Z_ARRVAL_P(val));
881 } else {
882 zend_string *zs = zval_get_string(val);
883
884 php_stream_write(ctx->stream, zs->val, zs->len);
885 php_stream_write_string(ctx->stream, PHP_HTTP_CRLF);
886 zend_string_release(zs);
887 }
888 }
889 ZEND_HASH_FOREACH_END();
890 }
891 static ZEND_RESULT_CODE php_http_env_response_stream_start(php_http_env_response_stream_ctx_t *ctx)
892 {
893 if (ctx->started || ctx->finished) {
894 return FAILURE;
895 }
896
897 php_stream_printf(ctx->stream TSRMLS_CC, "HTTP/%u.%u %ld %s" PHP_HTTP_CRLF, ctx->version.major, ctx->version.minor, ctx->status_code, php_http_env_get_response_status_for_code(ctx->status_code));
898 php_http_env_response_stream_header(ctx, &ctx->header);
899 php_stream_write_string(ctx->stream, PHP_HTTP_CRLF);
900 ctx->started = 1;
901 return SUCCESS;
902 }
903 static long php_http_env_response_stream_get_status(php_http_env_response_t *r)
904 {
905 php_http_env_response_stream_ctx_t *ctx = r->ctx;
906
907 return ctx->status_code;
908 }
909 static ZEND_RESULT_CODE php_http_env_response_stream_set_status(php_http_env_response_t *r, long http_code)
910 {
911 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
912
913 if (stream_ctx->started || stream_ctx->finished) {
914 return FAILURE;
915 }
916
917 stream_ctx->status_code = http_code;
918
919 return SUCCESS;
920 }
921 static ZEND_RESULT_CODE php_http_env_response_stream_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v)
922 {
923 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
924
925 if (stream_ctx->started || stream_ctx->finished) {
926 return FAILURE;
927 }
928
929 memcpy(&stream_ctx->version, v, sizeof(stream_ctx->version));
930
931 return SUCCESS;
932 }
933 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)
934 {
935 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
936 char *header_end, *header_str = NULL;
937 size_t header_len = 0;
938 zval zheader, *zheader_ptr;
939
940 if (stream_ctx->started || stream_ctx->finished) {
941 return FAILURE;
942 }
943
944 header_len = vspprintf(&header_str, 0, fmt, argv);
945
946 if (!(header_end = strchr(header_str, ':'))) {
947 efree(header_str);
948 return FAILURE;
949 }
950
951 *header_end = '\0';
952
953 if (!replace && (zheader_ptr = zend_hash_str_find(&stream_ctx->header, header_str, header_end - header_str))) {
954 convert_to_array(zheader_ptr);
955 *header_end = ':';
956 return add_next_index_str(zheader_ptr, php_http_cs2zs(header_str, header_len));
957 } else {
958 ZVAL_STR(&zheader, php_http_cs2zs(header_str, header_len));
959
960 if (SUCCESS != zend_hash_str_update(&stream_ctx->header, header_str, header_end - header_str, &zheader)) {
961 return FAILURE;
962 }
963
964 *header_end = ':';
965 return SUCCESS;
966 }
967 }
968 static ZEND_RESULT_CODE php_http_env_response_stream_set_header(php_http_env_response_t *r, const char *fmt, ...)
969 {
970 ZEND_RESULT_CODE ret;
971 va_list argv;
972
973 va_start(argv, fmt);
974 ret = php_http_env_response_stream_set_header_ex(r, 1, fmt, argv);
975 va_end(argv);
976
977 return ret;
978 }
979 static ZEND_RESULT_CODE php_http_env_response_stream_add_header(php_http_env_response_t *r, const char *fmt, ...)
980 {
981 ZEND_RESULT_CODE ret;
982 va_list argv;
983
984 va_start(argv, fmt);
985 ret = php_http_env_response_stream_set_header_ex(r, 0, fmt, argv);
986 va_end(argv);
987
988 return ret;
989 }
990 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)
991 {
992 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
993
994 if (stream_ctx->started || stream_ctx->finished) {
995 return FAILURE;
996 }
997
998 zend_hash_str_del(&stream_ctx->header, header_str, header_len);
999 return SUCCESS;
1000 }
1001 static ZEND_RESULT_CODE php_http_env_response_stream_write(php_http_env_response_t *r, const char *data_str, size_t data_len)
1002 {
1003 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
1004
1005 if (stream_ctx->finished) {
1006 return FAILURE;
1007 }
1008 if (!stream_ctx->started) {
1009 if (SUCCESS != php_http_env_response_stream_start(stream_ctx)) {
1010 return FAILURE;
1011 }
1012 }
1013
1014 if (data_len != php_stream_write(stream_ctx->stream, data_str, data_len)) {
1015 return FAILURE;
1016 }
1017
1018 return SUCCESS;
1019 }
1020 static ZEND_RESULT_CODE php_http_env_response_stream_flush(php_http_env_response_t *r)
1021 {
1022 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
1023
1024 if (stream_ctx->finished) {
1025 return FAILURE;
1026 }
1027 if (!stream_ctx->started) {
1028 if (SUCCESS != php_http_env_response_stream_start(stream_ctx)) {
1029 return FAILURE;
1030 }
1031 }
1032
1033 return php_stream_flush(stream_ctx->stream);
1034 }
1035 static ZEND_RESULT_CODE php_http_env_response_stream_finish(php_http_env_response_t *r)
1036 {
1037 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
1038
1039 if (stream_ctx->finished) {
1040 return FAILURE;
1041 }
1042 if (!stream_ctx->started) {
1043 if (SUCCESS != php_http_env_response_stream_start(stream_ctx)) {
1044 return FAILURE;
1045 }
1046 }
1047
1048 stream_ctx->finished = 1;
1049
1050 return SUCCESS;
1051 }
1052
1053 static php_http_env_response_ops_t php_http_env_response_stream_ops = {
1054 php_http_env_response_stream_init,
1055 php_http_env_response_stream_dtor,
1056 php_http_env_response_stream_get_status,
1057 php_http_env_response_stream_set_status,
1058 php_http_env_response_stream_set_protocol_version,
1059 php_http_env_response_stream_set_header,
1060 php_http_env_response_stream_add_header,
1061 php_http_env_response_stream_del_header,
1062 php_http_env_response_stream_write,
1063 php_http_env_response_stream_flush,
1064 php_http_env_response_stream_finish
1065 };
1066
1067 php_http_env_response_ops_t *php_http_env_response_get_stream_ops(void)
1068 {
1069 return &php_http_env_response_stream_ops;
1070 }
1071
1072 #define PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj) \
1073 do { \
1074 if (!obj->message) { \
1075 obj->message = php_http_message_init_env(NULL, PHP_HTTP_RESPONSE); \
1076 } \
1077 } while (0)
1078
1079 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___construct, 0, 0, 0)
1080 ZEND_END_ARG_INFO();
1081 static PHP_METHOD(HttpEnvResponse, __construct)
1082 {
1083 php_http_message_object_t *obj;
1084
1085 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1086
1087 obj = PHP_HTTP_OBJ(NULL, getThis());
1088
1089 php_http_expect(obj->message = php_http_message_init_env(obj->message, PHP_HTTP_RESPONSE), unexpected_val, return);
1090 }
1091
1092 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___invoke, 0, 0, 1)
1093 ZEND_ARG_INFO(0, ob_string)
1094 ZEND_ARG_INFO(0, ob_flags)
1095 ZEND_END_ARG_INFO();
1096 static PHP_METHOD(HttpEnvResponse, __invoke)
1097 {
1098 char *ob_str;
1099 size_t ob_len;
1100 zend_long ob_flags = 0;
1101
1102 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &ob_str, &ob_len, &ob_flags)) {
1103 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1104
1105 PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj);
1106
1107 php_http_message_object_init_body_object(obj);
1108 php_http_message_body_append(obj->message->body, ob_str, ob_len);
1109 RETURN_TRUE;
1110 }
1111 }
1112
1113 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEnvRequest, 0, 0, 1)
1114 ZEND_ARG_OBJ_INFO(0, env_request, http\\Message, 1)
1115 ZEND_END_ARG_INFO();
1116 static PHP_METHOD(HttpEnvResponse, setEnvRequest)
1117 {
1118 zval *env_req = NULL;
1119
1120 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O", &env_req, php_http_message_class_entry), invalid_arg, return);
1121
1122 set_option(getThis(), ZEND_STRL("request"), IS_OBJECT, env_req, 0);
1123 RETVAL_ZVAL_FAST(getThis());
1124 }
1125
1126 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentType, 0, 0, 1)
1127 ZEND_ARG_INFO(0, content_type)
1128 ZEND_END_ARG_INFO();
1129 static PHP_METHOD(HttpEnvResponse, setContentType)
1130 {
1131 char *ct_str = NULL;
1132 size_t ct_len = 0;
1133
1134 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &ct_str, &ct_len), invalid_arg, return);
1135
1136 set_option(getThis(), ZEND_STRL("contentType"), IS_STRING, ct_str, ct_len);
1137 RETVAL_ZVAL_FAST(getThis());
1138 }
1139
1140 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentDisposition, 0, 0, 1)
1141 ZEND_ARG_ARRAY_INFO(0, disposition_params, 1)
1142 ZEND_END_ARG_INFO();
1143 static PHP_METHOD(HttpEnvResponse, setContentDisposition)
1144 {
1145 zval *zdisposition;
1146
1147 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "a", &zdisposition), invalid_arg, return);
1148
1149 zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), zdisposition);
1150 RETVAL_ZVAL_FAST(getThis());
1151 }
1152
1153 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentEncoding, 0, 0, 1)
1154 ZEND_ARG_INFO(0, content_encoding)
1155 ZEND_END_ARG_INFO();
1156 static PHP_METHOD(HttpEnvResponse, setContentEncoding)
1157 {
1158 zend_long ce;
1159
1160 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ce), invalid_arg, return);
1161
1162 set_option(getThis(), ZEND_STRL("contentEncoding"), IS_LONG, &ce, 0);
1163 RETVAL_ZVAL_FAST(getThis());
1164 }
1165
1166 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCacheControl, 0, 0, 1)
1167 ZEND_ARG_INFO(0, cache_control)
1168 ZEND_END_ARG_INFO();
1169 static PHP_METHOD(HttpEnvResponse, setCacheControl)
1170 {
1171 char *cc_str = NULL;
1172 size_t cc_len = 0;
1173
1174 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &cc_str, &cc_len), invalid_arg, return);
1175
1176 set_option(getThis(), ZEND_STRL("cacheControl"), IS_STRING, cc_str, cc_len);
1177 RETVAL_ZVAL_FAST(getThis());
1178 }
1179
1180 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setLastModified, 0, 0, 1)
1181 ZEND_ARG_INFO(0, last_modified)
1182 ZEND_END_ARG_INFO();
1183 static PHP_METHOD(HttpEnvResponse, setLastModified)
1184 {
1185 zend_long last_modified;
1186
1187 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &last_modified), invalid_arg, return);
1188
1189 set_option(getThis(), ZEND_STRL("lastModified"), IS_LONG, &last_modified, 0);
1190 RETVAL_ZVAL_FAST(getThis());
1191 }
1192
1193 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByLastModified, 0, 0, 0)
1194 ZEND_ARG_INFO(0, header_name)
1195 ZEND_END_ARG_INFO();
1196 static PHP_METHOD(HttpEnvResponse, isCachedByLastModified)
1197 {
1198 char *header_name_str = NULL;
1199 size_t header_name_len = 0;
1200
1201 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str, &header_name_len)) {
1202 if (!header_name_str || !header_name_len) {
1203 header_name_str = "If-Modified-Since";
1204 header_name_len = lenof("If-Modified-Since");
1205 }
1206
1207 RETURN_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str, header_name_len, get_request(getThis())));
1208 }
1209 }
1210
1211 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEtag, 0, 0, 1)
1212 ZEND_ARG_INFO(0, etag)
1213 ZEND_END_ARG_INFO();
1214 static PHP_METHOD(HttpEnvResponse, setEtag)
1215 {
1216 char *etag_str = NULL;
1217 size_t etag_len = 0;
1218
1219 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &etag_str, &etag_len), invalid_arg, return);
1220
1221 set_option(getThis(), ZEND_STRL("etag"), IS_STRING, etag_str, etag_len);
1222 RETVAL_ZVAL_FAST(getThis());
1223 }
1224
1225 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByEtag, 0, 0, 0)
1226 ZEND_ARG_INFO(0, header_name)
1227 ZEND_END_ARG_INFO();
1228 static PHP_METHOD(HttpEnvResponse, isCachedByEtag)
1229 {
1230 char *header_name_str = NULL;
1231 size_t header_name_len = 0;
1232
1233 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
1234 if (!header_name_str || !header_name_len) {
1235 header_name_str = "If-None-Match";
1236 header_name_len = lenof("If-None-Match");
1237 }
1238 RETURN_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str, header_name_len, get_request(getThis())));
1239 }
1240 }
1241
1242 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setThrottleRate, 0, 0, 1)
1243 ZEND_ARG_INFO(0, chunk_size)
1244 ZEND_ARG_INFO(0, delay)
1245 ZEND_END_ARG_INFO();
1246 static PHP_METHOD(HttpEnvResponse, setThrottleRate)
1247 {
1248 zend_long chunk_size;
1249 double delay = 1;
1250
1251 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l|d", &chunk_size, &delay), invalid_arg, return);
1252
1253 set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE, &delay, 0);
1254 set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG, &chunk_size, 0);
1255 RETVAL_ZVAL_FAST(getThis());
1256 }
1257
1258 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCookie, 0, 0, 1)
1259 ZEND_ARG_INFO(0, cookie)
1260 ZEND_END_ARG_INFO();
1261 static PHP_METHOD(HttpEnvResponse, setCookie)
1262 {
1263 zval *zcookie_new, tmp;
1264 zend_string *zs;
1265 zend_error_handling zeh;
1266 php_http_cookie_list_t *list = NULL;
1267
1268 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zcookie_new), invalid_arg, return);
1269
1270 zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh);
1271 switch (Z_TYPE_P(zcookie_new)) {
1272 case IS_OBJECT:
1273 if (instanceof_function(Z_OBJCE_P(zcookie_new), php_http_cookie_class_entry)) {
1274 Z_ADDREF_P(zcookie_new);
1275 break;
1276 }
1277 /* no break */
1278 case IS_ARRAY:
1279 list = php_http_cookie_list_from_struct(NULL, zcookie_new);
1280 zcookie_new = &tmp;
1281 ZVAL_OBJ(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo);
1282 break;
1283
1284 default:
1285 zs = zval_get_string(zcookie_new);
1286 list = php_http_cookie_list_parse(NULL, zs->val, zs->len, 0, NULL);
1287 zcookie_new = &tmp;
1288 ZVAL_OBJ(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo);
1289 }
1290 zend_restore_error_handling(&zeh);
1291
1292 set_cookie(getThis(), zcookie_new);
1293 zval_ptr_dtor(zcookie_new);
1294
1295 RETVAL_ZVAL_FAST(getThis());
1296 }
1297
1298 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_send, 0, 0, 0)
1299 ZEND_ARG_INFO(0, stream)
1300 ZEND_END_ARG_INFO();
1301 static PHP_METHOD(HttpEnvResponse, send)
1302 {
1303 zval *zstream = NULL;
1304 php_stream *s = NULL;
1305
1306 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &zstream)) {
1307 /* first flush the output layer to avoid conflicting headers and output;
1308 * also, ob_start($thisEnvResponse) might have been called */
1309 php_output_end_all();
1310
1311 if (zstream) {
1312 php_http_env_response_t *r;
1313
1314 php_stream_from_zval(s, zstream);
1315 r = php_http_env_response_init(NULL, getThis(), php_http_env_response_get_stream_ops(), s);
1316 if (!r) {
1317 RETURN_FALSE;
1318 }
1319
1320 RETVAL_BOOL(SUCCESS == php_http_env_response_send(r));
1321 php_http_env_response_free(&r);
1322 } else {
1323 php_http_env_response_t r;
1324
1325 if (!php_http_env_response_init(&r, getThis(), NULL, NULL)) {
1326 RETURN_FALSE;
1327 }
1328
1329 RETVAL_BOOL(SUCCESS == php_http_env_response_send(&r));
1330 php_http_env_response_dtor(&r);
1331 }
1332 }
1333 }
1334
1335 static zend_function_entry php_http_env_response_methods[] = {
1336 PHP_ME(HttpEnvResponse, __construct, ai_HttpEnvResponse___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1337 PHP_ME(HttpEnvResponse, __invoke, ai_HttpEnvResponse___invoke, ZEND_ACC_PUBLIC)
1338 PHP_ME(HttpEnvResponse, setEnvRequest, ai_HttpEnvResponse_setEnvRequest, ZEND_ACC_PUBLIC)
1339 PHP_ME(HttpEnvResponse, setCookie, ai_HttpEnvResponse_setCookie, ZEND_ACC_PUBLIC)
1340 PHP_ME(HttpEnvResponse, setContentType, ai_HttpEnvResponse_setContentType, ZEND_ACC_PUBLIC)
1341 PHP_ME(HttpEnvResponse, setContentDisposition, ai_HttpEnvResponse_setContentDisposition, ZEND_ACC_PUBLIC)
1342 PHP_ME(HttpEnvResponse, setContentEncoding, ai_HttpEnvResponse_setContentEncoding, ZEND_ACC_PUBLIC)
1343 PHP_ME(HttpEnvResponse, setCacheControl, ai_HttpEnvResponse_setCacheControl, ZEND_ACC_PUBLIC)
1344 PHP_ME(HttpEnvResponse, setLastModified, ai_HttpEnvResponse_setLastModified, ZEND_ACC_PUBLIC)
1345 PHP_ME(HttpEnvResponse, isCachedByLastModified, ai_HttpEnvResponse_isCachedByLastModified, ZEND_ACC_PUBLIC)
1346 PHP_ME(HttpEnvResponse, setEtag, ai_HttpEnvResponse_setEtag, ZEND_ACC_PUBLIC)
1347 PHP_ME(HttpEnvResponse, isCachedByEtag, ai_HttpEnvResponse_isCachedByEtag, ZEND_ACC_PUBLIC)
1348 PHP_ME(HttpEnvResponse, setThrottleRate, ai_HttpEnvResponse_setThrottleRate, ZEND_ACC_PUBLIC)
1349 PHP_ME(HttpEnvResponse, send, ai_HttpEnvResponse_send, ZEND_ACC_PUBLIC)
1350 EMPTY_FUNCTION_ENTRY
1351 };
1352
1353 zend_class_entry *php_http_env_response_class_entry;
1354
1355 PHP_MINIT_FUNCTION(http_env_response)
1356 {
1357 zend_class_entry ce = {0};
1358
1359 INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Response", php_http_env_response_methods);
1360 php_http_env_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry);
1361
1362 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_NONE"), PHP_HTTP_CONTENT_ENCODING_NONE);
1363 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_GZIP"), PHP_HTTP_CONTENT_ENCODING_GZIP);
1364
1365 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_NO"), PHP_HTTP_CACHE_NO);
1366 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_HIT"), PHP_HTTP_CACHE_HIT);
1367 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_MISS"), PHP_HTTP_CACHE_MISS);
1368
1369 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("request"), ZEND_ACC_PROTECTED);
1370 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cookies"), ZEND_ACC_PROTECTED);
1371 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentType"), ZEND_ACC_PROTECTED);
1372 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentDisposition"), ZEND_ACC_PROTECTED);
1373 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentEncoding"), ZEND_ACC_PROTECTED);
1374 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cacheControl"), ZEND_ACC_PROTECTED);
1375 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("etag"), ZEND_ACC_PROTECTED);
1376 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("lastModified"), ZEND_ACC_PROTECTED);
1377 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleDelay"), ZEND_ACC_PROTECTED);
1378 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleChunk"), ZEND_ACC_PROTECTED);
1379
1380 return SUCCESS;
1381 }
1382
1383
1384 /*
1385 * Local variables:
1386 * tab-width: 4
1387 * c-basic-offset: 4
1388 * End:
1389 * vim600: noet sw=4 ts=4 fdm=marker
1390 * vim<600: noet sw=4 ts=4
1391 */