- renamed response.status to response.code
[m6w6/ext-http] / http_methods.c
index 1c3b99c599528e2d7d10eb9add79a62680657c3a..f43141bbc58ba638cee039245cde6ee2b987307e 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);
@@ -512,6 +521,82 @@ PHP_METHOD(HttpResponse, send)
 /* }}} */
 /* }}} */
 
+/* {{{ HttpMessage */
+
+/* {{{ 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);
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/", &message)) {
+               return;
+       }
+
+       if (message) {
+               convert_to_string(message);
+               SET_PROP(obj, raw, message);
+       }
+}
+/* }}} */
+
+/* {{{ 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);
+}
+/* }}} */
+
+/* }}} */
+
 #ifdef HTTP_HAVE_CURL
 /* {{{ HttpRequest */
 
@@ -527,22 +612,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;
-       }
+       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);
 
-       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();
 }
 /* }}} */
 
@@ -1353,7 +1438,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 {
@@ -1433,6 +1518,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;
@@ -1535,9 +1622,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;
@@ -1559,7 +1644,9 @@ PHP_METHOD(HttpRequest, send)
 
                RETURN_TRUE;
        }
-       /* */
+
+       SET_EH_NORMAL();
+       RETURN_SUCCESS(status);
 }
 /* }}} */
 /* }}} */