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