- yay, I guess this needs to be done by hand...
[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 http_message_object obj;
536
537 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
538 RETURN_NULL();
539 }
540
541 if (!(msg = http_message_parse(string, length))) {
542 RETURN_NULL();
543 }
544
545 Z_TYPE_P(return_value) = IS_OBJECT;
546 return_value->value.obj = http_message_object_from_msg(msg);
547 }
548 /* }}} */
549
550 /* {{{ proto string HttpMessage::getBody()
551 *
552 * Get the body of the parsed Message.
553 */
554 PHP_METHOD(HttpMessage, getBody)
555 {
556 zval *body;
557 getObject(http_message_object, obj);
558
559 NO_ARGS;
560
561 RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
562 }
563 /* }}} */
564
565 /* {{{ proto array HttpMessage::getHeaders()
566 *
567 * Get Message Headers.
568 */
569 PHP_METHOD(HttpMessage, getHeaders)
570 {
571 zval headers;
572 getObject(http_message_object, obj);
573
574 NO_ARGS;
575
576 Z_ARRVAL(headers) = &obj->message->hdrs;
577 array_init(return_value);
578 array_copy(&headers, return_value);
579 }
580 /* }}} */
581
582 /* {{{ proto void HttpMessage::setHeaders(array headers)
583 *
584 * Sets new headers.
585 */
586 PHP_METHOD(HttpMessage, setHeaders)
587 {
588 zval *new_headers, old_headers;
589 getObject(http_message_object, obj);
590
591 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
592 return;
593 }
594
595 zend_hash_clean(&obj->message->hdrs);
596 Z_ARRVAL(old_headers) = &obj->message->hdrs;
597 array_copy(new_headers, &old_headers);
598 }
599 /* }}} */
600
601 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
602 *
603 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
604 */
605 PHP_METHOD(HttpMessage, addHeaders)
606 {
607 zval old_headers, *new_headers;
608 zend_bool append = 0;
609 getObject(http_message_object, obj);
610
611 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
612 return;
613 }
614
615 Z_ARRVAL(old_headers) = &obj->message->hdrs;
616 if (append) {
617 array_append(new_headers, &old_headers);
618 } else {
619 array_merge(new_headers, &old_headers);
620 }
621 }
622 /* }}} */
623
624 /* {{{ proto long HttpMessage::getType()
625 *
626 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
627 */
628 PHP_METHOD(HttpMessage, getType)
629 {
630 getObject(http_message_object, obj);
631
632 NO_ARGS;
633
634 RETURN_LONG(obj->message->type);
635 }
636 /* }}} */
637
638 /* {{{ proto void HttpMessage::setType(long type)
639 *
640 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
641 */
642 PHP_METHOD(HttpMessage, setType)
643 {
644 long type;
645 getObject(http_message_object, obj);
646
647 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
648 return;
649 }
650 if (type != obj->message->type) {
651 if (obj->message->type == HTTP_MSG_REQUEST) {
652 if (obj->message->info.request.method) {
653 efree(obj->message->info.request.method);
654 }
655 if (obj->message->info.request.URI) {
656 efree(obj->message->info.request.URI);
657 }
658 }
659 obj->message->type = type;
660 if (obj->message->type == HTTP_MSG_REQUEST) {
661 obj->message->info.request.method = NULL;
662 obj->message->info.request.URI = NULL;
663 }
664 }
665 }
666 /* }}} */
667
668 /* {{{ proto long HttpMessage::getResponseCode()
669 *
670 * Get the Response Code of the Message.
671 */
672 PHP_METHOD(HttpMessage, getResponseCode)
673 {
674 getObject(http_message_object, obj);
675
676 NO_ARGS;
677
678 if (obj->message->type != HTTP_MSG_RESPONSE) {
679 http_error(E_NOTICE, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_RESPONSE");
680 RETURN_NULL();
681 }
682
683 RETURN_LONG(obj->message->info.response.code);
684 }
685 /* }}} */
686
687 /* {{{ proto bool HttpMessage::setResponseCode(long code)
688 *
689 * Set the response code of an HTTP Response Message.
690 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
691 * or if the response code is out of range (100-510).
692 */
693 PHP_METHOD(HttpMessage, setResponseCode)
694 {
695 long code;
696 getObject(http_message_object, obj);
697
698 if (obj->message->type != HTTP_MSG_RESPONSE) {
699 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_RESPONSE");
700 RETURN_FALSE;
701 }
702
703 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
704 RETURN_FALSE;
705 }
706 if (code < 100 || code > 510) {
707 http_error_ex(E_WARNING, HTTP_E_PARAM, "Invalid response code (100-510): %ld", code);
708 RETURN_FALSE;
709 }
710
711 obj->message->info.response.code = code;
712 RETURN_TRUE;
713 }
714 /* }}} */
715
716 /* {{{ proto string HttpMessage::getRequestMethod()
717 *
718 * Get the Request Method of the Message.
719 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
720 */
721 PHP_METHOD(HttpMessage, getRequestMethod)
722 {
723 getObject(http_message_object, obj);
724
725 NO_ARGS;
726
727 if (obj->message->type != HTTP_MSG_REQUEST) {
728 http_error(E_NOTICE, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
729 RETURN_NULL();
730 }
731
732 RETURN_STRING(obj->message->info.request.method, 1);
733 }
734 /* }}} */
735
736 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
737 *
738 * Set the Request Method of the HTTP Message.
739 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
740 */
741 PHP_METHOD(HttpMessage, setRequestMethod)
742 {
743 char *method;
744 int method_len;
745 getObject(http_message_object, obj);
746
747 if (obj->message->type != HTTP_MSG_REQUEST) {
748 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
749 RETURN_FALSE;
750 }
751
752 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
753 RETURN_FALSE;
754 }
755 if (method_len < 1) {
756 http_error(E_WARNING, HTTP_E_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
757 RETURN_FALSE;
758 }
759 if (SUCCESS != http_check_method(method)) {
760 http_error_ex(E_WARNING, HTTP_E_PARAM, "Unkown request method: %s", method);
761 RETURN_FALSE;
762 }
763
764 if (obj->message->info.request.method) {
765 efree(obj->message->info.request.method);
766 }
767 obj->message->info.request.method = estrndup(method, method_len);
768 RETURN_TRUE;
769 }
770 /* }}} */
771
772 /* {{{ proto string HttpMessage::getRequestUri()
773 *
774 * Get the Request URI of the Message.
775 */
776 PHP_METHOD(HttpMessage, getRequestUri)
777 {
778 zval *uri;
779 getObject(http_message_object, obj);
780
781 NO_ARGS;
782
783 if (obj->message->type != HTTP_MSG_REQUEST) {
784 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
785 RETURN_NULL();
786 }
787
788 RETURN_STRING(obj->message->info.request.URI, 1);
789 }
790 /* }}} */
791
792 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
793 *
794 * Set the Request URI of the HTTP Message.
795 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
796 * or if paramtere URI was empty.
797 */
798 PHP_METHOD(HttpMessage, setRequestUri)
799 {
800 char *URI;
801 int URIlen;
802 getObject(http_message_object, obj);
803
804 if (obj->message->type != HTTP_MSG_REQUEST) {
805 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
806 RETURN_FALSE;
807 }
808 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
809 RETURN_FALSE;
810 }
811 if (URIlen < 1) {
812 http_error(E_WARNING, HTTP_E_PARAM, "Cannot set HttpMessage::requestUri to an empty string");
813 RETURN_FALSE;
814 }
815
816 if (obj->message->info.request.URI) {
817 efree(obj->message->info.request.URI);
818 }
819 obj->message->info.request.URI = estrndup(URI, URIlen);
820 RETURN_TRUE;
821 }
822 /* }}} */
823
824 /* {{{ proto string HttpMessage::getHttpVersion()
825 *
826 * Get the HTTP Protocol Version of the Message.
827 */
828 PHP_METHOD(HttpMessage, getHttpVersion)
829 {
830 char ver[4] = {0};
831 float *version;
832 getObject(http_message_object, obj);
833
834 NO_ARGS;
835
836 switch (obj->message->type)
837 {
838 case HTTP_MSG_RESPONSE:
839 version = &obj->message->info.response.http_version;
840 break;
841 case HTTP_MSG_REQUEST:
842 version = &obj->message->info.request.http_version;
843 break;
844 case HTTP_MSG_NONE:
845 default:
846 RETURN_NULL();
847 }
848 sprintf(ver, "1.1f", version);
849 RETURN_STRINGL(ver, 3, 1);
850 }
851 /* }}} */
852
853 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
854 *
855 * Set the HTTP Protocol version of the Message.
856 * Returns false if version is invalid (1.0 and 1.1).
857 */
858 PHP_METHOD(HttpMessage, setHttpVersion)
859 {
860 char v[4];
861 zval *zv, *version;
862 getObject(http_message_object, obj);
863
864 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
865 return;
866 }
867
868 if (obj->message->type == HTTP_MSG_NONE) {
869 http_error(E_WARNING, HTTP_E_MSG, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
870 RETURN_FALSE;
871 }
872
873 convert_to_double_ex(&zv);
874 sprintf(v, "%1.1f", Z_DVAL_P(zv));
875 if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
876 http_error_ex(E_WARNING, HTTP_E_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %s", v);
877 RETURN_FALSE;
878 }
879
880 if (obj->message->type == HTTP_MSG_RESPONSE) {
881 obj->message->info.response.http_version = (float) Z_DVAL_P(zv);
882 } else {
883 obj->message->info.request.http_version = (float) Z_DVAL_P(zv);
884 }
885 RETURN_TRUE;
886 }
887 /* }}} */
888
889 /* {{{ proto HttpMessage HttpMessage::getNestedMessage()
890 *
891 * Get nested Message.
892 */
893 PHP_METHOD(HttpMessage, getNestedMessage)
894 {
895 getObject(http_message_object, obj);
896
897 NO_ARGS;
898
899 if (obj->message->nested) {
900 Z_TYPE_P(return_value) = IS_OBJECT;
901 return_value->value.obj = obj->nested;
902 return_value->is_ref = 1;
903 zend_objects_store_add_ref(return_value TSRMLS_CC);
904 } else {
905 RETVAL_NULL();
906 }
907 }
908 /* }}} */
909
910 /* {{{ proto string HttpMessage::toString()
911 *
912 * Get the string representation of the Message.
913 */
914 PHP_METHOD(HttpMessage, toString)
915 {
916 char *string;
917 size_t length;
918 getObject(http_message_object, obj);
919
920 NO_ARGS;
921
922 http_message_tostring(obj->message, &string, &length);
923 RETURN_STRINGL(string, length, 0);
924 }
925 /* }}} */
926
927 /* }}} */
928
929 #ifdef HTTP_HAVE_CURL
930 /* {{{ HttpRequest */
931
932 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
933 *
934 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
935 * and POST (including posting files) HTTP requests.
936 */
937 PHP_METHOD(HttpRequest, __construct)
938 {
939 char *URL = NULL;
940 int URL_len;
941 long meth = -1;
942 getObject(http_request_object, obj);
943
944 SET_EH_THROW_HTTP();
945 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
946 INIT_PARR(obj, options);
947 INIT_PARR(obj, responseInfo);
948 INIT_PARR(obj, responseData);
949 INIT_PARR(obj, postData);
950 INIT_PARR(obj, postFiles);
951
952 if (URL) {
953 UPD_PROP(obj, string, url, URL);
954 }
955 if (meth > -1) {
956 UPD_PROP(obj, long, method, meth);
957 }
958 }
959 SET_EH_NORMAL();
960 }
961 /* }}} */
962
963 /* {{{ proto void HttpRequest::__destruct()
964 *
965 * Destroys the HttpRequest object.
966 */
967 PHP_METHOD(HttpRequest, __destruct)
968 {
969 getObject(http_request_object, obj);
970
971 NO_ARGS;
972
973 FREE_PARR(obj, options);
974 FREE_PARR(obj, responseInfo);
975 FREE_PARR(obj, responseData);
976 FREE_PARR(obj, postData);
977 FREE_PARR(obj, postFiles);
978 }
979 /* }}} */
980
981 /* {{{ proto bool HttpRequest::setOptions(array options)
982 *
983 * Set the request options to use. See http_get() for a full list of available options.
984 */
985 PHP_METHOD(HttpRequest, setOptions)
986 {
987 char *key = NULL;
988 long idx = 0;
989 zval *opts, *old_opts, **opt;
990 getObject(http_request_object, obj);
991
992 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
993 RETURN_FALSE;
994 }
995
996 old_opts = GET_PROP(obj, options);
997
998 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
999 FOREACH_KEYVAL(opts, key, idx, opt) {
1000 if (key) {
1001 if (!strcmp(key, "headers")) {
1002 zval **headers;
1003 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
1004 array_merge(*opt, *headers);
1005 continue;
1006 }
1007 } else if (!strcmp(key, "cookies")) {
1008 zval **cookies;
1009 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1010 array_merge(*opt, *cookies);
1011 continue;
1012 }
1013 }
1014 zval_add_ref(opt);
1015 add_assoc_zval(old_opts, key, *opt);
1016
1017 /* reset */
1018 key = NULL;
1019 }
1020 }
1021
1022 RETURN_TRUE;
1023 }
1024 /* }}} */
1025
1026 /* {{{ proto array HttpRequest::getOptions()
1027 *
1028 * Get current set options.
1029 */
1030 PHP_METHOD(HttpRequest, getOptions)
1031 {
1032 zval *opts;
1033 getObject(http_request_object, obj);
1034
1035 NO_ARGS;
1036
1037 opts = GET_PROP(obj, options);
1038 array_init(return_value);
1039 array_copy(opts, return_value);
1040 }
1041 /* }}} */
1042
1043 /* {{{ proto void HttpRequest::unsetOptions()
1044 *
1045 * Unset all options/headers/cookies.
1046 */
1047 PHP_METHOD(HttpRequest, unsetOptions)
1048 {
1049 getObject(http_request_object, obj);
1050
1051 NO_ARGS;
1052
1053 FREE_PARR(obj, options);
1054 INIT_PARR(obj, options);
1055 }
1056 /* }}} */
1057
1058 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1059 *
1060 * Set additional SSL options.
1061 */
1062 PHP_METHOD(HttpRequest, setSslOptions)
1063 {
1064 zval *opts, *old_opts, **ssl_options;
1065 getObject(http_request_object, obj);
1066
1067 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
1068 RETURN_FALSE;
1069 }
1070
1071 old_opts = GET_PROP(obj, options);
1072
1073 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
1074 array_merge(opts, *ssl_options);
1075 } else {
1076 zval_add_ref(&opts);
1077 add_assoc_zval(old_opts, "ssl", opts);
1078 }
1079
1080 RETURN_TRUE;
1081 }
1082 /* }}} */
1083
1084 /* {{{ proto array HttpRequest::getSslOtpions()
1085 *
1086 * Get previously set SSL options.
1087 */
1088 PHP_METHOD(HttpRequest, getSslOptions)
1089 {
1090 zval *opts, **ssl_options;
1091 getObject(http_request_object, obj);
1092
1093 NO_ARGS;
1094
1095 opts = GET_PROP(obj, options);
1096
1097 array_init(return_value);
1098
1099 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
1100 array_copy(*ssl_options, return_value);
1101 }
1102 }
1103 /* }}} */
1104
1105 /* {{{ proto void HttpRequest::unsetSslOptions()
1106 *
1107 * Unset previously set SSL options.
1108 */
1109 PHP_METHOD(HttpRequest, unsetSslOptions)
1110 {
1111 zval *opts;
1112 getObject(http_request_object, obj);
1113
1114 NO_ARGS;
1115
1116 opts = GET_PROP(obj, options);
1117 zend_hash_del(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"));
1118 }
1119 /* }}} */
1120
1121 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1122 *
1123 * Add request header name/value pairs.
1124 */
1125 PHP_METHOD(HttpRequest, addHeaders)
1126 {
1127 zval *opts, **headers, *new_headers;
1128 getObject(http_request_object, obj);
1129
1130 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
1131 RETURN_FALSE;
1132 }
1133
1134 opts = GET_PROP(obj, options);
1135
1136 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
1137 array_merge(new_headers, *headers);
1138 } else {
1139 zval_add_ref(&new_headers);
1140 add_assoc_zval(opts, "headers", new_headers);
1141 }
1142
1143 RETURN_TRUE;
1144 }
1145 /* }}} */
1146
1147 /* {{{ proto array HttpRequest::getHeaders()
1148 *
1149 * Get previously set request headers.
1150 */
1151 PHP_METHOD(HttpRequest, getHeaders)
1152 {
1153 zval *opts, **headers;
1154 getObject(http_request_object, obj);
1155
1156 NO_ARGS;
1157
1158 opts = GET_PROP(obj, options);
1159
1160 array_init(return_value);
1161
1162 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
1163 array_copy(*headers, return_value);
1164 }
1165 }
1166 /* }}} */
1167
1168 /* {{{ proto void HttpRequest::unsetHeaders()
1169 *
1170 * Unset previously set request headers.
1171 */
1172 PHP_METHOD(HttpRequest, unsetHeaders)
1173 {
1174 zval *opts;
1175 getObject(http_request_object, obj);
1176
1177 NO_ARGS;
1178
1179 opts = GET_PROP(obj, options);
1180 zend_hash_del(Z_ARRVAL_P(opts), "headers", sizeof("headers"));
1181 }
1182 /* }}} */
1183
1184 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1185 *
1186 * Add cookies.
1187 */
1188 PHP_METHOD(HttpRequest, addCookies)
1189 {
1190 zval *opts, **cookies, *new_cookies;
1191 getObject(http_request_object, obj);
1192
1193 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_cookies)) {
1194 RETURN_FALSE;
1195 }
1196
1197 opts = GET_PROP(obj, options);
1198
1199 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1200 array_merge(new_cookies, *cookies);
1201 } else {
1202 zval_add_ref(&new_cookies);
1203 add_assoc_zval(opts, "cookies", new_cookies);
1204 }
1205
1206 RETURN_TRUE;
1207 }
1208 /* }}} */
1209
1210 /* {{{ proto array HttpRequest::getCookies()
1211 *
1212 * Get previously set cookies.
1213 */
1214 PHP_METHOD(HttpRequest, getCookies)
1215 {
1216 zval *opts, **cookies;
1217 getObject(http_request_object, obj);
1218
1219 NO_ARGS;
1220
1221 opts = GET_PROP(obj, options);
1222
1223 array_init(return_value);
1224
1225 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1226 array_copy(*cookies, return_value);
1227 }
1228 }
1229 /* }}} */
1230
1231 /* {{{ proto void HttpRequest::unsetCookies()
1232 *
1233 */
1234 PHP_METHOD(HttpRequest, unsetCookies)
1235 {
1236 zval *opts;
1237 getObject(http_request_object, obj);
1238
1239 NO_ARGS;
1240
1241 opts = GET_PROP(obj, options);
1242 zend_hash_del(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"));
1243 }
1244 /* }}} */
1245
1246 /* {{{ proto bool HttpRequest::setURL(string url)
1247 *
1248 * Set the request URL.
1249 */
1250 PHP_METHOD(HttpRequest, setURL)
1251 {
1252 char *URL = NULL;
1253 int URL_len;
1254 getObject(http_request_object, obj);
1255
1256 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
1257 RETURN_FALSE;
1258 }
1259
1260 UPD_PROP(obj, string, url, URL);
1261 RETURN_TRUE;
1262 }
1263 /* }}} */
1264
1265 /* {{{ proto string HttpRequest::getUrl()
1266 *
1267 * Get the previously set request URL.
1268 */
1269 PHP_METHOD(HttpRequest, getURL)
1270 {
1271 zval *URL;
1272 getObject(http_request_object, obj);
1273
1274 NO_ARGS;
1275
1276 URL = GET_PROP(obj, url);
1277 RETURN_STRINGL(Z_STRVAL_P(URL), Z_STRLEN_P(URL), 1);
1278 }
1279 /* }}} */
1280
1281 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1282 *
1283 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1284 * <tt>HTTP_POST</tt> constants.
1285 */
1286 PHP_METHOD(HttpRequest, setMethod)
1287 {
1288 long meth;
1289 getObject(http_request_object, obj);
1290
1291 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
1292 RETURN_FALSE;
1293 }
1294
1295 UPD_PROP(obj, long, method, meth);
1296 RETURN_TRUE;
1297 }
1298 /* }}} */
1299
1300 /* {{{ proto long HttpRequest::getMethod()
1301 *
1302 * Get the previously set request method.
1303 */
1304 PHP_METHOD(HttpRequest, getMethod)
1305 {
1306 zval *meth;
1307 getObject(http_request_object, obj);
1308
1309 NO_ARGS;
1310
1311 meth = GET_PROP(obj, method);
1312 RETURN_LONG(Z_LVAL_P(meth));
1313 }
1314 /* }}} */
1315
1316 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1317 *
1318 * Set the content type the post request should have.
1319 * Use this only if you know what you're doing.
1320 */
1321 PHP_METHOD(HttpRequest, setContentType)
1322 {
1323 char *ctype;
1324 int ct_len;
1325 getObject(http_request_object, obj);
1326
1327 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1328 RETURN_FALSE;
1329 }
1330
1331 if (!strchr(ctype, '/')) {
1332 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
1333 RETURN_FALSE;
1334 }
1335
1336 UPD_PROP(obj, string, contentType, ctype);
1337 RETURN_TRUE;
1338 }
1339 /* }}} */
1340
1341 /* {{{ proto string HttpRequest::getContentType()
1342 *
1343 * Get the previously content type.
1344 */
1345 PHP_METHOD(HttpRequest, getContentType)
1346 {
1347 zval *ctype;
1348 getObject(http_request_object, obj);
1349
1350 NO_ARGS;
1351
1352 ctype = GET_PROP(obj, contentType);
1353 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
1354 }
1355 /* }}} */
1356
1357 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1358 *
1359 * Set the URL query parameters to use.
1360 * Overwrites previously set query parameters.
1361 * Affects any request types.
1362 */
1363 PHP_METHOD(HttpRequest, setQueryData)
1364 {
1365 zval *qdata;
1366 char *query_data = NULL;
1367 getObject(http_request_object, obj);
1368
1369 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
1370 RETURN_FALSE;
1371 }
1372
1373 if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1374 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1375 RETURN_FALSE;
1376 }
1377 UPD_PROP(obj, string, queryData, query_data);
1378 efree(query_data);
1379 RETURN_TRUE;
1380 }
1381
1382 convert_to_string(qdata);
1383 UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
1384 RETURN_TRUE;
1385 }
1386 /* }}} */
1387
1388 /* {{{ proto string HttpRequest::getQueryData()
1389 *
1390 * Get the current query data in form of an urlencoded query string.
1391 */
1392 PHP_METHOD(HttpRequest, getQueryData)
1393 {
1394 zval *qdata;
1395 getObject(http_request_object, obj);
1396
1397 NO_ARGS;
1398
1399 qdata = GET_PROP(obj, queryData);
1400 RETURN_STRINGL(Z_STRVAL_P(qdata), Z_STRLEN_P(qdata), 1);
1401 }
1402 /* }}} */
1403
1404 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1405 *
1406 * Add parameters to the query parameter list.
1407 * Affects any request type.
1408 */
1409 PHP_METHOD(HttpRequest, addQueryData)
1410 {
1411 zval *qdata, *old_qdata;
1412 char *query_data = NULL;
1413 getObject(http_request_object, obj);
1414
1415 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1416 RETURN_FALSE;
1417 }
1418
1419 old_qdata = GET_PROP(obj, queryData);
1420
1421 if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, NULL)) {
1422 RETURN_FALSE;
1423 }
1424
1425 UPD_PROP(obj, string, queryData, query_data);
1426 efree(query_data);
1427
1428 RETURN_TRUE;
1429 }
1430 /* }}} */
1431
1432 /* {{{ proto void HttpRequest::unsetQueryData()
1433 *
1434 * Clean the query parameters.
1435 * Affects any request type.
1436 */
1437 PHP_METHOD(HttpRequest, unsetQueryData)
1438 {
1439 getObject(http_request_object, obj);
1440
1441 NO_ARGS;
1442
1443 UPD_PROP(obj, string, queryData, "");
1444 }
1445 /* }}} */
1446
1447 /* {{{ proto bool HttpRequest::addPostData(array post_data)
1448 *
1449 * Adds POST data entries.
1450 * Affects only POST requests.
1451 */
1452 PHP_METHOD(HttpRequest, addPostData)
1453 {
1454 zval *post, *post_data;
1455 getObject(http_request_object, obj);
1456
1457 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1458 RETURN_FALSE;
1459 }
1460
1461 post = GET_PROP(obj, postData);
1462 array_merge(post_data, post);
1463
1464 RETURN_TRUE;
1465 }
1466 /* }}} */
1467
1468 /* {{{ proto bool HttpRequest::setPostData(array post_data)
1469 *
1470 * Set the POST data entries.
1471 * Overwrites previously set POST data.
1472 * Affects only POST requests.
1473 */
1474 PHP_METHOD(HttpRequest, setPostData)
1475 {
1476 zval *post, *post_data;
1477 getObject(http_request_object, obj);
1478
1479 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1480 RETURN_FALSE;
1481 }
1482
1483 post = GET_PROP(obj, postData);
1484 zend_hash_clean(Z_ARRVAL_P(post));
1485 array_copy(post_data, post);
1486
1487 RETURN_TRUE;
1488 }
1489 /* }}}*/
1490
1491 /* {{{ proto array HttpRequest::getPostData()
1492 *
1493 * Get previously set POST data.
1494 */
1495 PHP_METHOD(HttpRequest, getPostData)
1496 {
1497 zval *post_data;
1498 getObject(http_request_object, obj);
1499
1500 NO_ARGS;
1501
1502 post_data = GET_PROP(obj, postData);
1503 array_init(return_value);
1504 array_copy(post_data, return_value);
1505 }
1506 /* }}} */
1507
1508 /* {{{ proto void HttpRequest::unsetPostData()
1509 *
1510 * Clean POST data entires.
1511 * Affects only POST requests.
1512 */
1513 PHP_METHOD(HttpRequest, unsetPostData)
1514 {
1515 zval *post_data;
1516 getObject(http_request_object, obj);
1517
1518 NO_ARGS;
1519
1520 post_data = GET_PROP(obj, postData);
1521 zend_hash_clean(Z_ARRVAL_P(post_data));
1522 }
1523 /* }}} */
1524
1525 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1526 *
1527 * Add a file to the POST request.
1528 * Affects only POST requests.
1529 */
1530 PHP_METHOD(HttpRequest, addPostFile)
1531 {
1532 zval *files, *entry;
1533 char *name, *file, *type = NULL;
1534 int name_len, file_len, type_len = 0;
1535 getObject(http_request_object, obj);
1536
1537 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1538 RETURN_FALSE;
1539 }
1540
1541 if (type_len) {
1542 if (!strchr(type, '/')) {
1543 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
1544 RETURN_FALSE;
1545 }
1546 } else {
1547 type = "application/x-octetstream";
1548 type_len = sizeof("application/x-octetstream") - 1;
1549 }
1550
1551 MAKE_STD_ZVAL(entry);
1552 array_init(entry);
1553
1554 add_assoc_stringl(entry, "name", name, name_len, 1);
1555 add_assoc_stringl(entry, "type", type, type_len, 1);
1556 add_assoc_stringl(entry, "file", file, file_len, 1);
1557
1558 files = GET_PROP(obj, postFiles);
1559 add_next_index_zval(files, entry);
1560
1561 RETURN_TRUE;
1562 }
1563 /* }}} */
1564
1565 /* {{{ proto array HttpRequest::getPostFiles()
1566 *
1567 * Get all previously added POST files.
1568 */
1569 PHP_METHOD(HttpRequest, getPostFiles)
1570 {
1571 zval *files;
1572 getObject(http_request_object, obj);
1573
1574 NO_ARGS;
1575
1576 files = GET_PROP(obj, postFiles);
1577
1578 array_init(return_value);
1579 array_copy(files, return_value);
1580 }
1581 /* }}} */
1582
1583 /* {{{ proto void HttpRequest::unsetPostFiles()
1584 *
1585 * Unset the POST files list.
1586 * Affects only POST requests.
1587 */
1588 PHP_METHOD(HttpRequest, unsetPostFiles)
1589 {
1590 zval *files;
1591 getObject(http_request_object, obj);
1592
1593 NO_ARGS;
1594
1595 files = GET_PROP(obj, postFiles);
1596 zend_hash_clean(Z_ARRVAL_P(files));
1597 }
1598 /* }}} */
1599
1600 /* {{{ proto array HttpRequest::getResponseData()
1601 *
1602 * Get all response data after the request has been sent.
1603 */
1604 PHP_METHOD(HttpRequest, getResponseData)
1605 {
1606 zval *data;
1607 getObject(http_request_object, obj);
1608
1609 NO_ARGS;
1610
1611 data = GET_PROP(obj, responseData);
1612 array_init(return_value);
1613 array_copy(data, return_value);
1614 }
1615 /* }}} */
1616
1617 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1618 *
1619 * Get response header(s) after the request has been sent.
1620 */
1621 PHP_METHOD(HttpRequest, getResponseHeader)
1622 {
1623 zval *data, **headers, **header;
1624 char *header_name = NULL;
1625 int header_len = 0;
1626 getObject(http_response_object, obj);
1627
1628 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1629 RETURN_FALSE;
1630 }
1631
1632 data = GET_PROP(obj, responseData);
1633 if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1634 RETURN_FALSE;
1635 }
1636
1637 if (!header_len || !header_name) {
1638 array_init(return_value);
1639 array_copy(*headers, return_value);
1640 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1641 RETURN_STRINGL(Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
1642 } else {
1643 RETURN_FALSE;
1644 }
1645 }
1646 /* }}} */
1647
1648 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1649 *
1650 * Get response cookie(s) after the request has been sent.
1651 */
1652 PHP_METHOD(HttpRequest, getResponseCookie)
1653 {
1654 zval *data, **headers;
1655 char *cookie_name = NULL;
1656 int cookie_len = 0;
1657 getObject(http_request_object, obj);
1658
1659 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
1660 RETURN_FALSE;
1661 }
1662
1663 array_init(return_value);
1664
1665 data = GET_PROP(obj, responseData);
1666 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1667 ulong idx = 0;
1668 char *key = NULL;
1669 zval **header = NULL;
1670
1671 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers), key, idx, header) {
1672 if (key && !strcasecmp(key, "Set-Cookie")) {
1673 /* several cookies? */
1674 if (Z_TYPE_PP(header) == IS_ARRAY) {
1675 zval **cookie;
1676
1677 FOREACH_HASH_VAL(Z_ARRVAL_PP(header), cookie) {
1678 zval *cookie_hash;
1679 MAKE_STD_ZVAL(cookie_hash);
1680 array_init(cookie_hash);
1681
1682 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
1683 if (!cookie_len) {
1684 add_next_index_zval(return_value, cookie_hash);
1685 } else {
1686 zval **name;
1687
1688 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1689 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1690 add_next_index_zval(return_value, cookie_hash);
1691 return; /* <<< FOUND >>> */
1692 } else {
1693 zval_dtor(cookie_hash);
1694 efree(cookie_hash);
1695 }
1696 }
1697 } else {
1698 zval_dtor(cookie_hash);
1699 efree(cookie_hash);
1700 }
1701 }
1702 } else {
1703 zval *cookie_hash;
1704 MAKE_STD_ZVAL(cookie_hash);
1705 array_init(cookie_hash);
1706
1707 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
1708 if (!cookie_len) {
1709 add_next_index_zval(return_value, cookie_hash);
1710 } else {
1711 zval **name;
1712
1713 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1714 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1715 add_next_index_zval(return_value, cookie_hash);
1716 } else {
1717 zval_dtor(cookie_hash);
1718 efree(cookie_hash);
1719 }
1720 }
1721 } else {
1722 zval_dtor(cookie_hash);
1723 efree(cookie_hash);
1724 }
1725 }
1726 break;
1727 }
1728 /* reset key */
1729 key = NULL;
1730 }
1731 }
1732 }
1733 /* }}} */
1734
1735 /* {{{ proto string HttpRequest::getResponseBody()
1736 *
1737 * Get the response body after the request has been sent.
1738 */
1739 PHP_METHOD(HttpRequest, getResponseBody)
1740 {
1741 zval *data, **body;
1742 getObject(http_request_object, obj);
1743
1744 NO_ARGS;
1745
1746 data = GET_PROP(obj, responseData);
1747 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1748 RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
1749 } else {
1750 RETURN_FALSE;
1751 }
1752 }
1753 /* }}} */
1754
1755 /* {{{ proto int HttpRequest::getResponseCode()
1756 *
1757 * Get the response code after the request has been sent.
1758 */
1759 PHP_METHOD(HttpRequest, getResponseCode)
1760 {
1761 zval *code;
1762 getObject(http_request_object, obj);
1763
1764 NO_ARGS;
1765
1766 code = GET_PROP(obj, responseCode);
1767 RETURN_LONG(Z_LVAL_P(code));
1768 }
1769 /* }}} */
1770
1771 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1772 *
1773 * Get response info after the request has been sent.
1774 * See http_get() for a full list of returned info.
1775 */
1776 PHP_METHOD(HttpRequest, getResponseInfo)
1777 {
1778 zval *info, **infop;
1779 char *info_name = NULL;
1780 int info_len = 0;
1781 getObject(http_request_object, obj);
1782
1783 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1784 RETURN_FALSE;
1785 }
1786
1787 info = GET_PROP(obj, responseInfo);
1788
1789 if (info_len && info_name) {
1790 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1791 RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
1792 } else {
1793 http_error_ex(E_NOTICE, HTTP_E_PARAM, "Could not find response info named %s", info_name);
1794 RETURN_FALSE;
1795 }
1796 } else {
1797 array_init(return_value);
1798 array_copy(info, return_value);
1799 }
1800 }
1801 /* }}}*/
1802
1803 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1804 *
1805 * Get the full response as HttpMessage object.
1806 */
1807 PHP_METHOD(HttpRequest, getResponseMessage)
1808 {
1809 zval *message;
1810 getObject(http_request_object, obj);
1811
1812 NO_ARGS;
1813
1814 message = GET_PROP(obj, responseMessage);
1815 Z_TYPE_P(return_value) = IS_OBJECT;
1816 return_value->is_ref = 1;
1817 return_value->value.obj = message->value.obj;
1818 zval_add_ref(&return_value);
1819 }
1820
1821 /* {{{ proto bool HttpRequest::send()
1822 *
1823 * Send the HTTP request.
1824 *
1825 * GET example:
1826 * <pre>
1827 * <?php
1828 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1829 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1830 * $r->addQueryData(array('category' => 3));
1831 * try {
1832 * $r->send();
1833 * if ($r->getResponseCode() == 200) {
1834 * file_put_contents('local.rss', $r->getResponseBody());
1835 * }
1836 * } catch (HttpException $ex) {
1837 * echo $ex;
1838 * }
1839 * ?>
1840 * </pre>
1841 *
1842 * POST example:
1843 * <pre>
1844 * <?php
1845 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1846 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1847 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1848 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1849 * if ($r->send()) {
1850 * echo $r->getResponseBody();
1851 * }
1852 * ?>
1853 * </pre>
1854 */
1855 PHP_METHOD(HttpRequest, send)
1856 {
1857 STATUS status = FAILURE;
1858 zval *meth, *URL, *qdata, *opts, *info, *resp;
1859 char *request_uri;
1860 getObject(http_request_object, obj);
1861
1862 NO_ARGS;
1863
1864 SET_EH_THROW_HTTP();
1865
1866 if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
1867 http_error(E_WARNING, HTTP_E_CURL, "Could not initilaize curl");
1868 RETURN_FALSE;
1869 }
1870
1871 meth = GET_PROP(obj, method);
1872 URL = GET_PROP(obj, url);
1873 qdata = GET_PROP(obj, queryData);
1874 opts = GET_PROP(obj, options);
1875 info = GET_PROP(obj, responseInfo);
1876 resp = GET_PROP(obj, responseData);
1877
1878 // HTTP_URI_MAXLEN+1 long char *
1879 request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0);
1880
1881 if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
1882 if (!strchr(request_uri, '?')) {
1883 strcat(request_uri, "?");
1884 } else {
1885 strcat(request_uri, "&");
1886 }
1887 strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
1888 }
1889
1890 switch (Z_LVAL_P(meth))
1891 {
1892 case HTTP_GET:
1893 status = http_get_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1894 break;
1895
1896 case HTTP_HEAD:
1897 status = http_head_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1898 break;
1899
1900 case HTTP_POST:
1901 {
1902 zval *post_files, *post_data;
1903
1904 post_files = GET_PROP(obj, postFiles);
1905 post_data = GET_PROP(obj, postData);
1906
1907 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files))) {
1908
1909 /* urlencoded post */
1910 status = http_post_array_ex(obj->ch, request_uri, Z_ARRVAL_P(post_data), Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1911
1912 } else {
1913
1914 /*
1915 * multipart post
1916 */
1917 char *key = NULL;
1918 long idx;
1919 zval **data;
1920 struct curl_httppost *http_post_data[2] = {NULL, NULL};
1921
1922 /* normal data */
1923 FOREACH_KEYVAL(post_data, key, idx, data) {
1924 if (key) {
1925 convert_to_string_ex(data);
1926 curl_formadd(&http_post_data[0], &http_post_data[1],
1927 CURLFORM_COPYNAME, key,
1928 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
1929 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
1930 CURLFORM_END
1931 );
1932
1933 /* reset */
1934 key = NULL;
1935 }
1936 }
1937
1938 /* file data */
1939 FOREACH_VAL(post_files, data) {
1940 zval **file, **type, **name;
1941
1942 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
1943 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
1944 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
1945
1946 curl_formadd(&http_post_data[0], &http_post_data[1],
1947 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
1948 CURLFORM_FILE, Z_STRVAL_PP(file),
1949 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
1950 CURLFORM_END
1951 );
1952 }
1953 }
1954
1955 status = http_post_curldata_ex(obj->ch, request_uri, http_post_data[0], Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1956 curl_formfree(http_post_data[0]);
1957 }
1958 }
1959 break;
1960
1961 default:
1962 break;
1963 }
1964
1965 efree(request_uri);
1966
1967 /* final data handling */
1968 if (status == SUCCESS) {
1969 http_message *msg;
1970
1971 if (msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response))) {
1972 zval *headers, *message;
1973 char *body;
1974 size_t body_len;
1975
1976 UPD_PROP(obj, long, responseCode, msg->info.response.code);
1977
1978 MAKE_STD_ZVAL(headers)
1979 array_init(headers);
1980
1981 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1982 phpstr_data(PHPSTR(msg), &body, &body_len);
1983
1984 add_assoc_zval(resp, "headers", headers);
1985 add_assoc_stringl(resp, "body", body, body_len, 0);
1986
1987 message = GET_PROP(obj, responseMessage);
1988 zval_dtor(message);
1989 Z_TYPE_P(message) = IS_OBJECT;
1990 message->value.obj = http_message_object_from_msg(msg);
1991 SET_PROP(obj, responseMessage, message);
1992 } else {
1993 status = FAILURE;
1994 }
1995 }
1996
1997 SET_EH_NORMAL();
1998 RETURN_SUCCESS(status);
1999 }
2000 /* }}} */
2001 /* }}} */
2002 #endif /* HTTP_HAVE_CURL */
2003
2004 #endif /* ZEND_ENGINE_2 */
2005
2006 /*
2007 * Local variables:
2008 * tab-width: 4
2009 * c-basic-offset: 4
2010 * End:
2011 * vim600: noet sw=4 ts=4 fdm=marker
2012 * vim<600: noet sw=4 ts=4
2013 */
2014