* typo
[m6w6/ext-http] / http_methods.c
index 522c2a36a7c8a53c7b2f3c8aeff88847b63a3462..0f80c6ce21e3441e3772523953ebf1de35ac689b 100644 (file)
@@ -15,9 +15,6 @@
 
 /* $Id$ */
 
-#define _WINSOCKAPI_
-#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
-
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
@@ -631,6 +628,73 @@ PHP_METHOD(HTTPi_Request, getOptions)
 }
 /* }}} */
 
+/* {{{ proto void HTTPi_Request::unsetOptions()
+ *
+ * Unset all options/headers/cookies.
+ */
+PHP_METHOD(HTTPi_Request, unsetOptions)
+{
+       getObject(httpi_request_object, obj);
+
+       NO_ARGS;
+
+       FREE_PARR(obj, options);
+       INIT_PARR(obj, options);
+}
+/* }}} */
+
+/* {{{ proto bool HTTPi_Request::addHeader(array header)
+ *
+ * Add (a) request header name/value pair(s).
+ */
+PHP_METHOD(HTTPi_Request, addHeader)
+{
+       zval *opts, **headers, *new_headers;
+       getObject(httpi_request_object, obj);
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
+               RETURN_FALSE;
+       }
+
+       opts = GET_PROP(obj, options);
+
+       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
+               array_merge(new_headers, *headers);
+       } else {
+               zval_add_ref(&new_headers);
+               add_assoc_zval(opts, "headers", new_headers);
+       }
+
+       RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto bool HTTPi_Request::addCookie(array cookie)
+ *
+ * Add (a) cookie(s).
+ */
+PHP_METHOD(HTTPi_Request, addCookie)
+{
+       zval *opts, **cookies, *new_cookies;
+       getObject(httpi_request_object, obj);
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_cookies)) {
+               RETURN_FALSE;
+       }
+
+       opts = GET_PROP(obj, options);
+
+       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
+               array_merge(new_cookies, *cookies);
+       } else {
+               zval_add_ref(&new_cookies);
+               add_assoc_zval(opts, "cookies", new_cookies);
+       }
+
+       RETURN_TRUE;
+}
+/* }}} */
+
 /* {{{ proto bool HTTPi_Request::setURL(string url)
  *
  * Set the request URL.
@@ -989,7 +1053,7 @@ PHP_METHOD(HTTPi_Request, unsetPostFiles)
 
 /* {{{ proto array HTTPi_Request::getResponseData()
  *
- * Get all resposonse data after the request has been sent.
+ * Get all response data after the request has been sent.
  */
 PHP_METHOD(HTTPi_Request, getResponseData)
 {
@@ -1014,16 +1078,16 @@ PHP_METHOD(HTTPi_Request, getResponseHeader)
        char *header_name = NULL;
        int header_len = 0;
        getObject(httpi_response_object, obj);
-       
+
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
                RETURN_FALSE;
        }
-       
+
        data = GET_PROP(obj, responseData);
        if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
                RETURN_FALSE;
        }
-       
+
        if (!header_len || !header_name) {
                array_init(return_value);
                array_copy(*headers, return_value);
@@ -1062,9 +1126,9 @@ PHP_METHOD(HTTPi_Request, getResponseCode)
 {
        zval **code, **hdrs, *data;
        getObject(httpi_request_object, obj);
-       
+
        NO_ARGS;
-       
+
        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))) {
@@ -1093,7 +1157,7 @@ PHP_METHOD(HTTPi_Request, getResponseInfo)
        }
 
        info = GET_PROP(obj, responseInfo);
-       
+
        if (info_len && info_name) {
                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);
@@ -1111,6 +1175,31 @@ PHP_METHOD(HTTPi_Request, getResponseInfo)
 /* {{{ proto bool HTTPi_Request::send()
  *
  * Send the HTTP request.
+ *
+ * GET example:
+ * <pre>
+ * <?php
+ * $r = new HTTPi_Request('http://example.com/feed.rss', HTTP_GET);
+ * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
+ * $r->addQueryData(array('category' => 3));
+ * if ($r->send() && $r->getResponseCode() == 200) {
+ *     file_put_contents('local.rss', $r->getResponseBody());
+ * }
+ * ?>
+ * </pre>
+ *
+ * POST example:
+ * <pre>
+ * <?php
+ * $r = new HTTPi_Request('http://example.com/form.php', HTTP_POST);
+ * $r->setOptions(array('cookies' => array('lang' => 'de')));
+ * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
+ * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
+ * if ($r->send()) {
+ *     echo $r->getResponseBody();
+ * }
+ * ?>
+ * </pre>
  */
 PHP_METHOD(HTTPi_Request, send)
 {