#include "php_http_request_object.h"
#include "php_http_request_api.h"
#include "php_http_request_pool_api.h"
+#include "php_http.h"
#include "php_http_api.h"
#include "php_http_url_api.h"
#include "php_http_message_api.h"
#define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
#define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
#define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
+#define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
HTTP_EMPTY_ARGS(__destruct, 0);
HTTP_BEGIN_ARGS(__construct, 0, 0)
HTTP_END_ARGS;
HTTP_EMPTY_ARGS(getResponseMessage, 1);
+HTTP_EMPTY_ARGS(getRequestMessage, 1);
+HTTP_EMPTY_ARGS(getHistory, 1);
HTTP_EMPTY_ARGS(send, 0);
+HTTP_BEGIN_ARGS(get, 0, 1)
+ HTTP_ARG_VAL(url, 0)
+ HTTP_ARG_VAL(options, 0)
+ HTTP_ARG_VAL(info, 1)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(head, 0, 1)
+ HTTP_ARG_VAL(url, 0)
+ HTTP_ARG_VAL(options, 0)
+ HTTP_ARG_VAL(info, 1)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(postData, 0, 2)
+ HTTP_ARG_VAL(url, 0)
+ HTTP_ARG_VAL(data, 0)
+ HTTP_ARG_VAL(options, 0)
+ HTTP_ARG_VAL(info, 1)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(postFields, 0, 2)
+ HTTP_ARG_VAL(url, 0)
+ HTTP_ARG_VAL(data, 0)
+ HTTP_ARG_VAL(options, 0)
+ HTTP_ARG_VAL(info, 1)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(putFile, 0, 2)
+ HTTP_ARG_VAL(url, 0)
+ HTTP_ARG_VAL(file, 0)
+ HTTP_ARG_VAL(options, 0)
+ HTTP_ARG_VAL(info, 1)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(putStream, 0, 2)
+ HTTP_ARG_VAL(url, 0)
+ HTTP_ARG_VAL(stream, 0)
+ HTTP_ARG_VAL(options, 0)
+ HTTP_ARG_VAL(info, 1)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(methodRegister, 0, 1)
+ HTTP_ARG_VAL(method_name, 0)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(methodUnregister, 0, 1)
+ HTTP_ARG_VAL(method, 0)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(methodName, 0, 1)
+ HTTP_ARG_VAL(method_id, 0)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(methodExists, 0, 1)
+ HTTP_ARG_VAL(method, 0)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(debugWrapper, 0, 2)
+ HTTP_ARG_VAL(info_type, 0)
+ HTTP_ARG_VAL(info_message, 0)
+HTTP_END_ARGS;
+
#define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
static inline void _http_request_object_declare_default_properties(TSRMLS_D);
HTTP_REQUEST_ME(getResponseBody, ZEND_ACC_PUBLIC)
HTTP_REQUEST_ME(getResponseInfo, ZEND_ACC_PUBLIC)
HTTP_REQUEST_ME(getResponseMessage, ZEND_ACC_PUBLIC)
+ HTTP_REQUEST_ME(getRequestMessage, ZEND_ACC_PUBLIC)
+ HTTP_REQUEST_ME(getHistory, ZEND_ACC_PUBLIC)
+
+ HTTP_REQUEST_ALIAS(get, http_get)
+ HTTP_REQUEST_ALIAS(head, http_head)
+ HTTP_REQUEST_ALIAS(postData, http_post_data)
+ HTTP_REQUEST_ALIAS(postFields, http_post_fields)
+ HTTP_REQUEST_ALIAS(putFile, http_put_file)
+ HTTP_REQUEST_ALIAS(putStream, http_put_stream)
+
+ HTTP_REQUEST_ALIAS(methodRegister, http_request_method_register)
+ HTTP_REQUEST_ALIAS(methodUnregister, http_request_method_unregister)
+ HTTP_REQUEST_ALIAS(methodName, http_request_method_name)
+ HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists)
+
+ HTTP_REQUEST_ME(debugWrapper, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
{NULL, NULL, NULL}
};
o->ch = curl_easy_init();
o->pool = NULL;
+ phpstr_init(&o->history);
+ phpstr_init(&o->request);
phpstr_init_ex(&o->response, HTTP_CURLBUF_SIZE, 0);
ALLOC_HASHTABLE(OBJ_PROP(o));
zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
- ov.handle = zend_objects_store_put(o, (zend_objects_store_dtor_t) zend_objects_destroy_object, http_request_object_free, NULL TSRMLS_CC);
+ ov.handle = putObject(http_request_object, o);
ov.handlers = &http_request_object_handlers;
return ov;
DCL_PROP(PROTECTED, string, contentType, "");
DCL_PROP(PROTECTED, string, queryData, "");
DCL_PROP(PROTECTED, string, putFile, "");
+
+ DCL_PROP_N(PRIVATE, dbg_user_cb);
+ DCL_PROP(PUBLIC, bool, recordHistory, 1);
}
void _http_request_object_free(zend_object *object TSRMLS_DC)
FREE_HASHTABLE(OBJ_PROP(o));
}
if (o->ch) {
+ /* avoid nasty segfaults with already cleaned up callbacks */
+ curl_easy_setopt(o->ch, CURLOPT_NOPROGRESS, 1);
+ curl_easy_setopt(o->ch, CURLOPT_VERBOSE, 0);
curl_easy_cleanup(o->ch);
}
phpstr_dtor(&o->response);
+ phpstr_dtor(&o->request);
+ phpstr_dtor(&o->history);
efree(o);
}
strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
}
+ /* ensure we have HttpRequest::debugWrapper as dbg callback */
+ {
+ zval *dbg_cb;
+ MAKE_STD_ZVAL(dbg_cb);
+ array_init(dbg_cb);
+ zval_add_ref(&getThis());
+ add_next_index_zval(dbg_cb, getThis());
+ add_next_index_stringl(dbg_cb, "debugWrapper", lenof("debugWrapper"), 1);
+ add_assoc_zval(opts, "ondebug", dbg_cb);
+ }
+ /* */
+
switch (Z_LVAL_P(meth))
{
case HTTP_GET:
break;
}
+ /* clean previous response */
+ phpstr_dtor(&obj->response);
+ /* clean previous request */
+ phpstr_dtor(&obj->request);
+
efree(request_uri);
return status;
}
{
http_message *msg;
+ phpstr_fix(&obj->request);
phpstr_fix(&obj->response);
if (msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response))) {
char *body;
size_t body_len;
- zval *headers, *message, *resp = GET_PROP(obj, responseData), *info = GET_PROP(obj, responseInfo);
+ zval *headers, *message,
+ *resp = GET_PROP(obj, responseData),
+ *info = GET_PROP(obj, responseInfo),
+ *hist = GET_PROP(obj, recordHistory);
+
+ if (Z_TYPE_P(hist) != IS_BOOL) {
+ convert_to_boolean_ex(&hist);
+ }
+ if (Z_LVAL_P(hist)) {
+ /* we need to act like a zipper, as we'll receive
+ * the requests and the responses in separate chains
+ * for redirects
+ */
+ http_message *response = msg, *request = http_message_parse(PHPSTR_VAL(&obj->request), PHPSTR_LEN(&obj->request));
+ http_message *free_msg = request;
+
+ do {
+ char *message;
+ size_t msglen;
+
+ http_message_tostring(response, &message, &msglen);
+ phpstr_append(&obj->history, message, msglen);
+ efree(message);
+
+ http_message_tostring(request, &message, &msglen);
+ phpstr_append(&obj->history, message, msglen);
+ efree(message);
+
+ } while ((response = response->parent) && (request = request->parent));
+
+ http_message_free(free_msg);
+ phpstr_fix(&obj->history);
+ }
UPD_PROP(obj, long, responseCode, msg->info.response.code);
old_opts = GET_PROP(obj, options);
- /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
+ /* some options need extra attention -- thus cannot use array_merge() directly */
FOREACH_KEYVAL(opts, key, idx, opt) {
if (key) {
if (!strcmp(key, "headers")) {
zval **headers;
if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
array_merge(*opt, *headers);
- continue;
}
} else if (!strcmp(key, "cookies")) {
zval **cookies;
if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
array_merge(*opt, *cookies);
- continue;
}
+ } else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) {
+ if (Z_TYPE_PP(opt) != IS_STRING) {
+ convert_to_string_ex(opt);
+ }
+ UPD_PROP(obj, string, url, Z_STRVAL_PP(opt));
+ } else if (!strcmp(key, "method")) {
+ if (Z_TYPE_PP(opt) != IS_LONG) {
+ convert_to_long_ex(opt);
+ }
+ UPD_PROP(obj, long, method, Z_LVAL_PP(opt));
+ } else {
+ if (!strcmp(key, "ondebug")) {
+ SET_PROP(obj, dbg_user_cb, *opt);
+ }
+ zval_add_ref(opt);
+ add_assoc_zval(old_opts, key, *opt);
}
- zval_add_ref(opt);
- add_assoc_zval(old_opts, key, *opt);
/* reset */
key = NULL;
FREE_PARR(obj, options);
INIT_PARR(obj, options);
+ zend_update_property_null(http_request_object_ce, getThis(), "dbg_user_cb", lenof("dbg_user_cb") TSRMLS_CC);
}
/* }}} */
zval *message;
getObject(http_request_object, obj);
+ SET_EH_THROW_HTTP();
message = GET_PROP(obj, responseMessage);
if (Z_TYPE_P(message) == IS_OBJECT) {
RETVAL_OBJECT(message);
} else {
- RETURN_NULL();
+ RETVAL_NULL();
}
+ SET_EH_NORMAL();
+ }
+}
+/* }}} */
+
+/* {{{ proto HttpMessage HttpRequest::getRequestMessage()
+ *
+ * Get sent HTTP message.
+ */
+PHP_METHOD(HttpRequest, getRequestMessage)
+{
+ NO_ARGS;
+
+ IF_RETVAL_USED {
+ zval *message;
+ http_message *msg;
+ getObject(http_request_object, obj);
+
+ SET_EH_THROW_HTTP();
+ if (msg = http_message_parse(PHPSTR_VAL(&obj->request), PHPSTR_LEN(&obj->request))) {
+ RETVAL_OBJVAL(http_message_object_from_msg(msg));
+ }
+ SET_EH_NORMAL();
}
}
/* }}} */
+PHP_METHOD(HttpRequest, getHistory)
+{
+ NO_ARGS;
+
+ IF_RETVAL_USED {
+ zval *history;
+ http_message *msg;
+ getObject(http_request_object, obj);
+
+ SET_EH_THROW_HTTP();
+ if (msg = http_message_parse(PHPSTR_VAL(&obj->history), PHPSTR_LEN(&obj->history))) {
+ RETVAL_OBJVAL(http_message_object_from_msg(msg));
+ }
+ SET_EH_NORMAL();
+ }
+}
+
/* {{{ proto bool HttpRequest::send()
*
* Send the HTTP request.
PHP_METHOD(HttpRequest, send)
{
STATUS status = FAILURE;
- http_request_body body = {0};
+ http_request_body body = {0, NULL, 0};
getObject(http_request_object, obj);
NO_ARGS;
}
/* }}} */
+/* {{{ proto private HttpRequest::debugWrapper(long type, string message)
+ */
+PHP_METHOD(HttpRequest, debugWrapper)
+{
+ static int curl_ignores_body = 0;
+ getObject(http_request_object, obj);
+ zval *type, *message, *dbg_user_cb = GET_PROP(obj, dbg_user_cb);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &type, &message)) {
+ RETURN_NULL();
+ }
+
+ if (Z_TYPE_P(type) != IS_LONG) {
+ convert_to_long_ex(&type);
+ }
+ if (Z_TYPE_P(message) != IS_STRING) {
+ convert_to_string_ex(&message);
+ }
+
+ switch (Z_LVAL_P(type))
+ {
+ case CURLINFO_DATA_IN:
+ /* fetch ignored body */
+ if (curl_ignores_body && Z_LVAL_P(type) == CURLINFO_DATA_IN) {
+ phpstr_append(&obj->response, Z_STRVAL_P(message), Z_STRLEN_P(message));
+ }
+ break;
+
+ case CURLINFO_TEXT:
+ /* check if following incoming data would be ignored */
+ curl_ignores_body = !strcmp(Z_STRVAL_P(message), "Ignoring the response-body\n");
+ break;
+
+ case CURLINFO_HEADER_OUT:
+ case CURLINFO_DATA_OUT:
+ /* fetch outgoing request message */
+ phpstr_append(&obj->request, Z_STRVAL_P(message), Z_STRLEN_P(message));
+ break;
+ }
+
+ /* call user debug callback */
+ if (Z_TYPE_P(dbg_user_cb) != IS_NULL) {
+ zval *args[2], cb_ret;
+
+ args[0] = type;
+ args[1] = message;
+ call_user_function(EG(function_table), NULL, dbg_user_cb, &cb_ret, 2, args TSRMLS_CC);
+ }
+
+ RETURN_NULL();
+}
+/* }}} */
+
#endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
/*