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