- ditch warnings
[m6w6/ext-http] / http_request_method_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_CURL
16 #include "php_http.h"
17
18 #include "php_http_api.h"
19 #include "php_http_request_api.h"
20 #include "php_http_request_method_api.h"
21
22 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL) && !defined(WONKY)
23 # include "php_http_request_object.h"
24 #endif
25
26 /* {{{ char *http_request_methods[] */
27 static const char *const http_request_methods[] = {
28 "UNKNOWN",
29 /* HTTP/1.1 */
30 "GET",
31 "HEAD",
32 "POST",
33 "PUT",
34 "DELETE",
35 "OPTIONS",
36 "TRACE",
37 "CONNECT",
38 /* WebDAV - RFC 2518 */
39 "PROPFIND",
40 "PROPPATCH",
41 "MKCOL",
42 "COPY",
43 "MOVE",
44 "LOCK",
45 "UNLOCK",
46 /* WebDAV Versioning - RFC 3253 */
47 "VERSION-CONTROL",
48 "REPORT",
49 "CHECKOUT",
50 "CHECKIN",
51 "UNCHECKOUT",
52 "MKWORKSPACE",
53 "UPDATE",
54 "LABEL",
55 "MERGE",
56 "BASELINE-CONTROL",
57 "MKACTIVITY",
58 /* WebDAV Access Control - RFC 3744 */
59 "ACL",
60 NULL
61 };
62 /* }}} */
63
64 /* {{{ */
65 PHP_MINIT_FUNCTION(http_request_method)
66 {
67 /* HTTP/1.1 */
68 HTTP_LONG_CONSTANT("HTTP_METH_GET", HTTP_GET);
69 HTTP_LONG_CONSTANT("HTTP_METH_HEAD", HTTP_HEAD);
70 HTTP_LONG_CONSTANT("HTTP_METH_POST", HTTP_POST);
71 HTTP_LONG_CONSTANT("HTTP_METH_PUT", HTTP_PUT);
72 HTTP_LONG_CONSTANT("HTTP_METH_DELETE", HTTP_DELETE);
73 HTTP_LONG_CONSTANT("HTTP_METH_OPTIONS", HTTP_OPTIONS);
74 HTTP_LONG_CONSTANT("HTTP_METH_TRACE", HTTP_TRACE);
75 HTTP_LONG_CONSTANT("HTTP_METH_CONNECT", HTTP_CONNECT);
76 /* WebDAV - RFC 2518 */
77 HTTP_LONG_CONSTANT("HTTP_METH_PROPFIND", HTTP_PROPFIND);
78 HTTP_LONG_CONSTANT("HTTP_METH_PROPPATCH", HTTP_PROPPATCH);
79 HTTP_LONG_CONSTANT("HTTP_METH_MKCOL", HTTP_MKCOL);
80 HTTP_LONG_CONSTANT("HTTP_METH_COPY", HTTP_COPY);
81 HTTP_LONG_CONSTANT("HTTP_METH_MOVE", HTTP_MOVE);
82 HTTP_LONG_CONSTANT("HTTP_METH_LOCK", HTTP_LOCK);
83 HTTP_LONG_CONSTANT("HTTP_METH_UNLOCK", HTTP_UNLOCK);
84 /* WebDAV Versioning - RFC 3253 */
85 HTTP_LONG_CONSTANT("HTTP_METH_VERSION_CONTROL", HTTP_VERSION_CONTROL);
86 HTTP_LONG_CONSTANT("HTTP_METH_REPORT", HTTP_REPORT);
87 HTTP_LONG_CONSTANT("HTTP_METH_CHECKOUT", HTTP_CHECKOUT);
88 HTTP_LONG_CONSTANT("HTTP_METH_CHECKIN", HTTP_CHECKIN);
89 HTTP_LONG_CONSTANT("HTTP_METH_UNCHECKOUT", HTTP_UNCHECKOUT);
90 HTTP_LONG_CONSTANT("HTTP_METH_MKWORKSPACE", HTTP_MKWORKSPACE);
91 HTTP_LONG_CONSTANT("HTTP_METH_UPDATE", HTTP_UPDATE);
92 HTTP_LONG_CONSTANT("HTTP_METH_LABEL", HTTP_LABEL);
93 HTTP_LONG_CONSTANT("HTTP_METH_MERGE", HTTP_MERGE);
94 HTTP_LONG_CONSTANT("HTTP_METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL);
95 HTTP_LONG_CONSTANT("HTTP_METH_MKACTIVITY", HTTP_MKACTIVITY);
96 /* WebDAV Access Control - RFC 3744 */
97 HTTP_LONG_CONSTANT("HTTP_METH_ACL", HTTP_ACL);
98
99 return SUCCESS;
100 }
101
102 PHP_RINIT_FUNCTION(http_request_method)
103 {
104 HTTP_G->request.methods.custom.entries = ecalloc(1, sizeof(http_request_method_entry *));
105
106 return SUCCESS;
107 }
108
109 PHP_RSHUTDOWN_FUNCTION(http_request_method)
110 {
111 int i;
112 http_request_method_entry **ptr = HTTP_G->request.methods.custom.entries;
113
114 for (i = 0; i < HTTP_G->request.methods.custom.count; ++i) {
115 if (ptr[i]) {
116 http_request_method_unregister(HTTP_CUSTOM_REQUEST_METHOD_START + i);
117 }
118 }
119 efree(HTTP_G->request.methods.custom.entries);
120
121 return SUCCESS;
122 }
123 /* }}} */
124
125 /* {{{ char *http_request_method_name(http_request_method) */
126 PHP_HTTP_API const char *_http_request_method_name(http_request_method m TSRMLS_DC)
127 {
128 http_request_method_entry **ptr = HTTP_G->request.methods.custom.entries;
129
130 if (HTTP_STD_REQUEST_METHOD(m)) {
131 return http_request_methods[m];
132 }
133
134 if ( (HTTP_CUSTOM_REQUEST_METHOD(m) >= 0) &&
135 (HTTP_CUSTOM_REQUEST_METHOD(m) < HTTP_G->request.methods.custom.count) &&
136 (ptr[HTTP_CUSTOM_REQUEST_METHOD(m)])) {
137 return ptr[HTTP_CUSTOM_REQUEST_METHOD(m)]->name;
138 }
139
140 return http_request_methods[0];
141 }
142 /* }}} */
143
144 /* {{{ int http_request_method_exists(zend_bool, ulong, char *) */
145 PHP_HTTP_API int _http_request_method_exists(zend_bool by_name, http_request_method id, const char *name TSRMLS_DC)
146 {
147 int i;
148 http_request_method_entry **ptr = HTTP_G->request.methods.custom.entries;
149
150 if (by_name) {
151 for (i = HTTP_MIN_REQUEST_METHOD; i < HTTP_MAX_REQUEST_METHOD; ++i) {
152 if (!strcasecmp(name, http_request_methods[i])) {
153 return i;
154 }
155 }
156 for (i = 0; i < HTTP_G->request.methods.custom.count; ++i) {
157 if (ptr[i] && !strcasecmp(name, ptr[i]->name)) {
158 return HTTP_CUSTOM_REQUEST_METHOD_START + i;
159 }
160 }
161 } else if (HTTP_STD_REQUEST_METHOD(id)) {
162 return id;
163 } else if ( (HTTP_CUSTOM_REQUEST_METHOD(id) >= 0) &&
164 (HTTP_CUSTOM_REQUEST_METHOD(id) < HTTP_G->request.methods.custom.count) &&
165 (ptr[HTTP_CUSTOM_REQUEST_METHOD(id)])) {
166 return id;
167 }
168
169 return 0;
170 }
171 /* }}} */
172
173 /* {{{ int http_request_method_register(char *) */
174 PHP_HTTP_API int _http_request_method_register(const char *method_name, int method_name_len TSRMLS_DC)
175 {
176 int i, meth_num;
177 char *http_method, *method, *mconst;
178 http_request_method_entry **ptr = HTTP_G->request.methods.custom.entries;
179
180 if (!isalpha(*method_name)) {
181 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Request method does not start with a character (%s)", method_name);
182 return 0;
183 }
184
185 if (http_request_method_exists(1, 0, method_name)) {
186 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Request method does already exist (%s)", method_name);
187 return 0;
188 }
189
190 method = emalloc(method_name_len + 1);
191 mconst = emalloc(method_name_len + 1);
192 for (i = 0; i < method_name_len; ++i) {
193 switch (method_name[i])
194 {
195 case '-':
196 method[i] = '-';
197 mconst[i] = '_';
198 break;
199
200 default:
201 if (!isalnum(method_name[i])) {
202 efree(method);
203 efree(mconst);
204 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Request method contains illegal characters (%s)", method_name);
205 return 0;
206 }
207 mconst[i] = method[i] = toupper(method_name[i]);
208 break;
209 }
210 }
211 method[method_name_len] = '\0';
212 mconst[method_name_len] = '\0';
213
214 ptr = erealloc(ptr, sizeof(http_request_method_entry *) * (HTTP_G->request.methods.custom.count + 1));
215 HTTP_G->request.methods.custom.entries = ptr;
216 ptr[HTTP_G->request.methods.custom.count] = emalloc(sizeof(http_request_method_entry));
217 ptr[HTTP_G->request.methods.custom.count]->name = method;
218 ptr[HTTP_G->request.methods.custom.count]->cnst = mconst;
219 meth_num = HTTP_CUSTOM_REQUEST_METHOD_START + HTTP_G->request.methods.custom.count++;
220
221 method_name_len = spprintf(&http_method, 0, "HTTP_METH_%s", mconst);
222 zend_register_long_constant(http_method, method_name_len + 1, meth_num, CONST_CS, http_module_number TSRMLS_CC);
223 efree(http_method);
224
225 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL) && !defined(WONKY)
226 method_name_len = spprintf(&http_method, 0, "METH_%s", mconst);
227 zend_declare_class_constant_long(http_request_object_ce, http_method, method_name_len, meth_num TSRMLS_CC);
228 efree(http_method);
229 #endif
230
231 return meth_num;
232 }
233 /* }}} */
234
235 /* {{{ STATUS http_request_method_unregister(int) */
236 PHP_HTTP_API STATUS _http_request_method_unregister(int method TSRMLS_DC)
237 {
238 char *http_method;
239 int method_len;
240 http_request_method_entry **ptr = HTTP_G->request.methods.custom.entries;
241
242 if (HTTP_STD_REQUEST_METHOD(method)) {
243 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Standard request methods cannot be unregistered");
244 return FAILURE;
245 }
246
247 if ( (HTTP_CUSTOM_REQUEST_METHOD(method) < 0) ||
248 (HTTP_CUSTOM_REQUEST_METHOD(method) > HTTP_G->request.methods.custom.count) ||
249 (!ptr[HTTP_CUSTOM_REQUEST_METHOD(method)])) {
250 http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Custom request method with id %lu does not exist", method);
251 return FAILURE;
252 }
253
254 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL) && !defined(WONKY)
255 method_len = spprintf(&http_method, 0, "METH_%s", ptr[HTTP_CUSTOM_REQUEST_METHOD(method)]->cnst);
256 if (SUCCESS != zend_hash_del(&http_request_object_ce->constants_table, http_method, method_len + 1)) {
257 http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Could not unregister request method: HttpRequest::%s", http_method);
258 efree(http_method);
259 return FAILURE;
260 }
261 efree(http_method);
262 #endif
263
264 method_len = spprintf(&http_method, 0, "HTTP_METH_%s", ptr[HTTP_CUSTOM_REQUEST_METHOD(method)]->cnst);
265 if (SUCCESS != zend_hash_del(EG(zend_constants), http_method, method_len + 1)) {
266 http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Could not unregister request method: %s", http_method);
267 efree(http_method);
268 return FAILURE;
269 }
270 efree(http_method);
271
272 efree(ptr[HTTP_CUSTOM_REQUEST_METHOD(method)]->name);
273 efree(ptr[HTTP_CUSTOM_REQUEST_METHOD(method)]->cnst);
274 efree(ptr[HTTP_CUSTOM_REQUEST_METHOD(method)]);
275 ptr[HTTP_CUSTOM_REQUEST_METHOD(method)] = NULL;
276
277 return SUCCESS;
278 }
279 /* }}} */
280
281 /*
282 * Local variables:
283 * tab-width: 4
284 * c-basic-offset: 4
285 * End:
286 * vim600: noet sw=4 ts=4 fdm=marker
287 * vim<600: noet sw=4 ts=4
288 */
289