- renamed response.status to response.code
[m6w6/ext-http] / http_methods.c
index ded27ff88ee5313b10fcb2b020ee1f320ee5cb9f..23631c5c6c4a04823c7e3b02dedfac195dd37034 100644 (file)
 
 #include "php.h"
 #include "php_http.h"
+#include "php_http_std_defs.h"
 #include "php_http_api.h"
+#include "php_http_cache_api.h"
 #include "php_http_curl_api.h"
-#include "php_http_std_defs.h"
+#include "php_http_date_api.h"
+#include "php_http_headers_api.h"
+#include "php_http_send_api.h"
+#include "php_http_url_api.h"
+
+#include "php_http_message_object.h"
+#include "php_http_response_object.h"
+#include "php_http_request_object.h"
+#include "php_http_exception_object.h"
 
 #ifdef ZEND_ENGINE_2
 
@@ -42,13 +52,12 @@ PHP_METHOD(HttpResponse, __construct)
        zend_bool do_cache = 0, do_gzip = 0;
        getObject(http_response_object, obj);
 
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
-               // throw exception
-               return;
+       SET_EH_THROW_HTTP();
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
+               UPD_PROP(obj, long, cache, do_cache);
+               UPD_PROP(obj, long, gzip, do_gzip);
        }
-
-       UPD_PROP(obj, long, cache, do_cache);
-       UPD_PROP(obj, long, gzip, do_gzip);
+       SET_EH_NORMAL();
 }
 /* }}} */
 
@@ -438,6 +447,11 @@ PHP_METHOD(HttpResponse, send)
        do_cache = GET_PROP(obj, cache);
        do_gzip  = GET_PROP(obj, gzip);
 
+       /* gzip */
+       if (Z_LVAL_P(do_gzip)) {
+               php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC);
+       }
+
        /* caching */
        if (Z_LVAL_P(do_cache)) {
                zval *cctrl, *etag, *lmod, *ccraw;
@@ -458,11 +472,6 @@ PHP_METHOD(HttpResponse, send)
                }
        }
 
-       /* gzip */
-       if (Z_LVAL_P(do_gzip)) {
-               /* ... */
-       }
-
        /* content type */
        {
                zval *ctype = GET_PROP(obj, contentType);
@@ -478,7 +487,7 @@ PHP_METHOD(HttpResponse, send)
                zval *dispo_file = GET_PROP(obj, dispoFile);
                if (Z_STRLEN_P(dispo_file)) {
                        zval *dispo_inline = GET_PROP(obj, dispoInline);
-                       http_send_content_disposition(Z_STRVAL_P(dispo_file), Z_STRLEN_P(dispo_file), Z_LVAL_P(dispo_inline));
+                       http_send_content_disposition(Z_STRVAL_P(dispo_file), Z_STRLEN_P(dispo_file), (zend_bool) Z_LVAL_P(dispo_inline));
                }
        }
 
@@ -512,6 +521,221 @@ PHP_METHOD(HttpResponse, send)
 /* }}} */
 /* }}} */
 
