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