- improved put support
[m6w6/ext-http] / http_methods.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "php.h"
23 #include "php_streams.h"
24
25 #include "php_http.h"
26 #include "php_http_std_defs.h"
27 #include "php_http_api.h"
28 #include "php_http_cache_api.h"
29 #include "php_http_request_api.h"
30 #include "php_http_date_api.h"
31 #include "php_http_headers_api.h"
32 #include "php_http_message_api.h"
33 #include "php_http_send_api.h"
34 #include "php_http_url_api.h"
35
36 #include "php_http_message_object.h"
37 #include "php_http_response_object.h"
38 #include "php_http_request_object.h"
39 #include "php_http_exception_object.h"
40
41 #ifdef ZEND_ENGINE_2
42
43 /* {{{ HttpResponse */
44
45 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
46 *
47 * Instantiates a new HttpResponse object, which can be used to send
48 * any data/resource/file to an HTTP client with caching and multiple
49 * ranges/resuming support.
50 *
51 * NOTE: GZIPping is not implemented yet.
52 */
53 PHP_METHOD(HttpResponse, __construct)
54 {
55 zend_bool do_cache = 0, do_gzip = 0;
56 getObject(http_response_object, obj);
57
58 SET_EH_THROW_HTTP();
59 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
60 UPD_PROP(obj, long, cache, do_cache);
61 UPD_PROP(obj, long, gzip, do_gzip);
62 }
63 SET_EH_NORMAL();
64 }
65 /* }}} */
66
67 /* {{{ proto bool HttpResponse::setCache(bool cache)
68 *
69 * Whether it sould be attempted to cache the entitity.
70 * This will result in necessary caching headers and checks of clients
71 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
72 * matches a "304 Not Modified" status code will be issued.
73 *
74 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
75 * to something more appropriate than "no-cache"!
76 */
77 PHP_METHOD(HttpResponse, setCache)
78 {
79 zend_bool do_cache = 0;
80 getObject(http_response_object, obj);
81
82 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
83 RETURN_FALSE;
84 }
85
86 UPD_PROP(obj, long, cache, do_cache);
87 RETURN_TRUE;
88 }
89 /* }}} */
90
91 /* {{{ proto bool HttpResponse::getCache()
92 *
93 * Get current caching setting.
94 */
95 PHP_METHOD(HttpResponse, getCache)
96 {
97 zval *do_cache = NULL;
98 getObject(http_response_object, obj);
99
100 NO_ARGS;
101
102 do_cache = GET_PROP(obj, cache);
103 RETURN_BOOL(Z_LVAL_P(do_cache));
104 }
105 /* }}}*/
106
107 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
108 *
109 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
110 */
111 PHP_METHOD(HttpResponse, setGzip)
112 {
113 zend_bool do_gzip = 0;
114 getObject(http_response_object, obj);
115
116 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
117 RETURN_FALSE;
118 }
119
120 UPD_PROP(obj, long, gzip, do_gzip);
121 RETURN_TRUE;
122 }
123 /* }}} */
124
125 /* {{{ proto bool HttpResponse::getGzip()
126 *
127 * Get current gzipping setting.
128 */
129 PHP_METHOD(HttpResponse, getGzip)
130 {
131 zval *do_gzip = NULL;
132 getObject(http_response_object, obj);
133
134 NO_ARGS;
135
136 do_gzip = GET_PROP(obj, gzip);
137 RETURN_BOOL(Z_LVAL_P(do_gzip));
138 }
139 /* }}} */
140
141 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
142 *
143 * Set a custom cache-control header, usually being "private" or "public"; if
144 * $raw is set to true the header will be sent as-is.
145 */
146 PHP_METHOD(HttpResponse, setCacheControl)
147 {
148 char *ccontrol;
149 int cc_len;
150 zend_bool raw = 0;
151 getObject(http_response_object, obj);
152
153 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
154 RETURN_FALSE;
155 }
156
157 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
158 http_error_ex(E_WARNING, HTTP_E_PARAM, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
159 RETURN_FALSE;
160 }
161
162 UPD_PROP(obj, long, raw_cache_header, raw);
163 UPD_PROP(obj, string, cacheControl, ccontrol);
164 RETURN_TRUE;
165 }
166 /* }}} */
167
168 /* {{{ proto string HttpResponse::getCacheControl()
169 *
170 * Get current Cache-Control header setting.
171 */
172 PHP_METHOD(HttpResponse, getCacheControl)
173 {
174 zval *ccontrol;
175 getObject(http_response_object, obj);
176
177 NO_ARGS;
178
179 ccontrol = GET_PROP(obj, cacheControl);
180 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
181 }
182 /* }}} */
183
184 /* {{{ proto bool HttpResponse::setContentType(string content_type)
185 *
186 * Set the content-type of the sent entity.
187 */
188 PHP_METHOD(HttpResponse, setContentType)
189 {
190 char *ctype;
191 int ctype_len;
192 getObject(http_response_object, obj);
193
194 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
195 RETURN_FALSE;
196 }
197
198 if (!strchr(ctype, '/')) {
199 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
200 RETURN_FALSE;
201 }
202
203 UPD_PROP(obj, string, contentType, ctype);
204
205 RETURN_TRUE;
206 }
207 /* }}} */
208
209 /* {{{ proto string HttpResponse::getContentType()
210 *
211 * Get current Content-Type header setting.
212 */
213 PHP_METHOD(HttpResponse, getContentType)
214 {
215 zval *ctype;
216 getObject(http_response_object, obj);
217
218 NO_ARGS;
219
220 ctype = GET_PROP(obj, contentType);
221 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
222 }
223 /* }}} */
224
225 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
226 *
227 * Set the Content-Disposition of the sent entity. This setting aims to suggest
228 * the receiveing user agent how to handle the sent entity; usually the client
229 * will show the user a "Save As..." popup.
230 */
231 PHP_METHOD(HttpResponse, setContentDisposition)
232 {
233 char *file;
234 int file_len;
235 zend_bool is_inline = 0;
236 getObject(http_response_object, obj);
237
238 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_inline)) {
239 RETURN_FALSE;
240 }
241
242 UPD_PROP(obj, string, dispoFile, file);
243 UPD_PROP(obj, long, dispoInline, is_inline);
244 RETURN_TRUE;
245 }
246 /* }}} */
247
248 /* {{{ proto array HttpResponse::getContentDisposition()
249 *
250 * Get current Content-Disposition setting.
251 * Will return an associative array like:
252 * <pre>
253 * array(
254 * 'filename' => 'foo.bar',
255 * 'inline' => false
256 * )
257 * </pre>
258 */
259 PHP_METHOD(HttpResponse, getContentDisposition)
260 {
261 zval *file;
262 zval *is_inline;
263 getObject(http_response_object, obj);
264
265 if (ZEND_NUM_ARGS()) {
266 WRONG_PARAM_COUNT;
267 }
268
269 file = GET_PROP(obj, dispoFile);
270 is_inline = GET_PROP(obj, dispoInline);
271
272 array_init(return_value);
273 add_assoc_stringl(return_value, "filename", Z_STRVAL_P(file), Z_STRLEN_P(file), 1);
274 add_assoc_bool(return_value, "inline", Z_LVAL_P(is_inline));
275 }
276 /* }}} */
277
278 /* {{{ proto bool HttpResponse::setETag(string etag)
279 *
280 * Set a custom ETag. Use this only if you know what you're doing.
281 */
282 PHP_METHOD(HttpResponse, setETag)
283 {
284 char *etag;
285 int etag_len;
286 getObject(http_response_object, obj);
287
288 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
289 RETURN_FALSE;
290 }
291
292 UPD_PROP(obj, string, eTag, etag);
293 RETURN_TRUE;
294 }
295 /* }}} */
296
297 /* {{{ proto string HttpResponse::getETag()
298 *
299 * Get the previously set custom ETag.
300 */
301 PHP_METHOD(HttpResponse, getETag)
302 {
303 zval *etag;
304 getObject(http_response_object, obj);
305
306 NO_ARGS;
307
308 etag = GET_PROP(obj, eTag);
309 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
310 }
311 /* }}} */
312
313 /* {{{ proto bool HttpResponse::setData(string data)
314 *
315 * Set the data to be sent.
316 */
317 PHP_METHOD(HttpResponse, setData)
318 {
319 zval *the_data;
320 getObject(http_response_object, obj);
321
322 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &the_data)) {
323 RETURN_FALSE;
324 }
325
326 convert_to_string_ex(&the_data);
327 SET_PROP(obj, data, the_data);
328 UPD_PROP(obj, long, lastModified, http_last_modified(the_data, SEND_DATA));
329 UPD_PROP(obj, long, send_mode, SEND_DATA);
330 RETURN_TRUE;
331 }
332 /* }}} */
333
334 /* {{{ proto string HttpResponse::getData()
335 *
336 * Get the previously set data to be sent.
337 */
338 PHP_METHOD(HttpResponse, getData)
339 {
340 zval *the_data;
341 getObject(http_response_object, obj);
342
343 NO_ARGS;
344
345 the_data = GET_PROP(obj, data);
346 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
347 }
348 /* }}} */
349
350 /* {{{ proto bool HttpResponse::setStream(resource stream)
351 *
352 * Set the resource to be sent.
353 */
354 PHP_METHOD(HttpResponse, setStream)
355 {
356 zval *the_stream;
357 php_stream *the_real_stream;
358 getObject(http_response_object, obj);
359
360 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
361 RETURN_FALSE;
362 }
363
364 php_stream_from_zval(the_real_stream, &the_stream);
365
366 SET_PROP(obj, stream, the_stream);
367 UPD_PROP(obj, long, lastModified, http_last_modified(the_real_stream, SEND_RSRC));
368 UPD_PROP(obj, long, send_mode, SEND_RSRC);
369 RETURN_TRUE;
370 }
371 /* }}} */
372
373 /* {{{ proto resource HttpResponse::getStream()
374 *
375 * Get the previously set resource to be sent.
376 */
377 PHP_METHOD(HttpResponse, getStream)
378 {
379 zval *the_stream;
380 getObject(http_response_object, obj);
381
382 NO_ARGS;
383
384 the_stream = GET_PROP(obj, stream);
385 RETURN_RESOURCE(Z_LVAL_P(the_stream));
386 }
387 /* }}} */
388
389 /* {{{ proto bool HttpResponse::setFile(string file)
390 *
391 * Set the file to be sent.
392 */
393 PHP_METHOD(HttpResponse, setFile)
394 {
395 zval *the_file;
396 getObject(http_response_object, obj);
397
398 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
399 RETURN_FALSE;
400 }
401
402 convert_to_string_ex(&the_file);
403
404 UPD_PROP(obj, string, file, Z_STRVAL_P(the_file));
405 UPD_PROP(obj, long, lastModified, http_last_modified(the_file, -1));
406 UPD_PROP(obj, long, send_mode, -1);
407 RETURN_TRUE;
408 }
409 /* }}} */
410
411 /* {{{ proto string HttpResponse::getFile()
412 *
413 * Get the previously set file to be sent.
414 */
415 PHP_METHOD(HttpResponse, getFile)
416 {
417 zval *the_file;
418 getObject(http_response_object, obj);
419
420 NO_ARGS;
421
422 the_file = GET_PROP(obj, file);
423 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
424 }
425 /* }}} */
426
427 /* {{{ proto bool HttpResponse::send()
428 *
429 * Finally send the entity.
430 *
431 * Example:
432 * <pre>
433 * <?php
434 * $r = new HttpResponse(true);
435 * $r->setFile('../hidden/contract.pdf');
436 * $r->setContentType('application/pdf');
437 * $r->send();
438 * ?>
439 * </pre>
440 *
441 */
442 PHP_METHOD(HttpResponse, send)
443 {
444 zval *do_cache, *do_gzip;
445 getObject(http_response_object, obj);
446
447 NO_ARGS;
448
449 do_cache = GET_PROP(obj, cache);
450 do_gzip = GET_PROP(obj, gzip);
451
452 /* gzip */
453 if (Z_LVAL_P(do_gzip)) {
454 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC);
455 }
456
457 /* caching */
458 if (Z_LVAL_P(do_cache)) {
459 zval *cctrl, *etag, *lmod, *ccraw;
460
461 etag = GET_PROP(obj, eTag);
462 lmod = GET_PROP(obj, lastModified);
463 cctrl = GET_PROP(obj, cacheControl);
464 ccraw = GET_PROP(obj, raw_cache_header);
465
466 if (Z_LVAL_P(ccraw)) {
467 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
468 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
469 } else {
470 char cc_header[42] = {0};
471 sprintf(cc_header, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl));
472 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), cc_header, strlen(cc_header));
473 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), cc_header, strlen(cc_header));
474 }
475 }
476
477 /* content type */
478 {
479 zval *ctype = GET_PROP(obj, contentType);
480 if (Z_STRLEN_P(ctype)) {
481 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
482 } else {
483 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
484 }
485 }
486
487 /* content disposition */
488 {
489 zval *dispo_file = GET_PROP(obj, dispoFile);
490 if (Z_STRLEN_P(dispo_file)) {
491 zval *dispo_inline = GET_PROP(obj, dispoInline);
492 http_send_content_disposition(Z_STRVAL_P(dispo_file), Z_STRLEN_P(dispo_file), (zend_bool) Z_LVAL_P(dispo_inline));
493 }
494 }
495
496 /* send */
497 {
498 zval *send_mode = GET_PROP(obj, send_mode);
499 switch (Z_LVAL_P(send_mode))
500 {
501 case SEND_DATA:
502 {
503 zval *zdata = GET_PROP(obj, data);
504 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
505 }
506
507 case SEND_RSRC:
508 {
509 php_stream *the_real_stream;
510 zval *the_stream = GET_PROP(obj, stream);
511 php_stream_from_zval(the_real_stream, &the_stream);
512 RETURN_SUCCESS(http_send_stream(the_real_stream));
513 }
514
515 default:
516 {
517 zval *zfile = GET_PROP(obj, file);
518 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile)));
519 }
520 }
521 }
522 }
523 /* }}} */
524 /* }}} */
525
526 /* {{{ HttpMessage */
527
528 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
529 *
530 * Create an HttpMessage object from a string.
531 */
532 PHP_METHOD(HttpMessage, fromString)
533 {
534 char *string = NULL;
535 int length = 0;
536 http_message *msg = NULL;
537 http_message_object obj;
538
539 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
540 RETURN_NULL();
541 }
542
543 if (!(msg = http_message_parse(string, length))) {
544 RETURN_NULL();
545 }
546
547 Z_TYPE_P(return_value) = IS_OBJECT;
548 return_value->value.obj = http_message_object_from_msg(msg);
549 }
550 /* }}} */
551
552 /* {{{ proto void HttpMessage::__construct([string message])
553 *
554 * Instantiate a new HttpMessage object.
555 */
556 PHP_METHOD(HttpMessage, __construct)
557 {
558 char *message = NULL;
559 int length = 0;
560 getObject(http_message_object, obj);
561
562 SET_EH_THROW_HTTP();
563 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
564 if (obj->message = http_message_parse(message, length)) {
565 if (obj->message->parent) {
566 obj->parent = http_message_object_from_msg(obj->message->parent);
567 }
568 }
569 } else if (!obj->message) {
570 obj->message = http_message_new();
571 }
572 SET_EH_NORMAL();
573 }
574 /* }}} */
575
576 /* {{{ proto string HttpMessage::getBody()
577 *
578 * Get the body of the parsed Message.
579 */
580 PHP_METHOD(HttpMessage, getBody)
581 {
582 zval *body;
583 getObject(http_message_object, obj);
584
585 NO_ARGS;
586
587 RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
588 }
589 /* }}} */
590
591 /* {{{ proto array HttpMessage::getHeaders()
592 *
593 * Get Message Headers.
594 */
595 PHP_METHOD(HttpMessage, getHeaders)
596 {
597 zval headers;
598 getObject(http_message_object, obj);
599
600 NO_ARGS;
601
602 Z_ARRVAL(headers) = &obj->message->hdrs;
603 array_init(return_value);
604 array_copy(&headers, return_value);
605 }
606 /* }}} */
607
608 /* {{{ proto void HttpMessage::setHeaders(array headers)
609 *
610 * Sets new headers.
611 */
612 PHP_METHOD(HttpMessage, setHeaders)
613 {
614 zval *new_headers, old_headers;
615 getObject(http_message_object, obj);
616
617 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
618 return;
619 }
620
621 zend_hash_clean(&obj->message->hdrs);
622 Z_ARRVAL(old_headers) = &obj->message->hdrs;
623 array_copy(new_headers, &old_headers);
624 }
625 /* }}} */
626
627 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
628 *
629 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
630 */
631 PHP_METHOD(HttpMessage, addHeaders)
632 {
633 zval old_headers, *new_headers;
634 zend_bool append = 0;
635 getObject(http_message_object, obj);
636
637 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
638 return;
639 }
640
641 Z_ARRVAL(old_headers) = &obj->message->hdrs;
642 if (append) {
643 array_append(new_headers, &old_headers);
644 } else {
645 array_merge(new_headers, &old_headers);
646 }
647 }
648 /* }}} */
649
650 /* {{{ proto long HttpMessage::getType()
651 *
652 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
653 */
654 PHP_METHOD(HttpMessage, getType)
655 {
656 getObject(http_message_object, obj);
657
658 NO_ARGS;
659
660 RETURN_LONG(obj->message->type);
661 }
662 /* }}} */
663
664 /* {{{ proto void HttpMessage::setType(long type)
665 *
666 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
667 */
668 PHP_METHOD(HttpMessage, setType)
669 {
670 long type;
671 getObject(http_message_object, obj);
672
673 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
674 return;
675 }
676 http_message_set_type(obj->message, type);
677 }
678 /* }}} */
679
680 /* {{{ proto long HttpMessage::getResponseCode()
681 *
682 * Get the Response Code of the Message.
683 */
684 PHP_METHOD(HttpMessage, getResponseCode)
685 {
686 getObject(http_message_object, obj);
687
688 NO_ARGS;
689
690 if (!HTTP_MSG_TYPE(RESPONSE, obj->message)) {
691 http_error(E_NOTICE, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_RESPONSE");
692 RETURN_NULL();
693 }
694
695 RETURN_LONG(obj->message->info.response.code);
696 }
697 /* }}} */
698
699 /* {{{ proto bool HttpMessage::setResponseCode(long code)
700 *
701 * Set the response code of an HTTP Response Message.
702 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
703 * or if the response code is out of range (100-510).
704 */
705 PHP_METHOD(HttpMessage, setResponseCode)
706 {
707 long code;
708 getObject(http_message_object, obj);
709
710 if (obj->message->type != HTTP_MSG_RESPONSE) {
711 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_RESPONSE");
712 RETURN_FALSE;
713 }
714
715 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
716 RETURN_FALSE;
717 }
718 if (code < 100 || code > 510) {
719 http_error_ex(E_WARNING, HTTP_E_PARAM, "Invalid response code (100-510): %ld", code);
720 RETURN_FALSE;
721 }
722
723 obj->message->info.response.code = code;
724 RETURN_TRUE;
725 }
726 /* }}} */
727
728 /* {{{ proto string HttpMessage::getRequestMethod()
729 *
730 * Get the Request Method of the Message.
731 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
732 */
733 PHP_METHOD(HttpMessage, getRequestMethod)
734 {
735 getObject(http_message_object, obj);
736
737 NO_ARGS;
738
739 if (obj->message->type != HTTP_MSG_REQUEST) {
740 http_error(E_NOTICE, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
741 RETURN_NULL();
742 }
743
744 RETURN_STRING(obj->message->info.request.method, 1);
745 }
746 /* }}} */
747
748 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
749 *
750 * Set the Request Method of the HTTP Message.
751 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
752 */
753 PHP_METHOD(HttpMessage, setRequestMethod)
754 {
755 char *method;
756 int method_len;
757 getObject(http_message_object, obj);
758
759 if (obj->message->type != HTTP_MSG_REQUEST) {
760 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
761 RETURN_FALSE;
762 }
763
764 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
765 RETURN_FALSE;
766 }
767 if (method_len < 1) {
768 http_error(E_WARNING, HTTP_E_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
769 RETURN_FALSE;
770 }
771 if (SUCCESS != http_check_method(method)) {
772 http_error_ex(E_WARNING, HTTP_E_PARAM, "Unkown request method: %s", method);
773 RETURN_FALSE;
774 }
775
776 if (obj->message->info.request.method) {
777 efree(obj->message->info.request.method);
778 }
779 obj->message->info.request.method = estrndup(method, method_len);
780 RETURN_TRUE;
781 }
782 /* }}} */
783
784 /* {{{ proto string HttpMessage::getRequestUri()
785 *
786 * Get the Request URI of the Message.
787 */
788 PHP_METHOD(HttpMessage, getRequestUri)
789 {
790 zval *uri;
791 getObject(http_message_object, obj);
792
793 NO_ARGS;
794
795 if (obj->message->type != HTTP_MSG_REQUEST) {
796 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
797 RETURN_NULL();
798 }
799
800 RETURN_STRING(obj->message->info.request.URI, 1);
801 }
802 /* }}} */
803
804 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
805 *
806 * Set the Request URI of the HTTP Message.
807 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
808 * or if paramtere URI was empty.
809 */
810 PHP_METHOD(HttpMessage, setRequestUri)
811 {
812 char *URI;
813 int URIlen;
814 getObject(http_message_object, obj);
815
816 if (obj->message->type != HTTP_MSG_REQUEST) {
817 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
818 RETURN_FALSE;
819 }
820 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
821 RETURN_FALSE;
822 }
823 if (URIlen < 1) {
824 http_error(E_WARNING, HTTP_E_PARAM, "Cannot set HttpMessage::requestUri to an empty string");
825 RETURN_FALSE;
826 }
827
828 if (obj->message->info.request.URI) {
829 efree(obj->message->info.request.URI);
830 }
831 obj->message->info.request.URI = estrndup(URI, URIlen);
832 RETURN_TRUE;
833 }
834 /* }}} */
835
836 /* {{{ proto string HttpMessage::getHttpVersion()
837 *
838 * Get the HTTP Protocol Version of the Message.
839 */
840 PHP_METHOD(HttpMessage, getHttpVersion)
841 {
842 char ver[4] = {0};
843 float version;
844 getObject(http_message_object, obj);
845
846 NO_ARGS;
847
848 switch (obj->message->type)
849 {
850 case HTTP_MSG_RESPONSE:
851 version = obj->message->info.response.http_version;
852 break;
853
854 case HTTP_MSG_REQUEST:
855 version = obj->message->info.request.http_version;
856 break;
857
858 case HTTP_MSG_NONE:
859 default:
860 RETURN_NULL();
861 }
862 sprintf(ver, "%1.1f", version);
863 RETURN_STRINGL(ver, 3, 1);
864 }
865 /* }}} */
866
867 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
868 *
869 * Set the HTTP Protocol version of the Message.
870 * Returns false if version is invalid (1.0 and 1.1).
871 */
872 PHP_METHOD(HttpMessage, setHttpVersion)
873 {
874 char v[4];
875 zval *zv, *version;
876 getObject(http_message_object, obj);
877
878 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
879 return;
880 }
881
882 if (obj->message->type == HTTP_MSG_NONE) {
883 http_error(E_WARNING, HTTP_E_MSG, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
884 RETURN_FALSE;
885 }
886
887 convert_to_double_ex(&zv);
888 sprintf(v, "%1.1f", Z_DVAL_P(zv));
889 if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
890 http_error_ex(E_WARNING, HTTP_E_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %s", v);
891 RETURN_FALSE;
892 }
893
894 if (obj->message->type == HTTP_MSG_RESPONSE) {
895 obj->message->info.response.http_version = (float) Z_DVAL_P(zv);
896 } else {
897 obj->message->info.request.http_version = (float) Z_DVAL_P(zv);
898 }
899 RETURN_TRUE;
900 }
901 /* }}} */
902
903 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
904 *
905 * Get parent Message.
906 */
907 PHP_METHOD(HttpMessage, getParentMessage)
908 {
909 getObject(http_message_object, obj);
910
911 NO_ARGS;
912
913 if (obj->message->parent) {
914 RETVAL_OBJVAL(obj->parent);
915 } else {
916 RETVAL_NULL();
917 }
918 }
919 /* }}} */
920
921 /* {{{ proto bool HttpMessage::send()
922 *
923 * Send the Message according to its type as Response or Request.
924 */
925 PHP_METHOD(HttpMessage, send)
926 {
927 getObject(http_message_object, obj);
928
929 NO_ARGS;
930
931 RETURN_SUCCESS(http_message_send(obj->message));
932 }
933 /* }}} */
934
935 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
936 *
937 * Get the string representation of the Message.
938 */
939 PHP_METHOD(HttpMessage, toString)
940 {
941 char *string;
942 size_t length;
943 zend_bool include_parent = 1;
944 getObject(http_message_object, obj);
945
946 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
947 RETURN_FALSE;
948 }
949
950 if (include_parent) {
951 http_message_serialize(obj->message, &string, &length);
952 } else {
953 http_message_tostring(obj->message, &string, &length);
954 }
955 RETURN_STRINGL(string, length, 0);
956 }
957 /* }}} */
958
959 /* }}} */
960
961 #ifdef HTTP_HAVE_CURL
962 /* {{{ HttpRequest */
963
964 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
965 *
966 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
967 * and POST (including posting files) HTTP requests.
968 */
969 PHP_METHOD(HttpRequest, __construct)
970 {
971 char *URL = NULL;
972 int URL_len;
973 long meth = -1;
974 getObject(http_request_object, obj);
975
976 SET_EH_THROW_HTTP();
977 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
978 INIT_PARR(obj, options);
979 INIT_PARR(obj, responseInfo);
980 INIT_PARR(obj, responseData);
981 INIT_PARR(obj, postFields);
982 INIT_PARR(obj, postFiles);
983
984 if (URL) {
985 UPD_PROP(obj, string, url, URL);
986 }
987 if (meth > -1) {
988 UPD_PROP(obj, long, method, meth);
989 }
990 }
991 SET_EH_NORMAL();
992 }
993 /* }}} */
994
995 /* {{{ proto void HttpRequest::__destruct()
996 *
997 * Destroys the HttpRequest object.
998 */
999 PHP_METHOD(HttpRequest, __destruct)
1000 {
1001 getObject(http_request_object, obj);
1002
1003 NO_ARGS;
1004
1005 FREE_PARR(obj, options);
1006 FREE_PARR(obj, responseInfo);
1007 FREE_PARR(obj, responseData);
1008 FREE_PARR(obj, postFields);
1009 FREE_PARR(obj, postFiles);
1010 }
1011 /* }}} */
1012
1013 /* {{{ proto bool HttpRequest::setOptions(array options)
1014 *
1015 * Set the request options to use. See http_get() for a full list of available options.
1016 */
1017 PHP_METHOD(HttpRequest, setOptions)
1018 {
1019 char *key = NULL;
1020 long idx = 0;
1021 zval *opts, *old_opts, **opt;
1022 getObject(http_request_object, obj);
1023
1024 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
1025 RETURN_FALSE;
1026 }
1027
1028 old_opts = GET_PROP(obj, options);
1029
1030 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
1031 FOREACH_KEYVAL(opts, key, idx, opt) {
1032 if (key) {
1033 if (!strcmp(key, "headers")) {
1034 zval **headers;
1035 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
1036 array_merge(*opt, *headers);
1037 continue;
1038 }
1039 } else if (!strcmp(key, "cookies")) {
1040 zval **cookies;
1041 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1042 array_merge(*opt, *cookies);
1043 continue;
1044 }
1045 }
1046 zval_add_ref(opt);
1047 add_assoc_zval(old_opts, key, *opt);
1048
1049 /* reset */
1050 key = NULL;
1051 }
1052 }
1053
1054 RETURN_TRUE;
1055 }
1056 /* }}} */
1057
1058 /* {{{ proto array HttpRequest::getOptions()
1059 *
1060 * Get current set options.
1061 */
1062 PHP_METHOD(HttpRequest, getOptions)
1063 {
1064 zval *opts;
1065 getObject(http_request_object, obj);
1066
1067 NO_ARGS;
1068
1069 opts = GET_PROP(obj, options);
1070 array_init(return_value);
1071 array_copy(opts, return_value);
1072 }
1073 /* }}} */
1074
1075 /* {{{ proto void HttpRequest::unsetOptions()
1076 *
1077 * Unset all options/headers/cookies.
1078 */
1079 PHP_METHOD(HttpRequest, unsetOptions)
1080 {
1081 getObject(http_request_object, obj);
1082
1083 NO_ARGS;
1084
1085 FREE_PARR(obj, options);
1086 INIT_PARR(obj, options);
1087 }
1088 /* }}} */
1089
1090 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1091 *
1092 * Set additional SSL options.
1093 */
1094 PHP_METHOD(HttpRequest, setSslOptions)
1095 {
1096 zval *opts, *old_opts, **ssl_options;
1097 getObject(http_request_object, obj);
1098
1099 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
1100 RETURN_FALSE;
1101 }
1102
1103 old_opts = GET_PROP(obj, options);
1104
1105 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
1106 array_merge(opts, *ssl_options);
1107 } else {
1108 zval_add_ref(&opts);
1109 add_assoc_zval(old_opts, "ssl", opts);
1110 }
1111
1112 RETURN_TRUE;
1113 }
1114 /* }}} */
1115
1116 /* {{{ proto array HttpRequest::getSslOtpions()
1117 *
1118 * Get previously set SSL options.
1119 */
1120 PHP_METHOD(HttpRequest, getSslOptions)
1121 {
1122 zval *opts, **ssl_options;
1123 getObject(http_request_object, obj);
1124
1125 NO_ARGS;
1126
1127 opts = GET_PROP(obj, options);
1128
1129 array_init(return_value);
1130
1131 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
1132 array_copy(*ssl_options, return_value);
1133 }
1134 }
1135 /* }}} */
1136
1137 /* {{{ proto void HttpRequest::unsetSslOptions()
1138 *
1139 * Unset previously set SSL options.
1140 */
1141 PHP_METHOD(HttpRequest, unsetSslOptions)
1142 {
1143 zval *opts;
1144 getObject(http_request_object, obj);
1145
1146 NO_ARGS;
1147
1148 opts = GET_PROP(obj, options);
1149 zend_hash_del(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"));
1150 }
1151 /* }}} */
1152
1153 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1154 *
1155 * Add request header name/value pairs.
1156 */
1157 PHP_METHOD(HttpRequest, addHeaders)
1158 {
1159 zval *opts, **headers, *new_headers;
1160 getObject(http_request_object, obj);
1161
1162 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
1163 RETURN_FALSE;
1164 }
1165
1166 opts = GET_PROP(obj, options);
1167
1168 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
1169 array_merge(new_headers, *headers);
1170 } else {
1171 zval_add_ref(&new_headers);
1172 add_assoc_zval(opts, "headers", new_headers);
1173 }
1174
1175 RETURN_TRUE;
1176 }
1177 /* }}} */
1178
1179 /* {{{ proto array HttpRequest::getHeaders()
1180 *
1181 * Get previously set request headers.
1182 */
1183 PHP_METHOD(HttpRequest, getHeaders)
1184 {
1185 zval *opts, **headers;
1186 getObject(http_request_object, obj);
1187
1188 NO_ARGS;
1189
1190 opts = GET_PROP(obj, options);
1191
1192 array_init(return_value);
1193
1194 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
1195 array_copy(*headers, return_value);
1196 }
1197 }
1198 /* }}} */
1199
1200 /* {{{ proto void HttpRequest::unsetHeaders()
1201 *
1202 * Unset previously set request headers.
1203 */
1204 PHP_METHOD(HttpRequest, unsetHeaders)
1205 {
1206 zval *opts;
1207 getObject(http_request_object, obj);
1208
1209 NO_ARGS;
1210
1211 opts = GET_PROP(obj, options);
1212 zend_hash_del(Z_ARRVAL_P(opts), "headers", sizeof("headers"));
1213 }
1214 /* }}} */
1215
1216 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1217 *
1218 * Add cookies.
1219 */
1220 PHP_METHOD(HttpRequest, addCookies)
1221 {
1222 zval *opts, **cookies, *new_cookies;
1223 getObject(http_request_object, obj);
1224
1225 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_cookies)) {
1226 RETURN_FALSE;
1227 }
1228
1229 opts = GET_PROP(obj, options);
1230
1231 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1232 array_merge(new_cookies, *cookies);
1233 } else {
1234 zval_add_ref(&new_cookies);
1235 add_assoc_zval(opts, "cookies", new_cookies);
1236 }
1237
1238 RETURN_TRUE;
1239 }
1240 /* }}} */
1241
1242 /* {{{ proto array HttpRequest::getCookies()
1243 *
1244 * Get previously set cookies.
1245 */
1246 PHP_METHOD(HttpRequest, getCookies)
1247 {
1248 zval *opts, **cookies;
1249 getObject(http_request_object, obj);
1250
1251 NO_ARGS;
1252
1253 opts = GET_PROP(obj, options);
1254
1255 array_init(return_value);
1256
1257 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1258 array_copy(*cookies, return_value);
1259 }
1260 }
1261 /* }}} */
1262
1263 /* {{{ proto void HttpRequest::unsetCookies()
1264 *
1265 */
1266 PHP_METHOD(HttpRequest, unsetCookies)
1267 {
1268 zval *opts;
1269 getObject(http_request_object, obj);
1270
1271 NO_ARGS;
1272
1273 opts = GET_PROP(obj, options);
1274 zend_hash_del(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"));
1275 }
1276 /* }}} */
1277
1278 /* {{{ proto bool HttpRequest::setURL(string url)
1279 *
1280 * Set the request URL.
1281 */
1282 PHP_METHOD(HttpRequest, setURL)
1283 {
1284 char *URL = NULL;
1285 int URL_len;
1286 getObject(http_request_object, obj);
1287
1288 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
1289 RETURN_FALSE;
1290 }
1291
1292 UPD_PROP(obj, string, url, URL);
1293 RETURN_TRUE;
1294 }
1295 /* }}} */
1296
1297 /* {{{ proto string HttpRequest::getUrl()
1298 *
1299 * Get the previously set request URL.
1300 */
1301 PHP_METHOD(HttpRequest, getURL)
1302 {
1303 zval *URL;
1304 getObject(http_request_object, obj);
1305
1306 NO_ARGS;
1307
1308 URL = GET_PROP(obj, url);
1309 RETURN_STRINGL(Z_STRVAL_P(URL), Z_STRLEN_P(URL), 1);
1310 }
1311 /* }}} */
1312
1313 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1314 *
1315 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1316 * <tt>HTTP_POST</tt> constants.
1317 */
1318 PHP_METHOD(HttpRequest, setMethod)
1319 {
1320 long meth;
1321 getObject(http_request_object, obj);
1322
1323 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
1324 RETURN_FALSE;
1325 }
1326
1327 UPD_PROP(obj, long, method, meth);
1328 RETURN_TRUE;
1329 }
1330 /* }}} */
1331
1332 /* {{{ proto long HttpRequest::getMethod()
1333 *
1334 * Get the previously set request method.
1335 */
1336 PHP_METHOD(HttpRequest, getMethod)
1337 {
1338 zval *meth;
1339 getObject(http_request_object, obj);
1340
1341 NO_ARGS;
1342
1343 meth = GET_PROP(obj, method);
1344 RETURN_LONG(Z_LVAL_P(meth));
1345 }
1346 /* }}} */
1347
1348 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1349 *
1350 * Set the content type the post request should have.
1351 * Use this only if you know what you're doing.
1352 */
1353 PHP_METHOD(HttpRequest, setContentType)
1354 {
1355 char *ctype;
1356 int ct_len;
1357 getObject(http_request_object, obj);
1358
1359 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1360 RETURN_FALSE;
1361 }
1362
1363 if (!strchr(ctype, '/')) {
1364 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
1365 RETURN_FALSE;
1366 }
1367
1368 UPD_PROP(obj, string, contentType, ctype);
1369 RETURN_TRUE;
1370 }
1371 /* }}} */
1372
1373 /* {{{ proto string HttpRequest::getContentType()
1374 *
1375 * Get the previously content type.
1376 */
1377 PHP_METHOD(HttpRequest, getContentType)
1378 {
1379 zval *ctype;
1380 getObject(http_request_object, obj);
1381
1382 NO_ARGS;
1383
1384 ctype = GET_PROP(obj, contentType);
1385 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
1386 }
1387 /* }}} */
1388
1389 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1390 *
1391 * Set the URL query parameters to use.
1392 * Overwrites previously set query parameters.
1393 * Affects any request types.
1394 */
1395 PHP_METHOD(HttpRequest, setQueryData)
1396 {
1397 zval *qdata;
1398 char *query_data = NULL;
1399 getObject(http_request_object, obj);
1400
1401 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
1402 RETURN_FALSE;
1403 }
1404
1405 if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1406 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1407 RETURN_FALSE;
1408 }
1409 UPD_PROP(obj, string, queryData, query_data);
1410 efree(query_data);
1411 RETURN_TRUE;
1412 }
1413
1414 convert_to_string(qdata);
1415 UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
1416 RETURN_TRUE;
1417 }
1418 /* }}} */
1419
1420 /* {{{ proto string HttpRequest::getQueryData()
1421 *
1422 * Get the current query data in form of an urlencoded query string.
1423 */
1424 PHP_METHOD(HttpRequest, getQueryData)
1425 {
1426 zval *qdata;
1427 getObject(http_request_object, obj);
1428
1429 NO_ARGS;
1430
1431 qdata = GET_PROP(obj, queryData);
1432 RETURN_STRINGL(Z_STRVAL_P(qdata), Z_STRLEN_P(qdata), 1);
1433 }
1434 /* }}} */
1435
1436 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1437 *
1438 * Add parameters to the query parameter list.
1439 * Affects any request type.
1440 */
1441 PHP_METHOD(HttpRequest, addQueryData)
1442 {
1443 zval *qdata, *old_qdata;
1444 char *query_data = NULL;
1445 getObject(http_request_object, obj);
1446
1447 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1448 RETURN_FALSE;
1449 }
1450
1451 old_qdata = GET_PROP(obj, queryData);
1452
1453 if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, NULL)) {
1454 RETURN_FALSE;
1455 }
1456
1457 UPD_PROP(obj, string, queryData, query_data);
1458 efree(query_data);
1459
1460 RETURN_TRUE;
1461 }
1462 /* }}} */
1463
1464 /* {{{ proto void HttpRequest::unsetQueryData()
1465 *
1466 * Clean the query parameters.
1467 * Affects any request type.
1468 */
1469 PHP_METHOD(HttpRequest, unsetQueryData)
1470 {
1471 getObject(http_request_object, obj);
1472
1473 NO_ARGS;
1474
1475 UPD_PROP(obj, string, queryData, "");
1476 }
1477 /* }}} */
1478
1479 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1480 *
1481 * Adds POST data entries.
1482 * Affects only POST requests.
1483 */
1484 PHP_METHOD(HttpRequest, addPostFields)
1485 {
1486 zval *post, *post_data;
1487 getObject(http_request_object, obj);
1488
1489 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1490 RETURN_FALSE;
1491 }
1492
1493 post = GET_PROP(obj, postFields);
1494 array_merge(post_data, post);
1495
1496 RETURN_TRUE;
1497 }
1498 /* }}} */
1499
1500 /* {{{ proto bool HttpRequest::setPostFields(array post_data)
1501 *
1502 * Set the POST data entries.
1503 * Overwrites previously set POST data.
1504 * Affects only POST requests.
1505 */
1506 PHP_METHOD(HttpRequest, setPostFields)
1507 {
1508 zval *post, *post_data;
1509 getObject(http_request_object, obj);
1510
1511 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1512 RETURN_FALSE;
1513 }
1514
1515 post = GET_PROP(obj, postFields);
1516 zend_hash_clean(Z_ARRVAL_P(post));
1517 array_copy(post_data, post);
1518
1519 RETURN_TRUE;
1520 }
1521 /* }}}*/
1522
1523 /* {{{ proto array HttpRequest::getPostFields()
1524 *
1525 * Get previously set POST data.
1526 */
1527 PHP_METHOD(HttpRequest, getPostFields)
1528 {
1529 zval *post_data;
1530 getObject(http_request_object, obj);
1531
1532 NO_ARGS;
1533
1534 post_data = GET_PROP(obj, postFields);
1535 array_init(return_value);
1536 array_copy(post_data, return_value);
1537 }
1538 /* }}} */
1539
1540 /* {{{ proto void HttpRequest::unsetPostFields()
1541 *
1542 * Clean POST data entires.
1543 * Affects only POST requests.
1544 */
1545 PHP_METHOD(HttpRequest, unsetPostFields)
1546 {
1547 zval *post_data;
1548 getObject(http_request_object, obj);
1549
1550 NO_ARGS;
1551
1552 post_data = GET_PROP(obj, postFields);
1553 zend_hash_clean(Z_ARRVAL_P(post_data));
1554 }
1555 /* }}} */
1556
1557 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1558 *
1559 * Add a file to the POST request.
1560 * Affects only POST requests.
1561 */
1562 PHP_METHOD(HttpRequest, addPostFile)
1563 {
1564 zval *files, *entry;
1565 char *name, *file, *type = NULL;
1566 int name_len, file_len, type_len = 0;
1567 getObject(http_request_object, obj);
1568
1569 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1570 RETURN_FALSE;
1571 }
1572
1573 if (type_len) {
1574 if (!strchr(type, '/')) {
1575 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
1576 RETURN_FALSE;
1577 }
1578 } else {
1579 type = "application/x-octetstream";
1580 type_len = sizeof("application/x-octetstream") - 1;
1581 }
1582
1583 MAKE_STD_ZVAL(entry);
1584 array_init(entry);
1585
1586 add_assoc_stringl(entry, "name", name, name_len, 1);
1587 add_assoc_stringl(entry, "type", type, type_len, 1);
1588 add_assoc_stringl(entry, "file", file, file_len, 1);
1589
1590 files = GET_PROP(obj, postFiles);
1591 add_next_index_zval(files, entry);
1592
1593 RETURN_TRUE;
1594 }
1595 /* }}} */
1596
1597 /* {{{ proto array HttpRequest::getPostFiles()
1598 *
1599 * Get all previously added POST files.
1600 */
1601 PHP_METHOD(HttpRequest, getPostFiles)
1602 {
1603 zval *files;
1604 getObject(http_request_object, obj);
1605
1606 NO_ARGS;
1607
1608 files = GET_PROP(obj, postFiles);
1609
1610 array_init(return_value);
1611 array_copy(files, return_value);
1612 }
1613 /* }}} */
1614
1615 /* {{{ proto void HttpRequest::unsetPostFiles()
1616 *
1617 * Unset the POST files list.
1618 * Affects only POST requests.
1619 */
1620 PHP_METHOD(HttpRequest, unsetPostFiles)
1621 {
1622 zval *files;
1623 getObject(http_request_object, obj);
1624
1625 NO_ARGS;
1626
1627 files = GET_PROP(obj, postFiles);
1628 zend_hash_clean(Z_ARRVAL_P(files));
1629 }
1630 /* }}} */
1631
1632 /* {{{ proto array HttpRequest::getResponseData()
1633 *
1634 * Get all response data after the request has been sent.
1635 */
1636 PHP_METHOD(HttpRequest, getResponseData)
1637 {
1638 zval *data;
1639 getObject(http_request_object, obj);
1640
1641 NO_ARGS;
1642
1643 data = GET_PROP(obj, responseData);
1644 array_init(return_value);
1645 array_copy(data, return_value);
1646 }
1647 /* }}} */
1648
1649 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1650 *
1651 * Get response header(s) after the request has been sent.
1652 */
1653 PHP_METHOD(HttpRequest, getResponseHeader)
1654 {
1655 zval *data, **headers, **header;
1656 char *header_name = NULL;
1657 int header_len = 0;
1658 getObject(http_response_object, obj);
1659
1660 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1661 RETURN_FALSE;
1662 }
1663
1664 data = GET_PROP(obj, responseData);
1665 if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1666 RETURN_FALSE;
1667 }
1668
1669 if (!header_len || !header_name) {
1670 array_init(return_value);
1671 array_copy(*headers, return_value);
1672 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1673 RETURN_STRINGL(Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
1674 } else {
1675 RETURN_FALSE;
1676 }
1677 }
1678 /* }}} */
1679
1680 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1681 *
1682 * Get response cookie(s) after the request has been sent.
1683 */
1684 PHP_METHOD(HttpRequest, getResponseCookie)
1685 {
1686 zval *data, **headers;
1687 char *cookie_name = NULL;
1688 int cookie_len = 0;
1689 getObject(http_request_object, obj);
1690
1691 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
1692 RETURN_FALSE;
1693 }
1694
1695 array_init(return_value);
1696
1697 data = GET_PROP(obj, responseData);
1698 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1699 ulong idx = 0;
1700 char *key = NULL;
1701 zval **header = NULL;
1702
1703 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers), key, idx, header) {
1704 if (key && !strcasecmp(key, "Set-Cookie")) {
1705 /* several cookies? */
1706 if (Z_TYPE_PP(header) == IS_ARRAY) {
1707 zval **cookie;
1708
1709 FOREACH_HASH_VAL(Z_ARRVAL_PP(header), cookie) {
1710 zval *cookie_hash;
1711 MAKE_STD_ZVAL(cookie_hash);
1712 array_init(cookie_hash);
1713
1714 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
1715 if (!cookie_len) {
1716 add_next_index_zval(return_value, cookie_hash);
1717 } else {
1718 zval **name;
1719
1720 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1721 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1722 add_next_index_zval(return_value, cookie_hash);
1723 return; /* <<< FOUND >>> */
1724 } else {
1725 zval_dtor(cookie_hash);
1726 efree(cookie_hash);
1727 }
1728 }
1729 } else {
1730 zval_dtor(cookie_hash);
1731 efree(cookie_hash);
1732 }
1733 }
1734 } else {
1735 zval *cookie_hash;
1736 MAKE_STD_ZVAL(cookie_hash);
1737 array_init(cookie_hash);
1738
1739 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
1740 if (!cookie_len) {
1741 add_next_index_zval(return_value, cookie_hash);
1742 } else {
1743 zval **name;
1744
1745 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1746 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1747 add_next_index_zval(return_value, cookie_hash);
1748 } else {
1749 zval_dtor(cookie_hash);
1750 efree(cookie_hash);
1751 }
1752 }
1753 } else {
1754 zval_dtor(cookie_hash);
1755 efree(cookie_hash);
1756 }
1757 }
1758 break;
1759 }
1760 /* reset key */
1761 key = NULL;
1762 }
1763 }
1764 }
1765 /* }}} */
1766
1767 /* {{{ proto string HttpRequest::getResponseBody()
1768 *
1769 * Get the response body after the request has been sent.
1770 */
1771 PHP_METHOD(HttpRequest, getResponseBody)
1772 {
1773 zval *data, **body;
1774 getObject(http_request_object, obj);
1775
1776 NO_ARGS;
1777
1778 data = GET_PROP(obj, responseData);
1779 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1780 RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
1781 } else {
1782 RETURN_FALSE;
1783 }
1784 }
1785 /* }}} */
1786
1787 /* {{{ proto int HttpRequest::getResponseCode()
1788 *
1789 * Get the response code after the request has been sent.
1790 */
1791 PHP_METHOD(HttpRequest, getResponseCode)
1792 {
1793 zval *code;
1794 getObject(http_request_object, obj);
1795
1796 NO_ARGS;
1797
1798 code = GET_PROP(obj, responseCode);
1799 RETURN_LONG(Z_LVAL_P(code));
1800 }
1801 /* }}} */
1802
1803 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1804 *
1805 * Get response info after the request has been sent.
1806 * See http_get() for a full list of returned info.
1807 */
1808 PHP_METHOD(HttpRequest, getResponseInfo)
1809 {
1810 zval *info, **infop;
1811 char *info_name = NULL;
1812 int info_len = 0;
1813 getObject(http_request_object, obj);
1814
1815 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1816 RETURN_FALSE;
1817 }
1818
1819 info = GET_PROP(obj, responseInfo);
1820
1821 if (info_len && info_name) {
1822 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1823 RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
1824 } else {
1825 http_error_ex(E_NOTICE, HTTP_E_PARAM, "Could not find response info named %s", info_name);
1826 RETURN_FALSE;
1827 }
1828 } else {
1829 array_init(return_value);
1830 array_copy(info, return_value);
1831 }
1832 }
1833 /* }}}*/
1834
1835 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1836 *
1837 * Get the full response as HttpMessage object.
1838 */
1839 PHP_METHOD(HttpRequest, getResponseMessage)
1840 {
1841 zval *message;
1842 getObject(http_request_object, obj);
1843
1844 NO_ARGS;
1845
1846 message = GET_PROP(obj, responseMessage);
1847 Z_TYPE_P(return_value) = IS_OBJECT;
1848 return_value->is_ref = 1;
1849 return_value->value.obj = message->value.obj;
1850 zval_add_ref(&return_value);
1851 }
1852
1853 /* {{{ proto bool HttpRequest::send()
1854 *
1855 * Send the HTTP request.
1856 *
1857 * GET example:
1858 * <pre>
1859 * <?php
1860 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1861 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1862 * $r->addQueryData(array('category' => 3));
1863 * try {
1864 * $r->send();
1865 * if ($r->getResponseCode() == 200) {
1866 * file_put_contents('local.rss', $r->getResponseBody());
1867 * }
1868 * } catch (HttpException $ex) {
1869 * echo $ex;
1870 * }
1871 * ?>
1872 * </pre>
1873 *
1874 * POST example:
1875 * <pre>
1876 * <?php
1877 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1878 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1879 * $r->addpostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1880 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1881 * if ($r->send()) {
1882 * echo $r->getResponseBody();
1883 * }
1884 * ?>
1885 * </pre>
1886 */
1887 PHP_METHOD(HttpRequest, send)
1888 {
1889 STATUS status = FAILURE;
1890 zval *meth, *URL, *qdata, *opts, *info, *resp;
1891 char *request_uri;
1892 getObject(http_request_object, obj);
1893
1894 NO_ARGS;
1895
1896 SET_EH_THROW_HTTP();
1897
1898 if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
1899 http_error(E_WARNING, HTTP_E_CURL, "Could not initilaize curl");
1900 RETURN_FALSE;
1901 }
1902
1903 meth = GET_PROP(obj, method);
1904 URL = GET_PROP(obj, url);
1905 qdata = GET_PROP(obj, queryData);
1906 opts = GET_PROP(obj, options);
1907 info = GET_PROP(obj, responseInfo);
1908 resp = GET_PROP(obj, responseData);
1909
1910 // HTTP_URI_MAXLEN+1 long char *
1911 request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0);
1912
1913 if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
1914 if (!strchr(request_uri, '?')) {
1915 strcat(request_uri, "?");
1916 } else {
1917 strcat(request_uri, "&");
1918 }
1919 strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
1920 }
1921
1922 switch (Z_LVAL_P(meth))
1923 {
1924 case HTTP_GET:
1925 case HTTP_HEAD:
1926 status = http_request_ex(obj->ch, Z_LVAL_P(meth), request_uri, NULL, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1927 break;
1928
1929 case HTTP_PUT:
1930 {
1931 http_request_body body;
1932 php_stream *stream;
1933 php_stream_statbuf ssb;
1934 zval *file = GET_PROP(obj, putFile);
1935
1936 if ( (stream = php_stream_open_wrapper(Z_STRVAL_P(file), , "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL)) &&
1937 !php_stream_stat(stream, &ssb)) {
1938 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
1939 body.data = stream;
1940 body.size = ssb.sb.st_size;
1941
1942 status = http_put_ex(obj->ch, request_uri, &body, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1943 http_request_body_dtor(&body);
1944 } else {
1945 status = FAILURE;
1946 }
1947 }
1948 break;
1949
1950 case HTTP_POST:
1951 {
1952 http_request_body body;
1953 zval *fields = GET_PROP(obj, postFields), *files = GET_PROP(obj, postFiles);
1954
1955 if (SUCCESS == (status = http_request_body_fill(&body, Z_ARRVAL_P(fields), Z_ARRVAL_P(files)))) {
1956 status = http_post_ex(obj->ch, request_uri, &body, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1957 http_request_body_dtor(&body);
1958 }
1959 }
1960 break;
1961
1962 default:
1963 {
1964 http_request_body body;
1965 zval *post = GET_PROP(obj, postData);
1966
1967 body.type = HTTP_REQUEST_BODY_CSTRING;
1968 body.data = Z_STRVAL_P(post);
1969 body.size = Z_STRLEN_P(post);
1970
1971 status = http_request_ex(obj->ch, Z_LVAL_P(meth), request_uri, &body, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1972 }
1973 break;
1974 }
1975
1976 efree(request_uri);
1977
1978 /* final data handling */
1979 if (status == SUCCESS) {
1980 http_message *msg;
1981
1982 if (msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response))) {
1983 zval *headers, *message;
1984 char *body;
1985 size_t body_len;
1986
1987 UPD_PROP(obj, long, responseCode, msg->info.response.code);
1988
1989 MAKE_STD_ZVAL(headers)
1990 array_init(headers);
1991
1992 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1993 phpstr_data(PHPSTR(msg), &body, &body_len);
1994
1995 add_assoc_zval(resp, "headers", headers);
1996 add_assoc_stringl(resp, "body", body, body_len, 0);
1997
1998 message = GET_PROP(obj, responseMessage);
1999 zval_dtor(message);
2000 Z_TYPE_P(message) = IS_OBJECT;
2001 message->value.obj = http_message_object_from_msg(msg);
2002 SET_PROP(obj, responseMessage, message);
2003 } else {
2004 status = FAILURE;
2005 }
2006 }
2007
2008 SET_EH_NORMAL();
2009 RETURN_SUCCESS(status);
2010 }
2011 /* }}} */
2012 /* }}} */
2013 #endif /* HTTP_HAVE_CURL */
2014
2015 #endif /* ZEND_ENGINE_2 */
2016
2017 /*
2018 * Local variables:
2019 * tab-width: 4
2020 * c-basic-offset: 4
2021 * End:
2022 * vim600: noet sw=4 ts=4 fdm=marker
2023 * vim<600: noet sw=4 ts=4
2024 */