- release 1.4.0RC1
[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 #if defined(HAVE_CURL_GETFORMDATA) || defined(HAVE_CURL_FORMGET)
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 #if defined(HAVE_CURL_GETFORMDATA) || defined(HAVE_CURL_FORMGET)
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 http_request_set_progress_callback(obj->request, NULL);
725
726 if (!EG(exception) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) {
727 zval *param;
728
729 MAKE_STD_ZVAL(param);
730 ZVAL_BOOL(param, ret == SUCCESS);
731 with_error_handling(EH_NORMAL, NULL) {
732 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param);
733 } end_error_handling();
734 zval_ptr_dtor(&param);
735 }
736
737 return ret;
738 }
739
740 static int apply_pretty_key(void *pDest, int num_args, va_list args, zend_hash_key *hash_key)
741 {
742 if (hash_key->nKeyLength > 1) {
743 hash_key->h = zend_get_hash_value(pretty_key(hash_key->arKey, hash_key->nKeyLength - 1, 1, 0), hash_key->nKeyLength);
744 }
745 return ZEND_HASH_APPLY_KEEP;
746 }
747
748 #define http_request_object_set_options_subr(key, ow, pk) \
749 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
750 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys)
751 {
752 zval *old_opts, *new_opts, *opts = NULL, **entry = NULL;
753
754 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
755 RETURN_FALSE;
756 }
757
758 MAKE_STD_ZVAL(new_opts);
759 array_init(new_opts);
760 old_opts = GET_PROP(options);
761 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
762 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
763 }
764
765 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
766 if (overwrite) {
767 zend_hash_clean(Z_ARRVAL_PP(entry));
768 }
769 if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
770 if (overwrite) {
771 array_copy(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry));
772 } else {
773 array_join(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry), 0, prettify_keys ? ARRAY_JOIN_PRETTIFY : 0);
774 }
775 }
776 } else if (opts) {
777 if (prettify_keys) {
778 zend_hash_apply_with_arguments(Z_ARRVAL_P(opts), apply_pretty_key, 0);
779 }
780 ZVAL_ADDREF(opts);
781 add_assoc_zval_ex(new_opts, key, len, opts);
782 }
783 SET_PROP(options, new_opts);
784 zval_ptr_dtor(&new_opts);
785
786 RETURN_TRUE;
787 }
788
789 #define http_request_object_get_options_subr(key) \
790 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
791 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
792 {
793 NO_ARGS;
794
795 if (return_value_used) {
796 zval *opts, **options;
797
798 opts = GET_PROP(options);
799 array_init(return_value);
800
801 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
802 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
803 convert_to_array(*options);
804 array_copy(Z_ARRVAL_PP(options), Z_ARRVAL_P(return_value));
805 }
806 }
807 }
808
809
810 /* ### USERLAND ### */
811
812 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
813 Create a new HttpRequest object instance. */
814 PHP_METHOD(HttpRequest, __construct)
815 {
816 char *URL = NULL;
817 int URL_len;
818 long meth = -1;
819 zval *options = NULL;
820
821 SET_EH_THROW_HTTP();
822 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla!", &URL, &URL_len, &meth, &options)) {
823 if (URL) {
824 UPD_STRL(url, URL, URL_len);
825 }
826 if (meth > -1) {
827 UPD_PROP(long, method, meth);
828 }
829 if (options) {
830 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
831 }
832 }
833 SET_EH_NORMAL();
834 }
835 /* }}} */
836
837 /* {{{ proto HttpRequest HttpRequest::factory([string url[, int request_method HTTP_METH_GET[, array options[, string class_name = "HttpRequest"]]]])
838 Create a new HttpRequest object instance. */
839 PHP_METHOD(HttpRequest, factory)
840 {
841 char *cn = NULL, *URL = NULL;
842 int cl = 0, URL_len = 0;
843 long meth = -1;
844 zval *options = NULL;
845 zend_object_value ov;
846
847 SET_EH_THROW_HTTP();
848 if ( SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla!s", &URL, &URL_len, &meth, &options, &cn, &cl) &&
849 SUCCESS == http_object_new(&ov, cn, cl, _http_request_object_new_ex, http_request_object_ce, NULL, NULL)) {
850 RETVAL_OBJVAL(ov, 0);
851 getThis() = return_value;
852 if (URL) {
853 UPD_STRL(url, URL, URL_len);
854 }
855 if (meth > -1) {
856 UPD_PROP(long, method, meth);
857 }
858 if (options) {
859 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
860 }
861 }
862 SET_EH_NORMAL();
863 }
864 /* }}} */
865
866 /* {{{ proto bool HttpRequest::setOptions([array options])
867 Set the request options to use. See http_get() for a full list of available options. */
868 PHP_METHOD(HttpRequest, setOptions)
869 {
870 HashKey key = initHashKey(0);
871 HashPosition pos;
872 zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt;
873
874 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
875 RETURN_FALSE;
876 }
877
878 MAKE_STD_ZVAL(new_opts);
879 array_init(new_opts);
880
881 if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
882 SET_PROP(options, new_opts);
883 zval_ptr_dtor(&new_opts);
884 RETURN_TRUE;
885 }
886
887 MAKE_STD_ZVAL(add_opts);
888 array_init(add_opts);
889 /* some options need extra attention -- thus cannot use array_merge() directly */
890 FOREACH_KEYVAL(pos, opts, key, opt) {
891 if (key.type == HASH_KEY_IS_STRING) {
892 if (!strcmp(key.str, "headers")) {
893 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, *opt);
894 } else if (!strcmp(key.str, "cookies")) {
895 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt);
896 } else if (!strcmp(key.str, "ssl")) {
897 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt);
898 } else if ((!strcasecmp(key.str, "url")) || (!strcasecmp(key.str, "uri"))) {
899 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt);
900 } else if (!strcmp(key.str, "method")) {
901 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt);
902 #if HTTP_CURL_VERSION(7,14,1)
903 } else if (!strcmp(key.str, "resetcookies")) {
904 getObject(http_request_object, obj);
905 http_request_reset_cookies(obj->request, 0);
906 #endif
907 } else if (!strcmp(key.str, "enablecookies")) {
908 getObject(http_request_object, obj);
909 http_request_enable_cookies(obj->request);
910 } else if (!strcasecmp(key.str, "recordHistory")) {
911 UPD_PROP(bool, recordHistory, 1);
912 } else {
913 ZVAL_ADDREF(*opt);
914 add_assoc_zval_ex(add_opts, key.str, key.len, *opt);
915 }
916 }
917 }
918
919 old_opts = GET_PROP(options);
920 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
921 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
922 }
923 array_join(Z_ARRVAL_P(add_opts), Z_ARRVAL_P(new_opts), 0, 0);
924 SET_PROP(options, new_opts);
925 zval_ptr_dtor(&new_opts);
926 zval_ptr_dtor(&add_opts);
927
928 RETURN_TRUE;
929 }
930 /* }}} */
931
932 /* {{{ proto array HttpRequest::getOptions()
933 Get currently set options. */
934 PHP_METHOD(HttpRequest, getOptions)
935 {
936 NO_ARGS;
937
938 if (return_value_used) {
939 RETURN_PROP(options);
940 }
941 }
942 /* }}} */
943
944 /* {{{ proto bool HttpRequest::setSslOptions([array options])
945 Set SSL options. */
946 PHP_METHOD(HttpRequest, setSslOptions)
947 {
948 http_request_object_set_options_subr("ssl", 1, 0);
949 }
950 /* }}} */
951
952 /* {{{ proto bool HttpRequest::addSslOptions(array options)
953 Set additional SSL options. */
954 PHP_METHOD(HttpRequest, addSslOptions)
955 {
956 http_request_object_set_options_subr("ssl", 0, 0);
957 }
958 /* }}} */
959
960 /* {{{ proto array HttpRequest::getSslOtpions()
961 Get previously set SSL options. */
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 Add request header name/value pairs. */
970 PHP_METHOD(HttpRequest, addHeaders)
971 {
972 http_request_object_set_options_subr("headers", 0, 1);
973 }
974
975 /* {{{ proto bool HttpRequest::setHeaders([array headers])
976 Set request header name/value pairs. */
977 PHP_METHOD(HttpRequest, setHeaders)
978 {
979 http_request_object_set_options_subr("headers", 1, 1);
980 }
981 /* }}} */
982
983 /* {{{ proto array HttpRequest::getHeaders()
984 Get previously set request headers. */
985 PHP_METHOD(HttpRequest, getHeaders)
986 {
987 http_request_object_get_options_subr("headers");
988 }
989 /* }}} */
990
991 /* {{{ proto bool HttpRequest::setCookies([array cookies])
992 Set cookies. */
993 PHP_METHOD(HttpRequest, setCookies)
994 {
995 http_request_object_set_options_subr("cookies", 1, 0);
996 }
997 /* }}} */
998
999 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1000 Add cookies. */
1001 PHP_METHOD(HttpRequest, addCookies)
1002 {
1003 http_request_object_set_options_subr("cookies", 0, 0);
1004 }
1005 /* }}} */
1006
1007 /* {{{ proto array HttpRequest::getCookies()
1008 Get previously set cookies. */
1009 PHP_METHOD(HttpRequest, getCookies)
1010 {
1011 http_request_object_get_options_subr("cookies");
1012 }
1013 /* }}} */
1014
1015 /* {{{ proto bool HttpRequest::enableCookies()
1016 Enable automatic sending of received cookies. Note that customly set cookies will be sent anyway. */
1017 PHP_METHOD(HttpRequest, enableCookies)
1018 {
1019 NO_ARGS {
1020 getObject(http_request_object, obj);
1021 RETURN_SUCCESS(http_request_enable_cookies(obj->request));
1022 }
1023
1024 }
1025 /* }}} */
1026
1027 /* {{{ proto bool HttpRequest::resetCookies([bool session_only = FALSE])
1028 Reset all automatically received/sent cookies. Note that customly set cookies are not affected. */
1029 PHP_METHOD(HttpRequest, resetCookies)
1030 {
1031 zend_bool session_only = 0;
1032 getObject(http_request_object, obj);
1033
1034 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &session_only)) {
1035 RETURN_FALSE;
1036 }
1037 RETURN_SUCCESS(http_request_reset_cookies(obj->request, session_only));
1038 }
1039 /* }}} */
1040
1041 /* {{{ proto bool HttpRequest::setUrl(string url)
1042 Set the request URL. */
1043 PHP_METHOD(HttpRequest, setUrl)
1044 {
1045 char *URL = NULL;
1046 int URL_len;
1047
1048 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
1049 RETURN_FALSE;
1050 }
1051
1052 UPD_STRL(url, URL, URL_len);
1053 RETURN_TRUE;
1054 }
1055 /* }}} */
1056
1057 /* {{{ proto string HttpRequest::getUrl()
1058 Get the previously set request URL. */
1059 PHP_METHOD(HttpRequest, getUrl)
1060 {
1061 NO_ARGS;
1062
1063 if (return_value_used) {
1064 RETURN_PROP(url);
1065 }
1066 }
1067 /* }}} */
1068
1069 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1070 Set the request method. */
1071 PHP_METHOD(HttpRequest, setMethod)
1072 {
1073 long meth;
1074
1075 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
1076 RETURN_FALSE;
1077 }
1078
1079 UPD_PROP(long, method, meth);
1080 RETURN_TRUE;
1081 }
1082 /* }}} */
1083
1084 /* {{{ proto int HttpRequest::getMethod()
1085 Get the previously set request method. */
1086 PHP_METHOD(HttpRequest, getMethod)
1087 {
1088 NO_ARGS;
1089
1090 if (return_value_used) {
1091 RETURN_PROP(method);
1092 }
1093 }
1094 /* }}} */
1095
1096 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1097 Set the content type the post request should have. */
1098 PHP_METHOD(HttpRequest, setContentType)
1099 {
1100 char *ctype;
1101 int ct_len;
1102
1103 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1104 RETURN_FALSE;
1105 }
1106
1107 if (ct_len) {
1108 HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
1109 }
1110 UPD_STRL(contentType, ctype, ct_len);
1111 RETURN_TRUE;
1112 }
1113 /* }}} */
1114
1115 /* {{{ proto string HttpRequest::getContentType()
1116 Get the previously content type. */
1117 PHP_METHOD(HttpRequest, getContentType)
1118 {
1119 NO_ARGS;
1120
1121 if (return_value_used) {
1122 RETURN_PROP(contentType);
1123 }
1124 }
1125 /* }}} */
1126
1127 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1128 Set the URL query parameters to use, overwriting previously set query parameters. */
1129 PHP_METHOD(HttpRequest, setQueryData)
1130 {
1131 zval *qdata = NULL;
1132
1133 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
1134 RETURN_FALSE;
1135 }
1136
1137 if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
1138 UPD_STRL(queryData, "", 0);
1139 } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1140 char *query_data = NULL;
1141
1142 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1143 RETURN_FALSE;
1144 }
1145
1146 UPD_PROP(string, queryData, query_data);
1147 efree(query_data);
1148 } else {
1149 zval *orig = qdata;
1150
1151 convert_to_string_ex(&qdata);
1152 UPD_STRL(queryData, Z_STRVAL_P(qdata), Z_STRLEN_P(qdata));
1153 if (orig != qdata) {
1154 zval_ptr_dtor(&qdata);
1155 }
1156 }
1157 RETURN_TRUE;
1158 }
1159 /* }}} */
1160
1161 /* {{{ proto string HttpRequest::getQueryData()
1162 Get the current query data in form of an urlencoded query string. */
1163 PHP_METHOD(HttpRequest, getQueryData)
1164 {
1165 NO_ARGS;
1166
1167 if (return_value_used) {
1168 RETURN_PROP(queryData);
1169 }
1170 }
1171 /* }}} */
1172
1173 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1174 Add parameters to the query parameter list, leaving previously set unchanged. */
1175 PHP_METHOD(HttpRequest, addQueryData)
1176 {
1177 zval *qdata, *old_qdata;
1178 char *query_data = NULL;
1179 size_t query_data_len = 0;
1180
1181 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &qdata)) {
1182 RETURN_FALSE;
1183 }
1184
1185 old_qdata = GET_PROP(queryData);
1186
1187 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)) {
1188 RETURN_FALSE;
1189 }
1190
1191 UPD_STRL(queryData, query_data, query_data_len);
1192 efree(query_data);
1193
1194 RETURN_TRUE;
1195 }
1196 /* }}} */
1197
1198 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1199 Adds POST data entries, leaving previously set unchanged, unless a post entry with the same name already exists. */
1200 PHP_METHOD(HttpRequest, addPostFields)
1201 {
1202 zval *post_data, *old_post, *new_post;
1203
1204 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &post_data)) {
1205 RETURN_FALSE;
1206 }
1207
1208 if (zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1209 MAKE_STD_ZVAL(new_post);
1210 array_init(new_post);
1211 old_post = GET_PROP(postFields);
1212 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1213 array_copy(Z_ARRVAL_P(old_post), Z_ARRVAL_P(new_post));
1214 }
1215 array_join(Z_ARRVAL_P(post_data), Z_ARRVAL_P(new_post), 0, 0);
1216 SET_PROP(postFields, new_post);
1217 zval_ptr_dtor(&new_post);
1218 }
1219
1220 RETURN_TRUE;
1221 }
1222 /* }}} */
1223
1224 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1225 Set the POST data entries, overwriting previously set POST data. */
1226 PHP_METHOD(HttpRequest, setPostFields)
1227 {
1228 zval *post, *post_data = NULL;
1229
1230 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &post_data)) {
1231 RETURN_FALSE;
1232 }
1233
1234 MAKE_STD_ZVAL(post);
1235 array_init(post);
1236 if (post_data && zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1237 array_copy(Z_ARRVAL_P(post_data), Z_ARRVAL_P(post));
1238 }
1239 SET_PROP(postFields, post);
1240 zval_ptr_dtor(&post);
1241
1242 RETURN_TRUE;
1243 }
1244 /* }}}*/
1245
1246 /* {{{ proto array HttpRequest::getPostFields()
1247 Get previously set POST data. */
1248 PHP_METHOD(HttpRequest, getPostFields)
1249 {
1250 NO_ARGS;
1251
1252 if (return_value_used) {
1253 RETURN_PROP(postFields);
1254 }
1255 }
1256 /* }}} */
1257
1258 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1259 Set raw post data to send, overwriting previously set raw post data. Don't forget to specify a content type. */
1260 PHP_METHOD(HttpRequest, setRawPostData)
1261 {
1262 char *raw_data = NULL;
1263 int data_len = 0;
1264
1265 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &raw_data, &data_len)) {
1266 RETURN_FALSE;
1267 }
1268
1269 if (!raw_data) {
1270 raw_data = "";
1271 }
1272
1273 UPD_STRL(rawPostData, raw_data, data_len);
1274 RETURN_TRUE;
1275 }
1276 /* }}} */
1277
1278 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1279 Add raw post data, leaving previously set raw post data unchanged. */
1280 PHP_METHOD(HttpRequest, addRawPostData)
1281 {
1282 char *raw_data;
1283 int data_len;
1284
1285 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw_data, &data_len)) {
1286 RETURN_FALSE;
1287 }
1288
1289 if (data_len) {
1290 zval *data = GET_PROP(rawPostData);
1291
1292 if (Z_STRLEN_P(data)) {
1293 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1294 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1295 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len);
1296 } else {
1297 UPD_STRL(putData, raw_data, data_len);
1298 }
1299 }
1300
1301 RETURN_TRUE;
1302 }
1303 /* }}} */
1304
1305 /* {{{ proto string HttpRequest::getRawPostData()
1306 Get previously set raw post data. */
1307 PHP_METHOD(HttpRequest, getRawPostData)
1308 {
1309 NO_ARGS;
1310
1311 if (return_value_used) {
1312 RETURN_PROP(rawPostData);
1313 }
1314 }
1315 /* }}} */
1316
1317 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1318 Add a file to the POST request, leaving previously set files unchanged. */
1319 PHP_METHOD(HttpRequest, addPostFile)
1320 {
1321 zval *entry, *old_post, *new_post;
1322 char *name, *file, *type = NULL;
1323 int name_len, file_len, type_len = 0;
1324
1325 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1326 RETURN_FALSE;
1327 }
1328
1329 if (type_len) {
1330 HTTP_CHECK_CONTENT_TYPE(type, RETURN_FALSE);
1331 } else {
1332 type = "application/x-octetstream";
1333 type_len = sizeof("application/x-octetstream") - 1;
1334 }
1335
1336 MAKE_STD_ZVAL(entry);
1337 array_init(entry);
1338
1339 add_assoc_stringl(entry, "name", name, name_len, 1);
1340 add_assoc_stringl(entry, "type", type, type_len, 1);
1341 add_assoc_stringl(entry, "file", file, file_len, 1);
1342
1343 MAKE_STD_ZVAL(new_post);
1344 array_init(new_post);
1345 old_post = GET_PROP(postFiles);
1346 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1347 array_copy(Z_ARRVAL_P(old_post), Z_ARRVAL_P(new_post));
1348 }
1349 add_next_index_zval(new_post, entry);
1350 SET_PROP(postFiles, new_post);
1351 zval_ptr_dtor(&new_post);
1352
1353 RETURN_TRUE;
1354 }
1355 /* }}} */
1356
1357 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1358 Set files to post, overwriting previously set post files. */
1359 PHP_METHOD(HttpRequest, setPostFiles)
1360 {
1361 zval *files = NULL, *post;
1362
1363 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!/", &files)) {
1364 RETURN_FALSE;
1365 }
1366
1367 MAKE_STD_ZVAL(post);
1368 array_init(post);
1369 if (files && (Z_TYPE_P(files) == IS_ARRAY)) {
1370 array_copy(Z_ARRVAL_P(files), Z_ARRVAL_P(post));
1371 }
1372 SET_PROP(postFiles, post);
1373 zval_ptr_dtor(&post);
1374
1375 RETURN_TRUE;
1376 }
1377 /* }}} */
1378
1379 /* {{{ proto array HttpRequest::getPostFiles()
1380 Get all previously added POST files. */
1381 PHP_METHOD(HttpRequest, getPostFiles)
1382 {
1383 NO_ARGS;
1384
1385 if (return_value_used) {
1386 RETURN_PROP(postFiles);
1387 }
1388 }
1389 /* }}} */
1390
1391 /* {{{ proto bool HttpRequest::setPutFile([string file])
1392 Set file to put. Affects only PUT requests. */
1393 PHP_METHOD(HttpRequest, setPutFile)
1394 {
1395 char *file = "";
1396 int file_len = 0;
1397
1398 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &file, &file_len)) {
1399 RETURN_FALSE;
1400 }
1401
1402 UPD_STRL(putFile, file, file_len);
1403 RETURN_TRUE;
1404 }
1405 /* }}} */
1406
1407 /* {{{ proto string HttpRequest::getPutFile()
1408 Get previously set put file. */
1409 PHP_METHOD(HttpRequest, getPutFile)
1410 {
1411 NO_ARGS;
1412
1413 if (return_value_used) {
1414 RETURN_PROP(putFile);
1415 }
1416 }
1417 /* }}} */
1418
1419 /* {{{ proto bool HttpRequest::setPutData([string put_data])
1420 Set PUT data to send, overwriting previously set PUT data. */
1421 PHP_METHOD(HttpRequest, setPutData)
1422 {
1423 char *put_data = NULL;
1424 int data_len = 0;
1425
1426 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &put_data, &data_len)) {
1427 RETURN_FALSE;
1428 }
1429
1430 if (!put_data) {
1431 put_data = "";
1432 }
1433
1434 UPD_STRL(putData, put_data, data_len);
1435 RETURN_TRUE;
1436 }
1437 /* }}} */
1438
1439 /* {{{ proto bool HttpRequest::addPutData(string put_data)
1440 Add PUT data, leaving previously set PUT data unchanged. */
1441 PHP_METHOD(HttpRequest, addPutData)
1442 {
1443 char *put_data;
1444 int data_len;
1445
1446 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &put_data, &data_len)) {
1447 RETURN_FALSE;
1448 }
1449
1450 if (data_len) {
1451 zval *data = GET_PROP(putData);
1452
1453 if (Z_STRLEN_P(data)) {
1454 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1455 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1456 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, put_data, data_len);
1457 } else {
1458 UPD_STRL(putData, put_data, data_len);
1459 }
1460 }
1461
1462 RETURN_TRUE;
1463 }
1464 /* }}} */
1465
1466 /* {{{ proto string HttpRequest::getPutData()
1467 Get previously set PUT data. */
1468 PHP_METHOD(HttpRequest, getPutData)
1469 {
1470 NO_ARGS;
1471
1472 if (return_value_used) {
1473 RETURN_PROP(putData);
1474 }
1475 }
1476 /* }}} */
1477
1478 /* {{{ proto array HttpRequest::getResponseData()
1479 Get all response data after the request has been sent. */
1480 PHP_METHOD(HttpRequest, getResponseData)
1481 {
1482 NO_ARGS;
1483
1484 if (return_value_used) {
1485 char *body;
1486 size_t body_len;
1487 zval *headers, *message = GET_PROP(responseMessage);
1488
1489 if (Z_TYPE_P(message) == IS_OBJECT) {
1490 getObjectEx(http_message_object, msg, message);
1491
1492 array_init(return_value);
1493
1494 MAKE_STD_ZVAL(headers);
1495 array_init(headers);
1496 zend_hash_copy(Z_ARRVAL_P(headers), &msg->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1497 add_assoc_zval(return_value, "headers", headers);
1498
1499 phpstr_data(PHPSTR(msg->message), &body, &body_len);
1500 add_assoc_stringl(return_value, "body", body, body_len, 0);
1501 }
1502 }
1503 }
1504 /* }}} */
1505
1506 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1507 Get response header(s) after the request has been sent. */
1508 PHP_METHOD(HttpRequest, getResponseHeader)
1509 {
1510 if (return_value_used) {
1511 zval *header;
1512 char *header_name = NULL;
1513 int header_len = 0;
1514
1515 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1516 zval *message = GET_PROP(responseMessage);
1517
1518 if (Z_TYPE_P(message) == IS_OBJECT) {
1519 getObjectEx(http_message_object, msg, message);
1520
1521 if (header_len) {
1522 if ((header = http_message_header_ex(msg->message, pretty_key(header_name, header_len, 1, 1), header_len + 1, 0))) {
1523 RETURN_ZVAL(header, 1, 1);
1524 }
1525 } else {
1526 array_init(return_value);
1527 zend_hash_copy(Z_ARRVAL_P(return_value), &msg->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1528 return;
1529 }
1530 }
1531 }
1532 RETURN_FALSE;
1533 }
1534 }
1535 /* }}} */
1536
1537 /* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]])
1538 Get response cookie(s) after the request has been sent. */
1539 PHP_METHOD(HttpRequest, getResponseCookies)
1540 {
1541 if (return_value_used) {
1542 long flags = 0;
1543 zval *allowed_extras_array = NULL;
1544
1545 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la!", &flags, &allowed_extras_array)) {
1546 int i = 0;
1547 HashKey key = initHashKey(0);
1548 char **allowed_extras = NULL;
1549 zval **header = NULL, **entry = NULL, *message = GET_PROP(responseMessage);
1550 HashPosition pos, pos1, pos2;
1551
1552 if (Z_TYPE_P(message) == IS_OBJECT) {
1553 getObjectEx(http_message_object, msg, message);
1554
1555 array_init(return_value);
1556
1557 if (allowed_extras_array) {
1558 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
1559 FOREACH_VAL(pos, allowed_extras_array, entry) {
1560 ZVAL_ADDREF(*entry);
1561 convert_to_string_ex(entry);
1562 allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
1563 zval_ptr_dtor(entry);
1564 }
1565 }
1566
1567 FOREACH_HASH_KEYVAL(pos1, &msg->message->hdrs, key, header) {
1568 if (key.type == HASH_KEY_IS_STRING && !strcasecmp(key.str, "Set-Cookie")) {
1569 http_cookie_list list;
1570
1571 if (Z_TYPE_PP(header) == IS_ARRAY) {
1572 zval **single_header;
1573
1574 FOREACH_VAL(pos2, *header, single_header) {
1575 ZVAL_ADDREF(*single_header);
1576 convert_to_string_ex(single_header);
1577 if (http_parse_cookie_ex(&list, Z_STRVAL_PP(single_header), flags, allowed_extras)) {
1578 zval *cookie;
1579
1580 MAKE_STD_ZVAL(cookie);
1581 object_init(cookie);
1582 http_cookie_list_tostruct(&list, cookie);
1583 add_next_index_zval(return_value, cookie);
1584 http_cookie_list_dtor(&list);
1585 }
1586 zval_ptr_dtor(single_header);
1587 }
1588 } else {
1589 ZVAL_ADDREF(*header);
1590 convert_to_string_ex(header);
1591 if (http_parse_cookie_ex(&list, Z_STRVAL_PP(header), flags, allowed_extras)) {
1592 zval *cookie;
1593
1594 MAKE_STD_ZVAL(cookie);
1595 object_init(cookie);
1596 http_cookie_list_tostruct(&list, cookie);
1597 add_next_index_zval(return_value, cookie);
1598 http_cookie_list_dtor(&list);
1599 }
1600 zval_ptr_dtor(header);
1601 }
1602 }
1603 }
1604
1605 if (allowed_extras) {
1606 for (i = 0; allowed_extras[i]; ++i) {
1607 efree(allowed_extras[i]);
1608 }
1609 efree(allowed_extras);
1610 }
1611
1612 return;
1613 }
1614 }
1615 RETURN_FALSE;
1616 }
1617 }
1618 /* }}} */
1619
1620 /* {{{ proto string HttpRequest::getResponseBody()
1621 Get the response body after the request has been sent. */
1622 PHP_METHOD(HttpRequest, getResponseBody)
1623 {
1624 NO_ARGS;
1625
1626 if (return_value_used) {
1627 zval *message = GET_PROP(responseMessage);
1628
1629 if (Z_TYPE_P(message) == IS_OBJECT) {
1630 getObjectEx(http_message_object, msg, message);
1631 RETURN_PHPSTR_DUP(&msg->message->body);
1632 } else {
1633 RETURN_FALSE;
1634 }
1635 }
1636 }
1637 /* }}} */
1638
1639 /* {{{ proto int HttpRequest::getResponseCode()
1640 Get the response code after the request has been sent. */
1641 PHP_METHOD(HttpRequest, getResponseCode)
1642 {
1643 NO_ARGS;
1644
1645 if (return_value_used) {
1646 RETURN_PROP(responseCode);
1647 }
1648 }
1649 /* }}} */
1650
1651 /* {{{ proto string HttpRequest::getResponseStatus()
1652 Get the response status (i.e. the string after the response code) after the message has been sent. */
1653 PHP_METHOD(HttpRequest, getResponseStatus)
1654 {
1655 NO_ARGS;
1656
1657 if (return_value_used) {
1658 RETURN_PROP(responseStatus);
1659 }
1660 }
1661 /* }}} */
1662
1663 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1664 Get response info after the request has been sent. */
1665 PHP_METHOD(HttpRequest, getResponseInfo)
1666 {
1667 if (return_value_used) {
1668 zval *info, **infop;
1669 char *info_name = NULL;
1670 int info_len = 0;
1671
1672 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1673 RETURN_FALSE;
1674 }
1675
1676 info = GET_PROP(responseInfo);
1677
1678 if (Z_TYPE_P(info) != IS_ARRAY) {
1679 RETURN_FALSE;
1680 }
1681
1682 if (info_len && info_name) {
1683 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) {
1684 RETURN_ZVAL(*infop, 1, 0);
1685 } else {
1686 http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
1687 RETURN_FALSE;
1688 }
1689 } else {
1690 RETURN_ZVAL(info, 1, 0);
1691 }
1692 }
1693 }
1694 /* }}}*/
1695
1696 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1697 Get the full response as HttpMessage object after the request has been sent. */
1698 PHP_METHOD(HttpRequest, getResponseMessage)
1699 {
1700 NO_ARGS {
1701 zval *message;
1702
1703 SET_EH_THROW_HTTP();
1704 message = GET_PROP(responseMessage);
1705 if (Z_TYPE_P(message) == IS_OBJECT) {
1706 RETVAL_OBJECT(message, 1);
1707 } else {
1708 http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpRequest does not contain a response message");
1709 }
1710 SET_EH_NORMAL();
1711 }
1712 }
1713 /* }}} */
1714
1715 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1716 Get sent HTTP message. */
1717 PHP_METHOD(HttpRequest, getRequestMessage)
1718 {
1719 NO_ARGS;
1720
1721 if (return_value_used) {
1722 http_message *msg;
1723 getObject(http_request_object, obj);
1724
1725 SET_EH_THROW_HTTP();
1726 if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
1727 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
1728 }
1729 SET_EH_NORMAL();
1730 }
1731 }
1732 /* }}} */
1733
1734 /* {{{ proto string HttpRequest::getRawRequestMessage()
1735 Get sent HTTP message. */
1736 PHP_METHOD(HttpRequest, getRawRequestMessage)
1737 {
1738 NO_ARGS;
1739
1740 if (return_value_used) {
1741 getObject(http_request_object, obj);
1742
1743 RETURN_PHPSTR_DUP(&obj->request->conv.request);
1744 }
1745 }
1746 /* }}} */
1747
1748 /* {{{ proto string HttpRequest::getRawResponseMessage()
1749 Get the entire HTTP response. */
1750 PHP_METHOD(HttpRequest, getRawResponseMessage)
1751 {
1752 NO_ARGS;
1753
1754 if (return_value_used) {
1755 getObject(http_request_object, obj);
1756
1757 RETURN_PHPSTR_DUP(&obj->request->conv.response);
1758 }
1759 }
1760 /* }}} */
1761
1762 /* {{{ proto HttpMessage HttpRequest::getHistory()
1763 Get all sent requests and received responses as an HttpMessage object. */
1764 PHP_METHOD(HttpRequest, getHistory)
1765 {
1766 NO_ARGS;
1767
1768 if (return_value_used) {
1769 zval *hist;
1770
1771 SET_EH_THROW_HTTP();
1772 hist = GET_PROP(history);
1773 if (Z_TYPE_P(hist) == IS_OBJECT) {
1774 RETVAL_OBJECT(hist, 1);
1775 } else {
1776 http_error(HE_WARNING, HTTP_E_RUNTIME, "The history is empty");
1777 }
1778 SET_EH_NORMAL();
1779 }
1780 }
1781 /* }}} */
1782
1783 /* {{{ proto void HttpRequest::clearHistory()
1784 Clear the history. */
1785 PHP_METHOD(HttpRequest, clearHistory)
1786 {
1787 NO_ARGS {
1788 zval *hist;
1789
1790 MAKE_STD_ZVAL(hist);
1791 ZVAL_NULL(hist);
1792 SET_PROP(history, hist);
1793 zval_ptr_dtor(&hist);
1794 }
1795 }
1796 /* }}} */
1797
1798 /* {{{ proto HttpMessage HttpRequest::send()
1799 Send the HTTP request. */
1800 PHP_METHOD(HttpRequest, send)
1801 {
1802 getObject(http_request_object, obj);
1803
1804 NO_ARGS;
1805
1806 SET_EH_THROW_HTTP();
1807
1808 RETVAL_FALSE;
1809
1810 if (obj->pool) {
1811 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1812 } else if (SUCCESS == http_request_object_requesthandler(obj, getThis())) {
1813 http_request_exec(obj->request);
1814 if (SUCCESS == http_request_object_responsehandler(obj, getThis())) {
1815 RETVAL_OBJECT(GET_PROP(responseMessage), 1);
1816 }
1817 }
1818
1819 SET_EH_NORMAL();
1820 }
1821 /* }}} */
1822
1823 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1824
1825 /*
1826 * Local variables:
1827 * tab-width: 4
1828 * c-basic-offset: 4
1829 * End:
1830 * vim600: noet sw=4 ts=4 fdm=marker
1831 * vim<600: noet sw=4 ts=4
1832 */
1833