+/* {{{ HttpMessage */
+
+/* {{{ static HttpMessage HttpMessage::fromString(string raw_message)
+ *
+ * Create an HttpMessage object from a string.
+ */
+PHP_METHOD(HttpMessage, fromString)
+{
+       char *string = NULL;
+       int length = 0;
+       http_message *msg = NULL;
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
+               RETURN_NULL();
+       }
+
+       if (!(msg = http_message_parse(string, length))) {
+               RETURN_NULL();
+       }
+
+       Z_TYPE_P(return_value) = IS_OBJECT;
+       return_value->value.obj = http_message_object_from_msg(msg);
+}
+/* }}} */
+
+/* {{{ void HttpMessage::__construct([string raw_message])
+ *
+ * Instantiate a new HttpMessage object based on the optionally provided
+ * raw message.  An HTTP Message can be either a response or a request.
+ */
+PHP_METHOD(HttpMessage, __construct)
+{
+       zval *message = NULL;
+       getObject(http_message_object, obj);
+
+       SET_EH_THROW_HTTP();
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/", &message)) {
+               if (message) {
+                       convert_to_string(message);
+                       SET_PROP(obj, raw, message);
+               }
+       }
+       SET_EH_NORMAL();
+}
+/* }}} */
+
+/* {{{ void HttpMessage::setRaw(string raw_message)
+ *
+ * Parse a new raw message.
+ */
+PHP_METHOD(HttpMessage, setRaw)
+{
+       zval *message;
+       getObject(http_message_object, obj);
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &message)) {
+               return;
+       }
+
+       convert_to_string(message);
+       SET_PROP(obj, raw, message);
+}
+/* }}} */
+
+/* {{{ string HttpMessage::getBody()
+ *
+ * Get the body of the parsed Message.
+ */
+PHP_METHOD(HttpMessage, getBody)
+{
+       zval *body;
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       body = GET_PROP(obj, body);
+       RETURN_STRINGL(Z_STRVAL_P(body), Z_STRLEN_P(body), 1);
+}
+/* }}} */
+
+/* {{{ array HttpMessage::getHeaders()
+ *
+ * Get Message Headers.
+ */
+PHP_METHOD(HttpMessage, getHeaders)
+{
+       zval *headers;
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       headers = GET_PROP(obj, headers);
+       array_init(return_value);
+       array_copy(headers, return_value);
+}
+/* }}} */
+
+/* {{{ long HttpMessage::getType()
+ *
+ * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
+ */
+PHP_METHOD(HttpMessage, getType)
+{
+       zval *type;
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       type = GET_PROP(obj, type);
+       RETURN_LONG(Z_LVAL_P(type));
+}
+/* }}} */
+
+/* {{{ int HttpMessage::getResponseCode()
+ *
+ * Get the Response Code of the Message.
+ */
+PHP_METHOD(HttpMessage, getResponseCode)
+{
+       zval *status;
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       if (obj->message->type != HTTP_MSG_RESPONSE) {
+               RETURN_NULL();
+       }
+
+       status = GET_PROP(obj, responseCode);
+       RETURN_LONG(Z_LVAL_P(status));
+}
+/* }}} */
+
+/* {{{ string HttpMessage::getRequestMethod()
+ *
+ * Get the Request Method of the Message.
+ */
+PHP_METHOD(HttpMessage, getRequestMethod)
+{
+       zval *method;
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       if (obj->message->type != HTTP_MSG_REQUEST) {
+               RETURN_NULL();
+       }
+
+       method = GET_PROP(obj, requestMethod);
+       RETURN_STRINGL(Z_STRVAL_P(method), Z_STRLEN_P(method), 1);
+}
+/* }}} */
+
+/* {{{ string HttpMessage::getRequestUri()
+ *
+ * Get the Request URI of the Message.
+ */
+PHP_METHOD(HttpMessage, getRequestUri)
+{
+       zval *uri;
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       if (obj->message->type != HTTP_MSG_REQUEST) {
+               RETURN_NULL();
+       }
+
+       uri = GET_PROP(obj, requestUri);
+       RETURN_STRINGL(Z_STRVAL_P(uri), Z_STRLEN_P(uri), 1);
+}
+/* }}} */
+
+/* {{{ string HttpMessage::getHttpVersion()
+ *
+ * Get the HTTP Protocol Version of the Message.
+ */
+PHP_METHOD(HttpMessage, getHttpVersion)
+{
+       zval *version;
+       char ver[4] = {0};
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       version = GET_PROP(obj, httpVersion);
+
+       if (Z_TYPE_P(version) == IS_NULL) {
+               RETURN_NULL();
+       }
+       
+       sprintf(ver, "1.1f", Z_DVAL_P(version));
+       RETURN_STRINGL(ver, 3, 1);
+}
+/* }}} */
+
+/* {{{ string HttpMessage::toString()
+ *
+ * Get the string representation of the Message.
+ */
+PHP_METHOD(HttpMessage, toString)
+{
+       char *string;
+       size_t length;
+       getObject(http_message_object, obj);
+       
+       NO_ARGS;
+       
+       http_message_tostring(obj->message, &string, &length);
+       RETURN_STRINGL(string, length, 0);
+}
+/* }}} */
+
+/* }}} */
+
 #ifdef HTTP_HAVE_CURL
 /* {{{ HttpRequest */
 
@@ -527,22 +751,22 @@ PHP_METHOD(HttpRequest, __construct)
        long meth = -1;
        getObject(http_request_object, obj);
 
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
-               return;
-       }
-
-       INIT_PARR(obj, options);
-       INIT_PARR(obj, responseInfo);
-       INIT_PARR(obj, responseData);
-       INIT_PARR(obj, postData);
-       INIT_PARR(obj, postFiles);
+       SET_EH_THROW_HTTP();
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
+               INIT_PARR(obj, options);
+               INIT_PARR(obj, responseInfo);
+               INIT_PARR(obj, responseData);
+               INIT_PARR(obj, postData);
+               INIT_PARR(obj, postFiles);
 
-       if (URL) {
-               UPD_PROP(obj, string, url, URL);
-       }
-       if (meth > -1) {
-               UPD_PROP(obj, long, method, meth);
+               if (URL) {
+                       UPD_PROP(obj, string, url, URL);
+               }
+               if (meth > -1) {
+                       UPD_PROP(obj, long, method, meth);
+               }
        }
