- php_info fun
[m6w6/ext-http] / http_request_object.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
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22 #include "php.h"
23
24 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
25
26 #include "php_http_std_defs.h"
27 #include "php_http_request_object.h"
28 #include "php_http_request_api.h"
29 #include "php_http_request_pool_api.h"
30 #include "php_http.h"
31 #include "php_http_api.h"
32 #include "php_http_url_api.h"
33 #include "php_http_message_api.h"
34 #include "php_http_message_object.h"
35 #include "php_http_exception_object.h"
36
37 #include "missing.h"
38
39 #ifdef PHP_WIN32
40 # include <winsock2.h>
41 #endif
42 #include <curl/curl.h>
43
44 ZEND_EXTERN_MODULE_GLOBALS(http);
45
46 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
47 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
48 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
49 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
50
51 HTTP_EMPTY_ARGS(__destruct, 0);
52 HTTP_BEGIN_ARGS(__construct, 0, 0)
53 HTTP_ARG_VAL(url, 0)
54 HTTP_ARG_VAL(method, 0)
55 HTTP_END_ARGS;
56
57 HTTP_EMPTY_ARGS(getOptions, 0);
58 HTTP_BEGIN_ARGS(setOptions, 0, 0)
59 HTTP_ARG_VAL(options, 0)
60 HTTP_END_ARGS;
61
62 HTTP_EMPTY_ARGS(getSslOptions, 0);
63 HTTP_BEGIN_ARGS(setSslOptions, 0, 0)
64 HTTP_ARG_VAL(ssl_options, 0)
65 HTTP_END_ARGS;
66
67 HTTP_EMPTY_ARGS(getHeaders, 0);
68 HTTP_BEGIN_ARGS(setHeaders, 0, 0)
69 HTTP_ARG_VAL(headers, 0)
70 HTTP_END_ARGS;
71
72 HTTP_BEGIN_ARGS(addHeaders, 0, 1)
73 HTTP_ARG_VAL(headers, 0)
74 HTTP_END_ARGS;
75
76 HTTP_EMPTY_ARGS(getCookies, 0);
77 HTTP_BEGIN_ARGS(setCookies, 0, 0)
78 HTTP_ARG_VAL(cookies, 0)
79 HTTP_END_ARGS;
80
81 HTTP_BEGIN_ARGS(addCookies, 0, 1)
82 HTTP_ARG_VAL(cookies, 0)
83 HTTP_END_ARGS;
84
85 HTTP_EMPTY_ARGS(getUrl, 0);
86 HTTP_BEGIN_ARGS(setUrl, 0, 1)
87 HTTP_ARG_VAL(url, 0)
88 HTTP_END_ARGS;
89
90 HTTP_EMPTY_ARGS(getMethod, 0);
91 HTTP_BEGIN_ARGS(setMethod, 0, 1)
92 HTTP_ARG_VAL(request_method, 0)
93 HTTP_END_ARGS;
94
95 HTTP_EMPTY_ARGS(getContentType, 0);
96 HTTP_BEGIN_ARGS(setContentType, 0, 1)
97 HTTP_ARG_VAL(content_type, 0)
98 HTTP_END_ARGS;
99
100 HTTP_EMPTY_ARGS(getQueryData, 0);
101 HTTP_BEGIN_ARGS(setQueryData, 0, 0)
102 HTTP_ARG_VAL(query_data, 0)
103 HTTP_END_ARGS;
104
105 HTTP_BEGIN_ARGS(addQueryData, 0, 1)
106 HTTP_ARG_VAL(query_data, 0)
107 HTTP_END_ARGS;
108
109 HTTP_EMPTY_ARGS(getPostFields, 0);
110 HTTP_BEGIN_ARGS(setPostFields, 0, 0)
111 HTTP_ARG_VAL(post_fields, 0)
112 HTTP_END_ARGS;
113
114 HTTP_BEGIN_ARGS(addPostFields, 0, 1)
115 HTTP_ARG_VAL(post_fields, 0)
116 HTTP_END_ARGS;
117
118 HTTP_EMPTY_ARGS(getPostFiles, 0);
119 HTTP_BEGIN_ARGS(setPostFiles, 0, 0)
120 HTTP_ARG_VAL(post_files, 0)
121 HTTP_END_ARGS;
122
123 HTTP_BEGIN_ARGS(addPostFile, 0, 2)
124 HTTP_ARG_VAL(formname, 0)
125 HTTP_ARG_VAL(filename, 0)
126 HTTP_ARG_VAL(content_type, 0)
127 HTTP_END_ARGS;
128
129 HTTP_EMPTY_ARGS(getRawPostData, 0);
130 HTTP_BEGIN_ARGS(setRawPostData, 0, 0)
131 HTTP_ARG_VAL(raw_post_data, 0)
132 HTTP_END_ARGS;
133
134 HTTP_BEGIN_ARGS(addRawPostData, 0, 1)
135 HTTP_ARG_VAL(raw_post_data, 0)
136 HTTP_END_ARGS;
137
138 HTTP_EMPTY_ARGS(getPutFile, 0);
139 HTTP_BEGIN_ARGS(setPutFile, 0, 0)
140 HTTP_ARG_VAL(filename, 0)
141 HTTP_END_ARGS;
142
143 HTTP_EMPTY_ARGS(getResponseData, 0);
144 HTTP_BEGIN_ARGS(getResponseHeader, 0, 0)
145 HTTP_ARG_VAL(name, 0)
146 HTTP_END_ARGS;
147
148 HTTP_BEGIN_ARGS(getResponseCookie, 0, 0)
149 HTTP_ARG_VAL(name, 0)
150 HTTP_END_ARGS;
151
152 HTTP_EMPTY_ARGS(getResponseBody, 0);
153 HTTP_EMPTY_ARGS(getResponseCode, 0);
154 HTTP_BEGIN_ARGS(getResponseInfo, 0, 0)
155 HTTP_ARG_VAL(name, 0)
156 HTTP_END_ARGS;
157
158 HTTP_EMPTY_ARGS(getResponseMessage, 1);
159 HTTP_EMPTY_ARGS(getRequestMessage, 1);
160 HTTP_EMPTY_ARGS(getHistory, 1);
161 HTTP_EMPTY_ARGS(send, 1);
162
163 HTTP_BEGIN_ARGS(get, 0, 1)
164 HTTP_ARG_VAL(url, 0)
165 HTTP_ARG_VAL(options, 0)
166 HTTP_ARG_VAL(info, 1)
167 HTTP_END_ARGS;
168
169 HTTP_BEGIN_ARGS(head, 0, 1)
170 HTTP_ARG_VAL(url, 0)
171 HTTP_ARG_VAL(options, 0)
172 HTTP_ARG_VAL(info, 1)
173 HTTP_END_ARGS;
174
175 HTTP_BEGIN_ARGS(postData, 0, 2)
176 HTTP_ARG_VAL(url, 0)
177 HTTP_ARG_VAL(data, 0)
178 HTTP_ARG_VAL(options, 0)
179 HTTP_ARG_VAL(info, 1)
180 HTTP_END_ARGS;
181
182 HTTP_BEGIN_ARGS(postFields, 0, 2)
183 HTTP_ARG_VAL(url, 0)
184 HTTP_ARG_VAL(data, 0)
185 HTTP_ARG_VAL(options, 0)
186 HTTP_ARG_VAL(info, 1)
187 HTTP_END_ARGS;
188
189 HTTP_BEGIN_ARGS(putFile, 0, 2)
190 HTTP_ARG_VAL(url, 0)
191 HTTP_ARG_VAL(file, 0)
192 HTTP_ARG_VAL(options, 0)
193 HTTP_ARG_VAL(info, 1)
194 HTTP_END_ARGS;
195
196 HTTP_BEGIN_ARGS(putStream, 0, 2)
197 HTTP_ARG_VAL(url, 0)
198 HTTP_ARG_VAL(stream, 0)
199 HTTP_ARG_VAL(options, 0)
200 HTTP_ARG_VAL(info, 1)
201 HTTP_END_ARGS;
202
203 HTTP_BEGIN_ARGS(methodRegister, 0, 1)
204 HTTP_ARG_VAL(method_name, 0)
205 HTTP_END_ARGS;
206
207 HTTP_BEGIN_ARGS(methodUnregister, 0, 1)
208 HTTP_ARG_VAL(method, 0)
209 HTTP_END_ARGS;
210
211 HTTP_BEGIN_ARGS(methodName, 0, 1)
212 HTTP_ARG_VAL(method_id, 0)
213 HTTP_END_ARGS;
214
215 HTTP_BEGIN_ARGS(methodExists, 0, 1)
216 HTTP_ARG_VAL(method, 0)
217 HTTP_END_ARGS;
218
219 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
220 static inline void _http_request_object_declare_default_properties(TSRMLS_D);
221
222 zend_class_entry *http_request_object_ce;
223 zend_function_entry http_request_object_fe[] = {
224 HTTP_REQUEST_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
225 HTTP_REQUEST_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
226
227 HTTP_REQUEST_ME(setOptions, ZEND_ACC_PUBLIC)
228 HTTP_REQUEST_ME(getOptions, ZEND_ACC_PUBLIC)
229 HTTP_REQUEST_ME(setSslOptions, ZEND_ACC_PUBLIC)
230 HTTP_REQUEST_ME(getSslOptions, ZEND_ACC_PUBLIC)
231
232 HTTP_REQUEST_ME(addHeaders, ZEND_ACC_PUBLIC)
233 HTTP_REQUEST_ME(getHeaders, ZEND_ACC_PUBLIC)
234 HTTP_REQUEST_ME(setHeaders, ZEND_ACC_PUBLIC)
235
236 HTTP_REQUEST_ME(addCookies, ZEND_ACC_PUBLIC)
237 HTTP_REQUEST_ME(getCookies, ZEND_ACC_PUBLIC)
238 HTTP_REQUEST_ME(setCookies, ZEND_ACC_PUBLIC)
239
240 HTTP_REQUEST_ME(setMethod, ZEND_ACC_PUBLIC)
241 HTTP_REQUEST_ME(getMethod, ZEND_ACC_PUBLIC)
242
243 HTTP_REQUEST_ME(setUrl, ZEND_ACC_PUBLIC)
244 HTTP_REQUEST_ME(getUrl, ZEND_ACC_PUBLIC)
245
246 HTTP_REQUEST_ME(setContentType, ZEND_ACC_PUBLIC)
247 HTTP_REQUEST_ME(getContentType, ZEND_ACC_PUBLIC)
248
249 HTTP_REQUEST_ME(setQueryData, ZEND_ACC_PUBLIC)
250 HTTP_REQUEST_ME(getQueryData, ZEND_ACC_PUBLIC)
251 HTTP_REQUEST_ME(addQueryData, ZEND_ACC_PUBLIC)
252
253 HTTP_REQUEST_ME(setPostFields, ZEND_ACC_PUBLIC)
254 HTTP_REQUEST_ME(getPostFields, ZEND_ACC_PUBLIC)
255 HTTP_REQUEST_ME(addPostFields, ZEND_ACC_PUBLIC)
256
257 HTTP_REQUEST_ME(setRawPostData, ZEND_ACC_PUBLIC)
258 HTTP_REQUEST_ME(getRawPostData, ZEND_ACC_PUBLIC)
259 HTTP_REQUEST_ME(addRawPostData, ZEND_ACC_PUBLIC)
260
261 HTTP_REQUEST_ME(setPostFiles, ZEND_ACC_PUBLIC)
262 HTTP_REQUEST_ME(addPostFile, ZEND_ACC_PUBLIC)
263 HTTP_REQUEST_ME(getPostFiles, ZEND_ACC_PUBLIC)
264
265 HTTP_REQUEST_ME(setPutFile, ZEND_ACC_PUBLIC)
266 HTTP_REQUEST_ME(getPutFile, ZEND_ACC_PUBLIC)
267
268 HTTP_REQUEST_ME(send, ZEND_ACC_PUBLIC)
269
270 HTTP_REQUEST_ME(getResponseData, ZEND_ACC_PUBLIC)
271 HTTP_REQUEST_ME(getResponseHeader, ZEND_ACC_PUBLIC)
272 HTTP_REQUEST_ME(getResponseCookie, ZEND_ACC_PUBLIC)
273 HTTP_REQUEST_ME(getResponseCode, ZEND_ACC_PUBLIC)
274 HTTP_REQUEST_ME(getResponseBody, ZEND_ACC_PUBLIC)
275 HTTP_REQUEST_ME(getResponseInfo, ZEND_ACC_PUBLIC)
276 HTTP_REQUEST_ME(getResponseMessage, ZEND_ACC_PUBLIC)
277 HTTP_REQUEST_ME(getRequestMessage, ZEND_ACC_PUBLIC)
278 HTTP_REQUEST_ME(getHistory, ZEND_ACC_PUBLIC)
279
280 HTTP_REQUEST_ALIAS(get, http_get)
281 HTTP_REQUEST_ALIAS(head, http_head)
282 HTTP_REQUEST_ALIAS(postData, http_post_data)
283 HTTP_REQUEST_ALIAS(postFields, http_post_fields)
284 HTTP_REQUEST_ALIAS(putFile, http_put_file)
285 HTTP_REQUEST_ALIAS(putStream, http_put_stream)
286
287 HTTP_REQUEST_ALIAS(methodRegister, http_request_method_register)
288 HTTP_REQUEST_ALIAS(methodUnregister, http_request_method_unregister)
289 HTTP_REQUEST_ALIAS(methodName, http_request_method_name)
290 HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists)
291
292 EMPTY_FUNCTION_ENTRY
293 };
294 static zend_object_handlers http_request_object_handlers;
295
296 void _http_request_object_init(INIT_FUNC_ARGS)
297 {
298 HTTP_REGISTER_CLASS_EX(HttpRequest, http_request_object, NULL, 0);
299 }
300
301 zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC)
302 {
303 zend_object_value ov;
304 http_request_object *o;
305
306 o = ecalloc(1, sizeof(http_request_object));
307 o->zo.ce = ce;
308 o->ch = curl_easy_init();
309 o->pool = NULL;
310
311 phpstr_init(&o->history);
312 phpstr_init(&o->request);
313 phpstr_init_ex(&o->response, HTTP_CURLBUF_SIZE, 0);
314
315 ALLOC_HASHTABLE(OBJ_PROP(o));
316 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
317 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
318
319 ov.handle = putObject(http_request_object, o);
320 ov.handlers = &http_request_object_handlers;
321
322 return ov;
323 }
324
325 static inline void _http_request_object_declare_default_properties(TSRMLS_D)
326 {
327 zend_class_entry *ce = http_request_object_ce;
328
329 DCL_PROP_N(PROTECTED, options);
330 DCL_PROP_N(PROTECTED, responseInfo);
331 DCL_PROP_N(PROTECTED, responseData);
332 DCL_PROP_N(PROTECTED, responseCode);
333 DCL_PROP_N(PROTECTED, responseMessage);
334 DCL_PROP_N(PROTECTED, postFields);
335 DCL_PROP_N(PROTECTED, postFiles);
336
337 DCL_PROP(PROTECTED, long, method, HTTP_GET);
338
339 DCL_PROP(PROTECTED, string, url, "");
340 DCL_PROP(PROTECTED, string, contentType, "");
341 DCL_PROP(PROTECTED, string, rawPostData, "");
342 DCL_PROP(PROTECTED, string, queryData, "");
343 DCL_PROP(PROTECTED, string, putFile, "");
344
345 DCL_PROP(PUBLIC, bool, recordHistory, 1);
346
347 #ifndef WONKY
348 /*
349 * Request Method Constants
350 */
351 /* HTTP/1.1 */
352 DCL_CONST(long, "METH_GET", HTTP_GET);
353 DCL_CONST(long, "METH_HEAD", HTTP_HEAD);
354 DCL_CONST(long, "METH_POST", HTTP_POST);
355 DCL_CONST(long, "METH_PUT", HTTP_PUT);
356 DCL_CONST(long, "METH_DELETE", HTTP_DELETE);
357 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS);
358 DCL_CONST(long, "METH_TRACE", HTTP_TRACE);
359 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT);
360 /* WebDAV - RFC 2518 */
361 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND);
362 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH);
363 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL);
364 DCL_CONST(long, "METH_COPY", HTTP_COPY);
365 DCL_CONST(long, "METH_MOVE", HTTP_MOVE);
366 DCL_CONST(long, "METH_LOCK", HTTP_LOCK);
367 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK);
368 /* WebDAV Versioning - RFC 3253 */
369 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL);
370 DCL_CONST(long, "METH_REPORT", HTTP_REPORT);
371 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT);
372 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN);
373 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT);
374 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE);
375 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE);
376 DCL_CONST(long, "METH_LABEL", HTTP_LABEL);
377 DCL_CONST(long, "METH_MERGE", HTTP_MERGE);
378 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL);
379 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY);
380 /* WebDAV Access Control - RFC 3744 */
381 DCL_CONST(long, "METH_ACL", HTTP_ACL);
382
383 /*
384 * Auth Constants
385 */
386 # if LIBCURL_VERSION_NUM >= 0x070a05
387 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC);
388 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST);
389 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM);
390 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY);
391 # endif /* LIBCURL_VERSION_NUM */
392 #endif /* WONKY */
393 }
394
395 void _http_request_object_free(zend_object *object TSRMLS_DC)
396 {
397 http_request_object *o = (http_request_object *) object;
398
399 if (OBJ_PROP(o)) {
400 zend_hash_destroy(OBJ_PROP(o));
401 FREE_HASHTABLE(OBJ_PROP(o));
402 }
403 if (o->ch) {
404 /* avoid nasty segfaults with already cleaned up callbacks */
405 curl_easy_setopt(o->ch, CURLOPT_NOPROGRESS, 1);
406 curl_easy_setopt(o->ch, CURLOPT_PROGRESSFUNCTION, NULL);
407 curl_easy_setopt(o->ch, CURLOPT_VERBOSE, 0);
408 curl_easy_setopt(o->ch, CURLOPT_DEBUGFUNCTION, NULL);
409 curl_easy_cleanup(o->ch);
410 }
411 phpstr_dtor(&o->response);
412 phpstr_dtor(&o->request);
413 phpstr_dtor(&o->history);
414 efree(o);
415 }
416
417 STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ptr, http_request_body *body TSRMLS_DC)
418 {
419 zval *meth, *URL;
420 char *request_uri;
421 STATUS status = SUCCESS;
422
423 if (!body) {
424 return FAILURE;
425 }
426 if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
427 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not initilaize curl");
428 return FAILURE;
429 }
430
431 URL = convert_to_type_ex(IS_STRING, GET_PROP(obj, url));
432 // HTTP_URI_MAXLEN+1 long char *
433 if (!(request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0))) {
434 return FAILURE;
435 }
436
437 meth = convert_to_type_ex(IS_LONG, GET_PROP(obj, method));
438 switch (Z_LVAL_P(meth))
439 {
440 case HTTP_GET:
441 case HTTP_HEAD:
442 body->type = -1;
443 body = NULL;
444 break;
445
446 case HTTP_PUT:
447 {
448 php_stream_statbuf ssb;
449 php_stream *stream = php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj, putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
450
451 if (stream && !php_stream_stat(stream, &ssb)) {
452 body->type = HTTP_REQUEST_BODY_UPLOADFILE;
453 body->data = stream;
454 body->size = ssb.sb.st_size;
455 } else {
456 status = FAILURE;
457 }
458 }
459 break;
460
461 case HTTP_POST:
462 default:
463 {
464 /* check for raw post data */
465 zval *raw_data = convert_to_type_ex(IS_STRING, GET_PROP(obj, rawPostData));
466
467 if (Z_STRLEN_P(raw_data)) {
468 zval *ctype = convert_to_type_ex(IS_STRING, GET_PROP(obj, contentType));
469
470 if (Z_STRLEN_P(ctype)) {
471 zval **headers, *opts = GET_PROP(obj, options);
472
473 convert_to_array(opts);
474
475 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
476 zval **ct_header;
477
478 convert_to_array(*headers);
479 /* only override if not already set */
480 if (SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void **) &ct_header)) {
481 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
482 }
483 } else {
484 zval *headers;
485
486 MAKE_STD_ZVAL(headers);
487 array_init(headers);
488 add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
489 add_assoc_zval(opts, "headers", headers);
490 }
491 }
492
493 body->type = HTTP_REQUEST_BODY_CSTRING;
494 body->data = estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data));
495 body->size = Z_STRLEN_P(raw_data);
496 } else {
497 status = http_request_body_fill(body, Z_ARRVAL_P(GET_PROP(obj, postFields)), Z_ARRVAL_P(GET_PROP(obj, postFiles)));
498 }
499 }
500 break;
501 }
502
503 if (status == SUCCESS) {
504 zval *qdata = convert_to_type_ex(IS_STRING, GET_PROP(obj, queryData));
505
506 if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
507 if (!strchr(request_uri, '?')) {
508 strcat(request_uri, "?");
509 } else {
510 strcat(request_uri, "&");
511 }
512 strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
513 }
514
515 status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, body, Z_ARRVAL_P(GET_PROP(obj, options)));
516 }
517 efree(request_uri);
518
519 /* clean previous response */
520 phpstr_dtor(&obj->response);
521 /* clean previous request */
522 phpstr_dtor(&obj->request);
523
524 return status;
525 }
526
527 STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
528 {
529 http_message *msg;
530
531 phpstr_fix(&obj->request);
532 phpstr_fix(&obj->response);
533
534 msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response));
535
536 if (!msg) {
537 return FAILURE;
538 } else {
539 char *body;
540 size_t body_len;
541 zval *headers, *message,
542 *resp = convert_to_type(IS_ARRAY, GET_PROP(obj, responseData)),
543 *info = convert_to_type(IS_ARRAY, GET_PROP(obj, responseInfo));
544
545 if (zval_is_true(GET_PROP(obj, recordHistory))) {
546 /* we need to act like a zipper, as we'll receive
547 * the requests and the responses in separate chains
548 * for redirects
549 */
550 http_message *response = msg, *request = http_message_parse(PHPSTR_VAL(&obj->request), PHPSTR_LEN(&obj->request));
551 http_message *free_msg = request;
552
553 do {
554 char *message;
555 size_t msglen;
556
557 http_message_tostring(response, &message, &msglen);
558 phpstr_append(&obj->history, message, msglen);
559 efree(message);
560
561 http_message_tostring(request, &message, &msglen);
562 phpstr_append(&obj->history, message, msglen);
563 efree(message);
564
565 } while ((response = response->parent) && (request = request->parent));
566
567 http_message_free(&free_msg);
568 phpstr_fix(&obj->history);
569 }
570
571 UPD_PROP(obj, long, responseCode, msg->http.info.response.code);
572
573 MAKE_STD_ZVAL(headers)
574 array_init(headers);
575
576 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
577 phpstr_data(PHPSTR(msg), &body, &body_len);
578
579 add_assoc_zval(resp, "headers", headers);
580 add_assoc_stringl(resp, "body", body, body_len, 0);
581
582 MAKE_STD_ZVAL(message);
583 ZVAL_OBJVAL(message, http_message_object_from_msg(msg));
584 SET_PROP(obj, responseMessage, message);
585 zval_ptr_dtor(&message);
586
587 http_request_info(obj->ch, Z_ARRVAL_P(info));
588 SET_PROP(obj, responseInfo, info);
589
590 return SUCCESS;
591 }
592 }
593
594 #define http_request_object_set_options_subr(key, ow) \
595 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
596 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite)
597 {
598 zval *opts, **options, *new_options = NULL;
599 getObject(http_request_object, obj);
600
601 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &new_options)) {
602 RETURN_FALSE;
603 }
604
605 opts = convert_to_type(IS_ARRAY, GET_PROP(obj, options));
606
607 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options)) {
608 convert_to_array(*options);
609 if (overwrite) {
610 zend_hash_clean(Z_ARRVAL_PP(options));
611 }
612 if (new_options && zend_hash_num_elements(Z_ARRVAL_P(new_options))) {
613 if (overwrite) {
614 array_copy(new_options, *options);
615 } else {
616 array_merge(new_options, *options);
617 }
618 }
619 } else if (new_options && zend_hash_num_elements(Z_ARRVAL_P(new_options))) {
620 zval_add_ref(&new_options);
621 add_assoc_zval(opts, key, new_options);
622 }
623
624 RETURN_TRUE;
625 }
626
627 #define http_request_object_get_options_subr(key) \
628 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
629 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
630 {
631 NO_ARGS;
632
633 IF_RETVAL_USED {
634 zval *opts, **options;
635 getObject(http_request_object, obj);
636
637 opts = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, options));
638
639 array_init(return_value);
640
641 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options)) {
642 convert_to_array(*options);
643 array_copy(*options, return_value);
644 }
645 }
646 }
647
648
649 /* ### USERLAND ### */
650
651 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
652 *
653 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
654 * and POST (including posting files) HTTP requests.
655 */
656 PHP_METHOD(HttpRequest, __construct)
657 {
658 char *URL = NULL;
659 int URL_len;
660 long meth = -1;
661 getObject(http_request_object, obj);
662
663 SET_EH_THROW_HTTP();
664 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
665 INIT_PARR(obj, options);
666 INIT_PARR(obj, responseInfo);
667 INIT_PARR(obj, responseData);
668 INIT_PARR(obj, postFields);
669 INIT_PARR(obj, postFiles);
670
671 if (URL) {
672 UPD_STRL(obj, url, URL, URL_len);
673 }
674 if (meth > -1) {
675 UPD_PROP(obj, long, method, meth);
676 }
677 }
678 SET_EH_NORMAL();
679 }
680 /* }}} */
681
682 /* {{{ proto void HttpRequest::__destruct()
683 *
684 * Destroys the HttpRequest object.
685 */
686 PHP_METHOD(HttpRequest, __destruct)
687 {
688 getObject(http_request_object, obj);
689
690 NO_ARGS;
691
692 FREE_PARR(obj, options);
693 FREE_PARR(obj, responseInfo);
694 FREE_PARR(obj, responseData);
695 FREE_PARR(obj, postFields);
696 FREE_PARR(obj, postFiles);
697 }
698 /* }}} */
699
700 /* {{{ proto bool HttpRequest::setOptions([array options])
701 *
702 * Set the request options to use. See http_get() for a full list of available options.
703 */
704 PHP_METHOD(HttpRequest, setOptions)
705 {
706 char *key = NULL;
707 ulong idx = 0;
708 zval *opts = NULL, *old_opts, **opt;
709 getObject(http_request_object, obj);
710
711 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
712 RETURN_FALSE;
713 }
714
715 old_opts = convert_to_type(IS_ARRAY, GET_PROP(obj, options));
716
717 if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
718 zend_hash_clean(Z_ARRVAL_P(old_opts));
719 RETURN_TRUE;
720 }
721
722 /* some options need extra attention -- thus cannot use array_merge() directly */
723 FOREACH_KEYVAL(opts, key, idx, opt) {
724 if (key) {
725 if (!strcmp(key, "headers")) {
726 zval **headers;
727 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
728 convert_to_array(*opt);
729 convert_to_array(*headers);
730 array_merge(*opt, *headers);
731 continue;
732 }
733 } else if (!strcmp(key, "cookies")) {
734 zval **cookies;
735 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
736 convert_to_array(*opt);
737 convert_to_array(*cookies);
738 array_merge(*opt, *cookies);
739 continue;
740 }
741 } else if (!strcmp(key, "ssl")) {
742 zval **ssl;
743 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl)) {
744 convert_to_array(*opt);
745 convert_to_array(*ssl);
746 array_merge(*opt, *ssl);
747 continue;
748 }
749 } else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) {
750 if (Z_TYPE_PP(opt) != IS_STRING) {
751 convert_to_string_ex(opt);
752 }
753 UPD_STRL(obj, url, Z_STRVAL_PP(opt), Z_STRLEN_PP(opt));
754 continue;
755 } else if (!strcmp(key, "method")) {
756 if (Z_TYPE_PP(opt) != IS_LONG) {
757 convert_to_long_ex(opt);
758 }
759 UPD_PROP(obj, long, method, Z_LVAL_PP(opt));
760 continue;
761 }
762
763 zval_add_ref(opt);
764 add_assoc_zval(old_opts, key, *opt);
765
766 /* reset */
767 key = NULL;
768 }
769 }
770
771 RETURN_TRUE;
772 }
773 /* }}} */
774
775 /* {{{ proto array HttpRequest::getOptions()
776 *
777 * Get currently set options.
778 */
779 PHP_METHOD(HttpRequest, getOptions)
780 {
781 NO_ARGS;
782
783 IF_RETVAL_USED {
784 zval *opts;
785 getObject(http_request_object, obj);
786
787 opts = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, options));
788 array_init(return_value);
789 array_copy(opts, return_value);
790 }
791 }
792 /* }}} */
793
794 /* {{{ proto bool HttpRequest::setSslOptions([array options])
795 *
796 * Set SSL options.
797 */
798 PHP_METHOD(HttpRequest, setSslOptions)
799 {
800 http_request_object_set_options_subr("ssl", 1);
801 }
802 /* }}} */
803
804 /* {{{ proto bool HttpRequest::addSslOptions(array options)
805 *
806 * Set additional SSL options.
807 */
808 PHP_METHOD(HttpRequest, addSslOptions)
809 {
810 http_request_object_set_options_subr("ssl", 0);
811 }
812 /* }}} */
813
814 /* {{{ proto array HttpRequest::getSslOtpions()
815 *
816 * Get previously set SSL options.
817 */
818 PHP_METHOD(HttpRequest, getSslOptions)
819 {
820 http_request_object_get_options_subr("ssl");
821 }
822 /* }}} */
823
824 /* {{{ proto bool HttpRequest::addHeaders(array headers)
825 *
826 * Add request header name/value pairs.
827 */
828 PHP_METHOD(HttpRequest, addHeaders)
829 {
830 http_request_object_set_options_subr("headers", 0);
831 }
832
833 /* {{{ proto bool HttpRequest::setHeaders([array headers])
834 *
835 * Set request header name/value pairs.
836 */
837 PHP_METHOD(HttpRequest, setHeaders)
838 {
839 http_request_object_set_options_subr("headers", 1);
840 }
841 /* }}} */
842
843 /* {{{ proto array HttpRequest::getHeaders()
844 *
845 * Get previously set request headers.
846 */
847 PHP_METHOD(HttpRequest, getHeaders)
848 {
849 http_request_object_get_options_subr("headers");
850 }
851 /* }}} */
852
853 /* {{{ proto bool HttpRequest::setCookies([array cookies])
854 *
855 * Set cookies.
856 */
857 PHP_METHOD(HttpRequest, setCookies)
858 {
859 http_request_object_set_options_subr("cookies", 1);
860 }
861 /* }}} */
862
863 /* {{{ proto bool HttpRequest::addCookies(array cookies)
864 *
865 * Add cookies.
866 */
867 PHP_METHOD(HttpRequest, addCookies)
868 {
869 http_request_object_set_options_subr("cookies", 0);
870 }
871 /* }}} */
872
873 /* {{{ proto array HttpRequest::getCookies()
874 *
875 * Get previously set cookies.
876 */
877 PHP_METHOD(HttpRequest, getCookies)
878 {
879 http_request_object_get_options_subr("cookies");
880 }
881 /* }}} */
882
883 /* {{{ proto bool HttpRequest::setUrl(string url)
884 *
885 * Set the request URL.
886 */
887 PHP_METHOD(HttpRequest, setUrl)
888 {
889 char *URL = NULL;
890 int URL_len;
891 getObject(http_request_object, obj);
892
893 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
894 RETURN_FALSE;
895 }
896
897 UPD_STRL(obj, url, URL, URL_len);
898 RETURN_TRUE;
899 }
900 /* }}} */
901
902 /* {{{ proto string HttpRequest::getUrl()
903 *
904 * Get the previously set request URL.
905 */
906 PHP_METHOD(HttpRequest, getUrl)
907 {
908 NO_ARGS;
909
910 IF_RETVAL_USED {
911 getObject(http_request_object, obj);
912 zval *URL = GET_PROP(obj, url);
913
914 RETURN_ZVAL(URL, 1, 0);
915 }
916 }
917 /* }}} */
918
919 /* {{{ proto bool HttpRequest::setMethod(long request_method)
920 *
921 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
922 * <tt>HTTP_POST</tt> constants.
923 */
924 PHP_METHOD(HttpRequest, setMethod)
925 {
926 long meth;
927 getObject(http_request_object, obj);
928
929 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
930 RETURN_FALSE;
931 }
932
933 UPD_PROP(obj, long, method, meth);
934 RETURN_TRUE;
935 }
936 /* }}} */
937
938 /* {{{ proto long HttpRequest::getMethod()
939 *
940 * Get the previously set request method.
941 */
942 PHP_METHOD(HttpRequest, getMethod)
943 {
944 NO_ARGS;
945
946 IF_RETVAL_USED {
947 getObject(http_request_object, obj);
948 zval *meth = GET_PROP(obj, method);
949
950 RETURN_ZVAL(meth, 1, 0);
951 }
952 }
953 /* }}} */
954
955 /* {{{ proto bool HttpRequest::setContentType(string content_type)
956 *
957 * Set the content type the post request should have.
958 * Use this only if you know what you're doing.
959 */
960 PHP_METHOD(HttpRequest, setContentType)
961 {
962 char *ctype;
963 int ct_len;
964 getObject(http_request_object, obj);
965
966 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
967 RETURN_FALSE;
968 }
969
970 if (!strchr(ctype, '/')) {
971 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
972 RETURN_FALSE;
973 }
974
975 UPD_STRL(obj, contentType, ctype, ct_len);
976 RETURN_TRUE;
977 }
978 /* }}} */
979
980 /* {{{ proto string HttpRequest::getContentType()
981 *
982 * Get the previously content type.
983 */
984 PHP_METHOD(HttpRequest, getContentType)
985 {
986 NO_ARGS;
987
988 IF_RETVAL_USED {
989 getObject(http_request_object, obj);
990 zval *ctype = GET_PROP(obj, contentType);
991
992 RETURN_ZVAL(ctype, 1, 0);
993 }
994 }
995 /* }}} */
996
997 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
998 *
999 * Set the URL query parameters to use.
1000 * Overwrites previously set query parameters.
1001 * Affects any request types.
1002 */
1003 PHP_METHOD(HttpRequest, setQueryData)
1004 {
1005 zval *qdata = NULL;
1006 getObject(http_request_object, obj);
1007
1008 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
1009 RETURN_FALSE;
1010 }
1011
1012 if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
1013 UPD_STRL(obj, queryData, "", 0);
1014 } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1015 char *query_data = NULL;
1016
1017 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1018 RETURN_FALSE;
1019 }
1020
1021 UPD_PROP(obj, string, queryData, query_data);
1022 efree(query_data);
1023 } else {
1024 convert_to_string_ex(&qdata);
1025 UPD_STRL(obj, queryData, Z_STRVAL_P(qdata), Z_STRLEN_P(qdata));
1026 }
1027 RETURN_TRUE;
1028 }
1029 /* }}} */
1030
1031 /* {{{ proto string HttpRequest::getQueryData()
1032 *
1033 * Get the current query data in form of an urlencoded query string.
1034 */
1035 PHP_METHOD(HttpRequest, getQueryData)
1036 {
1037 NO_ARGS;
1038
1039 IF_RETVAL_USED {
1040 getObject(http_request_object, obj);
1041 zval *qdata = convert_to_type_ex(IS_STRING, GET_PROP(obj, queryData));
1042
1043 RETURN_ZVAL(qdata, 1, 0);
1044 }
1045 }
1046 /* }}} */
1047
1048 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1049 *
1050 * Add parameters to the query parameter list.
1051 * Affects any request type.
1052 */
1053 PHP_METHOD(HttpRequest, addQueryData)
1054 {
1055 zval *qdata, *old_qdata;
1056 char *query_data = NULL;
1057 size_t query_data_len = 0;
1058 getObject(http_request_object, obj);
1059
1060 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1061 RETURN_FALSE;
1062 }
1063
1064 old_qdata = convert_to_type_ex(IS_STRING, GET_PROP(obj, queryData));
1065
1066 if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, &query_data_len)) {
1067 RETURN_FALSE;
1068 }
1069
1070 UPD_STRL(obj, queryData, query_data, query_data_len);
1071 efree(query_data);
1072
1073 RETURN_TRUE;
1074 }
1075 /* }}} */
1076
1077 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1078 *
1079 * Adds POST data entries.
1080 * Affects only POST requests.
1081 */
1082 PHP_METHOD(HttpRequest, addPostFields)
1083 {
1084 zval *post, *post_data;
1085 getObject(http_request_object, obj);
1086
1087 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1088 RETURN_FALSE;
1089 }
1090
1091 post = convert_to_type(IS_ARRAY, GET_PROP(obj, postFields));
1092 array_merge(post_data, post);
1093
1094 RETURN_TRUE;
1095 }
1096 /* }}} */
1097
1098 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1099 *
1100 * Set the POST data entries.
1101 * Overwrites previously set POST data.
1102 * Affects only POST requests.
1103 */
1104 PHP_METHOD(HttpRequest, setPostFields)
1105 {
1106 zval *post, *post_data = NULL;
1107 getObject(http_request_object, obj);
1108
1109 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!", &post_data)) {
1110 RETURN_FALSE;
1111 }
1112
1113 post = convert_to_type(IS_ARRAY, GET_PROP(obj, postFields));
1114 zend_hash_clean(Z_ARRVAL_P(post));
1115
1116 if (post_data && zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1117 array_copy(post_data, post);
1118 }
1119
1120 RETURN_TRUE;
1121 }
1122 /* }}}*/
1123
1124 /* {{{ proto array HttpRequest::getPostFields()
1125 *
1126 * Get previously set POST data.
1127 */
1128 PHP_METHOD(HttpRequest, getPostFields)
1129 {
1130 NO_ARGS;
1131
1132 IF_RETVAL_USED {
1133 getObject(http_request_object, obj);
1134 zval *post_data = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, postFields));
1135
1136 array_init(return_value);
1137 array_copy(post_data, return_value);
1138 }
1139 }
1140 /* }}} */
1141
1142 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1143 *
1144 * Set raw post data to send. Don't forget to specify a content type.
1145 * Affects only POST requests.
1146 */
1147 PHP_METHOD(HttpRequest, setRawPostData)
1148 {
1149 char *raw_data = NULL;
1150 int data_len = 0;
1151 getObject(http_request_object, obj);
1152
1153 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &raw_data, &data_len)) {
1154 RETURN_FALSE;
1155 }
1156
1157 if (!raw_data) {
1158 raw_data = "";
1159 }
1160
1161 UPD_STRL(obj, rawPostData, raw_data, data_len);
1162 RETURN_TRUE;
1163 }
1164 /* }}} */
1165
1166 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1167 *
1168 * Add raw post data.
1169 * Affects only POST requests.
1170 */
1171 PHP_METHOD(HttpRequest, addRawPostData)
1172 {
1173 char *raw_data, *new_data;
1174 int data_len;
1175 getObject(http_request_object, obj);
1176
1177 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw_data, &data_len)) {
1178 RETURN_FALSE;
1179 }
1180
1181 if (data_len) {
1182 zval *zdata = convert_to_type_ex(IS_STRING, GET_PROP(obj, rawPostData));
1183
1184 new_data = emalloc(Z_STRLEN_P(zdata) + data_len + 1);
1185 new_data[Z_STRLEN_P(zdata) + data_len] = '\0';
1186
1187 if (Z_STRLEN_P(zdata)) {
1188 memcpy(new_data, Z_STRVAL_P(zdata), Z_STRLEN_P(zdata));
1189 }
1190
1191 memcpy(new_data + Z_STRLEN_P(zdata), raw_data, data_len);
1192 UPD_STRL(obj, rawPostData, new_data, Z_STRLEN_P(zdata) + data_len);
1193 }
1194
1195 RETURN_TRUE;
1196 }
1197 /* }}} */
1198
1199 /* {{{ proto string HttpRequest::getRawPostData()
1200 *
1201 * Get previously set raw post data.
1202 */
1203 PHP_METHOD(HttpRequest, getRawPostData)
1204 {
1205 NO_ARGS;
1206
1207 IF_RETVAL_USED {
1208 getObject(http_request_object, obj);
1209 zval *raw_data = convert_to_type_ex(IS_STRING, GET_PROP(obj, rawPostData));
1210
1211 RETURN_ZVAL(raw_data, 1, 0);
1212 }
1213 }
1214 /* }}} */
1215
1216 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1217 *
1218 * Add a file to the POST request.
1219 * Affects only POST requests.
1220 */
1221 PHP_METHOD(HttpRequest, addPostFile)
1222 {
1223 zval *files, *entry;
1224 char *name, *file, *type = NULL;
1225 int name_len, file_len, type_len = 0;
1226 getObject(http_request_object, obj);
1227
1228 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1229 RETURN_FALSE;
1230 }
1231
1232 if (type_len) {
1233 if (!strchr(type, '/')) {
1234 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
1235 RETURN_FALSE;
1236 }
1237 } else {
1238 type = "application/x-octetstream";
1239 type_len = sizeof("application/x-octetstream") - 1;
1240 }
1241
1242 MAKE_STD_ZVAL(entry);
1243 array_init(entry);
1244
1245 add_assoc_stringl(entry, "name", name, name_len, 1);
1246 add_assoc_stringl(entry, "type", type, type_len, 1);
1247 add_assoc_stringl(entry, "file", file, file_len, 1);
1248
1249 files = convert_to_type(IS_ARRAY, GET_PROP(obj, postFiles));
1250 add_next_index_zval(files, entry);
1251
1252 RETURN_TRUE;
1253 }
1254 /* }}} */
1255
1256 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1257 *
1258 * Set files to post.
1259 * Overwrites previously set post files.
1260 * Affects only POST requests.
1261 */
1262 PHP_METHOD(HttpRequest, setPostFiles)
1263 {
1264 zval *files, *pFiles;
1265 getObject(http_request_object, obj);
1266
1267 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &files)) {
1268 RETURN_FALSE;
1269 }
1270
1271 pFiles = convert_to_type(IS_ARRAY, GET_PROP(obj, postFiles));
1272 zend_hash_clean(Z_ARRVAL_P(pFiles));
1273
1274 if (files && zend_hash_num_elements(Z_ARRVAL_P(files))) {
1275 array_copy(files, pFiles);
1276 }
1277
1278 RETURN_TRUE;
1279 }
1280 /* }}} */
1281
1282 /* {{{ proto array HttpRequest::getPostFiles()
1283 *
1284 * Get all previously added POST files.
1285 */
1286 PHP_METHOD(HttpRequest, getPostFiles)
1287 {
1288 NO_ARGS;
1289
1290 IF_RETVAL_USED {
1291 getObject(http_request_object, obj);
1292 zval *files = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, postFiles));
1293
1294 array_init(return_value);
1295 array_copy(files, return_value);
1296 }
1297 }
1298 /* }}} */
1299
1300 /* {{{ proto bool HttpRequest::setPutFile([string file])
1301 *
1302 * Set file to put.
1303 * Affects only PUT requests.
1304 */
1305 PHP_METHOD(HttpRequest, setPutFile)
1306 {
1307 char *file = "";
1308 int file_len = 0;
1309 getObject(http_request_object, obj);
1310
1311 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &file, &file_len)) {
1312 RETURN_FALSE;
1313 }
1314
1315 UPD_STRL(obj, putFile, file, file_len);
1316 RETURN_TRUE;
1317 }
1318 /* }}} */
1319
1320 /* {{{ proto string HttpRequest::getPutFile()
1321 *
1322 * Get previously set put file.
1323 */
1324 PHP_METHOD(HttpRequest, getPutFile)
1325 {
1326 NO_ARGS;
1327
1328 IF_RETVAL_USED {
1329 getObject(http_request_object, obj);
1330 zval *putfile = convert_to_type_ex(IS_STRING, GET_PROP(obj, putFile));
1331
1332 RETURN_ZVAL(putfile, 1, 0);
1333 }
1334 }
1335 /* }}} */
1336
1337 /* {{{ proto array HttpRequest::getResponseData()
1338 *
1339 * Get all response data after the request has been sent.
1340 */
1341 PHP_METHOD(HttpRequest, getResponseData)
1342 {
1343 NO_ARGS;
1344
1345 IF_RETVAL_USED {
1346 getObject(http_request_object, obj);
1347 zval *data = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, responseData));
1348
1349 array_init(return_value);
1350 array_copy(data, return_value);
1351 }
1352 }
1353 /* }}} */
1354
1355 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1356 *
1357 * Get response header(s) after the request has been sent.
1358 */
1359 PHP_METHOD(HttpRequest, getResponseHeader)
1360 {
1361 IF_RETVAL_USED {
1362 zval *data, **headers, **header;
1363 char *header_name = NULL;
1364 int header_len = 0;
1365 getObject(http_request_object, obj);
1366
1367 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1368 RETURN_FALSE;
1369 }
1370
1371 data = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, responseData));
1372 if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1373 RETURN_FALSE;
1374 }
1375 convert_to_array_ex(headers);
1376 if (!header_len || !header_name) {
1377 array_init(return_value);
1378 array_copy(*headers, return_value);
1379 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1380 RETURN_ZVAL(*header, 1, 0);
1381 } else {
1382 RETURN_FALSE;
1383 }
1384 }
1385 }
1386 /* }}} */
1387
1388 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1389 *
1390 * Get response cookie(s) after the request has been sent.
1391 */
1392 PHP_METHOD(HttpRequest, getResponseCookie)
1393 {
1394 IF_RETVAL_USED {
1395 zval *data, **headers;
1396 char *cookie_name = NULL;
1397 int cookie_len = 0;
1398 getObject(http_request_object, obj);
1399
1400 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
1401 RETURN_FALSE;
1402 }
1403
1404 array_init(return_value);
1405
1406 data = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, responseData));
1407 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1408 ulong idx = 0;
1409 char *key = NULL;
1410 zval **header = NULL;
1411
1412 convert_to_array_ex(headers);
1413 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers), key, idx, header) {
1414 if (key && !strcasecmp(key, "Set-Cookie")) {
1415 /* several cookies? */
1416 if (Z_TYPE_PP(header) == IS_ARRAY) {
1417 zval **cookie;
1418
1419 FOREACH_HASH_VAL(Z_ARRVAL_PP(header), cookie) {
1420 zval *cookie_hash;
1421 MAKE_STD_ZVAL(cookie_hash);
1422 array_init(cookie_hash);
1423
1424 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
1425 if (!cookie_len) {
1426 add_next_index_zval(return_value, cookie_hash);
1427 } else {
1428 zval **name;
1429
1430 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1431 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1432 add_next_index_zval(return_value, cookie_hash);
1433 return; /* <<< FOUND >>> */
1434 } else {
1435 zval_dtor(cookie_hash);
1436 efree(cookie_hash);
1437 }
1438 }
1439 } else {
1440 zval_dtor(cookie_hash);
1441 efree(cookie_hash);
1442 }
1443 }
1444 } else {
1445 zval *cookie_hash;
1446
1447 MAKE_STD_ZVAL(cookie_hash);
1448 array_init(cookie_hash);
1449 convert_to_string_ex(header);
1450
1451 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
1452 if (!cookie_len) {
1453 add_next_index_zval(return_value, cookie_hash);
1454 } else {
1455 zval **name;
1456
1457 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1458 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1459 add_next_index_zval(return_value, cookie_hash);
1460 } else {
1461 zval_dtor(cookie_hash);
1462 efree(cookie_hash);
1463 }
1464 }
1465 } else {
1466 zval_dtor(cookie_hash);
1467 efree(cookie_hash);
1468 }
1469 }
1470 break;
1471 }
1472 /* reset key */
1473 key = NULL;
1474 }
1475 }
1476 }
1477 }
1478 /* }}} */
1479
1480 /* {{{ proto string HttpRequest::getResponseBody()
1481 *
1482 * Get the response body after the request has been sent.
1483 */
1484 PHP_METHOD(HttpRequest, getResponseBody)
1485 {
1486 NO_ARGS;
1487
1488 IF_RETVAL_USED {
1489 zval **body;
1490 getObject(http_request_object, obj);
1491 zval *data = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, responseData));
1492
1493 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1494 convert_to_string_ex(body);
1495 RETURN_ZVAL(*body, 1, 0);
1496 } else {
1497 RETURN_FALSE;
1498 }
1499 }
1500 }
1501 /* }}} */
1502
1503 /* {{{ proto int HttpRequest::getResponseCode()
1504 *
1505 * Get the response code after the request has been sent.
1506 */
1507 PHP_METHOD(HttpRequest, getResponseCode)
1508 {
1509 NO_ARGS;
1510
1511 IF_RETVAL_USED {
1512 getObject(http_request_object, obj);
1513 zval *code = convert_to_type_ex(IS_LONG, GET_PROP(obj, responseCode));
1514
1515 RETURN_ZVAL(code, 1, 0);
1516 }
1517 }
1518 /* }}} */
1519
1520 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1521 *
1522 * Get response info after the request has been sent.
1523 * See http_get() for a full list of returned info.
1524 */
1525 PHP_METHOD(HttpRequest, getResponseInfo)
1526 {
1527 IF_RETVAL_USED {
1528 zval *info, **infop;
1529 char *info_name = NULL;
1530 int info_len = 0;
1531 getObject(http_request_object, obj);
1532
1533 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1534 RETURN_FALSE;
1535 }
1536
1537 info = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, responseInfo));
1538
1539 if (info_len && info_name) {
1540 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1541 RETURN_ZVAL(*infop, 1, 0);
1542 } else {
1543 http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
1544 RETURN_FALSE;
1545 }
1546 } else {
1547 array_init(return_value);
1548 array_copy(info, return_value);
1549 }
1550 }
1551 }
1552 /* }}}*/
1553
1554 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1555 *
1556 * Get the full response as HttpMessage object.
1557 */
1558 PHP_METHOD(HttpRequest, getResponseMessage)
1559 {
1560 NO_ARGS;
1561
1562 IF_RETVAL_USED {
1563 zval *message;
1564 getObject(http_request_object, obj);
1565
1566 SET_EH_THROW_HTTP();
1567 message = GET_PROP(obj, responseMessage);
1568 if (Z_TYPE_P(message) == IS_OBJECT) {
1569 RETVAL_OBJECT(message);
1570 } else {
1571 RETVAL_NULL();
1572 }
1573 SET_EH_NORMAL();
1574 }
1575 }
1576 /* }}} */
1577
1578 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1579 *
1580 * Get sent HTTP message.
1581 */
1582 PHP_METHOD(HttpRequest, getRequestMessage)
1583 {
1584 NO_ARGS;
1585
1586 IF_RETVAL_USED {
1587 http_message *msg;
1588 getObject(http_request_object, obj);
1589
1590 SET_EH_THROW_HTTP();
1591 if (msg = http_message_parse(PHPSTR_VAL(&obj->request), PHPSTR_LEN(&obj->request))) {
1592 RETVAL_OBJVAL(http_message_object_from_msg(msg));
1593 }
1594 SET_EH_NORMAL();
1595 }
1596 }
1597 /* }}} */
1598
1599 PHP_METHOD(HttpRequest, getHistory)
1600 {
1601 NO_ARGS;
1602
1603 IF_RETVAL_USED {
1604 http_message *msg;
1605 getObject(http_request_object, obj);
1606
1607 SET_EH_THROW_HTTP();
1608 if (msg = http_message_parse(PHPSTR_VAL(&obj->history), PHPSTR_LEN(&obj->history))) {
1609 RETVAL_OBJVAL(http_message_object_from_msg(msg));
1610 }
1611 SET_EH_NORMAL();
1612 }
1613 }
1614
1615 /* {{{ proto HttpMessage HttpRequest::send()
1616 *
1617 * Send the HTTP request.
1618 *
1619 * GET example:
1620 * <pre>
1621 * <?php
1622 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1623 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1624 * $r->addQueryData(array('category' => 3));
1625 * try {
1626 * $r->send();
1627 * if ($r->getResponseCode() == 200) {
1628 * file_put_contents('local.rss', $r->getResponseBody());
1629 * }
1630 * } catch (HttpException $ex) {
1631 * echo $ex;
1632 * }
1633 * ?>
1634 * </pre>
1635 *
1636 * POST example:
1637 * <pre>
1638 * <?php
1639 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1640 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1641 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1642 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1643 * try {
1644 * echo $r->send()->getBody();
1645 * } catch (HttpException $ex) {
1646 * echo $ex;
1647 * }
1648 * ?>
1649 * </pre>
1650 */
1651 PHP_METHOD(HttpRequest, send)
1652 {
1653 http_request_body body = {0, NULL, 0};
1654 getObject(http_request_object, obj);
1655
1656 NO_ARGS;
1657
1658 SET_EH_THROW_HTTP();
1659
1660 if (obj->pool) {
1661 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1662 SET_EH_NORMAL();
1663 RETURN_FALSE;
1664 }
1665
1666 RETVAL_NULL();
1667
1668 if ( (SUCCESS == http_request_object_requesthandler(obj, getThis(), &body)) &&
1669 (SUCCESS == http_request_exec(obj->ch, NULL, &obj->response, &obj->request)) &&
1670 (SUCCESS == http_request_object_responsehandler(obj, getThis()))) {
1671 RETVAL_OBJECT(GET_PROP(obj, responseMessage));
1672 }
1673 http_request_body_dtor(&body);
1674
1675 SET_EH_NORMAL();
1676 }
1677 /* }}} */
1678
1679 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1680
1681 /*
1682 * Local variables:
1683 * tab-width: 4
1684 * c-basic-offset: 4
1685 * End:
1686 * vim600: noet sw=4 ts=4 fdm=marker
1687 * vim<600: noet sw=4 ts=4
1688 */
1689