- missing include
[m6w6/ext-http] / http_request_object.c
index 4d862279e0807a13b05cb45cea093fc2bad16902..0c68635b1a662f1af3814bdf8b1abeb4336938b9 100644 (file)
 #include "php_http_message_object.h"
 #include "php_http_exception_object.h"
 
+#include "missing.h"
+
 #ifdef PHP_WIN32
 #      include <winsock2.h>
 #endif
 #include <curl/curl.h>
 
+ZEND_EXTERN_MODULE_GLOBALS(http);
+
 #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)
@@ -143,61 +147,63 @@ HTTP_BEGIN_ARGS(getResponseInfo, 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, 1, 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, 1, 0)
+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, 2, 0)
+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, 2, 0)
+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, 2, 0)
+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, 2, 0)
+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, 1, 0)
+HTTP_BEGIN_ARGS(methodRegister, 0, 1)
        HTTP_ARG_VAL(method_name, 0)
 HTTP_END_ARGS;
 
-HTTP_BEGIN_ARGS(methodUnregister, 1, 0)
+HTTP_BEGIN_ARGS(methodUnregister, 0, 1)
        HTTP_ARG_VAL(method, 0)
 HTTP_END_ARGS;
 
-HTTP_BEGIN_ARGS(methodName, 1, 0)
+HTTP_BEGIN_ARGS(methodName, 0, 1)
        HTTP_ARG_VAL(method_id, 0)
 HTTP_END_ARGS;
 
-HTTP_BEGIN_ARGS(methodExists, 1, 0)
+HTTP_BEGIN_ARGS(methodExists, 0, 1)
        HTTP_ARG_VAL(method, 0)
 HTTP_END_ARGS;
 
@@ -260,6 +266,8 @@ zend_function_entry http_request_object_fe[] = {
        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)
@@ -331,13 +339,15 @@ zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC)
        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;
@@ -361,6 +371,8 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D)
        DCL_PROP(PROTECTED, string, contentType, "");
        DCL_PROP(PROTECTED, string, queryData, "");
        DCL_PROP(PROTECTED, string, putFile, "");
+
+       DCL_PROP(PUBLIC, bool, recordHistory, 1);
 }
 
 void _http_request_object_free(zend_object *object TSRMLS_DC)
@@ -372,9 +384,16 @@ 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_PROGRESSFUNCTION, NULL);
+               curl_easy_setopt(o->ch, CURLOPT_VERBOSE, 0);
+               curl_easy_setopt(o->ch, CURLOPT_DEBUGFUNCTION, NULL);
                curl_easy_cleanup(o->ch);
        }
        phpstr_dtor(&o->response);
+       phpstr_dtor(&o->request);
+       phpstr_dtor(&o->history);
        efree(o);
 }
 
@@ -388,7 +407,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                return FAILURE;
        }
        if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
-               http_error(E_WARNING, HTTP_E_CURL, "Could not initilaize curl");
+               http_error(HE_WARNING, HTTP_E_REQUEST, "Could not initilaize curl");
                return FAILURE;
        }
 
@@ -416,7 +435,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                case HTTP_GET:
                case HTTP_HEAD:
                        body->type = -1;
-                       status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, NULL, Z_ARRVAL_P(opts), &obj->response);
+                       status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, NULL, Z_ARRVAL_P(opts));
                break;
 
                case HTTP_PUT:
@@ -431,7 +450,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                                body->data = stream;
                                body->size = ssb.sb.st_size;
 
-                               status = http_request_init(obj->ch, HTTP_PUT, request_uri, body, Z_ARRVAL_P(opts), &obj->response);
+                               status = http_request_init(obj->ch, HTTP_PUT, request_uri, body, Z_ARRVAL_P(opts));
                        } else {
                                status = FAILURE;
                        }
@@ -443,7 +462,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                        zval *fields = GET_PROP(obj, postFields), *files = GET_PROP(obj, postFiles);
 
                        if (SUCCESS == (status = http_request_body_fill(body, Z_ARRVAL_P(fields), Z_ARRVAL_P(files)))) {
-                               status = http_request_init(obj->ch, HTTP_POST, request_uri, body, Z_ARRVAL_P(opts), &obj->response);
+                               status = http_request_init(obj->ch, HTTP_POST, request_uri, body, Z_ARRVAL_P(opts));
                        }
                }
                break;
@@ -456,13 +475,15 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                        body->data = Z_STRVAL_P(post);
                        body->size = Z_STRLEN_P(post);
 
-                       status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, body, Z_ARRVAL_P(opts), &obj->response);
+                       status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, body, Z_ARRVAL_P(opts));
                }
                break;
        }
 
        /* clean previous response */
        phpstr_dtor(&obj->response);
+       /* clean previous request */
+       phpstr_dtor(&obj->request);
 
        efree(request_uri);
        return status;
@@ -472,12 +493,46 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
 {
        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);
+
+               /* should we record history? */
+               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);
 
@@ -573,7 +628,7 @@ PHP_METHOD(HttpRequest, setOptions)
 
        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")) {
@@ -588,7 +643,26 @@ PHP_METHOD(HttpRequest, setOptions)
                                        array_merge(*opt, *cookies);
                                        continue;
                                }
+                       } else if (!strcmp(key, "ssl")) {
+                               zval **ssl;
+                               if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl)) {
+                                       array_merge(*opt, *ssl);
+                                       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));
+                               continue;
+                       } 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));
+                               continue;
                        }
+
                        zval_add_ref(opt);
                        add_assoc_zval(old_opts, key, *opt);
 
@@ -919,7 +993,7 @@ PHP_METHOD(HttpRequest, setContentType)
        }
 
        if (!strchr(ctype, '/')) {
-               http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
+               http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
                RETURN_FALSE;
        }
 
@@ -1136,7 +1210,7 @@ PHP_METHOD(HttpRequest, addPostFile)
 
        if (type_len) {
                if (!strchr(type, '/')) {
-                       http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
+                       http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
                        RETURN_FALSE;
                }
        } else {
@@ -1475,7 +1549,7 @@ PHP_METHOD(HttpRequest, getResponseInfo)
                        if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
                                RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
                        } else {
-                               http_error_ex(E_NOTICE, HTTP_E_PARAM, "Could not find response info named %s", info_name);
+                               http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
                                RETURN_FALSE;
                        }
                } else {
@@ -1498,16 +1572,55 @@ PHP_METHOD(HttpRequest, getResponseMessage)
                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 {
+               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 {
+               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.
@@ -1545,7 +1658,7 @@ PHP_METHOD(HttpRequest, getResponseMessage)
 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;
@@ -1553,13 +1666,13 @@ PHP_METHOD(HttpRequest, send)
        SET_EH_THROW_HTTP();
 
        if (obj->pool) {
-               http_error(E_WARNING, HTTP_E_CURL, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
+               http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
                SET_EH_NORMAL();
                RETURN_FALSE;
        }
 
        if (SUCCESS == (status = http_request_object_requesthandler(obj, getThis(), &body))) {
-               status = http_request_exec(obj->ch, NULL);
+               status = http_request_exec(obj->ch, NULL, &obj->response, &obj->request);
        }
        http_request_body_dtor(&body);