+ STATUS status = SUCCESS;
+
+ http_request_reset(obj->request);
+ HTTP_CHECK_CURL_INIT(obj->request->ch, http_curl_init(obj->request), return FAILURE);
+
+ obj->request->url = http_absolute_url(Z_STRVAL_P(GET_PROP(url)));
+
+ switch (obj->request->meth = Z_LVAL_P(GET_PROP(method)))
+ {
+ case HTTP_GET:
+ case HTTP_HEAD:
+ break;
+
+ case HTTP_PUT:
+ {
+ zval *put_data = GET_PROP(putData);
+
+ http_request_object_check_request_content_type(getThis());
+ if (Z_STRLEN_P(put_data)) {
+ obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
+ estrndup(Z_STRVAL_P(put_data), Z_STRLEN_P(put_data)), Z_STRLEN_P(put_data), 1);
+ } else {
+ php_stream_statbuf ssb;
+ php_stream *stream = php_stream_open_wrapper_ex(Z_STRVAL_P(GET_PROP(putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT);
+
+ if (stream && !php_stream_stat(stream, &ssb)) {
+ obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1);
+ } else {
+ status = FAILURE;
+ }
+ }
+ break;
+ }
+
+ case HTTP_POST:
+ default:
+ {
+ /* check for raw post data */
+ zval *raw_data = GET_PROP(rawPostData);
+
+ if (Z_STRLEN_P(raw_data)) {
+ http_request_object_check_request_content_type(getThis());
+ obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
+ estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data)), Z_STRLEN_P(raw_data), 1);
+ } else {
+ zval *zfields = GET_PROP(postFields), *zfiles = GET_PROP(postFiles);
+ HashTable *fields;
+ HashTable *files;
+
+ fields = (Z_TYPE_P(zfields) == IS_ARRAY) ? Z_ARRVAL_P(zfields) : NULL;
+ files = (Z_TYPE_P(zfiles) == IS_ARRAY) ? Z_ARRVAL_P(zfiles) : NULL;
+
+ if ((fields && zend_hash_num_elements(fields)) || (files && zend_hash_num_elements(files))) {
+ if (!(obj->request->body = http_request_body_fill(obj->request->body, fields, files))) {
+ status = FAILURE;
+ }
+ }
+ }
+ break;
+ }
+ }
+
+ if (status == SUCCESS) {
+ zval *qdata = GET_PROP(queryData);
+ zval *options = GET_PROP(options);
+
+ if (Z_STRLEN_P(qdata)) {
+ if (!strchr(obj->request->url, '?')) {
+ strlcat(obj->request->url, "?", HTTP_URL_MAXLEN);
+ } else {
+ strlcat(obj->request->url, "&", HTTP_URL_MAXLEN);
+ }
+ strlcat(obj->request->url, Z_STRVAL_P(qdata), HTTP_URL_MAXLEN);
+ }
+
+ http_request_prepare(obj->request, Z_ARRVAL_P(options));
+
+ /* check if there's a onProgress method and add it as progress callback if one isn't already set */
+ if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onprogress", sizeof("onprogress"))) {
+ zval **entry, *pcb;
+
+ if ( (Z_TYPE_P(options) != IS_ARRAY)
+ || (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void *) &entry)
+ || (!zval_is_true(*entry)))) {
+ MAKE_STD_ZVAL(pcb);
+ array_init(pcb);
+ ZVAL_ADDREF(getThis());
+ add_next_index_zval(pcb, getThis());
+ add_next_index_stringl(pcb, "onprogress", lenof("onprogress"), 1);
+ http_request_set_progress_callback(obj->request, pcb);
+ zval_ptr_dtor(&pcb);
+ }
+ }
+ }
+
+ return status;
+}
+
+STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
+{
+ STATUS ret;
+ zval *info;
+ http_message *msg;
+
+ /* always fetch info */
+ MAKE_STD_ZVAL(info);
+ array_init(info);
+ http_request_info(obj->request, Z_ARRVAL_P(info));
+ SET_PROP(responseInfo, info);
+ zval_ptr_dtor(&info);
+
+ /* parse response message */
+ phpstr_fix(&obj->request->conv.request);
+ phpstr_fix(&obj->request->conv.response);
+
+ if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)))) {
+ char *body;
+ size_t body_len;
+ zval *headers, *message, *resp;
+
+ if (zval_is_true(GET_PROP(recordHistory))) {
+ zval *hist, *history = GET_PROP(history);
+ http_message *response = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response));
+ http_message *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
+
+ MAKE_STD_ZVAL(hist);
+ ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, http_message_interconnect(response, request), NULL), 0);
+ if (Z_TYPE_P(history) == IS_OBJECT) {
+ http_message_object_prepend(hist, history);
+ }
+ SET_PROP(history, hist);
+ zval_ptr_dtor(&hist);
+ }
+
+ UPD_PROP(long, responseCode, msg->http.info.response.code);
+ UPD_PROP(string, responseStatus, msg->http.info.response.status ? msg->http.info.response.status : "");
+
+ MAKE_STD_ZVAL(resp);
+ array_init(resp);
+ MAKE_STD_ZVAL(headers);
+ array_init(headers);
+ zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+ add_assoc_zval(resp, "headers", headers);
+ phpstr_data(PHPSTR(msg), &body, &body_len);
+ add_assoc_stringl(resp, "body", body, body_len, 0);
+ SET_PROP(responseData, resp);
+ zval_ptr_dtor(&resp);
+
+ MAKE_STD_ZVAL(message);
+ ZVAL_OBJVAL(message, http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
+ SET_PROP(responseMessage, message);
+ zval_ptr_dtor(&message);
+
+ ret = SUCCESS;
+ } else {
+ /* update properties with empty values*/
+ zval *resp = GET_PROP(responseData), *znull;
+
+ MAKE_STD_ZVAL(znull);
+ ZVAL_NULL(znull);
+ SET_PROP(responseMessage, znull);
+ zval_ptr_dtor(&znull);
+
+ if (Z_TYPE_P(resp) == IS_ARRAY) {
+ zend_hash_clean(Z_ARRVAL_P(resp));
+ }
+
+ UPD_PROP(long, responseCode, 0);
+ UPD_PROP(string, responseStatus, "");
+
+ /* append request message to history */
+ if (zval_is_true(GET_PROP(recordHistory))) {
+ http_message *request;
+
+ if ((request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
+ zval *hist, *history = GET_PROP(history);
+
+ MAKE_STD_ZVAL(hist);
+ ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, request, NULL), 0);
+ if (Z_TYPE_P(history) == IS_OBJECT) {
+ http_message_object_prepend(hist, history);
+ }
+ SET_PROP(history, hist);
+ zval_ptr_dtor(&hist);
+ }
+ }
+
+ ret = FAILURE;
+ }
+
+ if (!EG(exception) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) {
+ zval *param;
+
+ MAKE_STD_ZVAL(param);
+ ZVAL_BOOL(param, ret == SUCCESS);
+ with_error_handling(EH_NORMAL, NULL) {
+ zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param);
+ } end_error_handling();
+ zval_ptr_dtor(¶m);
+ }
+
+ return ret;
+}
+
+static int apply_pretty_key(void *pDest, int num_args, va_list args, zend_hash_key *hash_key)
+{
+ if (hash_key->nKeyLength > 1) {
+ hash_key->h = zend_get_hash_value(pretty_key(hash_key->arKey, hash_key->nKeyLength - 1, 1, 0), hash_key->nKeyLength);
+ }
+ return ZEND_HASH_APPLY_KEEP;
+}
+
+#define http_request_object_set_options_subr(key, ow, pk) \
+ _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
+static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys)
+{
+ zval *old_opts, *new_opts, *opts = NULL, **entry = NULL;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
+ RETURN_FALSE;
+ }
+
+ MAKE_STD_ZVAL(new_opts);
+ array_init(new_opts);
+ old_opts = GET_PROP(options);
+ if (Z_TYPE_P(old_opts) == IS_ARRAY) {
+ array_copy(old_opts, new_opts);
+ }
+
+ if (prettify_keys && opts) {
+ zend_hash_apply_with_arguments(Z_ARRVAL_P(opts), apply_pretty_key, 0);
+ }
+ if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
+ if (overwrite) {
+ zend_hash_clean(Z_ARRVAL_PP(entry));
+ }
+ if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
+ if (overwrite) {
+ array_copy(opts, *entry);
+ } else {
+ array_merge(opts, *entry);
+ }
+ }
+ } else if (opts) {
+ ZVAL_ADDREF(opts);
+ add_assoc_zval(new_opts, key, opts);
+ }
+ SET_PROP(options, new_opts);
+ zval_ptr_dtor(&new_opts);
+
+ RETURN_TRUE;
+}
+
+#define http_request_object_get_options_subr(key) \
+ _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
+static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *opts, **options;
+
+ opts = GET_PROP(options);
+ array_init(return_value);