3f37f46f9ada3d1549d8ccbf474f6ed4985973ba
[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_BEGIN_ARGS(factory, 0)
44 HTTP_ARG_VAL(url, 0)
45 HTTP_ARG_VAL(method, 0)
46 HTTP_ARG_VAL(options, 0)
47 HTTP_ARG_VAL(class_name, 0)
48 HTTP_END_ARGS;
49
50 HTTP_EMPTY_ARGS(getOptions);
51 HTTP_BEGIN_ARGS(setOptions, 0)
52 HTTP_ARG_VAL(options, 0)
53 HTTP_END_ARGS;
54
55 HTTP_EMPTY_ARGS(getSslOptions);
56 HTTP_BEGIN_ARGS(setSslOptions, 0)
57 HTTP_ARG_VAL(ssl_options, 0)
58 HTTP_END_ARGS;
59
60 HTTP_BEGIN_ARGS(addSslOptions, 0)
61 HTTP_ARG_VAL(ssl_optins, 0)
62 HTTP_END_ARGS;
63
64 HTTP_EMPTY_ARGS(getHeaders);
65 HTTP_BEGIN_ARGS(setHeaders, 0)
66 HTTP_ARG_VAL(headers, 0)
67 HTTP_END_ARGS;
68
69 HTTP_BEGIN_ARGS(addHeaders, 1)
70 HTTP_ARG_VAL(headers, 0)
71 HTTP_END_ARGS;
72
73 HTTP_EMPTY_ARGS(getCookies);
74 HTTP_BEGIN_ARGS(setCookies, 0)
75 HTTP_ARG_VAL(cookies, 0)
76 HTTP_END_ARGS;
77
78 HTTP_BEGIN_ARGS(addCookies, 1)
79 HTTP_ARG_VAL(cookies, 0)
80 HTTP_END_ARGS;
81
82 HTTP_EMPTY_ARGS(enableCookies);
83 #if HTTP_CURL_VERSION(7,14,1)
84 HTTP_BEGIN_ARGS(resetCookies, 0)
85 HTTP_ARG_VAL(session_only, 0)
86 HTTP_END_ARGS;
87 #endif
88
89 HTTP_EMPTY_ARGS(getUrl);
90 HTTP_BEGIN_ARGS(setUrl, 1)
91 HTTP_ARG_VAL(url, 0)
92 HTTP_END_ARGS;
93
94 HTTP_EMPTY_ARGS(getMethod);
95 HTTP_BEGIN_ARGS(setMethod, 1)
96 HTTP_ARG_VAL(request_method, 0)
97 HTTP_END_ARGS;
98
99 HTTP_EMPTY_ARGS(getContentType);
100 HTTP_BEGIN_ARGS(setContentType, 1)
101 HTTP_ARG_VAL(content_type, 0)
102 HTTP_END_ARGS;
103
104 HTTP_EMPTY_ARGS(getQueryData);
105 HTTP_BEGIN_ARGS(setQueryData, 0)
106 HTTP_ARG_VAL(query_data, 0)
107 HTTP_END_ARGS;
108
109 HTTP_BEGIN_ARGS(addQueryData, 1)
110 HTTP_ARG_VAL(query_data, 0)
111 HTTP_END_ARGS;
112
113 HTTP_EMPTY_ARGS(getPostFields);
114 HTTP_BEGIN_ARGS(setPostFields, 0)
115 HTTP_ARG_VAL(post_fields, 0)
116 HTTP_END_ARGS;
117
118 HTTP_BEGIN_ARGS(addPostFields, 1)
119 HTTP_ARG_VAL(post_fields, 0)
120 HTTP_END_ARGS;
121
122 HTTP_EMPTY_ARGS(getPostFiles);
123 HTTP_BEGIN_ARGS(setPostFiles, 0)
124 HTTP_ARG_VAL(post_files, 0)
125 HTTP_END_ARGS;
126
127 HTTP_BEGIN_ARGS(addPostFile, 2)
128 HTTP_ARG_VAL(formname, 0)
129 HTTP_ARG_VAL(filename, 0)
130 HTTP_ARG_VAL(content_type, 0)
131 HTTP_END_ARGS;
132
133 HTTP_EMPTY_ARGS(getRawPostData);
134 HTTP_BEGIN_ARGS(setRawPostData, 0)
135 HTTP_ARG_VAL(raw_post_data, 0)
136 HTTP_END_ARGS;
137
138 HTTP_BEGIN_ARGS(addRawPostData, 1)
139 HTTP_ARG_VAL(raw_post_data, 0)
140 HTTP_END_ARGS;
141
142 HTTP_EMPTY_ARGS(getPutFile);
143 HTTP_BEGIN_ARGS(setPutFile, 0)
144 HTTP_ARG_VAL(filename, 0)
145 HTTP_END_ARGS;
146
147 HTTP_EMPTY_ARGS(getPutData);
148 HTTP_BEGIN_ARGS(setPutData, 0)
149 HTTP_ARG_VAL(put_data, 0)
150 HTTP_END_ARGS;
151
152 HTTP_BEGIN_ARGS(addPutData, 1)
153 HTTP_ARG_VAL(put_data, 0)
154 HTTP_END_ARGS;
155
156 HTTP_EMPTY_ARGS(getResponseData);
157 HTTP_BEGIN_ARGS(getResponseHeader, 0)
158 HTTP_ARG_VAL(name, 0)
159 HTTP_END_ARGS;
160
161 HTTP_BEGIN_ARGS(getResponseCookies, 0)
162 HTTP_ARG_VAL(flags, 0)
163 HTTP_ARG_VAL(allowed_extras, 0)
164 HTTP_END_ARGS;
165
166 HTTP_EMPTY_ARGS(getResponseBody);
167 HTTP_EMPTY_ARGS(getResponseCode);
168 HTTP_EMPTY_ARGS(getResponseStatus);
169 HTTP_BEGIN_ARGS(getResponseInfo, 0)
170 HTTP_ARG_VAL(name, 0)
171 HTTP_END_ARGS;
172
173 HTTP_EMPTY_ARGS(getResponseMessage);
174 HTTP_EMPTY_ARGS(getRawResponseMessage);
175 HTTP_EMPTY_ARGS(getRequestMessage);
176 HTTP_EMPTY_ARGS(getRawRequestMessage);
177 HTTP_EMPTY_ARGS(getHistory);
178 HTTP_EMPTY_ARGS(clearHistory);
179 HTTP_EMPTY_ARGS(send);
180
181 HTTP_BEGIN_ARGS(get, 1)
182 HTTP_ARG_VAL(url, 0)
183 HTTP_ARG_VAL(options, 0)
184 HTTP_ARG_VAL(info, 1)
185 HTTP_END_ARGS;
186
187 HTTP_BEGIN_ARGS(head, 1)
188 HTTP_ARG_VAL(url, 0)
189 HTTP_ARG_VAL(options, 0)
190 HTTP_ARG_VAL(info, 1)
191 HTTP_END_ARGS;
192
193 HTTP_BEGIN_ARGS(postData, 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(postFields, 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(putData, 2)
208 HTTP_ARG_VAL(url, 0)
209 HTTP_ARG_VAL(data, 0)
210 HTTP_ARG_VAL(options, 0)
211 HTTP_ARG_VAL(info, 1)
212 HTTP_END_ARGS;
213
214 HTTP_BEGIN_ARGS(putFile, 2)
215 HTTP_ARG_VAL(url, 0)
216 HTTP_ARG_VAL(file, 0)
217 HTTP_ARG_VAL(options, 0)
218 HTTP_ARG_VAL(info, 1)
219 HTTP_END_ARGS;
220
221 HTTP_BEGIN_ARGS(putStream, 2)
222 HTTP_ARG_VAL(url, 0)
223 HTTP_ARG_VAL(stream, 0)
224 HTTP_ARG_VAL(options, 0)
225 HTTP_ARG_VAL(info, 1)
226 HTTP_END_ARGS;
227
228 HTTP_BEGIN_ARGS(methodRegister, 1)
229 HTTP_ARG_VAL(method_name, 0)
230 HTTP_END_ARGS;
231
232 HTTP_BEGIN_ARGS(methodUnregister, 1)
233 HTTP_ARG_VAL(method, 0)
234 HTTP_END_ARGS;
235
236 HTTP_BEGIN_ARGS(methodName, 1)
237 HTTP_ARG_VAL(method_id, 0)
238 HTTP_END_ARGS;
239
240 HTTP_BEGIN_ARGS(methodExists, 1)
241 HTTP_ARG_VAL(method, 0)
242 HTTP_END_ARGS;
243
244 #ifdef HAVE_CURL_GETFORMDATA
245 HTTP_BEGIN_ARGS(encodeBody, 2)
246 HTTP_ARG_VAL(fields, 0)
247 HTTP_ARG_VAL(files, 0)
248 HTTP_END_ARGS;
249 #endif
250
251 #define OBJ_PROP_CE http_request_object_ce
252 zend_class_entry *http_request_object_ce;
253 zend_function_entry http_request_object_fe[] = {
254 HTTP_REQUEST_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
255
256 HTTP_REQUEST_ME(setOptions, ZEND_ACC_PUBLIC)
257 HTTP_REQUEST_ME(getOptions, ZEND_ACC_PUBLIC)
258 HTTP_REQUEST_ME(setSslOptions, ZEND_ACC_PUBLIC)
259 HTTP_REQUEST_ME(getSslOptions, ZEND_ACC_PUBLIC)
260 HTTP_REQUEST_ME(addSslOptions, ZEND_ACC_PUBLIC)
261
262 HTTP_REQUEST_ME(addHeaders, ZEND_ACC_PUBLIC)
263 HTTP_REQUEST_ME(getHeaders, ZEND_ACC_PUBLIC)
264 HTTP_REQUEST_ME(setHeaders, ZEND_ACC_PUBLIC)
265
266 HTTP_REQUEST_ME(addCookies, ZEND_ACC_PUBLIC)
267 HTTP_REQUEST_ME(getCookies, ZEND_ACC_PUBLIC)
268 HTTP_REQUEST_ME(setCookies, ZEND_ACC_PUBLIC)
269
270 HTTP_REQUEST_ME(enableCookies, ZEND_ACC_PUBLIC)
271 #if HTTP_CURL_VERSION(7,14,1)
272 HTTP_REQUEST_ME(resetCookies, ZEND_ACC_PUBLIC)
273 #endif
274
275 HTTP_REQUEST_ME(setMethod, ZEND_ACC_PUBLIC)
276 HTTP_REQUEST_ME(getMethod, ZEND_ACC_PUBLIC)
277
278 HTTP_REQUEST_ME(setUrl, ZEND_ACC_PUBLIC)
279 HTTP_REQUEST_ME(getUrl, ZEND_ACC_PUBLIC)
280
281 HTTP_REQUEST_ME(setContentType, ZEND_ACC_PUBLIC)
282 HTTP_REQUEST_ME(getContentType, ZEND_ACC_PUBLIC)
283
284 HTTP_REQUEST_ME(setQueryData, ZEND_ACC_PUBLIC)
285 HTTP_REQUEST_ME(getQueryData, ZEND_ACC_PUBLIC)
286 HTTP_REQUEST_ME(addQueryData, ZEND_ACC_PUBLIC)
287
288 HTTP_REQUEST_ME(setPostFields, ZEND_ACC_PUBLIC)
289 HTTP_REQUEST_ME(getPostFields, ZEND_ACC_PUBLIC)
290 HTTP_REQUEST_ME(addPostFields, ZEND_ACC_PUBLIC)
291
292 HTTP_REQUEST_ME(setRawPostData, ZEND_ACC_PUBLIC)
293 HTTP_REQUEST_ME(getRawPostData, ZEND_ACC_PUBLIC)
294 HTTP_REQUEST_ME(addRawPostData, ZEND_ACC_PUBLIC)
295
296 HTTP_REQUEST_ME(setPostFiles, ZEND_ACC_PUBLIC)
297 HTTP_REQUEST_ME(addPostFile, ZEND_ACC_PUBLIC)
298 HTTP_REQUEST_ME(getPostFiles, ZEND_ACC_PUBLIC)
299
300 HTTP_REQUEST_ME(setPutFile, ZEND_ACC_PUBLIC)
301 HTTP_REQUEST_ME(getPutFile, ZEND_ACC_PUBLIC)
302
303 HTTP_REQUEST_ME(setPutData, ZEND_ACC_PUBLIC)
304 HTTP_REQUEST_ME(getPutData, ZEND_ACC_PUBLIC)
305 HTTP_REQUEST_ME(addPutData, ZEND_ACC_PUBLIC)
306
307 HTTP_REQUEST_ME(send, ZEND_ACC_PUBLIC)
308
309 HTTP_REQUEST_ME(getResponseData, ZEND_ACC_PUBLIC)
310 HTTP_REQUEST_ME(getResponseHeader, ZEND_ACC_PUBLIC)
311 HTTP_REQUEST_ME(getResponseCookies, ZEND_ACC_PUBLIC)
312 HTTP_REQUEST_ME(getResponseCode, ZEND_ACC_PUBLIC)
313 HTTP_REQUEST_ME(getResponseStatus, ZEND_ACC_PUBLIC)
314 HTTP_REQUEST_ME(getResponseBody, ZEND_ACC_PUBLIC)
315 HTTP_REQUEST_ME(getResponseInfo, ZEND_ACC_PUBLIC)
316 HTTP_REQUEST_ME(getResponseMessage, ZEND_ACC_PUBLIC)
317 HTTP_REQUEST_ME(getRawResponseMessage, ZEND_ACC_PUBLIC)
318 HTTP_REQUEST_ME(getRequestMessage, ZEND_ACC_PUBLIC)
319 HTTP_REQUEST_ME(getRawRequestMessage, ZEND_ACC_PUBLIC)
320 HTTP_REQUEST_ME(getHistory, ZEND_ACC_PUBLIC)
321 HTTP_REQUEST_ME(clearHistory, ZEND_ACC_PUBLIC)
322
323 HTTP_REQUEST_ME(factory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
324
325 HTTP_REQUEST_ALIAS(get, http_get)
326 HTTP_REQUEST_ALIAS(head, http_head)
327 HTTP_REQUEST_ALIAS(postData, http_post_data)
328 HTTP_REQUEST_ALIAS(postFields, http_post_fields)
329 HTTP_REQUEST_ALIAS(putData, http_put_data)
330 HTTP_REQUEST_ALIAS(putFile, http_put_file)
331 HTTP_REQUEST_ALIAS(putStream, http_put_stream)
332
333 HTTP_REQUEST_ALIAS(methodRegister, http_request_method_register)
334 HTTP_REQUEST_ALIAS(methodUnregister, http_request_method_unregister)
335 HTTP_REQUEST_ALIAS(methodName, http_request_method_name)
336 HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists)
337 #ifdef HAVE_CURL_GETFORMDATA
338 HTTP_REQUEST_ALIAS(encodeBody, http_request_body_encode)
339 #endif
340 EMPTY_FUNCTION_ENTRY
341 };
342 static zend_object_handlers http_request_object_handlers;
343
344 PHP_MINIT_FUNCTION(http_request_object)
345 {
346 HTTP_REGISTER_CLASS_EX(HttpRequest, http_request_object, NULL, 0);
347 http_request_object_handlers.clone_obj = _http_request_object_clone_obj;
348
349 DCL_PROP_N(PRIVATE, options);
350 DCL_PROP_N(PRIVATE, postFields);
351 DCL_PROP_N(PRIVATE, postFiles);
352 DCL_PROP_N(PRIVATE, responseInfo);
353 DCL_PROP_N(PRIVATE, responseMessage);
354 DCL_PROP(PRIVATE, long, responseCode, 0);
355 DCL_PROP(PRIVATE, string, responseStatus, "");
356 DCL_PROP(PRIVATE, long, method, HTTP_GET);
357 DCL_PROP(PRIVATE, string, url, "");
358 DCL_PROP(PRIVATE, string, contentType, "");
359 DCL_PROP(PRIVATE, string, rawPostData, "");
360 DCL_PROP(PRIVATE, string, queryData, "");
361 DCL_PROP(PRIVATE, string, putFile, "");
362 DCL_PROP(PRIVATE, string, putData, "");
363 DCL_PROP_N(PRIVATE, history);
364 DCL_PROP(PUBLIC, bool, recordHistory, 0);
365
366 #ifndef WONKY
367 /*
368 * Request Method Constants
369 */
370 /* HTTP/1.1 */
371 DCL_CONST(long, "METH_GET", HTTP_GET);
372 DCL_CONST(long, "METH_HEAD", HTTP_HEAD);
373 DCL_CONST(long, "METH_POST", HTTP_POST);
374 DCL_CONST(long, "METH_PUT", HTTP_PUT);
375 DCL_CONST(long, "METH_DELETE", HTTP_DELETE);
376 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS);
377 DCL_CONST(long, "METH_TRACE", HTTP_TRACE);
378 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT);
379 /* WebDAV - RFC 2518 */
380 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND);
381 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH);
382 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL);
383 DCL_CONST(long, "METH_COPY", HTTP_COPY);
384 DCL_CONST(long, "METH_MOVE", HTTP_MOVE);
385 DCL_CONST(long, "METH_LOCK", HTTP_LOCK);
386 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK);
387 /* WebDAV Versioning - RFC 3253 */
388 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL);
389 DCL_CONST(long, "METH_REPORT", HTTP_REPORT);
390 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT);
391 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN);
392 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT);
393 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE);
394 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE);
395 DCL_CONST(long, "METH_LABEL", HTTP_LABEL);
396 DCL_CONST(long, "METH_MERGE", HTTP_MERGE);
397 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL);
398 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY);
399 /* WebDAV Access Control - RFC 3744 */
400 DCL_CONST(long, "METH_ACL", HTTP_ACL);
401
402 /*
403 * HTTP Protocol Version Constants
404 */
405 DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0);
406 DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1);
407 DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE); /* to be removed */
408 DCL_CONST(long, "VERSION_ANY", CURL_HTTP_VERSION_NONE);
409
410 /*
411 * SSL Version Constants
412 */
413 DCL_CONST(long, "SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1);
414 DCL_CONST(long, "SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2);
415 DCL_CONST(long, "SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3);
416 DCL_CONST(long, "SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT);
417
418 /*
419 * DNS IPvX resolving
420 */
421 DCL_CONST(long, "IPRESOLVE_V4", CURL_IPRESOLVE_V4);
422 DCL_CONST(long, "IPRESOLVE_V6", CURL_IPRESOLVE_V6);
423 DCL_CONST(long, "IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER);
424
425 /*
426 * Auth Constants
427 */
428 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC);
429 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST);
430 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM);
431 DCL_CONST(long, "AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE);
432 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY);
433
434 /*
435 * Proxy Type Constants
436 */
437 # if HTTP_CURL_VERSION(7,15,2)
438 DCL_CONST(long, "PROXY_SOCKS4", CURLPROXY_SOCKS4);
439 # endif
440 DCL_CONST(long, "PROXY_SOCKS5", CURLPROXY_SOCKS5);
441 DCL_CONST(long, "PROXY_HTTP", CURLPROXY_HTTP);
442 #endif /* WONKY */
443
444 return SUCCESS;
445 }
446
447 zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC)
448 {
449 return http_request_object_new_ex(ce, NULL, NULL);
450 }
451
452 zend_object_value _http_request_object_new_ex(zend_class_entry *ce, CURL *ch, http_request_object **ptr TSRMLS_DC)
453 {
454 zend_object_value ov;
455 http_request_object *o;
456
457 o = ecalloc(1, sizeof(http_request_object));
458 o->zo.ce = ce;
459 o->request = http_request_init_ex(NULL, ch, 0, NULL);
460
461 if (ptr) {
462 *ptr = o;
463 }
464
465 ALLOC_HASHTABLE(OBJ_PROP(o));
466 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
467 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
468
469 ov.handle = putObject(http_request_object, o);
470 ov.handlers = &http_request_object_handlers;
471
472 return ov;
473 }
474
475 zend_object_value _http_request_object_clone_obj(zval *this_ptr TSRMLS_DC)
476 {
477 zend_object_value new_ov;
478 http_request_object *new_obj;
479 getObject(http_request_object, old_obj);
480
481 new_ov = http_request_object_new_ex(old_obj->zo.ce, NULL, &new_obj);
482 if (old_obj->request->ch) {
483 http_curl_init_ex(curl_easy_duphandle(old_obj->request->ch), new_obj->request);
484 }
485
486 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
487 phpstr_append(&new_obj->request->conv.request, old_obj->request->conv.request.data, old_obj->request->conv.request.used);
488 phpstr_append(&new_obj->request->conv.response, old_obj->request->conv.response.data, old_obj->request->conv.response.used);
489
490 return new_ov;
491 }
492
493 void _http_request_object_free(zend_object *object TSRMLS_DC)
494 {
495 http_request_object *o = (http_request_object *) object;
496
497 http_request_free(&o->request);
498 freeObject(o);
499 }
500
501 #define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC)
502 static inline void _http_request_object_check_request_content_type(zval *this_ptr TSRMLS_DC)
503 {
504 zval *ctype = GET_PROP(contentType);
505
506 if (Z_STRLEN_P(ctype)) {
507 zval **headers, *opts = GET_PROP(options);
508
509 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
510 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void *) &headers)) &&
511 (Z_TYPE_PP(headers) == IS_ARRAY)) {
512 zval **ct_header;
513
514 /* only override if not already set */
515 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void *) &ct_header))) {
516 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
517 } else
518 /* or not a string, zero length string or a string of spaces */
519 if ((Z_TYPE_PP(ct_header) != IS_STRING) || !Z_STRLEN_PP(ct_header)) {
520 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
521 } else {
522 int i, only_space = 1;
523
524 /* check for spaces only */
525 for (i = 0; i < Z_STRLEN_PP(ct_header); ++i) {
526 if (!HTTP_IS_CTYPE(space, Z_STRVAL_PP(ct_header)[i])) {
527 only_space = 0;
528 break;
529 }
530 }
531 if (only_space) {
532 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
533 }
534 }
535 } else {
536 zval *headers;
537
538 MAKE_STD_ZVAL(headers);
539 array_init(headers);
540 add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
541 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers);
542 zval_ptr_dtor(&headers);
543 }
544 }
545 }
546
547 STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
548 {
549 STATUS status = SUCCESS;
550
551 http_request_reset(obj->request);
552 HTTP_CHECK_CURL_INIT(obj->request->ch, http_curl_init(obj->request), return FAILURE);
553
554 obj->request->url = http_absolute_url(Z_STRVAL_P(GET_PROP(url)));
555
556 switch (obj->request->meth = Z_LVAL_P(GET_PROP(method)))
557 {
558 case HTTP_GET:
559 case HTTP_HEAD:
560 break;
561
562 case HTTP_PUT:
563 {
564 zval *put_file = GET_PROP(putFile);
565
566 http_request_object_check_request_content_type(getThis());
567
568 if (Z_STRLEN_P(put_file)) {
569 php_stream_statbuf ssb;
570 php_stream *stream = php_stream_open_wrapper_ex(Z_STRVAL_P(put_file), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT);
571
572 if (stream && SUCCESS == php_stream_stat(stream, &ssb)) {
573 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1);
574 } else {
575 status = FAILURE;
576 }
577 } else {
578 zval *put_data = GET_PROP(putData);
579 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
580 estrndup(Z_STRVAL_P(put_data), Z_STRLEN_P(put_data)), Z_STRLEN_P(put_data), 1);
581 }
582 break;
583 }
584
585 case HTTP_POST:
586 default:
587 {
588 /* check for raw post data */
589 zval *raw_data = GET_PROP(rawPostData);
590
591 if (Z_STRLEN_P(raw_data)) {
592 http_request_object_check_request_content_type(getThis());
593 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
594 estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data)), Z_STRLEN_P(raw_data), 1);
595 } else {
596 zval *zfields = GET_PROP(postFields), *zfiles = GET_PROP(postFiles);
597 HashTable *fields;
598 HashTable *files;
599
600 fields = (Z_TYPE_P(zfields) == IS_ARRAY) ? Z_ARRVAL_P(zfields) : NULL;
601 files = (Z_TYPE_P(zfiles) == IS_ARRAY) ? Z_ARRVAL_P(zfiles) : NULL;
602
603 if ((fields && zend_hash_num_elements(fields)) || (files && zend_hash_num_elements(files))) {
604 if (!(obj->request->body = http_request_body_fill(obj->request->body, fields, files))) {
605 status = FAILURE;
606 }
607 }
608 }
609 break;
610 }
611 }
612
613 if (status == SUCCESS) {
614 zval *qdata = GET_PROP(queryData);
615 zval *options = GET_PROP(options);
616
617 if (Z_STRLEN_P(qdata)) {
618 if (!strchr(obj->request->url, '?')) {
619 strlcat(obj->request->url, "?", HTTP_URL_MAXLEN);
620 } else {
621 strlcat(obj->request->url, "&", HTTP_URL_MAXLEN);
622 }
623 strlcat(obj->request->url, Z_STRVAL_P(qdata), HTTP_URL_MAXLEN);
624 }
625
626 http_request_prepare(obj->request, Z_ARRVAL_P(options));
627
628 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
629 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onprogress", sizeof("onprogress"))) {
630 zval **entry, *pcb;
631
632 if ( (Z_TYPE_P(options) != IS_ARRAY)
633 || (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void *) &entry)
634 || (!zval_is_true(*entry)))) {
635 MAKE_STD_ZVAL(pcb);
636 array_init(pcb);
637 ZVAL_ADDREF(getThis());
638 add_next_index_zval(pcb, getThis());
639 add_next_index_stringl(pcb, "onprogress", lenof("onprogress"), 1);
640 http_request_set_progress_callback(obj->request, pcb);
641 zval_ptr_dtor(&pcb);
642 }
643 }
644 }
645
646 return status;
647 }
648
649 STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
650 {
651 STATUS ret;
652 zval *info;
653 http_message *msg;
654
655 /* always fetch info */
656 MAKE_STD_ZVAL(info);
657 array_init(info);
658 http_request_info(obj->request, Z_ARRVAL_P(info));
659 SET_PROP(responseInfo, info);
660 zval_ptr_dtor(&info);
661
662 /* parse response message */
663 phpstr_fix(&obj->request->conv.request);
664 phpstr_fix(&obj->request->conv.response);
665
666 if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)))) {
667 zval *message;
668
669 if (zval_is_true(GET_PROP(recordHistory))) {
670 zval *hist, *history = GET_PROP(history);
671 http_message *response = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response));
672 http_message *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
673
674 MAKE_STD_ZVAL(hist);
675 ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, http_message_interconnect(response, request), NULL), 0);
676 if (Z_TYPE_P(history) == IS_OBJECT) {
677 http_message_object_prepend(hist, history);
678 }
679 SET_PROP(history, hist);
680 zval_ptr_dtor(&hist);
681 }
682
683 UPD_PROP(long, responseCode, msg->http.info.response.code);
684 UPD_PROP(string, responseStatus, STR_PTR(msg->http.info.response.status));
685
686 MAKE_STD_ZVAL(message);
687 ZVAL_OBJVAL(message, http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
688 SET_PROP(responseMessage, message);
689 zval_ptr_dtor(&message);
690
691 ret = SUCCESS;
692 } else {
693 /* update properties with empty values*/
694 zval *znull;
695
696 MAKE_STD_ZVAL(znull);
697 ZVAL_NULL(znull);
698 SET_PROP(responseMessage, znull);
699 zval_ptr_dtor(&znull);
700
701 UPD_PROP(long, responseCode, 0);
702 UPD_PROP(string, responseStatus, "");
703
704 /* append request message to history */
705 if (zval_is_true(GET_PROP(recordHistory))) {
706 http_message *request;
707
708 if ((request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
709 zval *hist, *history = GET_PROP(history);
710
711 MAKE_STD_ZVAL(hist);
712 ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, request, NULL), 0);
713 if (Z_TYPE_P(history) == IS_OBJECT) {
714 http_message_object_prepend(hist, history);
715 }
716 SET_PROP(history, hist);
717 zval_ptr_dtor(&hist);
718 }
719 }
720
721 ret = FAILURE;
722 }
723
724 if (!EG(exception) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) {
725 zval *param;
726
727 MAKE_STD_ZVAL(param);
728 ZVAL_BOOL(param, ret == SUCCESS);
729 with_error_handling(EH_NORMAL, NULL) {
730 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param);
731 } end_error_handling();
732 zval_ptr_dtor(&param);
733 }
734
735 return ret;
736 }
737
738 static int apply_pretty_key(void *pDest, int num_args, va_list args, zend_hash_key *hash_key)
739 {
740 if (hash_key->nKeyLength > 1) {
741 hash_key->h = zend_get_hash_value(pretty_key(hash_key->arKey, hash_key->nKeyLength - 1, 1, 0), hash_key->nKeyLength);
742 }
743 return ZEND_HASH_APPLY_KEEP;
744 }
745
746 #define http_request_object_set_options_subr(key, ow, pk) \
747 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
748 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys)
749 {
750 zval *old_opts, *new_opts, *opts = NULL, **entry = NULL;
751
752 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
753 RETURN_FALSE;
754 }
755
756 MAKE_STD_ZVAL(new_opts);
757 array_init(new_opts);
758 old_opts = GET_PROP(options);
759 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
760 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
761 }
762
763 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
764 if (overwrite) {
765 zend_hash_clean(Z_ARRVAL_PP(entry));
766 }
767 if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
768 if (overwrite) {
769 array_copy(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry));
770 } else {
771 array_join(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry), 0, prettify_keys ? ARRAY_JOIN_PRETTIFY : 0);
772 }
773 }
774 } else if (opts) {
775 if (prettify_keys) {
776 zend_hash_apply_with_arguments(Z_ARRVAL_P(opts), apply_pretty_key, 0);
777 }
778 ZVAL_ADDREF(opts);
779 add_assoc_zval_ex(new_opts, key, len, opts);
780 }
781 SET_PROP(options, new_opts);
782 zval_ptr_dtor(&new_opts);
783
784 RETURN_TRUE;
785 }
786
787 #define http_request_object_get_options_subr(key) \
788 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
789 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
790 {
791 NO_ARGS;
792
793 if (return_value_used) {
794 zval *opts, **options;
795
796 opts = GET_PROP(options);
797 array_init(return_value);
798
799 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
800 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
801 convert_to_array(*options);
802 array_copy(Z_ARRVAL_PP(options), Z_ARRVAL_P(return_value));
803 }
804 }
805 }
806
807
808 /* ### USERLAND ### */
809
810 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
811 Create a new HttpRequest object instance. */
812 PHP_METHOD(HttpRequest, __construct)
813 {
814 char *URL = NULL;
815 int URL_len;
816 long meth = -1;
817 zval *options = NULL;
818
819 SET_EH_THROW_HTTP();
820 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla!", &URL, &URL_len, &meth, &options)) {
821 if (URL) {
822 UPD_STRL(url, URL, URL_len);
823 }
824 if (meth > -1) {
825 UPD_PROP(long, method, meth);
826 }
827 if (options) {
828 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
829 }
830 }
831 SET_EH_NORMAL();
832 }
833 /* }}} */
834
835 /* {{{ proto HttpRequest HttpRequest::factory([string url[, int request_method HTTP_METH_GET[, array options[, string class_name = "HttpRequest"]]]])
836 Create a new HttpRequest object instance. */
837 PHP_METHOD(HttpRequest, factory)
838 {
839 char *cn = NULL, *URL = NULL;
840 int cl = 0, URL_len = 0;
841 long meth = -1;
842 zval *options = NULL;
843 zend_object_value ov;
844
845 SET_EH_THROW_HTTP();
846 if ( SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla!s", &URL, &URL_len, &meth, &options, &cn, &cl) &&
847 SUCCESS == http_object_new(&ov, cn, cl, _http_request_object_new_ex, http_request_object_ce, NULL, NULL)) {
848 RETVAL_OBJVAL(ov, 0);
849 getThis() = return_value;
850 if (URL) {
851 UPD_STRL(url, URL, URL_len);
852 }
853 if (meth > -1) {
854 UPD_PROP(long, method, meth);
855 }
856 if (options) {
857 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
858 }
859 }
860 SET_EH_NORMAL();
861 }
862 /* }}} */
863
864 /* {{{ proto bool HttpRequest::setOptions([array options])
865 Set the request options to use. See http_get() for a full list of available options. */
866 PHP_METHOD(HttpRequest, setOptions)
867 {
868 HashKey key = initHashKey(0);
869 HashPosition pos;
870 zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt;
871
872 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
873 RETURN_FALSE;
874 }
875
876 MAKE_STD_ZVAL(new_opts);
877 array_init(new_opts);
878
879 if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
880 SET_PROP(options, new_opts);
881 zval_ptr_dtor(&new_opts);
882 RETURN_TRUE;
883 }
884
885 MAKE_STD_ZVAL(add_opts);
886 array_init(add_opts);
887 /* some options need extra attention -- thus cannot use array_merge() directly */
888 FOREACH_KEYVAL(pos, opts, key, opt) {
889 if (key.type == HASH_KEY_IS_STRING) {
890 if (!strcmp(key.str, "headers")) {
891 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, *opt);
892 } else if (!strcmp(key.str, "cookies")) {
893 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt);
894 } else if (!strcmp(key.str, "ssl")) {
895 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt);
896 } else if ((!strcasecmp(key.str, "url")) || (!strcasecmp(key.str, "uri"))) {
897 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt);
898 } else if (!strcmp(key.str, "method")) {
899 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt);
900 #if HTTP_CURL_VERSION(7,14,1)
901 } else if (!strcmp(key.str, "resetcookies")) {
902 getObject(http_request_object, obj);
903 http_request_reset_cookies(obj->request, 0);
904 #endif
905 } else if (!strcmp(key.str, "enablecookies")) {
906 getObject(http_request_object, obj);
907 http_request_enable_cookies(obj->request);
908 } else if (!strcasecmp(key.str, "recordHistory")) {
909 UPD_PROP(bool, recordHistory, 1);
910 } else {
911 ZVAL_ADDREF(*opt);
912 add_assoc_zval_ex(add_opts, key.str, key.len, *opt);
913 }
914 }
915 }
916
917 old_opts = GET_PROP(options);
918 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
919 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
920 }
921 array_join(Z_ARRVAL_P(add_opts), Z_ARRVAL_P(new_opts), 0, 0);
922 SET_PROP(options, new_opts);
923 zval_ptr_dtor(&new_opts);
924 zval_ptr_dtor(&add_opts);
925
926 RETURN_TRUE;
927 }
928 /* }}} */
929
930 /* {{{ proto array HttpRequest::getOptions()
931 Get currently set options. */
932 PHP_METHOD(HttpRequest, getOptions)
933 {
934 NO_ARGS;
935
936 if (return_value_used) {
937 RETURN_PROP(options);
938 }
939 }
940 /* }}} */
941
942 /* {{{ proto bool HttpRequest::setSslOptions([array options])
943 Set SSL options. */
944 PHP_METHOD(HttpRequest, setSslOptions)
945 {
946 http_request_object_set_options_subr("ssl", 1, 0);
947 }
948 /* }}} */
949
950 /* {{{ proto bool HttpRequest::addSslOptions(array options)
951 Set additional SSL options. */
952 PHP_METHOD(HttpRequest, addSslOptions)
953 {
954 http_request_object_set_options_subr("ssl", 0, 0);
955 }
956 /* }}} */
957
958 /* {{{ proto array HttpRequest::getSslOtpions()
959 Get previously set SSL options. */
960 PHP_METHOD(HttpRequest, getSslOptions)
961 {
962 http_request_object_get_options_subr("ssl");
963 }
964 /* }}} */
965
966 /* {{{ proto bool HttpRequest::addHeaders(array headers)
967 Add request header name/value pairs. */
968 PHP_METHOD(HttpRequest, addHeaders)
969 {
970 http_request_object_set_options_subr("headers", 0, 1);
971 }
972
973 /* {{{ proto bool HttpRequest::setHeaders([array headers])
974 Set request header name/value pairs. */
975 PHP_METHOD(HttpRequest, setHeaders)
976 {
977 http_request_object_set_options_subr("headers", 1, 1);
978 }
979 /* }}} */
980
981 /* {{{ proto array HttpRequest::getHeaders()
982 Get previously set request headers. */
983 PHP_METHOD(HttpRequest, getHeaders)
984 {
985 http_request_object_get_options_subr("headers");
986 }
987 /* }}} */
988
989 /* {{{ proto bool HttpRequest::setCookies([array cookies])
990 Set cookies. */
991 PHP_METHOD(HttpRequest, setCookies)
992 {
993 http_request_object_set_options_subr("cookies", 1, 0);
994 }
995 /* }}} */
996
997 /* {{{ proto bool HttpRequest::addCookies(array cookies)
998 Add cookies. */
999 PHP_METHOD(HttpRequest, addCookies)
1000 {
1001 http_request_object_set_options_subr("cookies", 0, 0);
1002 }
1003 /* }}} */
1004
1005 /* {{{ proto array HttpRequest::getCookies()
1006 Get previously set cookies. */
1007 PHP_METHOD(HttpRequest, getCookies)
1008 {
1009 http_request_object_get_options_subr("cookies");
1010 }
1011 /* }}} */
1012
1013 /* {{{ proto bool HttpRequest::enableCookies()
1014 Enable automatic sending of received cookies. Note that customly set cookies will be sent anyway. */
1015 PHP_METHOD(HttpRequest, enableCookies)
1016 {
1017 NO_ARGS {
1018 getObject(http_request_object, obj);
1019 RETURN_SUCCESS(http_request_enable_cookies(obj->request));
1020 }
1021
1022 }
1023 /* }}} */
1024
1025 /* {{{ proto bool HttpRequest::resetCookies([bool session_only = FALSE])
1026 Reset all automatically received/sent cookies. Note that customly set cookies are not affected. */
1027 PHP_METHOD(HttpRequest, resetCookies)
1028 {
1029 zend_bool session_only = 0;
1030 getObject(http_request_object, obj);
1031
1032 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &session_only)) {
1033 RETURN_FALSE;
1034 }
1035 RETURN_SUCCESS(http_request_reset_cookies(obj->request, session_only));
1036 }
1037 /* }}} */
1038
1039 /* {{{ proto bool HttpRequest::setUrl(string url)
1040 Set the request URL. */
1041 PHP_METHOD(HttpRequest, setUrl)
1042 {
1043 char *URL = NULL;
1044 int URL_len;
1045
1046 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
1047 RETURN_FALSE;
1048 }
1049
1050 UPD_STRL(url, URL, URL_len);
1051 RETURN_TRUE;
1052 }
1053 /* }}} */
1054
1055 /* {{{ proto string HttpRequest::getUrl()
1056 Get the previously set request URL. */
1057 PHP_METHOD(HttpRequest, getUrl)
1058 {
1059 NO_ARGS;
1060
1061 if (return_value_used) {
1062 RETURN_PROP(url);
1063 }
1064 }
1065 /* }}} */
1066
1067 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1068 Set the request method. */
1069 PHP_METHOD(HttpRequest, setMethod)
1070 {
1071 long meth;
1072
1073 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
1074 RETURN_FALSE;
1075 }
1076
1077 UPD_PROP(long, method, meth);
1078 RETURN_TRUE;
1079 }
1080 /* }}} */
1081
1082 /* {{{ proto int HttpRequest::getMethod()
1083 Get the previously set request method. */
1084 PHP_METHOD(HttpRequest, getMethod)
1085 {
1086 NO_ARGS;
1087
1088 if (return_value_used) {
1089 RETURN_PROP(method);
1090 }
1091 }
1092 /* }}} */
1093
1094 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1095 Set the content type the post request should have. */
1096 PHP_METHOD(HttpRequest, setContentType)
1097 {
1098 char *ctype;
1099 int ct_len;
1100
1101 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1102 RETURN_FALSE;
1103 }
1104
1105 if (ct_len) {
1106 HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
1107 }
1108 UPD_STRL(contentType, ctype, ct_len);
1109 RETURN_TRUE;
1110 }
1111 /* }}} */
1112
1113 /* {{{ proto string HttpRequest::getContentType()
1114 Get the previously content type. */
1115 PHP_METHOD(HttpRequest, getContentType)
1116 {
1117 NO_ARGS;
1118
1119 if (return_value_used) {
1120 RETURN_PROP(contentType);
1121 }
1122 }
1123 /* }}} */
1124
1125 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1126 Set the URL query parameters to use, overwriting previously set query parameters. */
1127 PHP_METHOD(HttpRequest, setQueryData)
1128 {
1129 zval *qdata = NULL;
1130
1131 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
1132 RETURN_FALSE;
1133 }
1134
1135 if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
1136 UPD_STRL(queryData, "", 0);
1137 } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1138 char *query_data = NULL;
1139
1140 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1141 RETURN_FALSE;
1142 }
1143
1144 UPD_PROP(string, queryData, query_data);
1145 efree(query_data);
1146 } else {
1147 zval *orig = qdata;
1148
1149 convert_to_string_ex(&qdata);
1150 UPD_STRL(queryData, Z_STRVAL_P(qdata), Z_STRLEN_P(qdata));
1151 if (orig != qdata) {
1152 zval_ptr_dtor(&qdata);
1153 }
1154 }
1155 RETURN_TRUE;
1156 }
1157 /* }}} */
1158
1159 /* {{{ proto string HttpRequest::getQueryData()
1160 Get the current query data in form of an urlencoded query string. */
1161 PHP_METHOD(HttpRequest, getQueryData)
1162 {
1163 NO_ARGS;
1164
1165 if (return_value_used) {
1166 RETURN_PROP(queryData);
1167 }
1168 }
1169 /* }}} */
1170
1171 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1172 Add parameters to the query parameter list, leaving previously set unchanged. */
1173 PHP_METHOD(HttpRequest, addQueryData)
1174 {
1175 zval *qdata, *old_qdata;
1176 char *query_data = NULL;
1177 size_t query_data_len = 0;
1178
1179 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &qdata)) {
1180 RETURN_FALSE;
1181 }
1182
1183 old_qdata = GET_PROP(queryData);
1184
1185 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)) {
1186 RETURN_FALSE;
1187 }
1188
1189 UPD_STRL(queryData, query_data, query_data_len);
1190 efree(query_data);
1191
1192 RETURN_TRUE;
1193 }
1194 /* }}} */
1195
1196 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1197 Adds POST data entries, leaving previously set unchanged, unless a post entry with the same name already exists. */
1198 PHP_METHOD(HttpRequest, addPostFields)
1199 {
1200 zval *post_data, *old_post, *new_post;
1201
1202 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &post_data)) {
1203 RETURN_FALSE;
1204 }
1205
1206 if (zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1207 MAKE_STD_ZVAL(new_post);
1208 array_init(new_post);
1209 old_post = GET_PROP(postFields);
1210 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1211 array_copy(Z_ARRVAL_P(old_post), Z_ARRVAL_P(new_post));
1212 }
1213 array_join(Z_ARRVAL_P(post_data), Z_ARRVAL_P(new_post), 0, 0);
1214 SET_PROP(postFields, new_post);
1215 zval_ptr_dtor(&new_post);
1216 }
1217
1218 RETURN_TRUE;
1219 }
1220 /* }}} */
1221
1222 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1223 Set the POST data entries, overwriting previously set POST data. */
1224 PHP_METHOD(HttpRequest, setPostFields)
1225 {
1226 zval *post, *post_data = NULL;
1227
1228 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &post_data)) {
1229 RETURN_FALSE;
1230 }
1231
1232 MAKE_STD_ZVAL(post);
1233 array_init(post);
1234 if (post_data && zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1235 array_copy(Z_ARRVAL_P(post_data), Z_ARRVAL_P(post));
1236 }
1237 SET_PROP(postFields, post);
1238 zval_ptr_dtor(&post);
1239
1240 RETURN_TRUE;
1241 }
1242 /* }}}*/
1243
1244 /* {{{ proto array HttpRequest::getPostFields()
1245 Get previously set POST data. */
1246 PHP_METHOD(HttpRequest, getPostFields)
1247 {
1248 NO_ARGS;
1249
1250 if (return_value_used) {
1251 RETURN_PROP(postFields);
1252 }
1253 }
1254 /* }}} */
1255
1256 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1257 Set raw post data to send, overwriting previously set raw post data. Don't forget to specify a content type. */
1258 PHP_METHOD(HttpRequest, setRawPostData)
1259 {
1260 char *raw_data = NULL;
1261 int data_len = 0;
1262
1263 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &raw_data, &data_len)) {
1264 RETURN_FALSE;
1265 }
1266
1267 if (!raw_data) {
1268 raw_data = "";
1269 }
1270
1271 UPD_STRL(rawPostData, raw_data, data_len);
1272 RETURN_TRUE;
1273 }
1274 /* }}} */
1275
1276 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1277 Add raw post data, leaving previously set raw post data unchanged. */
1278 PHP_METHOD(HttpRequest, addRawPostData)
1279 {
1280 char *raw_data;
1281 int data_len;
1282
1283 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw_data, &data_len)) {
1284 RETURN_FALSE;
1285 }
1286
1287 if (data_len) {
1288 zval *data = GET_PROP(rawPostData);
1289
1290 if (Z_STRLEN_P(data)) {
1291 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1292 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1293 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len);
1294 } else {
1295 UPD_STRL(putData, raw_data, data_len);
1296 }
1297 }
1298
1299 RETURN_TRUE;
1300 }
1301 /* }}} */
1302
1303 /* {{{ proto string HttpRequest::getRawPostData()
1304 Get previously set raw post data. */
1305 PHP_METHOD(HttpRequest, getRawPostData)
1306 {
1307 NO_ARGS;
1308
1309 if (return_value_used) {
1310 RETURN_PROP(rawPostData);
1311 }
1312 }
1313 /* }}} */
1314
1315 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1316 Add a file to the POST request, leaving previously set files unchanged. */
1317 PHP_METHOD(HttpRequest, addPostFile)
1318 {
1319 zval *entry, *old_post, *new_post;
1320 char *name, *file, *type = NULL;
1321 int name_len, file_len, type_len = 0;
1322
1323 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1324 RETURN_FALSE;
1325 }
1326
1327 if (type_len) {
1328 HTTP_CHECK_CONTENT_TYPE(type, RETURN_FALSE);
1329 } else {
1330 type = "application/x-octetstream";
1331 type_len = sizeof("application/x-octetstream") - 1;
1332 }
1333
1334 MAKE_STD_ZVAL(entry);
1335 array_init(entry);
1336
1337 add_assoc_stringl(entry, "name", name, name_len, 1);
1338 add_assoc_stringl(entry, "type", type, type_len, 1);
1339 add_assoc_stringl(entry, "file", file, file_len, 1);
1340
1341 MAKE_STD_ZVAL(new_post);
1342 array_init(new_post);
1343 old_post = GET_PROP(postFiles);
1344 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1345 array_copy(Z_ARRVAL_P(old_post), Z_ARRVAL_P(new_post));
1346 }
1347 add_next_index_zval(new_post, entry);
1348 SET_PROP(postFiles, new_post);
1349 zval_ptr_dtor(&new_post);
1350
1351 RETURN_TRUE;
1352 }
1353 /* }}} */
1354
1355 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1356 Set files to post, overwriting previously set post files. */
1357 PHP_METHOD(HttpRequest, setPostFiles)
1358 {
1359 zval *files = NULL, *post;
1360
1361 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!/", &files)) {
1362 RETURN_FALSE;
1363 }
1364
1365 MAKE_STD_ZVAL(post);
1366 array_init(post);
1367 if (files && (Z_TYPE_P(files) == IS_ARRAY)) {
1368 array_copy(Z_ARRVAL_P(files), Z_ARRVAL_P(post));
1369 }
1370 SET_PROP(postFiles, post);
1371 zval_ptr_dtor(&post);
1372
1373 RETURN_TRUE;
1374 }
1375 /* }}} */
1376
1377 /* {{{ proto array HttpRequest::getPostFiles()
1378 Get all previously added POST files. */
1379 PHP_METHOD(HttpRequest, getPostFiles)
1380 {
1381 NO_ARGS;
1382
1383 if (return_value_used) {
1384 RETURN_PROP(postFiles);
1385 }
1386 }
1387 /* }}} */
1388
1389 /* {{{ proto bool HttpRequest::setPutFile([string file])
1390 Set file to put. Affects only PUT requests. */
1391 PHP_METHOD(HttpRequest, setPutFile)
1392 {
1393 char *file = "";
1394 int file_len = 0;
1395
1396 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &file, &file_len)) {
1397 RETURN_FALSE;
1398 }
1399
1400 UPD_STRL(putFile, file, file_len);
1401 RETURN_TRUE;
1402 }
1403 /* }}} */
1404
1405 /* {{{ proto string HttpRequest::getPutFile()
1406 Get previously set put file. */
1407 PHP_METHOD(HttpRequest, getPutFile)
1408 {
1409 NO_ARGS;
1410
1411 if (return_value_used) {
1412 RETURN_PROP(putFile);
1413 }
1414 }
1415 /* }}} */
1416
1417 /* {{{ proto bool HttpRequest::setPutData([string put_data])
1418 Set PUT data to send, overwriting previously set PUT data. */
1419 PHP_METHOD(HttpRequest, setPutData)
1420 {
1421 char *put_data = NULL;
1422 int data_len = 0;
1423
1424 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &put_data, &data_len)) {
1425 RETURN_FALSE;
1426 }
1427
1428 if (!put_data) {
1429 put_data = "";
1430 }
1431
1432 UPD_STRL(putData, put_data, data_len);
1433 RETURN_TRUE;
1434 }
1435 /* }}} */
1436
1437 /* {{{ proto bool HttpRequest::addPutData(string put_data)
1438 Add PUT data, leaving previously set PUT data unchanged. */
1439 PHP_METHOD(HttpRequest, addPutData)
1440 {
1441 char *put_data;
1442 int data_len;
1443
1444 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &put_data, &data_len)) {
1445 RETURN_FALSE;
1446 }
1447
1448 if (data_len) {
1449 zval *data = GET_PROP(putData);
1450
1451 if (Z_STRLEN_P(data)) {
1452 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1453 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1454 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, put_data, data_len);
1455 } else {
1456 UPD_STRL(putData, put_data, data_len);
1457 }
1458 }
1459
1460 RETURN_TRUE;
1461 }
1462 /* }}} */
1463
1464 /* {{{ proto string HttpRequest::getPutData()
1465 Get previously set PUT data. */
1466 PHP_METHOD(HttpRequest, getPutData)
1467 {
1468 NO_ARGS;
1469
1470 if (return_value_used) {
1471 RETURN_PROP(putData);
1472 }
1473 }
1474 /* }}} */
1475
1476 /* {{{ proto array HttpRequest::getResponseData()
1477 Get all response data after the request has been sent. */
1478 PHP_METHOD(HttpRequest, getResponseData)
1479 {
1480 NO_ARGS;
1481
1482 if (return_value_used) {
1483 char *body;
1484 size_t body_len;
1485 zval *headers, *message = GET_PROP(responseMessage);
1486
1487 if (Z_TYPE_P(message) == IS_OBJECT) {
1488 getObjectEx(http_message_object, msg, message);
1489
1490 array_init(return_value);
1491
1492 MAKE_STD_ZVAL(headers);
1493 array_init(headers);
1494 zend_hash_copy(Z_ARRVAL_P(headers), &msg->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1495 add_assoc_zval(return_value, "headers", headers);
1496
1497 phpstr_data(PHPSTR(msg->message), &body, &body_len);
1498 add_assoc_stringl(return_value, "body", body, body_len, 0);
1499 }
1500 }
1501 }
1502 /* }}} */
1503
1504 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1505 Get response header(s) after the request has been sent. */
1506 PHP_METHOD(HttpRequest, getResponseHeader)
1507 {
1508 if (return_value_used) {
1509 zval *header;
1510 char *header_name = NULL;
1511 int header_len = 0;
1512
1513 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1514 zval *message = GET_PROP(responseMessage);
1515
1516 if (Z_TYPE_P(message) == IS_OBJECT) {
1517 getObjectEx(http_message_object, msg, message);
1518
1519 if (header_len) {
1520 if ((header = http_message_header_ex(msg->message, pretty_key(header_name, header_len, 1, 1), header_len + 1, 0))) {
1521 RETURN_ZVAL(header, 1, 1);
1522 }
1523 } else {
1524 array_init(return_value);
1525 zend_hash_copy(Z_ARRVAL_P(return_value), &msg->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1526 return;
1527 }
1528 }
1529 }
1530 RETURN_FALSE;
1531 }
1532 }
1533 /* }}} */
1534
1535 /* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]])
1536 Get response cookie(s) after the request has been sent. */
1537 PHP_METHOD(HttpRequest, getResponseCookies)
1538 {
1539 if (return_value_used) {
1540 long flags = 0;
1541 zval *allowed_extras_array = NULL;
1542
1543 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la!", &flags, &allowed_extras_array)) {
1544 int i = 0;
1545 HashKey key = initHashKey(0);
1546 char **allowed_extras = NULL;
1547 zval **header = NULL, **entry = NULL, *message = GET_PROP(responseMessage);
1548 HashPosition pos, pos1, pos2;
1549
1550 if (Z_TYPE_P(message) == IS_OBJECT) {
1551 getObjectEx(http_message_object, msg, message);
1552
1553 array_init(return_value);
1554
1555 if (allowed_extras_array) {
1556 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
1557 FOREACH_VAL(pos, allowed_extras_array, entry) {
1558 ZVAL_ADDREF(*entry);
1559 convert_to_string_ex(entry);
1560 allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
1561 zval_ptr_dtor(entry);
1562 }
1563 }
1564
1565 FOREACH_HASH_KEYVAL(pos1, &msg->message->hdrs, key, header) {
1566 if (key.type == HASH_KEY_IS_STRING && !strcasecmp(key.str, "Set-Cookie")) {
1567 http_cookie_list list;
1568
1569 if (Z_TYPE_PP(header) == IS_ARRAY) {
1570 zval **single_header;
1571
1572 FOREACH_VAL(pos2, *header, single_header) {
1573 ZVAL_ADDREF(*single_header);
1574 convert_to_string_ex(single_header);
1575 if (http_parse_cookie_ex(&list, Z_STRVAL_PP(single_header), flags, allowed_extras)) {
1576 zval *cookie;
1577
1578 MAKE_STD_ZVAL(cookie);
1579 object_init(cookie);
1580 http_cookie_list_tostruct(&list, cookie);
1581 add_next_index_zval(return_value, cookie);
1582 http_cookie_list_dtor(&list);
1583 }
1584 zval_ptr_dtor(single_header);
1585 }
1586 } else {
1587 ZVAL_ADDREF(*header);
1588 convert_to_string_ex(header);
1589 if (http_parse_cookie_ex(&list, Z_STRVAL_PP(header), flags, allowed_extras)) {
1590 zval *cookie;
1591
1592 MAKE_STD_ZVAL(cookie);
1593 object_init(cookie);
1594 http_cookie_list_tostruct(&list, cookie);
1595 add_next_index_zval(return_value, cookie);
1596 http_cookie_list_dtor(&list);
1597 }
1598 zval_ptr_dtor(header);
1599 }
1600 }
1601 }
1602
1603 if (allowed_extras) {
1604 for (i = 0; allowed_extras[i]; ++i) {
1605 efree(allowed_extras[i]);
1606 }
1607 efree(allowed_extras);
1608 }
1609
1610 return;
1611 }
1612 }
1613 RETURN_FALSE;
1614 }
1615 }
1616 /* }}} */
1617
1618 /* {{{ proto string HttpRequest::getResponseBody()
1619 Get the response body after the request has been sent. */
1620 PHP_METHOD(HttpRequest, getResponseBody)
1621 {
1622 NO_ARGS;
1623
1624 if (return_value_used) {
1625 zval *message = GET_PROP(responseMessage);
1626
1627 if (Z_TYPE_P(message) == IS_OBJECT) {
1628 getObjectEx(http_message_object, msg, message);
1629 RETURN_PHPSTR_DUP(&msg->message->body);
1630 } else {
1631 RETURN_FALSE;
1632 }
1633 }
1634 }
1635 /* }}} */
1636
1637 /* {{{ proto int HttpRequest::getResponseCode()
1638 Get the response code after the request has been sent. */
1639 PHP_METHOD(HttpRequest, getResponseCode)
1640 {
1641 NO_ARGS;
1642
1643 if (return_value_used) {
1644 RETURN_PROP(responseCode);
1645 }
1646 }
1647 /* }}} */
1648
1649 /* {{{ proto string HttpRequest::getResponseStatus()
1650 Get the response status (i.e. the string after the response code) after the message has been sent. */
1651 PHP_METHOD(HttpRequest, getResponseStatus)
1652 {
1653 NO_ARGS;
1654
1655 if (return_value_used) {
1656 RETURN_PROP(responseStatus);
1657 }
1658 }
1659 /* }}} */
1660
1661 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1662 Get response info after the request has been sent. */
1663 PHP_METHOD(HttpRequest, getResponseInfo)
1664 {
1665 if (return_value_used) {
1666 zval *info, **infop;
1667 char *info_name = NULL;
1668 int info_len = 0;
1669
1670 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1671 RETURN_FALSE;
1672 }
1673
1674 info = GET_PROP(responseInfo);
1675
1676 if (Z_TYPE_P(info) != IS_ARRAY) {
1677 RETURN_FALSE;
1678 }
1679
1680 if (info_len && info_name) {
1681 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) {
1682 RETURN_ZVAL(*infop, 1, 0);
1683 } else {
1684 http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
1685 RETURN_FALSE;
1686 }
1687 } else {
1688 RETURN_ZVAL(info, 1, 0);
1689 }
1690 }
1691 }
1692 /* }}}*/
1693
1694 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1695 Get the full response as HttpMessage object after the request has been sent. */
1696 PHP_METHOD(HttpRequest, getResponseMessage)
1697 {
1698 NO_ARGS {
1699 zval *message;
1700
1701 SET_EH_THROW_HTTP();
1702 message = GET_PROP(responseMessage);
1703 if (Z_TYPE_P(message) == IS_OBJECT) {
1704 RETVAL_OBJECT(message, 1);
1705 } else {
1706 http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpRequest does not contain a response message");
1707 }
1708 SET_EH_NORMAL();
1709 }
1710 }
1711 /* }}} */
1712
1713 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1714 Get sent HTTP message. */
1715 PHP_METHOD(HttpRequest, getRequestMessage)
1716 {
1717 NO_ARGS;
1718
1719 if (return_value_used) {
1720 http_message *msg;
1721 getObject(http_request_object, obj);
1722
1723 SET_EH_THROW_HTTP();
1724 if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
1725 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
1726 }
1727 SET_EH_NORMAL();
1728 }
1729 }
1730 /* }}} */
1731
1732 /* {{{ proto string HttpRequest::getRawRequestMessage()
1733 Get sent HTTP message. */
1734 PHP_METHOD(HttpRequest, getRawRequestMessage)
1735 {
1736 NO_ARGS;
1737
1738 if (return_value_used) {
1739 getObject(http_request_object, obj);
1740
1741 RETURN_PHPSTR_DUP(&obj->request->conv.request);
1742 }
1743 }
1744 /* }}} */
1745
1746 /* {{{ proto string HttpRequest::getRawResponseMessage()
1747 Get the entire HTTP response. */
1748 PHP_METHOD(HttpRequest, getRawResponseMessage)
1749 {
1750 NO_ARGS;
1751
1752 if (return_value_used) {
1753 getObject(http_request_object, obj);
1754
1755 RETURN_PHPSTR_DUP(&obj->request->conv.response);
1756 }
1757 }
1758 /* }}} */
1759
1760 /* {{{ proto HttpMessage HttpRequest::getHistory()
1761 Get all sent requests and received responses as an HttpMessage object. */
1762 PHP_METHOD(HttpRequest, getHistory)
1763 {
1764 NO_ARGS;
1765
1766 if (return_value_used) {
1767 zval *hist;
1768
1769 SET_EH_THROW_HTTP();
1770 hist = GET_PROP(history);
1771 if (Z_TYPE_P(hist) == IS_OBJECT) {
1772 RETVAL_OBJECT(hist, 1);
1773 } else {
1774 http_error(HE_WARNING, HTTP_E_RUNTIME, "The history is empty");
1775 }
1776 SET_EH_NORMAL();
1777 }
1778 }
1779 /* }}} */
1780
1781 /* {{{ proto void HttpRequest::clearHistory()
1782 Clear the history. */
1783 PHP_METHOD(HttpRequest, clearHistory)
1784 {
1785 NO_ARGS {
1786 zval *hist;
1787
1788 MAKE_STD_ZVAL(hist);
1789 ZVAL_NULL(hist);
1790 SET_PROP(history, hist);
1791 zval_ptr_dtor(&hist);
1792 }
1793 }
1794 /* }}} */
1795
1796 /* {{{ proto HttpMessage HttpRequest::send()
1797 Send the HTTP request. */
1798 PHP_METHOD(HttpRequest, send)
1799 {
1800 getObject(http_request_object, obj);
1801
1802 NO_ARGS;
1803
1804 SET_EH_THROW_HTTP();
1805
1806 RETVAL_FALSE;
1807
1808 if (obj->pool) {
1809 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1810 } else if (SUCCESS == http_request_object_requesthandler(obj, getThis())) {
1811 http_request_exec(obj->request);
1812 if (SUCCESS == http_request_object_responsehandler(obj, getThis())) {
1813 RETVAL_OBJECT(GET_PROP(responseMessage), 1);
1814 }
1815 }
1816
1817 SET_EH_NORMAL();
1818 }
1819 /* }}} */
1820
1821 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1822
1823 /*
1824 * Local variables:
1825 * tab-width: 4
1826 * c-basic-offset: 4
1827 * End:
1828 * vim600: noet sw=4 ts=4 fdm=marker
1829 * vim<600: noet sw=4 ts=4
1830 */
1831