- Fixed build on php-trunk
[m6w6/ext-http] / http_request_method_api.c
index 39c9aceb526a489bcce1d11c581d030e66dac131..5be28c17add665c621eac813d2e7fccc323bf1dd 100644 (file)
@@ -1,41 +1,31 @@
 /*
-   +----------------------------------------------------------------------+
-   | PECL :: http                                                         |
-   +----------------------------------------------------------------------+
-   | This source file is subject to version 3.0 of the PHP license, that  |
-   | is bundled with this package in the file LICENSE, and is available   |
-   | through the world-wide-web at http://www.php.net/license/3_0.txt.    |
-   | If you did not receive a copy of the PHP license and are unable to   |
-   | obtain it through the world-wide-web, please send a note to          |
-   | license@php.net so we can mail you a copy immediately.               |
-   +----------------------------------------------------------------------+
-   | Copyright (c) 2004-2005 Michael Wallner <mike@php.net>               |
-   +----------------------------------------------------------------------+
+    +--------------------------------------------------------------------+
+    | PECL :: http                                                       |
+    +--------------------------------------------------------------------+
+    | Redistribution and use in source and binary forms, with or without |
+    | modification, are permitted provided that the conditions mentioned |
+    | in the accompanying LICENSE file are met.                          |
+    +--------------------------------------------------------------------+
+    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
+    +--------------------------------------------------------------------+
 */
 
 /* $Id$ */
 
-#ifdef HAVE_CONFIG_H
-#      include "config.h"
-#endif
-#include "php.h"
-
+#define HTTP_WANT_CURL
 #include "php_http.h"
-#include "php_http_std_defs.h"
+
 #include "php_http_api.h"
+#include "php_http_request_api.h"
 #include "php_http_request_method_api.h"
-#if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
+
+#if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL) && !defined(WONKY)
 #      include "php_http_request_object.h"
 #endif
 
