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