b40ef8992457e8a9833cf8e23c6324676aa4682c
[m6w6/ext-http] / http_request_object.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_CURL
16 #include "php_http.h"
17
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
19
20 #include "zend_interfaces.h"
21
22 #include "php_http_api.h"
23 #include "php_http_cookie_api.h"
24 #include "php_http_exception_object.h"
25 #include "php_http_message_api.h"
26 #include "php_http_message_object.h"
27 #include "php_http_request_api.h"
28 #include "php_http_request_object.h"
29 #include "php_http_request_pool_api.h"
30 #include "php_http_url_api.h"
31
32 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, 0, req_args)
33 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpRequest, method, 0)
34 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
35 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
36
37 HTTP_BEGIN_ARGS(__construct, 0)
38 HTTP_ARG_VAL(url, 0)
39 HTTP_ARG_VAL(method, 0)
40 HTTP_ARG_VAL(options, 0)
41 HTTP_END_ARGS;
42
43 HTTP_EMPTY_ARGS(getOptions);
44 HTTP_BEGIN_ARGS(setOptions, 0)
45 HTTP_ARG_VAL(options, 0)
46 HTTP_END_ARGS;
47
48 HTTP_EMPTY_ARGS(getSslOptions);
49 HTTP_BEGIN_ARGS(setSslOptions, 0)
50 HTTP_ARG_VAL(ssl_options, 0)
51 HTTP_END_ARGS;
52
53 HTTP_BEGIN_ARGS(addSslOptions, 0)
54 HTTP_ARG_VAL(ssl_optins, 0)
55 HTTP_END_ARGS;
56
57 HTTP_EMPTY_ARGS(getHeaders);
58 HTTP_BEGIN_ARGS(setHeaders, 0)
59 HTTP_ARG_VAL(headers, 0)
60 HTTP_END_ARGS;
61
62 HTTP_BEGIN_ARGS(addHeaders, 1)
63 HTTP_ARG_VAL(headers, 0)
64 HTTP_END_ARGS;
65
66 HTTP_EMPTY_ARGS(getCookies);
67 HTTP_BEGIN_ARGS(setCookies, 0)
68 HTTP_ARG_VAL(cookies, 0)
69 HTTP_END_ARGS;
70
71 HTTP_BEGIN_ARGS(addCookies, 1)
72 HTTP_ARG_VAL(cookies, 0)
73 HTTP_END_ARGS;
74
75 HTTP_EMPTY_ARGS(enableCookies);
76 #if HTTP_CURL_VERSION(7,14,1)
77 HTTP_BEGIN_ARGS(resetCookies, 0)
78 HTTP_ARG_VAL(session_only, 0)
79 HTTP_END_ARGS;
80 #endif
81
82 HTTP_EMPTY_ARGS(getUrl);
83 HTTP_BEGIN_ARGS(setUrl, 1)
84 HTTP_ARG_VAL(url, 0)
85 HTTP_END_ARGS;
86
87 HTTP_EMPTY_ARGS(getMethod);
88 HTTP_BEGIN_ARGS(setMethod, 1)
89 HTTP_ARG_VAL(request_method, 0)
90 HTTP_END_ARGS;
91
92 HTTP_EMPTY_ARGS(getContentType);
93 HTTP_BEGIN_ARGS(setContentType, 1)
94 HTTP_ARG_VAL(content_type, 0)
95 HTTP_END_ARGS;
96
97 HTTP_EMPTY_ARGS(getQueryData);
98 HTTP_BEGIN_ARGS(setQueryData, 0)
99 HTTP_ARG_VAL(query_data, 0)
100 HTTP_END_ARGS;
101
102 HTTP_BEGIN_ARGS(addQueryData, 1)
103 HTTP_ARG_VAL(query_data, 0)
104 HTTP_END_ARGS;
105
106 HTTP_EMPTY_ARGS(getPostFields);
107 HTTP_BEGIN_ARGS(setPostFields, 0)
108 HTTP_ARG_VAL(post_fields, 0)
109 HTTP_END_ARGS;
110
111 HTTP_BEGIN_ARGS(addPostFields, 1)
112 HTTP_ARG_VAL(post_fields, 0)
113 HTTP_END_ARGS;
114
115 HTTP_EMPTY_ARGS(getPostFiles);
116 HTTP_BEGIN_ARGS(setPostFiles, 0)
117 HTTP_ARG_VAL(post_files, 0)
118 HTTP_END_ARGS;
119
120 HTTP_BEGIN_ARGS(addPostFile, 2)
121 HTTP_ARG_VAL(formname, 0)
122 HTTP_ARG_VAL(filename, 0)
123 HTTP_ARG_VAL(content_type, 0)
124 HTTP_END_ARGS;
125
126 HTTP_EMPTY_ARGS(getRawPostData);
127 HTTP_BEGIN_ARGS(setRawPostData, 0)
128 HTTP_ARG_VAL(raw_post_data, 0)
129 HTTP_END_ARGS;
130
131 HTTP_BEGIN_ARGS(addRawPostData, 1)
132 HTTP_ARG_VAL(raw_post_data, 0)
133 HTTP_END_ARGS;
134
135 HTTP_EMPTY_ARGS(getPutFile);
136 HTTP_BEGIN_ARGS(setPutFile, 0)
137 HTTP_ARG_VAL(filename, 0)
138 HTTP_END_ARGS;
139
140 HTTP_EMPTY_ARGS(getPutData);
141 HTTP_BEGIN_ARGS(setPutData, 0)
142 HTTP_ARG_VAL(put_data, 0)
143 HTTP_END_ARGS;
144
145 HTTP_BEGIN_ARGS(addPutData, 1)
146 HTTP_ARG_VAL(put_data, 0)
147 HTTP_END_ARGS;
148
149 HTTP_EMPTY_ARGS(getResponseData);
150 HTTP_BEGIN_ARGS(getResponseHeader, 0)
151 HTTP_ARG_VAL(name, 0)
152 HTTP_END_ARGS;
153
154 HTTP_BEGIN_ARGS(getResponseCookies, 0)
155 HTTP_ARG_VAL(flags, 0)
156 HTTP_ARG_VAL(allowed_extras, 0)
157 HTTP_END_ARGS;
158
159 HTTP_EMPTY_ARGS(getResponseBody);
160 HTTP_EMPTY_ARGS(getResponseCode);
161 HTTP_EMPTY_ARGS(getResponseStatus);
162 HTTP_BEGIN_ARGS(getResponseInfo, 0)
163 HTTP_ARG_VAL(name, 0)
164 HTTP_END_ARGS;
165
166 HTTP_EMPTY_ARGS(getResponseMessage);
167 HTTP_EMPTY_ARGS(getRawResponseMessage);
168 HTTP_EMPTY_ARGS(getRequestMessage);
169 HTTP_EMPTY_ARGS(getRawRequestMessage);
170 HTTP_EMPTY_ARGS(getHistory);
171 HTTP_EMPTY_ARGS(clearHistory);
172 HTTP_EMPTY_ARGS(send);
173
174 HTTP_BEGIN_ARGS(get, 1)
175 HTTP_ARG_VAL(url, 0)
176 HTTP_ARG_VAL(options, 0)
177 HTTP_ARG_VAL(info, 1)
178 HTTP_END_ARGS;
179
180 HTTP_BEGIN_ARGS(head, 1)
181 HTTP_ARG_VAL(url, 0)
182 HTTP_ARG_VAL(options, 0)
183 HTTP_ARG_VAL(info, 1)
184 HTTP_END_ARGS;
185
186 HTTP_BEGIN_ARGS(postData, 2)
187 HTTP_ARG_VAL(url, 0)
188 HTTP_ARG_VAL(data, 0)
189 HTTP_ARG_VAL(options, 0)
190 HTTP_ARG_VAL(info, 1)
191 HTTP_END_ARGS;
192
193 HTTP_BEGIN_ARGS(postFields, 2)
194 HTTP_ARG_VAL(url, 0)
195 HTTP_ARG_VAL(data, 0)
196 HTTP_ARG_VAL(options, 0)
197 HTTP_ARG_VAL(info, 1)
198 HTTP_END_ARGS;
199
200 HTTP_BEGIN_ARGS(putData, 2)
201 HTTP_ARG_VAL(url, 0)
202 HTTP_ARG_VAL(data, 0)
203 HTTP_ARG_VAL(options, 0)
204 HTTP_ARG_VAL(info, 1)
205 HTTP_END_ARGS;
206
207 HTTP_BEGIN_ARGS(putFile, 2)
208 HTTP_ARG_VAL(url, 0)
209 HTTP_ARG_VAL(file, 0)
210 HTTP_ARG_VAL(options, 0)
211 HTTP_ARG_VAL(info, 1)
212 HTTP_END_ARGS;
213
214 HTTP_BEGIN_ARGS(putStream, 2)
215 HTTP_ARG_VAL(url, 0)
216 HTTP_ARG_VAL(stream, 0)
217 HTTP_ARG_VAL(options, 0)
218 HTTP_ARG_VAL(info, 1)
219 HTTP_END_ARGS;
220
221 HTTP_BEGIN_ARGS(methodRegister, 1)
222 HTTP_ARG_VAL(method_name, 0)
223 HTTP_END_ARGS;
224
225 HTTP_BEGIN_ARGS(methodUnregister, 1)
226 HTTP_ARG_VAL(method, 0)
227 HTTP_END_ARGS;
228
229 HTTP_BEGIN_ARGS(methodName, 1)
230 HTTP_ARG_VAL(method_id, 0)
231 HTTP_END_ARGS;
232
233 HTTP_BEGIN_ARGS(methodExists, 1)
234 HTTP_ARG_VAL(method, 0)
235 HTTP_END_ARGS;
236
237 #ifdef HAVE_CURL_GETFORMDATA
238 HTTP_BEGIN_ARGS(encodeBody, 2)
239 HTTP_ARG_VAL(fields, 0)
240 HTTP_ARG_VAL(files, 0)
241 HTTP_END_ARGS;
242 #endif
243
244 #define OBJ_PROP_CE http_request_object_ce
245 zend_class_entry *http_request_object_ce;
246 zend_function_entry http_request_object_fe[] = {
247 HTTP_REQUEST_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
248
249 HTTP_REQUEST_ME(setOptions, ZEND_ACC_PUBLIC)
250 HTTP_REQUEST_ME(getOptions, ZEND_ACC_PUBLIC)
251 HTTP_REQUEST_ME(setSslOptions, ZEND_ACC_PUBLIC)
252 HTTP_REQUEST_ME(getSslOptions, ZEND_ACC_PUBLIC)
253 HTTP_REQUEST_ME(addSslOptions, ZEND_ACC_PUBLIC)
254
255 HTTP_REQUEST_ME(addHeaders, ZEND_ACC_PUBLIC)
256 HTTP_REQUEST_ME(getHeaders, ZEND_ACC_PUBLIC)
257 HTTP_REQUEST_ME(setHeaders, ZEND_ACC_PUBLIC)
258
259 HTTP_REQUEST_ME(addCookies, ZEND_ACC_PUBLIC)
260 HTTP_REQUEST_ME(getCookies, ZEND_ACC_PUBLIC)
261 HTTP_REQUEST_ME(setCookies, ZEND_ACC_PUBLIC)
262
263 HTTP_REQUEST_ME(enableCookies, ZEND_ACC_PUBLIC)
264 #if HTTP_CURL_VERSION(7,14,1)
265 HTTP_REQUEST_ME(resetCookies, ZEND_ACC_PUBLIC)
266 #endif
267
268 HTTP_REQUEST_ME(setMethod, ZEND_ACC_PUBLIC)
269 HTTP_REQUEST_ME(getMethod, ZEND_ACC_PUBLIC)
270
271 HTTP_REQUEST_ME(setUrl, ZEND_ACC_PUBLIC)
272 HTTP_REQUEST_ME(getUrl, ZEND_ACC_PUBLIC)
273
274 HTTP_REQUEST_ME(setContentType, ZEND_ACC_PUBLIC)
275 HTTP_REQUEST_ME(getContentType, ZEND_ACC_PUBLIC)
276
277 HTTP_REQUEST_ME(setQueryData, ZEND_ACC_PUBLIC)
278 HTTP_REQUEST_ME(getQueryData, ZEND_ACC_PUBLIC)
279 HTTP_REQUEST_ME(addQueryData, ZEND_ACC_PUBLIC)
280
281 HTTP_REQUEST_ME(setPostFields, ZEND_ACC_PUBLIC)
282 HTTP_REQUEST_ME(getPostFields, ZEND_ACC_PUBLIC)
283 HTTP_REQUEST_ME(addPostFields, ZEND_ACC_PUBLIC)
284
285 HTTP_REQUEST_ME(setRawPostData, ZEND_ACC_PUBLIC)
286 HTTP_REQUEST_ME(getRawPostData, ZEND_ACC_PUBLIC)
287 HTTP_REQUEST_ME(addRawPostData, ZEND_ACC_PUBLIC)
288
289 HTTP_REQUEST_ME(setPostFiles, ZEND_ACC_PUBLIC)
290 HTTP_REQUEST_ME(addPostFile, ZEND_ACC_PUBLIC)
291 HTTP_REQUEST_ME(getPostFiles, ZEND_ACC_PUBLIC)
292
293 HTTP_REQUEST_ME(setPutFile, ZEND_ACC_PUBLIC)
294 HTTP_REQUEST_ME(getPutFile, ZEND_ACC_PUBLIC)
295
296 HTTP_REQUEST_ME(setPutData, ZEND_ACC_PUBLIC)
297 HTTP_REQUEST_ME(getPutData, ZEND_ACC_PUBLIC)
298 HTTP_REQUEST_ME(addPutData, ZEND_ACC_PUBLIC)
299
300 HTTP_REQUEST_ME(send, ZEND_ACC_PUBLIC)
301
302 HTTP_REQUEST_ME(getResponseData, ZEND_ACC_PUBLIC)
303 HTTP_REQUEST_ME(getResponseHeader, ZEND_ACC_PUBLIC)
304 HTTP_REQUEST_ME(getResponseCookies, ZEND_ACC_PUBLIC)
305 HTTP_REQUEST_ME(getResponseCode, ZEND_ACC_PUBLIC)
306 HTTP_REQUEST_ME(getResponseStatus, ZEND_ACC_PUBLIC)
307 HTTP_REQUEST_ME(getResponseBody, ZEND_ACC_PUBLIC)
308 HTTP_REQUEST_ME(getResponseInfo, ZEND_ACC_PUBLIC)
309 HTTP_REQUEST_ME(getResponseMessage, ZEND_ACC_PUBLIC)
310 HTTP_REQUEST_ME(getRawResponseMessage, ZEND_ACC_PUBLIC)
311 HTTP_REQUEST_ME(getRequestMessage, ZEND_ACC_PUBLIC)
312 HTTP_REQUEST_ME(getRawRequestMessage, ZEND_ACC_PUBLIC)
313 HTTP_REQUEST_ME(getHistory, ZEND_ACC_PUBLIC)
314 HTTP_REQUEST_ME(clearHistory, ZEND_ACC_PUBLIC)
315
316 HTTP_REQUEST_ALIAS(get, http_get)
317 HTTP_REQUEST_ALIAS(head, http_head)
318 HTTP_REQUEST_ALIAS(postData, http_post_data)
319 HTTP_REQUEST_ALIAS(postFields, http_post_fields)
320 HTTP_REQUEST_ALIAS(putData, http_put_data)
321 HTTP_REQUEST_ALIAS(putFile, http_put_file)
322 HTTP_REQUEST_ALIAS(putStream, http_put_stream)
323
324 HTTP_REQUEST_ALIAS(methodRegister, http_request_method_register)
325 HTTP_REQUEST_ALIAS(methodUnregister, http_request_method_unregister)
326 HTTP_REQUEST_ALIAS(methodName, http_request_method_name)
327 HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists)
328 #ifdef HAVE_CURL_GETFORMDATA
329 HTTP_REQUEST_ALIAS(encodeBody, http_request_body_encode)
330 #endif
331 EMPTY_FUNCTION_ENTRY
332 };
333 static zend_object_handlers http_request_object_handlers;
334
335 PHP_MINIT_FUNCTION(http_request_object)
336 {
337 HTTP_REGISTER_CLASS_EX(HttpRequest, http_request_object, NULL, 0);
338 http_request_object_handlers.clone_obj = _http_request_object_clone_obj;
339
340 DCL_PROP_N(PRIVATE, options);
341 DCL_PROP_N(PRIVATE, postFields);
342 DCL_PROP_N(PRIVATE, postFiles);
343 DCL_PROP_N(PRIVATE, responseInfo);
344 DCL_PROP_N(PRIVATE, responseData);
345 DCL_PROP_N(PRIVATE, responseMessage);
346 DCL_PROP(PRIVATE, long, responseCode, 0);
347 DCL_PROP(PRIVATE, string, responseStatus, "");
348 DCL_PROP(PRIVATE, long, method, HTTP_GET);
349 DCL_PROP(PRIVATE, string, url, "");
350 DCL_PROP(PRIVATE, string, contentType, "");
351 DCL_PROP(PRIVATE, string, rawPostData, "");
352 DCL_PROP(PRIVATE, string, queryData, "");
353 DCL_PROP(PRIVATE, string, putFile, "");
354 DCL_PROP(PRIVATE, string, putData, "");
355 DCL_PROP_N(PRIVATE, history);
356 DCL_PROP(PUBLIC, bool, recordHistory, 0);
357
358 #ifndef WONKY
359 /*
360 * Request Method Constants
361 */
362 /* HTTP/1.1 */
363 DCL_CONST(long, "METH_GET", HTTP_GET);
364 DCL_CONST(long, "METH_HEAD", HTTP_HEAD);
365 DCL_CONST(long, "METH_POST", HTTP_POST);
366 DCL_CONST(long, "METH_PUT", HTTP_PUT);
367 DCL_CONST(long, "METH_DELETE", HTTP_DELETE);
368 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS);
369 DCL_CONST(long, "METH_TRACE", HTTP_TRACE);
370 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT);
371 /* WebDAV - RFC 2518 */
372 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND);
373 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH);
374 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL);
375 DCL_CONST(long, "METH_COPY", HTTP_COPY);
376 DCL_CONST(long, "METH_MOVE", HTTP_MOVE);
377 DCL_CONST(long, "METH_LOCK", HTTP_LOCK);
378 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK);
379 /* WebDAV Versioning - RFC 3253 */
380 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL);
381 DCL_CONST(long, "METH_REPORT", HTTP_REPORT);
382 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT);
383 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN);
384 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT);
385 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE);
386 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE);
387 DCL_CONST(long, "METH_LABEL", HTTP_LABEL);
388 DCL_CONST(long, "METH_MERGE", HTTP_MERGE);
389 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL);
390 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY);
391 /* WebDAV Access Control - RFC 3744 */
392 DCL_CONST(long, "METH_ACL", HTTP_ACL);
393
394 /*
395 * HTTP Protocol Version Constants
396 */
397 DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0);
398 DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1);
399 DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE);
400
401 /*
402 * Auth Constants
403 */
404 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC);
405 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST);
406 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM);
407 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY);
408
409 /*
410 * Proxy Type Constants
411 */
412 # if HTTP_CURL_VERSION(7,15,2)
413 DCL_CONST(long, "PROXY_SOCKS4", CURLPROXY_SOCKS4);
414 # endif
415 DCL_CONST(long, "PROXY_SOCKS5", CURLPROXY_SOCKS5);
416 DCL_CONST(long, "PROXY_HTTP", CURLPROXY_HTTP);
417 #endif /* WONKY */
418
419 return SUCCESS;
420 }
421
422 zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC)
423 {
424 return http_request_object_new_ex(ce, NULL, NULL);
425 }
426
427 zend_object_value _http_request_object_new_ex(zend_class_entry *ce, CURL *ch, http_request_object **ptr TSRMLS_DC)
428 {
429 zend_object_value ov;
430 http_request_object *o;
431
432 o = ecalloc(1, sizeof(http_request_object));
433 o->zo.ce = ce;
434 o->request = http_request_init_ex(NULL, ch, 0, NULL);
435
436 if (ptr) {
437 *ptr = o;
438 }
439
440 ALLOC_HASHTABLE(OBJ_PROP(o));
441 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
442 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
443
444 ov.handle = putObject(http_request_object, o);
445 ov.handlers = &http_request_object_handlers;
446
447 return ov;
448 }
449
450 zend_object_value _http_request_object_clone_obj(zval *this_ptr TSRMLS_DC)
451 {
452 zend_object_value new_ov;
453 http_request_object *new_obj;
454 getObject(http_request_object, old_obj);
455
456 new_ov = http_request_object_new_ex(old_obj->zo.ce, NULL, &new_obj);
457 if (old_obj->request->ch) {
458 http_curl_init_ex(curl_easy_duphandle(old_obj->request->ch), new_obj->request);
459 }
460
461 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
462 phpstr_append(&new_obj->request->conv.request, old_obj->request->conv.request.data, old_obj->request->conv.request.used);
463 phpstr_append(&new_obj->request->conv.response, old_obj->request->conv.response.data, old_obj->request->conv.response.used);
464
465 return new_ov;
466 }
467
468 void _http_request_object_free(zend_object *object TSRMLS_DC)
469 {
470 http_request_object *o = (http_request_object *) object;
471
472 http_request_free(&o->request);
473 freeObject(o);
474 }
475
476 #define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC)
477 static inline void _http_request_object_check_request_content_type(zval *this_ptr TSRMLS_DC)
478 {
479 zval *ctype = GET_PROP(contentType);
480
481 if (Z_STRLEN_P(ctype)) {
482 zval **headers, *opts = GET_PROP(options);
483
484 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
485 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void *) &headers)) &&
486 (Z_TYPE_PP(headers) == IS_ARRAY)) {
487 zval **ct_header;
488
489 /* only override if not already set */
490 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void *) &ct_header))) {
491 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
492 } else
493 /* or not a string, zero length string or a string of spaces */
494 if ((Z_TYPE_PP(ct_header) != IS_STRING) || !Z_STRLEN_PP(ct_header)) {
495 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
496 } else {
497 int i, only_space = 1;
498
499 /* check for spaces only */
500 for (i = 0; i < Z_STRLEN_PP(ct_header); ++i) {
501 if (!isspace(Z_STRVAL_PP(ct_header)[i])) {
502 only_space = 0;
503 break;
504 }
505 }
506 if (only_space) {
507 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
508 }
509 }
510 } else {
511 zval *headers;
512
513 MAKE_STD_ZVAL(headers);
514 array_init(headers);
515 add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
516 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers);
517 zval_ptr_dtor(&headers);
518 }
519 }
520 }
521
522 STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
523 {
524 STATUS status = SUCCESS;
525
526 http_request_reset(obj->request);
527 HTTP_CHECK_CURL_INIT(obj->request->ch, http_curl_init(obj->request), return FAILURE);
528
529 obj->request->url = http_absolute_url(Z_STRVAL_P(GET_PROP(url)));
530
531 switch (obj->request->meth = Z_LVAL_P(GET_PROP(method)))
532 {
533 case HTTP_GET:
534 case HTTP_HEAD:
535 break;
536
537 case HTTP_PUT:
538 {
539 zval *put_data = GET_PROP(putData);
540
541 http_request_object_check_request_content_type(getThis());
542 if (Z_STRLEN_P(put_data)) {
543 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
544 estrndup(Z_STRVAL_P(put_data), Z_STRLEN_P(put_data)), Z_STRLEN_P(put_data), 1);
545 } else {
546 php_stream_statbuf ssb;
547 php_stream *stream = php_stream_open_wrapper_ex(Z_STRVAL_P(GET_PROP(putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT);
548
549 if (stream && !php_stream_stat(stream, &ssb)) {
550 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1);
551 } else {
552 status = FAILURE;
553 }
554 }
555 break;
556 }
557
558 case HTTP_POST:
559 default:
560 {
561 /* check for raw post data */
562 zval *raw_data = GET_PROP(rawPostData);
563
564 if (Z_STRLEN_P(raw_data)) {
565 http_request_object_check_request_content_type(getThis());
566 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
567 estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data)), Z_STRLEN_P(raw_data), 1);
568 } else {
569 zval *zfields = GET_PROP(postFields), *zfiles = GET_PROP(postFiles);
570 HashTable *fields;
571 HashTable *files;
572
573 fields = (Z_TYPE_P(zfields) == IS_ARRAY) ? Z_ARRVAL_P(zfields) : NULL;
574 files = (Z_TYPE_P(zfiles) == IS_ARRAY) ? Z_ARRVAL_P(zfiles) : NULL;
575
576 if ((fields && zend_hash_num_elements(fields)) || (files && zend_hash_num_elements(files))) {
577 if (!(obj->request->body = http_request_body_fill(obj->request->body, fields, files))) {
578 status = FAILURE;
579 }
580 }
581 }
582 break;
583 }
584 }
585
586 if (status == SUCCESS) {
587 zval *qdata = GET_PROP(queryData);
588 zval *options = GET_PROP(options);
589
590 if (Z_STRLEN_P(qdata)) {
591 if (!strchr(obj->request->url, '?')) {
592 strlcat(obj->request->url, "?", HTTP_URL_MAXLEN);
593 } else {
594 strlcat(obj->request->url, "&", HTTP_URL_MAXLEN);
595 }
596 strlcat(obj->request->url, Z_STRVAL_P(qdata), HTTP_URL_MAXLEN);
597 }
598
599 http_request_prepare(obj->request, Z_ARRVAL_P(options));
600
601 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
602 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onprogress", sizeof("onprogress"))) {
603 zval **entry, *pcb;
604
605 if ( (Z_TYPE_P(options) != IS_ARRAY)
606 || (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void *) &entry)
607 || (!zval_is_true(*entry)))) {
608 MAKE_STD_ZVAL(pcb);
609 array_init(pcb);
610 ZVAL_ADDREF(getThis());
611 add_next_index_zval(pcb, getThis());
612 add_next_index_stringl(pcb, "onprogress", lenof("onprogress"), 1);
613 http_request_set_progress_callback(obj->request, pcb);
614 zval_ptr_dtor(&pcb);
615 }
616 }
617 }
618
619 return status;
620 }
621
622 STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
623 {
624 STATUS ret;
625 zval *info;
626 http_message *msg;
627
628 /* always fetch info */
629 MAKE_STD_ZVAL(info);
630 array_init(info);
631 http_request_info(obj->request, Z_ARRVAL_P(info));
632 SET_PROP(responseInfo, info);
633 zval_ptr_dtor(&info);
634
635 /* parse response message */
636 phpstr_fix(&obj->request->conv.request);
637 phpstr_fix(&obj->request->conv.response);
638
639 if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)))) {
640 char *body;
641 size_t body_len;
642 zval *headers, *message, *resp;
643
644 if (zval_is_true(GET_PROP(recordHistory))) {
645 zval *hist, *history = GET_PROP(history);
646 http_message *response = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response));
647 http_message *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
648
649 MAKE_STD_ZVAL(hist);
650 ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, http_message_interconnect(response, request), NULL), 0);
651 if (Z_TYPE_P(history) == IS_OBJECT) {
652 http_message_object_prepend(hist, history);
653 }
654 SET_PROP(history, hist);
655 zval_ptr_dtor(&hist);
656 }
657
658 UPD_PROP(long, responseCode, msg->http.info.response.code);
659 UPD_PROP(string, responseStatus, msg->http.info.response.status ? msg->http.info.response.status : "");
660
661 MAKE_STD_ZVAL(resp);
662 array_init(resp);
663 MAKE_STD_ZVAL(headers);
664 array_init(headers);
665 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
666 add_assoc_zval(resp, "headers", headers);
667 phpstr_data(PHPSTR(msg), &body, &body_len);
668 add_assoc_stringl(resp, "body", body, body_len, 0);
669 SET_PROP(responseData, resp);
670 zval_ptr_dtor(&resp);
671
672 MAKE_STD_ZVAL(message);
673 ZVAL_OBJVAL(message, http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
674 SET_PROP(responseMessage, message);
675 zval_ptr_dtor(&message);
676
677 ret = SUCCESS;
678 } else {
679 /* update properties with empty values*/
680 zval *resp = GET_PROP(responseData), *znull;
681
682 MAKE_STD_ZVAL(znull);
683 ZVAL_NULL(znull);
684 SET_PROP(responseMessage, znull);
685 zval_ptr_dtor(&znull);
686
687 if (Z_TYPE_P(resp) == IS_ARRAY) {
688 zend_hash_clean(Z_ARRVAL_P(resp));
689 }
690
691 UPD_PROP(long, responseCode, 0);
692 UPD_PROP(string, responseStatus, "");
693
694 /* append request message to history */
695 if (zval_is_true(GET_PROP(recordHistory))) {
696 http_message *request;
697
698 if ((request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
699 zval *hist, *history = GET_PROP(history);
700
701 MAKE_STD_ZVAL(hist);
702 ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, request, NULL), 0);
703 if (Z_TYPE_P(history) == IS_OBJECT) {
704 http_message_object_prepend(hist, history);
705 }
706 SET_PROP(history, hist);
707 zval_ptr_dtor(&hist);
708 }
709 }
710
711 ret = FAILURE;
712 }
713
714 if (!EG(exception) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) {
715 zval *param;
716
717 MAKE_STD_ZVAL(param);
718 ZVAL_BOOL(param, ret == SUCCESS);
719 with_error_handling(EH_NORMAL, NULL) {
720 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param);
721 } end_error_handling();
722 zval_ptr_dtor(&param);
723 }
724
725 return ret;
726 }
727
728 static int apply_pretty_key(void *pDest, int num_args, va_list args, zend_hash_key *hash_key)
729 {
730 if (hash_key->nKeyLength > 1) {
731 hash_key->h = zend_get_hash_value(pretty_key(hash_key->arKey, hash_key->nKeyLength - 1, 1, 0), hash_key->nKeyLength);
732 }
733 return ZEND_HASH_APPLY_KEEP;
734 }
735
736 #define http_request_object_set_options_subr(key, ow, pk) \
737 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
738 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys)
739 {
740 zval *old_opts, *new_opts, *opts = NULL, **entry = NULL;
741
742 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
743 RETURN_FALSE;
744 }
745
746 MAKE_STD_ZVAL(new_opts);
747 array_init(new_opts);
748 old_opts = GET_PROP(options);
749 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
750 array_copy(old_opts, new_opts);
751 }
752
753 if (prettify_keys && opts) {
754 zend_hash_apply_with_arguments(Z_ARRVAL_P(opts), apply_pretty_key, 0);
755 }
756 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
757 if (overwrite) {
758 zend_hash_clean(Z_ARRVAL_PP(entry));
759 }
760 if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
761 if (overwrite) {
762 array_copy(opts, *entry);
763 } else {
764 array_merge(opts, *entry);
765 }
766 }
767 } else if (opts) {
768 ZVAL_ADDREF(opts);
769 add_assoc_zval(new_opts, key, opts);
770 }
771 SET_PROP(options, new_opts);
772 zval_ptr_dtor(&new_opts);
773
774 RETURN_TRUE;
775 }
776
777 #define http_request_object_get_options_subr(key) \
778 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
779 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
780 {
781 NO_ARGS;
782
783 if (return_value_used) {
784 zval *opts, **options;
785
786 opts = GET_PROP(options);
787 array_init(return_value);
788
789 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
790 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
791 convert_to_array(*options);
792 array_copy(*options, return_value);
793 }
794 }
795 }
796
797
798 /* ### USERLAND ### */
799
800 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
801 *
802 * Instantiate a new HttpRequest object.
803 *
804 * Accepts a string as optional parameter containing the target request url.
805 * Additionally accepts an optional int parameter specifying the request method
806 * to use and an associative array as optional third parameter which will be
807 * passed to HttpRequest::setOptions().
808 *
809 * Throws HttpException.
810 */
811 PHP_METHOD(HttpRequest, __construct)
812 {
813 char *URL = NULL;
814 int URL_len;
815 long meth = -1;
816 zval *options = NULL;
817
818 SET_EH_THROW_HTTP();
819 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla", &URL, &URL_len, &meth, &options)) {
820 if (URL) {
821 UPD_STRL(url, URL, URL_len);
822 }
823 if (meth > -1) {
824 UPD_PROP(long, method, meth);
825 }
826 if (options) {
827 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
828 }
829 }
830 SET_EH_NORMAL();
831 }
832 /* }}} */
833
834 /* {{{ proto bool HttpRequest::setOptions([array options])
835 *
836 * Set the request options to use. See http_get() for a full list of available options.
837 *
838 * Accepts an array as optional parameters, which values will overwrite the
839 * currently set request options. If the parameter is empty or omitted,
840 * the options of the HttpRequest object will be reset.
841 *
842 * Returns TRUE on success, or FALSE on failure.
843 */
844 PHP_METHOD(HttpRequest, setOptions)
845 {
846 char *key = NULL;
847 ulong idx = 0;
848 HashPosition pos;
849 zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt;
850
851 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
852 RETURN_FALSE;
853 }
854
855 MAKE_STD_ZVAL(new_opts);
856 array_init(new_opts);
857
858 if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
859 SET_PROP(options, new_opts);
860 zval_ptr_dtor(&new_opts);
861 RETURN_TRUE;
862 }
863
864 MAKE_STD_ZVAL(add_opts);
865 array_init(add_opts);
866 /* some options need extra attention -- thus cannot use array_merge() directly */
867 FOREACH_KEYVAL(pos, opts, key, idx, opt) {
868 if (key) {
869 if (!strcmp(key, "headers")) {
870 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, *opt);
871 } else if (!strcmp(key, "cookies")) {
872 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt);
873 } else if (!strcmp(key, "ssl")) {
874 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt);
875 } else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) {
876 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt);
877 } else if (!strcmp(key, "method")) {
878 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt);
879 #if HTTP_CURL_VERSION(7,14,1)
880 } else if (!strcmp(key, "resetcookies")) {
881 getObject(http_request_object, obj);
882 http_request_reset_cookies(obj->request, 0);
883 #endif
884 } else if (!strcmp(key, "enablecookies")) {
885 getObject(http_request_object, obj);
886 http_request_enable_cookies(obj->request);
887 } else if (!strcasecmp(key, "recordHistory")) {
888 UPD_PROP(bool, recordHistory, 1);
889 } else {
890 ZVAL_ADDREF(*opt);
891 add_assoc_zval(add_opts, key, *opt);
892 }
893 /* reset */
894 key = NULL;
895 }
896 }
897
898 old_opts = GET_PROP(options);
899 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
900 array_copy(old_opts, new_opts);
901 }
902 array_merge(add_opts, new_opts);
903 SET_PROP(options, new_opts);
904 zval_ptr_dtor(&new_opts);
905 zval_ptr_dtor(&add_opts);
906
907 RETURN_TRUE;
908 }
909 /* }}} */
910
911 /* {{{ proto array HttpRequest::getOptions()
912 *
913 * Get currently set options.
914 *
915 * Returns an associative array containing currently set options.
916 */
917 PHP_METHOD(HttpRequest, getOptions)
918 {
919 NO_ARGS;
920
921 if (return_value_used) {
922 RETURN_PROP(options);
923 }
924 }
925 /* }}} */
926
927 /* {{{ proto bool HttpRequest::setSslOptions([array options])
928 *
929 * Set SSL options.
930 *
931 * Accepts an associative array as parameter containing any SSL specific options.
932 * If the parameter is empty or omitted, the SSL options will be reset.
933 *
934 * Returns TRUE on success, or FALSE on failure.
935 */
936 PHP_METHOD(HttpRequest, setSslOptions)
937 {
938 http_request_object_set_options_subr("ssl", 1, 0);
939 }
940 /* }}} */
941
942 /* {{{ proto bool HttpRequest::addSslOptions(array options)
943 *
944 * Set additional SSL options.
945 *
946 * Expects an associative array as parameter containing additional SSL specific options.
947 *
948 * Returns TRUE on success, or FALSE on failure.
949 */
950 PHP_METHOD(HttpRequest, addSslOptions)
951 {
952 http_request_object_set_options_subr("ssl", 0, 0);
953 }
954 /* }}} */
955
956 /* {{{ proto array HttpRequest::getSslOtpions()
957 *
958 * Get previously set SSL options.
959 *
960 * Returns an associative array containing any previously set SSL options.
961 */
962 PHP_METHOD(HttpRequest, getSslOptions)
963 {
964 http_request_object_get_options_subr("ssl");
965 }
966 /* }}} */
967
968 /* {{{ proto bool HttpRequest::addHeaders(array headers)
969 *
970 * Add request header name/value pairs.
971 *
972 * Expects an associative array as parameter containing additional header
973 * name/value pairs.
974 *
975 * Returns TRUE on success, or FALSE on failure.
976 */
977 PHP_METHOD(HttpRequest, addHeaders)
978 {
979 http_request_object_set_options_subr("headers", 0, 1);
980 }
981
982 /* {{{ proto bool HttpRequest::setHeaders([array headers])
983 *
984 * Set request header name/value pairs.
985 *
986 * Accepts an associative array as parameter containing header name/value pairs.
987 * If the parameter is empty or omitted, all previously set headers will be unset.
988 *
989 * Returns TRUE on success, or FALSE on failure.
990 */
991 PHP_METHOD(HttpRequest, setHeaders)
992 {
993 http_request_object_set_options_subr("headers", 1, 1);
994 }
995 /* }}} */
996
997 /* {{{ proto array HttpRequest::getHeaders()
998 *
999 * Get previously set request headers.
1000 *
1001 * Returns an associative array containing all currently set headers.
1002 */
1003 PHP_METHOD(HttpRequest, getHeaders)
1004 {
1005 http_request_object_get_options_subr("headers");
1006 }
1007 /* }}} */
1008
1009 /* {{{ proto bool HttpRequest::setCookies([array cookies])
1010 *
1011 * Set cookies.
1012 *
1013 * Accepts an associative array as parameter containing cookie name/value pairs.
1014 * If the parameter is empty or omitted, all previously set cookies will be unset.
1015 *
1016 * Returns TRUE on success, or FALSE on failure.
1017 */
1018 PHP_METHOD(HttpRequest, setCookies)
1019 {
1020 http_request_object_set_options_subr("cookies", 1, 0);
1021 }
1022 /* }}} */
1023
1024 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1025 *
1026 * Add cookies.
1027 *
1028 * Expects an associative array as parameter containing any cookie name/value
1029 * pairs to add.
1030 *
1031 * Returns TRUE on success, or FALSE on failure.
1032 */
1033 PHP_METHOD(HttpRequest, addCookies)
1034 {
1035 http_request_object_set_options_subr("cookies", 0, 0);
1036 }
1037 /* }}} */
1038
1039 /* {{{ proto array HttpRequest::getCookies()
1040 *
1041 * Get previously set cookies.
1042 *
1043 * Returns an associative array containing any previously set cookies.
1044 */
1045 PHP_METHOD(HttpRequest, getCookies)
1046 {
1047 http_request_object_get_options_subr("cookies");
1048 }
1049 /* }}} */
1050
1051 /* {{{ proto bool HttpRequest::enableCookies()
1052 *
1053 * Enable automatic sending of received cookies.
1054 * Note that cuutomly set cookies will be sent anyway.
1055 */
1056 PHP_METHOD(HttpRequest, enableCookies)
1057 {
1058 NO_ARGS {
1059 getObject(http_request_object, obj);
1060 RETURN_SUCCESS(http_request_enable_cookies(obj->request));
1061 }
1062
1063 }
1064 /* }}} */
1065
1066 /* {{{ proto bool HttpRequest::resetCookies([bool session_only = FALSE])
1067 *
1068 * Reset all automatically received/sent cookies.
1069 * Note that customly set cookies are not affected.
1070 *
1071 * Accepts an optional bool parameter specifying
1072 * whether only session cookies should be reset
1073 * (needs libcurl >= v7.15.4, else libcurl >= v7.14.1).
1074 *
1075 * Returns TRUE on success, or FALSE on failure.
1076 */
1077 PHP_METHOD(HttpRequest, resetCookies)
1078 {
1079 zend_bool session_only = 0;
1080 getObject(http_request_object, obj);
1081
1082 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &session_only)) {
1083 RETURN_FALSE;
1084 }
1085 RETURN_SUCCESS(http_request_reset_cookies(obj->request, session_only));
1086 }
1087 /* }}} */
1088
1089 /* {{{ proto bool HttpRequest::setUrl(string url)
1090 *
1091 * Set the request URL.
1092 *
1093 * Expects a string as parameter specifying the request url.
1094 *
1095 * Returns TRUE on success, or FALSE on failure.
1096 */
1097 PHP_METHOD(HttpRequest, setUrl)
1098 {
1099 char *URL = NULL;
1100 int URL_len;
1101
1102 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
1103 RETURN_FALSE;
1104 }
1105
1106 UPD_STRL(url, URL, URL_len);
1107 RETURN_TRUE;
1108 }
1109 /* }}} */
1110
1111 /* {{{ proto string HttpRequest::getUrl()
1112 *
1113 * Get the previously set request URL.
1114 *
1115 * Returns the currently set request url as string.
1116 */
1117 PHP_METHOD(HttpRequest, getUrl)
1118 {
1119 NO_ARGS;
1120
1121 if (return_value_used) {
1122 RETURN_PROP(url);
1123 }
1124 }
1125 /* }}} */
1126
1127 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1128 *
1129 * Set the request method.
1130 *
1131 * Expects an int as parameter specifying the request method to use.
1132 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
1133 *
1134 * Returns TRUE on success, or FALSE on failure.
1135 */
1136 PHP_METHOD(HttpRequest, setMethod)
1137 {
1138 long meth;
1139
1140 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
1141 RETURN_FALSE;
1142 }
1143
1144 UPD_PROP(long, method, meth);
1145 RETURN_TRUE;
1146 }
1147 /* }}} */
1148
1149 /* {{{ proto int HttpRequest::getMethod()
1150 *
1151 * Get the previously set request method.
1152 *
1153 * Returns the currently set request method.
1154 */
1155 PHP_METHOD(HttpRequest, getMethod)
1156 {
1157 NO_ARGS;
1158
1159 if (return_value_used) {
1160 RETURN_PROP(method);
1161 }
1162 }
1163 /* }}} */
1164
1165 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1166 *
1167 * Set the content type the post request should have.
1168 *
1169 * Expects a string as parameters containing the content type of the request
1170 * (primary/secondary).
1171 *
1172 * Returns TRUE on success, or FALSE if the content type does not seem to
1173 * contain a primary and a secondary part.
1174 */
1175 PHP_METHOD(HttpRequest, setContentType)
1176 {
1177 char *ctype;
1178 int ct_len;
1179
1180 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1181 RETURN_FALSE;
1182 }
1183
1184 if (ct_len) {
1185 HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
1186 }
1187 UPD_STRL(contentType, ctype, ct_len);
1188 RETURN_TRUE;
1189 }
1190 /* }}} */
1191
1192 /* {{{ proto string HttpRequest::getContentType()
1193 *
1194 * Get the previously content type.
1195 *
1196 * Returns the previously set content type as string.
1197 */
1198 PHP_METHOD(HttpRequest, getContentType)
1199 {
1200 NO_ARGS;
1201
1202 if (return_value_used) {
1203 RETURN_PROP(contentType);
1204 }
1205 }
1206 /* }}} */
1207
1208 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1209 *
1210 * Set the URL query parameters to use, overwriting previously set query parameters.
1211 * Affects any request types.
1212 *
1213 * Accepts a string or associative array parameter containing the pre-encoded
1214 * query string or to be encoded query fields. If the parameter is empty or
1215 * omitted, the query data will be unset.
1216 *
1217 * Returns TRUE on success, or FALSE on failure.
1218 */
1219 PHP_METHOD(HttpRequest, setQueryData)
1220 {
1221 zval *qdata = NULL;
1222
1223 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
1224 RETURN_FALSE;
1225 }
1226
1227 if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
1228 UPD_STRL(queryData, "", 0);
1229 } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1230 char *query_data = NULL;
1231
1232 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1233 RETURN_FALSE;
1234 }
1235
1236 UPD_PROP(string, queryData, query_data);
1237 efree(query_data);
1238 } else {
1239 zval *orig = qdata;
1240
1241 convert_to_string_ex(&qdata);
1242 UPD_STRL(queryData, Z_STRVAL_P(qdata), Z_STRLEN_P(qdata));
1243 if (orig != qdata) {
1244 zval_ptr_dtor(&qdata);
1245 }
1246 }
1247 RETURN_TRUE;
1248 }
1249 /* }}} */
1250
1251 /* {{{ proto string HttpRequest::getQueryData()
1252 *
1253 * Get the current query data in form of an urlencoded query string.
1254 *
1255 * Returns a string containing the urlencoded query.
1256 */
1257 PHP_METHOD(HttpRequest, getQueryData)
1258 {
1259 NO_ARGS;
1260
1261 if (return_value_used) {
1262 RETURN_PROP(queryData);
1263 }
1264 }
1265 /* }}} */
1266
1267 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1268 *
1269 * Add parameters to the query parameter list, leaving previously set unchanged.
1270 * Affects any request type.
1271 *
1272 * Expects an associative array as parameter containing the query fields to add.
1273 *
1274 * Returns TRUE on success, or FALSE on failure.
1275 */
1276 PHP_METHOD(HttpRequest, addQueryData)
1277 {
1278 zval *qdata, *old_qdata;
1279 char *query_data = NULL;
1280 size_t query_data_len = 0;
1281
1282 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &qdata)) {
1283 RETURN_FALSE;
1284 }
1285
1286 old_qdata = GET_PROP(queryData);
1287
1288 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)) {
1289 RETURN_FALSE;
1290 }
1291
1292 UPD_STRL(queryData, query_data, query_data_len);
1293 efree(query_data);
1294
1295 RETURN_TRUE;
1296 }
1297 /* }}} */
1298
1299 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1300 *
1301 * Adds POST data entries, leaving previously set unchanged, unless a
1302 * post entry with the same name already exists.
1303 * Affects only POST and custom requests.
1304 *
1305 * Expects an associative array as parameter containing the post fields.
1306 *
1307 * Returns TRUE on success, or FALSE on failure.
1308 */
1309 PHP_METHOD(HttpRequest, addPostFields)
1310 {
1311 zval *post_data, *old_post, *new_post;
1312
1313 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &post_data)) {
1314 RETURN_FALSE;
1315 }
1316
1317 if (zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1318 MAKE_STD_ZVAL(new_post);
1319 array_init(new_post);
1320 old_post = GET_PROP(postFields);
1321 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1322 array_copy(old_post, new_post);
1323 }
1324 array_merge(post_data, new_post);
1325 SET_PROP(postFields, new_post);
1326 zval_ptr_dtor(&new_post);
1327 }
1328
1329 RETURN_TRUE;
1330 }
1331 /* }}} */
1332
1333 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1334 *
1335 * Set the POST data entries, overwriting previously set POST data.
1336 * Affects only POST and custom requests.
1337 *
1338 * Accepts an associative array as parameter containing the post fields.
1339 * If the parameter is empty or omitted, the post data will be unset.
1340 *
1341 * Returns TRUE on success, or FALSE on failure.
1342 */
1343 PHP_METHOD(HttpRequest, setPostFields)
1344 {
1345 zval *post, *post_data = NULL;
1346
1347 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &post_data)) {
1348 RETURN_FALSE;
1349 }
1350
1351 MAKE_STD_ZVAL(post);
1352 array_init(post);
1353 if (post_data && zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1354 array_copy(post_data, post);
1355 }
1356 SET_PROP(postFields, post);
1357 zval_ptr_dtor(&post);
1358
1359 RETURN_TRUE;
1360 }
1361 /* }}}*/
1362
1363 /* {{{ proto array HttpRequest::getPostFields()
1364 *
1365 * Get previously set POST data.
1366 *
1367 * Returns the currently set post fields as associative array.
1368 */
1369 PHP_METHOD(HttpRequest, getPostFields)
1370 {
1371 NO_ARGS;
1372
1373 if (return_value_used) {
1374 RETURN_PROP(postFields);
1375 }
1376 }
1377 /* }}} */
1378
1379 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1380 *
1381 * Set raw post data to send, overwriting previously set raw post data. Don't
1382 * forget to specify a content type. Affects only POST and custom requests.
1383 * Only either post fields or raw post data can be used for each request.
1384 * Raw post data has higher precedence and will be used even if post fields
1385 * are set.
1386 *
1387 * Accepts a string as parameter containing the *raw* post data.
1388 *
1389 * Returns TRUE on success, or FALSE on failure.
1390 */
1391 PHP_METHOD(HttpRequest, setRawPostData)
1392 {
1393 char *raw_data = NULL;
1394 int data_len = 0;
1395
1396 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &raw_data, &data_len)) {
1397 RETURN_FALSE;
1398 }
1399
1400 if (!raw_data) {
1401 raw_data = "";
1402 }
1403
1404 UPD_STRL(rawPostData, raw_data, data_len);
1405 RETURN_TRUE;
1406 }
1407 /* }}} */
1408
1409 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1410 *
1411 * Add raw post data, leaving previously set raw post data unchanged.
1412 * Affects only POST and custom requests.
1413 *
1414 * Expects a string as parameter containing the raw post data to concatenate.
1415 *
1416 * Returns TRUE on success, or FALSE on failure.
1417 */
1418 PHP_METHOD(HttpRequest, addRawPostData)
1419 {
1420 char *raw_data;
1421 int data_len;
1422
1423 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw_data, &data_len)) {
1424 RETURN_FALSE;
1425 }
1426
1427 if (data_len) {
1428 zval *data = GET_PROP(rawPostData);
1429
1430 if (Z_STRLEN_P(data)) {
1431 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1432 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1433 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len);
1434 } else {
1435 UPD_STRL(putData, raw_data, data_len);
1436 }
1437 }
1438
1439 RETURN_TRUE;
1440 }
1441 /* }}} */
1442
1443 /* {{{ proto string HttpRequest::getRawPostData()
1444 *
1445 * Get previously set raw post data.
1446 *
1447 * Returns a string containing the currently set raw post data.
1448 */
1449 PHP_METHOD(HttpRequest, getRawPostData)
1450 {
1451 NO_ARGS;
1452
1453 if (return_value_used) {
1454 RETURN_PROP(rawPostData);
1455 }
1456 }
1457 /* }}} */
1458
1459 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1460 *
1461 * Add a file to the POST request, leaving previously set files unchanged.
1462 * Affects only POST and custom requests. Cannot be used with raw post data.
1463 *
1464 * Expects a string parameter containing the form element name, and a string
1465 * paremeter containing the path to the file which should be uploaded.
1466 * Additionally accepts an optional string parameter which should contain
1467 * the content type of the file.
1468 *
1469 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1470 * primary and a secondary content type part.
1471 */
1472 PHP_METHOD(HttpRequest, addPostFile)
1473 {
1474 zval *entry, *old_post, *new_post;
1475 char *name, *file, *type = NULL;
1476 int name_len, file_len, type_len = 0;
1477
1478 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1479 RETURN_FALSE;
1480 }
1481
1482 if (type_len) {
1483 HTTP_CHECK_CONTENT_TYPE(type, RETURN_FALSE);
1484 } else {
1485 type = "application/x-octetstream";
1486 type_len = sizeof("application/x-octetstream") - 1;
1487 }
1488
1489 MAKE_STD_ZVAL(entry);
1490 array_init(entry);
1491
1492 add_assoc_stringl(entry, "name", name, name_len, 1);
1493 add_assoc_stringl(entry, "type", type, type_len, 1);
1494 add_assoc_stringl(entry, "file", file, file_len, 1);
1495
1496 MAKE_STD_ZVAL(new_post);
1497 array_init(new_post);
1498 old_post = GET_PROP(postFiles);
1499 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1500 array_copy(old_post, new_post);
1501 }
1502 add_next_index_zval(new_post, entry);
1503 SET_PROP(postFiles, new_post);
1504 zval_ptr_dtor(&new_post);
1505
1506 RETURN_TRUE;
1507 }
1508 /* }}} */
1509
1510 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1511 *
1512 * Set files to post, overwriting previously set post files.
1513 * Affects only POST and requests. Cannot be used with raw post data.
1514 *
1515 * Accepts an array containing the files to post. Each entry should be an
1516 * associative array with "name", "file" and "type" keys. If the parameter
1517 * is empty or omitted the post files will be unset.
1518 *
1519 * Returns TRUE on success, or FALSE on failure.
1520 */
1521 PHP_METHOD(HttpRequest, setPostFiles)
1522 {
1523 zval *files = NULL, *post;
1524
1525 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!/", &files)) {
1526 RETURN_FALSE;
1527 }
1528
1529 MAKE_STD_ZVAL(post);
1530 array_init(post);
1531 if (files && (Z_TYPE_P(files) == IS_ARRAY)) {
1532 array_copy(files, post);
1533 }
1534 SET_PROP(postFiles, post);
1535 zval_ptr_dtor(&post);
1536
1537 RETURN_TRUE;
1538 }
1539 /* }}} */
1540
1541 /* {{{ proto array HttpRequest::getPostFiles()
1542 *
1543 * Get all previously added POST files.
1544 *
1545 * Returns an array containing currently set post files.
1546 */
1547 PHP_METHOD(HttpRequest, getPostFiles)
1548 {
1549 NO_ARGS;
1550
1551 if (return_value_used) {
1552 RETURN_PROP(postFiles);
1553 }
1554 }
1555 /* }}} */
1556
1557 /* {{{ proto bool HttpRequest::setPutFile([string file])
1558 *
1559 * Set file to put. Affects only PUT requests.
1560 *
1561 * Accepts a string as parameter referencing the path to file.
1562 * If the parameter is empty or omitted the put file will be unset.
1563 *
1564 * Returns TRUE on success, or FALSE on failure.
1565 */
1566 PHP_METHOD(HttpRequest, setPutFile)
1567 {
1568 char *file = "";
1569 int file_len = 0;
1570
1571 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &file, &file_len)) {
1572 RETURN_FALSE;
1573 }
1574
1575 UPD_STRL(putFile, file, file_len);
1576 RETURN_TRUE;
1577 }
1578 /* }}} */
1579
1580 /* {{{ proto string HttpRequest::getPutFile()
1581 *
1582 * Get previously set put file.
1583 *
1584 * Returns a string containing the path to the currently set put file.
1585 */
1586 PHP_METHOD(HttpRequest, getPutFile)
1587 {
1588 NO_ARGS;
1589
1590 if (return_value_used) {
1591 RETURN_PROP(putFile);
1592 }
1593 }
1594 /* }}} */
1595
1596 /* {{{ proto bool HttpRequest::setPutData([string put_data])
1597 *
1598 * Set PUT data to send, overwriting previously set PUT data.
1599 * Affects only PUT requests.
1600 * Only either PUT data or PUT file can be used for each request.
1601 * PUT data has higher precedence and will be used even if a PUT
1602 * file is set.
1603 *
1604 * Accepts a string as parameter containing the data to upload.
1605 *
1606 * Returns TRUE on success, or FALSE on failure.
1607 */
1608 PHP_METHOD(HttpRequest, setPutData)
1609 {
1610 char *put_data = NULL;
1611 int data_len = 0;
1612
1613 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &put_data, &data_len)) {
1614 RETURN_FALSE;
1615 }
1616
1617 if (!put_data) {
1618 put_data = "";
1619 }
1620
1621 UPD_STRL(putData, put_data, data_len);
1622 RETURN_TRUE;
1623 }
1624 /* }}} */
1625
1626 /* {{{ proto bool HttpRequest::addPutData(string put_data)
1627 *
1628 * Add PUT data, leaving previously set PUT data unchanged.
1629 * Affects only PUT requests.
1630 *
1631 * Expects a string as parameter containing the data to concatenate.
1632 *
1633 * Returns TRUE on success, or FALSE on failure.
1634 */
1635 PHP_METHOD(HttpRequest, addPutData)
1636 {
1637 char *put_data;
1638 int data_len;
1639
1640 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &put_data, &data_len)) {
1641 RETURN_FALSE;
1642 }
1643
1644 if (data_len) {
1645 zval *data = GET_PROP(putData);
1646
1647 if (Z_STRLEN_P(data)) {
1648 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1649 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1650 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, put_data, data_len);
1651 } else {
1652 UPD_STRL(putData, put_data, data_len);
1653 }
1654 }
1655
1656 RETURN_TRUE;
1657 }
1658 /* }}} */
1659
1660 /* {{{ proto string HttpRequest::getPutData()
1661 *
1662 * Get previously set PUT data.
1663 *
1664 * Returns a string containing the currently set raw post data.
1665 */
1666 PHP_METHOD(HttpRequest, getPutData)
1667 {
1668 NO_ARGS;
1669
1670 if (return_value_used) {
1671 RETURN_PROP(putData);
1672 }
1673 }
1674 /* }}} */
1675
1676 /* {{{ proto array HttpRequest::getResponseData()
1677 *
1678 * Get all response data after the request has been sent.
1679 *
1680 * Returns an associative array with the key "headers" containing an associative
1681 * array holding all response headers, as well as the key "body" containing a
1682 * string with the response body.
1683 *
1684 * If redirects were allowed and several responses were received, the data
1685 * references the last received response.
1686 */
1687 PHP_METHOD(HttpRequest, getResponseData)
1688 {
1689 NO_ARGS;
1690
1691 if (return_value_used) {
1692 RETURN_PROP(responseData);
1693 }
1694 }
1695 /* }}} */
1696
1697 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1698 *
1699 * Get response header(s) after the request has been sent.
1700 *
1701 * Accepts an string as optional parameter specifying a certain header to read.
1702 * If the parameter is empty or omitted all response headers will be returned.
1703 *
1704 * Returns either a string with the value of the header matching name if requested,
1705 * FALSE on failure, or an associative array containing all response headers.
1706 *
1707 * If redirects were allowed and several responses were received, the data
1708 * references the last received response.
1709 */
1710 PHP_METHOD(HttpRequest, getResponseHeader)
1711 {
1712 if (return_value_used) {
1713 zval *data, **headers, **header;
1714 char *header_name = NULL;
1715 int header_len = 0;
1716
1717 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1718 RETURN_FALSE;
1719 }
1720
1721 data = GET_PROP(responseData);
1722 if ( (Z_TYPE_P(data) == IS_ARRAY) &&
1723 (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void *) &headers)) &&
1724 (Z_TYPE_PP(headers) == IS_ARRAY)) {
1725 if (!header_len || !header_name) {
1726 RETVAL_ZVAL(*headers, 1, 0);
1727 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void *) &header)) {
1728 RETVAL_ZVAL(*header, 1, 0);
1729 } else {
1730 RETVAL_FALSE;
1731 }
1732 } else {
1733 RETVAL_FALSE;
1734 }
1735 }
1736 }
1737 /* }}} */
1738
1739 /* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]])
1740 *
1741 * Get response cookie(s) after the request has been sent.
1742 *
1743 * Returns an array of stdClass objects like http_parse_cookie would return.
1744 *
1745 * If redirects were allowed and several responses were received, the data
1746 * references the last received response.
1747 */
1748 PHP_METHOD(HttpRequest, getResponseCookies)
1749 {
1750 if (return_value_used) {
1751 long flags = 0;
1752 zval *allowed_extras_array = NULL, *data, **headers;
1753
1754 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la", &flags, &allowed_extras_array)) {
1755 RETURN_FALSE;
1756 }
1757
1758 data = GET_PROP(responseData);
1759 if ( (Z_TYPE_P(data) == IS_ARRAY) &&
1760 (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void *) &headers)) &&
1761 (Z_TYPE_PP(headers) == IS_ARRAY)) {
1762 int i = 0;
1763 ulong idx = 0;
1764 char *key = NULL, **allowed_extras = NULL;
1765 zval **header = NULL, **entry = NULL;
1766 HashPosition pos, pos1, pos2;
1767
1768 array_init(return_value);
1769
1770 if (allowed_extras_array) {
1771 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
1772 FOREACH_VAL(pos, allowed_extras_array, entry) {
1773 ZVAL_ADDREF(*entry);
1774 convert_to_string_ex(entry);
1775 allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
1776 zval_ptr_dtor(entry);
1777 }
1778 }
1779
1780 FOREACH_HASH_KEYVAL(pos1, Z_ARRVAL_PP(headers), key, idx, header) {
1781 if (key && !strcasecmp(key, "Set-Cookie")) {
1782 http_cookie_list list;
1783
1784 if (Z_TYPE_PP(header) == IS_ARRAY) {
1785 zval **single_header;
1786
1787 FOREACH_VAL(pos2, *header, single_header) {
1788 ZVAL_ADDREF(*single_header);
1789 convert_to_string_ex(single_header);
1790 if (http_parse_cookie_ex(&list, Z_STRVAL_PP(single_header), flags, allowed_extras)) {
1791 zval *cookie;
1792
1793 MAKE_STD_ZVAL(cookie);
1794 object_init(cookie);
1795 http_cookie_list_tostruct(&list, cookie);
1796 add_next_index_zval(return_value, cookie);
1797 http_cookie_list_dtor(&list);
1798 }
1799 zval_ptr_dtor(single_header);
1800 }
1801 } else {
1802 ZVAL_ADDREF(*header);
1803 convert_to_string_ex(header);
1804 if (http_parse_cookie_ex(&list, Z_STRVAL_PP(header), flags, allowed_extras)) {
1805 zval *cookie;
1806
1807 MAKE_STD_ZVAL(cookie);
1808 object_init(cookie);
1809 http_cookie_list_tostruct(&list, cookie);
1810 add_next_index_zval(return_value, cookie);
1811 http_cookie_list_dtor(&list);
1812 }
1813 zval_ptr_dtor(header);
1814 }
1815 }
1816 /* reset key */
1817 key = NULL;
1818 }
1819
1820 if (allowed_extras) {
1821 for (i = 0; allowed_extras[i]; ++i) {
1822 efree(allowed_extras[i]);
1823 }
1824 efree(allowed_extras);
1825 }
1826 } else {
1827 RETURN_FALSE;
1828 }
1829 }
1830 }
1831 /* }}} */
1832
1833 /* {{{ proto string HttpRequest::getResponseBody()
1834 *
1835 * Get the response body after the request has been sent.
1836 *
1837 * Returns a string containing the response body.
1838 *
1839 * If redirects were allowed and several responses were received, the data
1840 * references the last received response.
1841 */
1842 PHP_METHOD(HttpRequest, getResponseBody)
1843 {
1844 NO_ARGS;
1845
1846 if (return_value_used) {
1847 zval **body;
1848 zval *data = GET_PROP(responseData);
1849
1850 if ( (Z_TYPE_P(data) == IS_ARRAY) &&
1851 (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void *) &body))) {
1852 RETURN_ZVAL(*body, 1, 0);
1853 } else {
1854 RETURN_FALSE;
1855 }
1856 }
1857 }
1858 /* }}} */
1859
1860 /* {{{ proto int HttpRequest::getResponseCode()
1861 *
1862 * Get the response code after the request has been sent.
1863 *
1864 * Returns an int representing the response code.
1865 *
1866 * If redirects were allowed and several responses were received, the data
1867 * references the last received response.
1868 */
1869 PHP_METHOD(HttpRequest, getResponseCode)
1870 {
1871 NO_ARGS;
1872
1873 if (return_value_used) {
1874 RETURN_PROP(responseCode);
1875 }
1876 }
1877 /* }}} */
1878
1879 /* {{{ proto string HttpRequest::getResponseStatus()
1880 *
1881 * Get the response status (i.e. the string after the response code) after the message has been sent.
1882 *
1883 * Returns a string containing the response status text.
1884 */
1885 PHP_METHOD(HttpRequest, getResponseStatus)
1886 {
1887 NO_ARGS;
1888
1889 if (return_value_used) {
1890 RETURN_PROP(responseStatus);
1891 }
1892 }
1893 /* }}} */
1894
1895 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1896 *
1897 * Get response info after the request has been sent.
1898 * See http_get() for a full list of returned info.
1899 *
1900 * Accepts a string as optional parameter specifying the info to read.
1901 * If the parameter is empty or omitted, an associative array containing
1902 * all available info will be returned.
1903 *
1904 * Returns either a scalar containing the value of the info matching name if
1905 * requested, FALSE on failure, or an associative array containing all
1906 * available info.
1907 *
1908 * If redirects were allowed and several responses were received, the data
1909 * references the last received response.
1910 */
1911 PHP_METHOD(HttpRequest, getResponseInfo)
1912 {
1913 if (return_value_used) {
1914 zval *info, **infop;
1915 char *info_name = NULL;
1916 int info_len = 0;
1917
1918 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1919 RETURN_FALSE;
1920 }
1921
1922 info = GET_PROP(responseInfo);
1923
1924 if (Z_TYPE_P(info) != IS_ARRAY) {
1925 RETURN_FALSE;
1926 }
1927
1928 if (info_len && info_name) {
1929 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) {
1930 RETURN_ZVAL(*infop, 1, 0);
1931 } else {
1932 http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
1933 RETURN_FALSE;
1934 }
1935 } else {
1936 RETURN_ZVAL(info, 1, 0);
1937 }
1938 }
1939 }
1940 /* }}}*/
1941
1942 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1943 *
1944 * Get the full response as HttpMessage object after the request has been sent.
1945 *
1946 * Returns an HttpMessage object of the response.
1947 *
1948 * If redirects were allowed and several responses were received, the data
1949 * references the last received response. Use HttpMessage::getParentMessage()
1950 * to access the data of previously received responses within this request
1951 * cycle.
1952 *
1953 * Throws HttpException, HttpRuntimeException.
1954 */
1955 PHP_METHOD(HttpRequest, getResponseMessage)
1956 {
1957 NO_ARGS {
1958 zval *message;
1959
1960 SET_EH_THROW_HTTP();
1961 message = GET_PROP(responseMessage);
1962 if (Z_TYPE_P(message) == IS_OBJECT) {
1963 RETVAL_OBJECT(message, 1);
1964 } else {
1965 http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpRequest does not contain a response message");
1966 }
1967 SET_EH_NORMAL();
1968 }
1969 }
1970 /* }}} */
1971
1972 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1973 *
1974 * Get sent HTTP message.
1975 *
1976 * Returns an HttpMessage object representing the sent request.
1977 *
1978 * If redirects were allowed and several responses were received, the data
1979 * references the last received response. Use HttpMessage::getParentMessage()
1980 * to access the data of previously sent requests within this request
1981 * cycle.
1982 *
1983 * Note that the internal request message is immutable, that means that the
1984 * request message received through HttpRequest::getRequestMessage() will
1985 * always look the same for the same request, regardless of any changes you
1986 * may have made to the returned object.
1987 *
1988 * Throws HttpMalformedHeadersException, HttpEncodingException.
1989 */
1990 PHP_METHOD(HttpRequest, getRequestMessage)
1991 {
1992 NO_ARGS;
1993
1994 if (return_value_used) {
1995 http_message *msg;
1996 getObject(http_request_object, obj);
1997
1998 SET_EH_THROW_HTTP();
1999 if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
2000 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
2001 }
2002 SET_EH_NORMAL();
2003 }
2004 }
2005 /* }}} */
2006
2007 /* {{{ proto string HttpRequest::getRawRequestMessage()
2008 *
2009 * Get sent HTTP message.
2010 *
2011 * Returns an HttpMessage in a form of a string
2012 */
2013 PHP_METHOD(HttpRequest, getRawRequestMessage)
2014 {
2015 NO_ARGS;
2016
2017 if (return_value_used) {
2018 getObject(http_request_object, obj);
2019
2020 RETURN_PHPSTR_DUP(&obj->request->conv.request);
2021 }
2022 }
2023 /* }}} */
2024
2025 /* {{{ proto string HttpRequest::getRawResponseMessage()
2026 *
2027 * Get the entire HTTP response.
2028 *
2029 * Returns the complete web server response, including the headers in a form of a string.
2030 */
2031 PHP_METHOD(HttpRequest, getRawResponseMessage)
2032 {
2033 NO_ARGS;
2034
2035 if (return_value_used) {
2036 getObject(http_request_object, obj);
2037
2038 RETURN_PHPSTR_DUP(&obj->request->conv.response);
2039 }
2040 }
2041 /* }}} */
2042
2043 /* {{{ proto HttpMessage HttpRequest::getHistory()
2044 *
2045 * Get all sent requests and received responses as an HttpMessage object.
2046 *
2047 * If you want to record history, set the instance variable
2048 * HttpRequest::$recordHistory to TRUE.
2049 *
2050 * Returns an HttpMessage object representing the complete request/response
2051 * history.
2052 *
2053 * The object references the last received response, use HttpMessage::getParentMessage()
2054 * to access the data of previously sent requests and received responses.
2055 *
2056 * Throws HttpRuntimeException.
2057 */
2058 PHP_METHOD(HttpRequest, getHistory)
2059 {
2060 NO_ARGS;
2061
2062 if (return_value_used) {
2063 zval *hist;
2064
2065 SET_EH_THROW_HTTP();
2066 hist = GET_PROP(history);
2067 if (Z_TYPE_P(hist) == IS_OBJECT) {
2068 RETVAL_OBJECT(hist, 1);
2069 } else {
2070 http_error(HE_WARNING, HTTP_E_RUNTIME, "The history is empty");
2071 }
2072 SET_EH_NORMAL();
2073 }
2074 }
2075 /* }}} */
2076
2077 /* {{{ proto void HttpRequest::clearHistory()
2078 *
2079 * Clear the history.
2080 */
2081 PHP_METHOD(HttpRequest, clearHistory)
2082 {
2083 NO_ARGS {
2084 zval *hist;
2085
2086 MAKE_STD_ZVAL(hist);
2087 ZVAL_NULL(hist);
2088 SET_PROP(history, hist);
2089 zval_ptr_dtor(&hist);
2090 }
2091 }
2092 /* }}} */
2093
2094 /* {{{ proto HttpMessage HttpRequest::send()
2095 *
2096 * Send the HTTP request.
2097 *
2098 * Returns the received response as HttpMessage object.
2099 *
2100 * NOTE: While an exception may be thrown, the transfer could have succeeded
2101 * at least partially, so you might want to check the return values of various
2102 * HttpRequest::getResponse*() methods.
2103 *
2104 * Throws HttpRuntimeException, HttpRequestException,
2105 * HttpMalformedHeaderException, HttpEncodingException.
2106 *
2107 * GET example:
2108 * <pre>
2109 * <?php
2110 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
2111 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
2112 * $r->addQueryData(array('category' => 3));
2113 * try {
2114 * $r->send();
2115 * if ($r->getResponseCode() == 200) {
2116 * file_put_contents('local.rss', $r->getResponseBody());
2117 * }
2118 * } catch (HttpException $ex) {
2119 * echo $ex;
2120 * }
2121 * ?>
2122 * </pre>
2123 *
2124 * POST example:
2125 * <pre>
2126 * <?php
2127 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
2128 * $r->setOptions(array('cookies' => array('lang' => 'de')));
2129 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
2130 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
2131 * try {
2132 * echo $r->send()->getBody();
2133 * } catch (HttpException $ex) {
2134 * echo $ex;
2135 * }
2136 * ?>
2137 * </pre>
2138 */
2139 PHP_METHOD(HttpRequest, send)
2140 {
2141 getObject(http_request_object, obj);
2142
2143 NO_ARGS;
2144
2145 SET_EH_THROW_HTTP();
2146
2147 RETVAL_FALSE;
2148
2149 if (obj->pool) {
2150 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
2151 } else if (SUCCESS == http_request_object_requesthandler(obj, getThis())) {
2152 http_request_exec(obj->request);
2153 if (SUCCESS == http_request_object_responsehandler(obj, getThis())) {
2154 RETVAL_OBJECT(GET_PROP(responseMessage), 1);
2155 }
2156 }
2157
2158 SET_EH_NORMAL();
2159 }
2160 /* }}} */
2161
2162 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
2163
2164 /*
2165 * Local variables:
2166 * tab-width: 4
2167 * c-basic-offset: 4
2168 * End:
2169 * vim600: noet sw=4 ts=4 fdm=marker
2170 * vim<600: noet sw=4 ts=4
2171 */
2172