-#include "missing.h"
-#include "phpstr/phpstr.h"
-
-ZEND_EXTERN_MODULE_GLOBALS(http);
-
 /* {{{ char *http_request_methods[] */
 static const char *const http_request_methods[] = {
-       "UNKOWN",
+       "UNKNOWN",
        /* HTTP/1.1 */
        "GET",
        "HEAD",
@@ -71,7 +61,8 @@ static const char *const http_request_methods[] = {
 };
 /* }}} */
 
-STATUS _http_request_method_global_init(INIT_FUNC_ARGS)
+/* {{{ */
+PHP_MINIT_FUNCTION(http_request_method)
 {
        /* HTTP/1.1 */
        HTTP_LONG_CONSTANT("HTTP_METH_GET", HTTP_GET);
@@ -108,117 +99,216 @@ STATUS _http_request_method_global_init(INIT_FUNC_ARGS)
        return SUCCESS;
 }
 
-/* {{{ char *http_request_method_name(http_request_method) */
-PHP_HTTP_API const char *_http_request_method_name(http_request_method m TSRMLS_DC)
+static void free_method(void *el)
 {
-       zval **meth;
+       efree(*(char **)el);
+}
 
-       if (HTTP_STD_REQUEST_METHOD(m)) {
-               return http_request_methods[m];
+static void unregister_method(const char *name TSRMLS_DC)
+{
+       char *ptr, tmp[sizeof("HTTP_METH_") + HTTP_REQUEST_METHOD_MAXLEN] = "HTTP_METH_";
+       
+       strlcpy(tmp + lenof("HTTP_METH_"), name, HTTP_REQUEST_METHOD_MAXLEN);
+       for (ptr = tmp + lenof("HTTP_METH_"); *ptr; ++ptr) {
+               if (*ptr == '-') {
+                       *ptr = '_';
+               }
        }
-
-       if (SUCCESS == zend_hash_index_find(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(m), (void **) &meth)) {
-               return Z_STRVAL_PP(meth);
+       
+#if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL) && !defined(WONKY)
+       if (SUCCESS != zend_hash_del(&http_request_object_ce->constants_table, tmp + lenof("HTTP_"), strlen(tmp + lenof("HTTP_")) + 1)) {
+               http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Could not unregister request method: HttpRequest::%s", tmp + lenof("HTTP_"));
+       }
+#endif
+       if (SUCCESS != zend_hash_del(EG(zend_constants), tmp, strlen(tmp) + 1)) {
+               http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Could not unregister request method: %s", tmp);
        }
-
-       return http_request_methods[0];
 }
-/* }}} */
 
-/* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
-PHP_HTTP_API unsigned long _http_request_method_exists(zend_bool by_name, unsigned long id, const char *name TSRMLS_DC)
+PHP_RINIT_FUNCTION(http_request_method)
 {
-       if (by_name) {
-               unsigned i;
-
-               for (i = HTTP_NO_REQUEST_METHOD + 1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
-                       if (!strcmp(name, http_request_methods[i])) {
-                               return i;
+       HashTable ht;
+       
+       zend_hash_init(&HTTP_G->request.methods.registered, 0, NULL, free_method, 0);
+#define HTTP_METH_REG(m) \
+       { \
+               char *_m=estrdup(m); \
+               zend_hash_next_index_insert(&HTTP_G->request.methods.registered, (void *) &_m, sizeof(char *), NULL); \
+       }
+       HTTP_METH_REG("UNKNOWN");
+       /* HTTP/1.1 */
+       HTTP_METH_REG("GET");
+       HTTP_METH_REG("HEAD");
+       HTTP_METH_REG("POST");
+       HTTP_METH_REG("PUT");
+       HTTP_METH_REG("DELETE");
+       HTTP_METH_REG("OPTIONS");
+       HTTP_METH_REG("TRACE");
+       HTTP_METH_REG("CONNECT");
+       /* WebDAV - RFC 2518 */
+       HTTP_METH_REG("PROPFIND");
+       HTTP_METH_REG("PROPPATCH");
+       HTTP_METH_REG("MKCOL");
+       HTTP_METH_REG("COPY");
+       HTTP_METH_REG("MOVE");
+       HTTP_METH_REG("LOCK");
+       HTTP_METH_REG("UNLOCK");
+       /* WebDAV Versioning - RFC 3253 */
+       HTTP_METH_REG("VERSION-CONTROL");
+       HTTP_METH_REG("REPORT");
+       HTTP_METH_REG("CHECKOUT");
+       HTTP_METH_REG("CHECKIN");
+       HTTP_METH_REG("UNCHECKOUT");
+       HTTP_METH_REG("MKWORKSPACE");
+       HTTP_METH_REG("UPDATE");
+       HTTP_METH_REG("LABEL");
+       HTTP_METH_REG("MERGE");
+       HTTP_METH_REG("BASELINE-CONTROL");
+       HTTP_METH_REG("MKACTIVITY");
+       /* WebDAV Access Control - RFC 3744 */
+       HTTP_METH_REG("ACL");
+       
+       zend_hash_init(&ht, 0, NULL, ZVAL_PTR_DTOR, 0);
+       if (*HTTP_G->request.methods.custom && SUCCESS == http_parse_params(HTTP_G->request.methods.custom, HTTP_PARAMS_DEFAULT, &ht)) {
+               HashPosition pos;
+               zval **val;
+               
+               FOREACH_HASH_VAL(pos, &ht, val) {
+                       if (Z_TYPE_PP(val) == IS_STRING) {
+                               http_request_method_register(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
                        }
                }
-               {
-                       zval **data;
-                       char *key;
-                       ulong idx;
-
-                       FOREACH_HASH_KEYVAL(&HTTP_G(request).methods.custom, key, idx, data) {
-                               if (!strcmp(name, Z_STRVAL_PP(data))) {
-                                       return idx + HTTP_MAX_REQUEST_METHOD;
+       }
+       zend_hash_destroy(&ht);
+       
+       return SUCCESS;
+}
+
+PHP_RSHUTDOWN_FUNCTION(http_request_method)
+{
+       char **name;
+       int i, c = zend_hash_next_free_element(&HTTP_G->request.methods.registered);
+       
+       for (i = HTTP_MAX_REQUEST_METHOD; i < c; ++i) {
+               if (SUCCESS == zend_hash_index_find(&HTTP_G->request.methods.registered, i, (void *) &name)) {
+                       unregister_method(*name TSRMLS_CC);
+               }
+       }
+       
+       zend_hash_destroy(&HTTP_G->request.methods.registered);
+       return SUCCESS;
+}
+
+#define http_request_method_cncl(m, c) _http_request_method_cncl_ex((m), strlen(m), (c) TSRMLS_CC)
+#define http_request_method_cncl_ex(m, l, c) _http_request_method_cncl_ex((m), (l), (c) TSRMLS_CC)
+static STATUS _http_request_method_cncl_ex(const char *method_name, int method_name_len, char **cnst TSRMLS_DC)
+{
+       int i;
+       char *cncl;
+       
+       if (method_name_len >= HTTP_REQUEST_METHOD_MAXLEN) {
+               http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Request method too long (%s)", method_name);
+       }
+       cncl = emalloc(method_name_len + 1);
+       
+       for (i = 0; i < method_name_len; ++i) {
+               switch (method_name[i]) {
+                       case '-':
+                               cncl[i] = '-';
+                               break;
+                       
+                       default:
+                               if (!HTTP_IS_CTYPE(alnum, method_name[i])) {
+                                       efree(cncl);
+                                       http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Request method contains illegal characters (%s)", method_name);
+                                       return FAILURE;
                                }
-                       }
+                               cncl[i] = HTTP_TO_CTYPE(upper, method_name[i]);
+                               break;
                }
-               return 0;
-       } else {
-               return HTTP_STD_REQUEST_METHOD(id) || zend_hash_index_exists(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(id)) ? id : 0;
        }
+       cncl[method_name_len] = '\0';
+       
+       *cnst = cncl;
+       return SUCCESS;
 }
-/* }}} */
 
-/* {{{ unsigned long http_request_method_register(char *) */
-PHP_HTTP_API unsigned long _http_request_method_register(const char *method_name TSRMLS_DC)
+PHP_HTTP_API const char *_http_request_method_name(http_request_method m TSRMLS_DC)
 {
-       zval array;
-       char *http_method, *method;
-       int i, method_len = strlen(method_name);
-       unsigned long meth_num = HTTP_G(request).methods.custom.nNextFreeElement + HTTP_MAX_REQUEST_METHOD;
-
-       method = emalloc(method_len + 1);
-       for (i = 0; i < method_len; ++i) {
-               method[i] = toupper(method_name[i]);
+       char **name;
+       
+       if (SUCCESS == zend_hash_index_find(&HTTP_G->request.methods.registered, m, (void *) &name)) {
+               return *name;
        }
-       method[method_len] = '\0';
+       return "UNKNOWN";
+}
+
+PHP_HTTP_API int _http_request_method_exists(int by_name, http_request_method id_num, const char *id_str TSRMLS_DC)
+{
+       char *id_dup;
        
-       INIT_ZARR(array, &HTTP_G(request).methods.custom);
-       add_next_index_stringl(&array, method, method_len, 0);
+       if (by_name && (SUCCESS == http_request_method_cncl(id_str, &id_dup))) {
+               char **name;
+               HashPosition pos;
+               HashKey key = initHashKey(0);
+               
+               FOREACH_HASH_KEYVAL(pos, &HTTP_G->request.methods.registered, key, name) {
+                       if (key.type == HASH_KEY_IS_LONG && !strcmp(*name, id_dup)) {
+                               efree(id_dup);
+                               return key.num;
+                       }
+               }
+               efree(id_dup);
+       } else if (zend_hash_index_exists(&HTTP_G->request.methods.registered, id_num)){
+               return id_num;
+       }
+       return 0;
+}
 
-       method_len = spprintf(&http_method, 0, "HTTP_METH_%s", method);
-       zend_register_long_constant(http_method, method_len + 1, meth_num, CONST_CS, http_module_number TSRMLS_CC);
-       efree(http_method);
+PHP_HTTP_API int _http_request_method_register(const char *method_str, int method_len TSRMLS_DC)
+{
+       char *method_dup, *ptr, tmp[sizeof("HTTP_METH_") + HTTP_REQUEST_METHOD_MAXLEN] = "HTTP_METH_";
+       int method_num = http_request_method_exists(1, 0, method_str);
        
+       if (!method_num && (SUCCESS == http_request_method_cncl_ex(method_str, method_len, &method_dup))) {
+               method_num = zend_hash_next_free_element(&HTTP_G->request.methods.registered);
+               zend_hash_index_update(&HTTP_G->request.methods.registered, method_num, (void *) &method_dup, sizeof(char *), NULL);
+               
+               strlcpy(tmp + lenof("HTTP_METH_"), method_dup, HTTP_REQUEST_METHOD_MAXLEN);
+               for (ptr = tmp + lenof("HTTP_METH_"); *ptr; ++ptr) {
+                       if (*ptr == '-') {
+                               *ptr = '_';
+                       }
+               }
+               
+               zend_register_long_constant(tmp, strlen(tmp) + 1, method_num, CONST_CS, http_module_number TSRMLS_CC);
 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL) && !defined(WONKY)
-       method_len = spprintf(&http_method, 0, "METH_%s", method);
-       zend_declare_class_constant_long(http_request_object_ce, http_method, method_len, meth_num TSRMLS_CC);
-       efree(http_method);
+               zend_declare_class_constant_long(http_request_object_ce, tmp + lenof("HTTP_"), strlen(tmp + lenof("HTTP_")), method_num TSRMLS_CC);
 #endif
-
-       return meth_num;
+       }
+       
+       return method_num;
 }
-/* }}} */
 
-/* {{{ STATUS http_request_method_unregister(usngigned long) */
-PHP_HTTP_API STATUS _http_request_method_unregister(unsigned long method TSRMLS_DC)
+PHP_HTTP_API STATUS _http_request_method_unregister(int method TSRMLS_DC)
 {
-       zval **zmethod;
-       char *http_method;
-       int method_len;
-
-       if (SUCCESS != zend_hash_index_find(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(method), (void **) &zmethod)) {
-               http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Request method with id %lu does not exist", method);
+       char **name;
+       
+       if (HTTP_STD_REQUEST_METHOD(method)) {
+               http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Standard request methods cannot be unregistered");
                return FAILURE;
        }
 
-#if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL) && !defined(WONKY)
-       method_len = spprintf(&http_method, 0, "METH_%s", Z_STRVAL_PP(zmethod));
-       if ((SUCCESS != zend_hash_del(&http_request_object_ce->constants_table, http_method, method_len + 1))) {
-               http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Could not unregister request method: HttpRequest::%s", http_method);
-               efree(http_method);
+       if (SUCCESS != zend_hash_index_find(&HTTP_G->request.methods.registered, method, (void *) &name)) {
+               http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Custom request method with id %d does not exist", method);
                return FAILURE;
        }
-       efree(http_method);
-#endif
        
-       method_len = spprintf(&http_method, 0, "HTTP_METH_%s", Z_STRVAL_PP(zmethod));
-       if (    (SUCCESS != zend_hash_index_del(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(method)))
-               ||      (SUCCESS != zend_hash_del(EG(zend_constants), http_method, method_len + 1))) {
-               http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Could not unregister request method: %s", http_method);
-               efree(http_method);
-               return FAILURE;
-       }
-       efree(http_method);
+       unregister_method(*name TSRMLS_CC);
        
+       zend_hash_index_del(&HTTP_G->request.methods.registered, method);
        return SUCCESS;
 }
-/* }}} */
 
 /*
  * Local variables: