- add 'range' request option
[m6w6/ext-http] / http_request_object.c
index 87bfb65ce5a1187126f7c706d7bd945cac90f5c5..9b0dd91f12d3652160ff3fae37a8712880733c67 100644 (file)
@@ -419,7 +419,9 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D)
        /* WebDAV Access Control - RFC 3744 */
        DCL_CONST(long, "METH_ACL", HTTP_ACL);
 
-       /* cURL HTTP protocol versions */
+       /*
+        * HTTP Protocol Version Constants
+        */
        DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0);
        DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1);
        DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE);
@@ -431,6 +433,15 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D)
        DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST);
        DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM);
        DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY);
+       
+       /*
+        * Proxy Type Constants
+        */
+#      if HTTP_CURL_VERSION(7,15,2)
+       DCL_CONST(long, "PROXY_SOCKS4", CURLPROXY_SOCKS4);
+#      endif
+       DCL_CONST(long, "PROXY_SOCKS5", CURLPROXY_SOCKS5);
+       DCL_CONST(long, "PROXY_HTTP", CURLPROXY_HTTP);
 #endif /* WONKY */
 }
 
@@ -453,15 +464,32 @@ static inline void _http_request_object_check_request_content_type(zval *this_pt
                                
        if (Z_STRLEN_P(ctype)) {
                zval **headers, *opts = GET_PROP(options);
-                                       
+               
                if (    (Z_TYPE_P(opts) == IS_ARRAY) &&
                                (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) && 
                                (Z_TYPE_PP(headers) == IS_ARRAY)) {
                        zval **ct_header;
-                                               
+                       
                        /* only override if not already set */
                        if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void **) &ct_header))) {
                                add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
+                       } else
+                       /* or not a string, zero length string or a string of spaces */
+                       if ((Z_TYPE_PP(ct_header) != IS_STRING) || !Z_STRLEN_PP(ct_header)) {
+                               add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
+                       } else {
+                               int i, only_space = 1;
+                               
+                               /* check for spaces only */
+                               for (i = 0; i < Z_STRLEN_PP(ct_header); ++i) {
+                                       if (!isspace(Z_STRVAL_PP(ct_header)[i])) {
+                                               only_space = 0;
+                                               break;
+                                       }
+                               }
+                               if (only_space) {
+                                       add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
+                               }
                        }
                } else {
                        zval *headers;
@@ -679,11 +707,19 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
        return ret;
 }
 
-#define http_request_object_set_options_subr(key, ow) \
-       _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
-static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite)
+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;
+       zval *old_opts, *new_opts, *opts = NULL, **entry = NULL;
 
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
                RETURN_FALSE;
@@ -696,6 +732,9 @@ static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM
                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));
@@ -868,7 +907,7 @@ PHP_METHOD(HttpRequest, getOptions)
  */
 PHP_METHOD(HttpRequest, setSslOptions)
 {
-       http_request_object_set_options_subr("ssl", 1);
+       http_request_object_set_options_subr("ssl", 1, 0);
 }
 /* }}} */
 
@@ -882,7 +921,7 @@ PHP_METHOD(HttpRequest, setSslOptions)
  */
 PHP_METHOD(HttpRequest, addSslOptions)
 {
-       http_request_object_set_options_subr("ssl", 0);
+       http_request_object_set_options_subr("ssl", 0, 0);
 }
 /* }}} */
 
@@ -909,7 +948,7 @@ PHP_METHOD(HttpRequest, getSslOptions)
  */
 PHP_METHOD(HttpRequest, addHeaders)
 {
-       http_request_object_set_options_subr("headers", 0);
+       http_request_object_set_options_subr("headers", 0, 1);
 }
 
 /* {{{ proto bool HttpRequest::setHeaders([array headers])
@@ -923,7 +962,7 @@ PHP_METHOD(HttpRequest, addHeaders)
  */
 PHP_METHOD(HttpRequest, setHeaders)
 {
-       http_request_object_set_options_subr("headers", 1);
+       http_request_object_set_options_subr("headers", 1, 1);
 }
 /* }}} */
 
@@ -950,7 +989,7 @@ PHP_METHOD(HttpRequest, getHeaders)
  */
 PHP_METHOD(HttpRequest, setCookies)
 {
-       http_request_object_set_options_subr("cookies", 1);
+       http_request_object_set_options_subr("cookies", 1, 0);
 }
 /* }}} */
 
@@ -965,7 +1004,7 @@ PHP_METHOD(HttpRequest, setCookies)
  */
 PHP_METHOD(HttpRequest, addCookies)
 {
-       http_request_object_set_options_subr("cookies", 0);
+       http_request_object_set_options_subr("cookies", 0, 0);
 }
 /* }}} */
 
@@ -1076,7 +1115,9 @@ PHP_METHOD(HttpRequest, setContentType)
                RETURN_FALSE;
        }
 
-       HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
+       if (ct_len) {
+               HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
+       }
        UPD_STRL(contentType, ctype, ct_len);
        RETURN_TRUE;
 }
@@ -1535,8 +1576,6 @@ PHP_METHOD(HttpRequest, addPutData)
        }
        
        if (data_len) {
-               char *new_data;
-               size_t new_data_len;
                zval *data = GET_PROP(putData);
                
                if (Z_STRLEN_P(data)) {