+       SET_EH_NORMAL();
 }
 /* }}} */
 
@@ -641,11 +865,74 @@ PHP_METHOD(HttpRequest, unsetOptions)
 }
 /* }}} */
 
-/* {{{ proto bool HttpRequest::addHeader(array header)
+/* {{{ proto bool HttpRequest::setSslOptions(array options)
+ *
+ * Set additional SSL options.
+ */
+PHP_METHOD(HttpRequest, setSslOptions)
+{
+       zval *opts, *old_opts, **ssl_options;
+       getObject(http_request_object, obj);
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
+               RETURN_FALSE;
+       }
+
+       old_opts = GET_PROP(obj, options);
+
+       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
+               array_merge(opts, *ssl_options);
+       } else {
+               zval_add_ref(&opts);
+               add_assoc_zval(old_opts, "ssl", opts);
+       }
+
+       RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto array HttpRequest::getSslOtpions()
+ *
+ * Get previously set SSL options.
+ */
+PHP_METHOD(HttpRequest, getSslOptions)
+{
+       zval *opts, **ssl_options;
+       getObject(http_request_object, obj);
+
+       NO_ARGS;
+
+       opts = GET_PROP(obj, options);
+
+       array_init(return_value);
+
+       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
+               array_copy(*ssl_options, return_value);
+       }
+}
+/* }}} */
+
+/* {{{ proto void HttpRequest::unsetSslOptions()
  *
- * Add (a) request header name/value pair(s).
+ * Unset previously set SSL options.
  */
-PHP_METHOD(HttpRequest, addHeader)
+PHP_METHOD(HttpRequest, unsetSslOptions)
+{
+       zval *opts;
+       getObject(http_request_object, obj);
+
+       NO_ARGS;
+
+       opts = GET_PROP(obj, options);
+       zend_hash_del(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"));
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::addHeaders(array headers)
+ *
+ * Add request header name/value pairs.
+ */
+PHP_METHOD(HttpRequest, addHeaders)
 {
        zval *opts, **headers, *new_headers;
        getObject(http_request_object, obj);
@@ -667,11 +954,48 @@ PHP_METHOD(HttpRequest, addHeader)
 }
 /* }}} */
 
-/* {{{ proto bool HttpRequest::addCookie(array cookie)
+/* {{{ proto array HttpRequest::getHeaders()
+ *
+ * Get previously set request headers.
+ */
+PHP_METHOD(HttpRequest, getHeaders)
+{
+       zval *opts, **headers;
+       getObject(http_request_object, obj);
+
+       NO_ARGS;
+
+       opts = GET_PROP(obj, options);
+
+       array_init(return_value);
+
+       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
+               array_copy(*headers, return_value);
+       }
+}
+/* }}} */
+
+/* {{{ proto void HttpRequest::unsetHeaders()
+ *
+ * Unset previously set request headers.
+ */
+PHP_METHOD(HttpRequest, unsetHeaders)
+{
+       zval *opts;
+       getObject(http_request_object, obj);
+
+       NO_ARGS;
+
+       opts = GET_PROP(obj, options);
+       zend_hash_del(Z_ARRVAL_P(opts), "headers", sizeof("headers"));
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::addCookies(array cookies)
  *
- * Add (a) cookie(s).
+ * Add cookies.
  */
-PHP_METHOD(HttpRequest, addCookie)
+PHP_METHOD(HttpRequest, addCookies)
 {
        zval *opts, **cookies, *new_cookies;
        getObject(http_request_object, obj);
@@ -693,6 +1017,42 @@ PHP_METHOD(HttpRequest, addCookie)
 }
 /* }}} */
 
+/* {{{ proto array HttpRequest::getCookies()
+ *
+ * Get previously set cookies.
+ */
+PHP_METHOD(HttpRequest, getCookies)
+{
+       zval *opts, **cookies;
+       getObject(http_request_object, obj);
+
+       NO_ARGS;
+
+       opts = GET_PROP(obj, options);
+
+       array_init(return_value);
+
+       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
+               array_copy(*cookies, return_value);
+       }
+}
+/* }}} */
+
+/* {{{ proto void HttpRequest::unsetCookies()
+ *
+ */
+PHP_METHOD(HttpRequest, unsetCookies)
+{
+       zval *opts;
+       getObject(http_request_object, obj);
+
+       NO_ARGS;
+
+       opts = GET_PROP(obj, options);
+       zend_hash_del(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"));
+}
+/* }}} */
+
 /* {{{ proto bool HttpRequest::setURL(string url)
  *
  * Set the request URL.
@@ -1066,7 +1426,7 @@ PHP_METHOD(HttpRequest, getResponseData)
 }
 /* }}} */
 
-/* {{{ proto string HttpRequest::getResponseHeader([string name])
+/* {{{ proto mixed HttpRequest::getResponseHeader([string name])
  *
  * Get response header(s) after the request has been sent.
  */
@@ -1095,6 +1455,94 @@ PHP_METHOD(HttpRequest, getResponseHeader)
                RETURN_FALSE;
        }
 }
