- prepare release
[m6w6/ext-http] / http_request_method_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "php_http.h"
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_request_method_api.h"
27
28 #include "phpstr/phpstr.h"
29
30 ZEND_EXTERN_MODULE_GLOBALS(http);
31
32 /* {{{ char *http_request_methods[] */
33 static const char *const http_request_methods[] = {
34 "UNKOWN",
35 /* HTTP/1.1 */
36 "GET",
37 "HEAD",
38 "POST",
39 "PUT",
40 "DELETE",
41 "OPTIONS",
42 "TRACE",
43 "CONNECT",
44 /* WebDAV - RFC 2518 */
45 "PROPFIND",
46 "PROPPATCH",
47 "MKCOL",
48 "COPY",
49 "MOVE",
50 "LOCK",
51 "UNLOCK",
52 /* WebDAV Versioning - RFC 3253 */
53 "VERSION-CONTROL",
54 "REPORT",
55 "CHECKOUT",
56 "CHECKIN",
57 "UNCHECKOUT",
58 "MKWORKSPACE",
59 "UPDATE",
60 "LABEL",
61 "MERGE",
62 "BASELINE-CONTROL",
63 "MKACTIVITY",
64 /* WebDAV Access Control - RFC 3744 */
65 "ACL",
66 NULL
67 };
68 /* }}} */
69
70 /* {{{ char *http_request_method_name(http_request_method) */
71 PHP_HTTP_API const char *_http_request_method_name(http_request_method m TSRMLS_DC)
72 {
73 zval **meth;
74
75 if (HTTP_STD_REQUEST_METHOD(m)) {
76 return http_request_methods[m];
77 }
78
79 if (SUCCESS == zend_hash_index_find(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(m), (void **) &meth)) {
80 return Z_STRVAL_PP(meth);
81 }
82
83 return http_request_methods[0];
84 }
85 /* }}} */
86
87 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
88 PHP_HTTP_API unsigned long _http_request_method_exists(zend_bool by_name, unsigned long id, const char *name TSRMLS_DC)
89 {
90 if (by_name) {
91 unsigned i;
92
93 for (i = HTTP_NO_REQUEST_METHOD + 1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
94 if (!strcmp(name, http_request_methods[i])) {
95 return i;
96 }
97 }
98 {
99 zval **data;
100 char *key;
101 ulong idx;
102
103 FOREACH_HASH_KEYVAL(&HTTP_G(request).methods.custom, key, idx, data) {
104 if (!strcmp(name, Z_STRVAL_PP(data))) {
105 return idx + HTTP_MAX_REQUEST_METHOD;
106 }
107 }
108 }
109 return 0;
110 } else {
111 return HTTP_STD_REQUEST_METHOD(id) || zend_hash_index_exists(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(id)) ? id : 0;
112 }
113 }
114 /* }}} */
115
116 /* {{{ unsigned long http_request_method_register(char *) */
117 PHP_HTTP_API unsigned long _http_request_method_register(const char *method TSRMLS_DC)
118 {
119 zval array;
120 char *http_method;
121 unsigned long meth_num = HTTP_G(request).methods.custom.nNextFreeElement + HTTP_MAX_REQUEST_METHOD;
122
123 Z_ARRVAL(array) = &HTTP_G(request).methods.custom;
124 add_next_index_string(&array, estrdup(method), 0);
125
126 spprintf(&http_method, 0, "HTTP_%s", method);
127 zend_register_long_constant(http_method, strlen(http_method) + 1, meth_num, CONST_CS, http_module_number TSRMLS_CC);
128 efree(http_method);
129
130 return meth_num;
131 }
132 /* }}} */
133
134 /* {{{ STATUS http_request_method_unregister(usngigned long) */
135 PHP_HTTP_API STATUS _http_request_method_unregister(unsigned long method TSRMLS_DC)
136 {
137 zval **zmethod;
138 char *http_method;
139
140 if (SUCCESS != zend_hash_index_find(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(method), (void **) &zmethod)) {
141 http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Request method with id %lu does not exist", method);
142 return FAILURE;
143 }
144
145 spprintf(&http_method, 0, "HTTP_%s", Z_STRVAL_PP(zmethod));
146
147 if ( (SUCCESS != zend_hash_index_del(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(method)))
148 || (SUCCESS != zend_hash_del(EG(zend_constants), http_method, strlen(http_method) + 1))) {
149 http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Could not unregister request method: %s", http_method);
150 efree(http_method);
151 return FAILURE;
152 }
153
154 efree(http_method);
155 return SUCCESS;
156 }
157 /* }}} */
158
159 /*
160 * Local variables:
161 * tab-width: 4
162 * c-basic-offset: 4
163 * End:
164 * vim600: noet sw=4 ts=4 fdm=marker
165 * vim<600: noet sw=4 ts=4
166 */
167