- solve that another way
[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 getGlobals(G);
113 http_request_method_entry **ptr = G->request.methods.custom.entries;
114
115 for (i = 0; i < G->request.methods.custom.count; ++i) {
116 if (ptr[i]) {
117 http_request_method_unregister(HTTP_CUSTOM_REQUEST_METHOD_START + i);
118 }
119 }
120 efree(G->request.methods.custom.entries);
121
122 return SUCCESS;
123 }
124 /* }}} */
125
126 /* {{{ char *http_request_method_name(http_request_method) */
127 PHP_HTTP_API const char *_http_request_method_name(http_request_method m TSRMLS_DC)
128 {
129 getGlobals(G);
130 http_request_method_entry **ptr = G->request.methods.custom.entries;
131
132 if (HTTP_STD_REQUEST_METHOD(m)) {
133 return http_request_methods[m];
134 }
135
136 if ( (HTTP_CUSTOM_REQUEST_METHOD(m) >= 0) &&
137 (HTTP_CUSTOM_REQUEST_METHOD(m) < G->request.methods.custom.count) &&
138 (ptr[HTTP_CUSTOM_REQUEST_METHOD(m)])) {
139 return ptr[HTTP_CUSTOM_REQUEST_METHOD(m)]->name;
140 }
141
142 return http_request_methods[0];
143 }
144 /* }}} */
145
146 /* {{{ int http_request_method_exists(zend_bool, ulong, char *) */
147 PHP_HTTP_API int _http_request_method_exists(zend_bool by_name, http_request_method id, const char *name TSRMLS_DC)
148 {
149 int i;
150 getGlobals(G);
151 http_request_method_entry **ptr = G->request.methods.custom.entries;
152
153 if (by_name) {
154 for (i = HTTP_MIN_REQUEST_METHOD; i < HTTP_MAX_REQUEST_METHOD; ++i) {
155 if (!strcasecmp(name, http_request_methods[i])) {
156 return i;
157 }
158 }
159 for (i = 0; i < G->request.methods.custom.count; ++i) {
160 if (ptr[i] && !strcasecmp(name, ptr[i]->name)) {
161 return HTTP_CUSTOM_REQUEST_METHOD_START + i;
162 }
163 }
164 } else if (HTTP_STD_REQUEST_METHOD(id)) {
165 return id;
166 } else if ( (HTTP_CUSTOM_REQUEST_METHOD(id) >= 0) &&
167 (HTTP_CUSTOM_REQUEST_METHOD(id) < G->request.methods.custom.count) &&
168 (ptr[HTTP_CUSTOM_REQUEST_METHOD(id)])) {
169 return id;
170 }
171
172 return 0;
173 }
174 /* }}} */
175
176 /* {{{ int http_request_method_register(char *) */
177 PHP_HTTP_API int _http_request_method_register(const char *method_name, int method_name_len TSRMLS_DC)
178 {
179 int i, meth_num;
180 char *http_method, *method, *mconst;
181 getGlobals(G);
182 http_request_method_entry **ptr = G->request.methods.custom.entries;
183
184 if (!isalpha(*method_name)) {
185 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Request method does not start with a character (%s)", method_name);
186 return 0;
187 }
188
189 if (http_request_method_exists(1, 0, method_name)) {
190 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Request method does already exist (%s)", method_name);
191 return 0;
192 }
193
194 method = emalloc(method_name_len + 1);
195 mconst = emalloc(method_name_len + 1);
196 for (i = 0; i < method_name_len; ++i) {
197 switch (method_name[i])
198 {
199 case '-':
200 method[i] = '-';
201 mconst[i] = '_';
202 break;
203
204 default:
205 if (!isalnum(method_name[i])) {
206 efree(method);
207 efree(mconst);
208 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Request method contains illegal characters (%s)", method_name);
209 return 0;
210 }
211 mconst[i] = method[i] = toupper(method_name[i]);
212 break;
213 }
214 }
215 method[method_name_len] = '\0';
216 mconst[method_name_len] = '\0';
217
218 ptr = erealloc(ptr, sizeof(http_request_method_entry *) * (G->request.methods.custom.count + 1));
219 G->request.methods.custom.entries = ptr;
220 ptr[G->request.methods.custom.count] = emalloc(sizeof(http_request_method_entry));
221 ptr[G->request.methods.custom.count]->name = method;
222 ptr[G->request.methods.custom.count]->cnst = mconst;
223 meth_num = HTTP_CUSTOM_REQUEST_METHOD_START + G->request.methods.custom.count++;
224
225 method_name_len = spprintf(&http_method, 0, "HTTP_METH_%s", mconst);
226 zend_register_long_constant(http_method, method_name_len + 1, meth_num, CONST_CS, http_module_number TSRMLS_CC);
227 efree(http_method);
228
229 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL) && !defined(WONKY)
230 method_name_len = spprintf(&http_method, 0, "METH_%s", mconst);
231 zend_declare_class_constant_long(http_request_object_ce, http_method, method_name_len, meth_num TSRMLS_CC);
232 efree(http_method);
233 #endif
234
235 return meth_num;
236 }
237 /* }}} */
238
239 /* {{{ STATUS http_request_method_unregister(int) */
240 PHP_HTTP_API STATUS _http_request_method_unregister(int method TSRMLS_DC)
241 {
242 char *http_method;
243 int method_len;
244 getGlobals(G);
245 http_request_method_entry **ptr = G->request.methods.custom.entries;
246
247 if (HTTP_STD_REQUEST_METHOD(method)) {
248 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Standard request methods cannot be unregistered");
249 return FAILURE;
250 }
251
252 if ( (HTTP_CUSTOM_REQUEST_METHOD(method) < 0) ||
253 (HTTP_CUSTOM_REQUEST_METHOD(method) > G->request.methods.custom.count) ||
254 (!ptr[HTTP_CUSTOM_REQUEST_METHOD(method)])) {
255 http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Custom request method with id %lu does not exist", method);
256 return FAILURE;
257 }
258
259 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL) && !defined(WONKY)
260 method_len = spprintf(&http_method, 0, "METH_%s", ptr[HTTP_CUSTOM_REQUEST_METHOD(method)]->cnst);
261 if (SUCCESS != zend_hash_del(&http_request_object_ce->constants_table, http_method, method_len + 1)) {
262 http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Could not unregister request method: HttpRequest::%s", http_method);
263 efree(http_method);
264 return FAILURE;
265 }
266 efree(http_method);
267 #endif
268
269 method_len = spprintf(&http_method, 0, "HTTP_METH_%s", ptr[HTTP_CUSTOM_REQUEST_METHOD(method)]->cnst);
270 if (SUCCESS != zend_hash_del(EG(zend_constants), http_method, method_len + 1)) {
271 http_error_ex(HE_NOTICE, HTTP_E_REQUEST_METHOD, "Could not unregister request method: %s", http_method);
272 efree(http_method);
273 return FAILURE;
274 }
275 efree(http_method);
276
277 efree(ptr[HTTP_CUSTOM_REQUEST_METHOD(method)]->name);
278 efree(ptr[HTTP_CUSTOM_REQUEST_METHOD(method)]->cnst);
279 STR_SET(ptr[HTTP_CUSTOM_REQUEST_METHOD(method)], NULL);
280
281 return SUCCESS;
282 }
283 /* }}} */
284
285 /*
286 * Local variables:
287 * tab-width: 4
288 * c-basic-offset: 4
289 * End:
290 * vim600: noet sw=4 ts=4 fdm=marker
291 * vim<600: noet sw=4 ts=4
292 */
293