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