+/* }}} */
+
+/* {{{ proto array HttpRequest::getResponseCookie([string name])
+ *
+ * Get response cookie(s) after the request has been sent.
+ */
+PHP_METHOD(HttpRequest, getResponseCookie)
+{
+       zval *data, **headers;
+       char *cookie_name = NULL;
+       int cookie_len = 0;
+       getObject(http_request_object, obj);
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
+               RETURN_FALSE;
+       }
+
+       array_init(return_value);
+
+       data = GET_PROP(obj, responseData);
+       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
+               ulong idx = 0;
+               char *key = NULL;
+               zval **header = NULL;
+
+               FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers), key, idx, header) {
+                       if (key && !strcasecmp(key, "Set-Cookie")) {
+                               /* several cookies? */
+                               if (Z_TYPE_PP(header) == IS_ARRAY) {
+                                       zval **cookie;
+
+                                       FOREACH_HASH_VAL(Z_ARRVAL_PP(header), cookie) {
+                                               zval *cookie_hash;
+                                               MAKE_STD_ZVAL(cookie_hash);
+                                               array_init(cookie_hash);
+
+                                               if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
+                                                       if (!cookie_len) {
+                                                               add_next_index_zval(return_value, cookie_hash);
+                                                       } else {
+                                                               zval **name;
+
+                                                               if (    (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
+                                                                               (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
+                                                                       add_next_index_zval(return_value, cookie_hash);
+                                                                       return; /* <<< FOUND >>> */
+                                                               } else {
+                                                                       zval_dtor(cookie_hash);
+                                                                       efree(cookie_hash);
+                                                               }
+                                                       }
+                                               } else {
+                                                       zval_dtor(cookie_hash);
+                                                       efree(cookie_hash);
+                                               }
+                                       }
+                               } else {
+                                       zval *cookie_hash;
+                                       MAKE_STD_ZVAL(cookie_hash);
+                                       array_init(cookie_hash);
+
+                                       if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
+                                               if (!cookie_len) {
+                                                       add_next_index_zval(return_value, cookie_hash);
+                                               } else {
+                                                       zval **name;
+
+                                                       if (    (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
+                                                                       (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
+                                                               add_next_index_zval(return_value, cookie_hash);
+                                                       } else {
+                                                               zval_dtor(cookie_hash);
+                                                               efree(cookie_hash);
+                                                       }
+                                               }
+                                       } else {
+                                               zval_dtor(cookie_hash);
+                                               efree(cookie_hash);
+                                       }
+                               }
+                               break;
+                       }
+                       /* reset key */
+                       key = NULL;
+               }
+       }
+}
+/* }}} */
 
 /* {{{ proto string HttpRequest::getResponseBody()
  *
@@ -1129,7 +1577,7 @@ PHP_METHOD(HttpRequest, getResponseCode)
 
        data = GET_PROP(obj, responseData);
        if (    (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &hdrs)) &&
-                       (SUCCESS == zend_hash_find(Z_ARRVAL_PP(hdrs), "Status", sizeof("Status"), (void **) &code))) {
+                       (SUCCESS == zend_hash_find(Z_ARRVAL_PP(hdrs), "Response Status", sizeof("Response Status"), (void **) &code))) {
                RETVAL_STRINGL(Z_STRVAL_PP(code), Z_STRLEN_PP(code), 1);
                convert_to_long(return_value);
        } else {
@@ -1209,6 +1657,8 @@ PHP_METHOD(HttpRequest, send)
 
        NO_ARGS;
 
+       SET_EH_THROW_HTTP();
+
        if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initilaize cURL");
                RETURN_FALSE;
@@ -1311,9 +1761,7 @@ PHP_METHOD(HttpRequest, send)
        efree(request_uri);
 
        /* final data handling */
-       if (status != SUCCESS) {
-               RETURN_FALSE;
-       } else {
+       if (status == SUCCESS) {
                char *body = NULL;
                size_t body_len = 0;
                zval *zheaders;
@@ -1335,7 +1783,9 @@ PHP_METHOD(HttpRequest, send)
 
                RETURN_TRUE;
        }
-       /* */
+
+       SET_EH_NORMAL();
+       RETURN_SUCCESS(status);
 }
 /* }}} */
 /* }}} */