Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 207 lines (163 sloc) 4.925 kB
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Cherokee
4 *
5 * Authors:
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 *
920e20b @alobbs Happy new year!
alobbs authored
8 * Copyright (C) 2001-2011 Alvaro Lopez Ortega
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
2c39092 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2660 5dc97367-9…
alobbs authored
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
23 */
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
24
25 #include "common-internal.h"
26 #include "handler_error_nn.h"
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <dirent.h>
32
33 #include "thread.h"
34 #include "connection.h"
35 #include "connection-protected.h"
36 #include "module.h"
37 #include "plugin_loader.h"
38 #include "connection.h"
39 #include "levenshtein_distance.h"
40 #include "handler_error.h"
e9aa9a9 @alobbs There were a few syscalls where EINTR was not being handled properly.
alobbs authored
41 #include "util.h"
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
42
43
44 /* Plug-in initialization
45 */
46 PLUGIN_INFO_HANDLER_EASIEST_INIT (error_nn, http_all_methods);
47
48
49 /* Methods implementation
50 */
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
51 ret_t
52 cherokee_handler_error_nn_configure (cherokee_config_node_t *conf,
53 cherokee_server_t *srv,
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
54 cherokee_module_props_t **_props)
55 {
56 UNUSED(conf);
57 UNUSED(srv);
58 UNUSED(_props);
59
60 return ret_ok;
61 }
62
63
64 static ret_t
65 get_nearest_from_directory (char *directory, char *request, cherokee_buffer_t *output)
66 {
f032773 @alobbs Fixes an invalid memory access
alobbs authored
67 int re;
e9aa9a9 @alobbs There were a few syscalls where EINTR was not being handled properly.
alobbs authored
68 int dis;
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
69 DIR *dir;
e9aa9a9 @alobbs There were a few syscalls where EINTR was not being handled properly.
alobbs authored
70 char entry_buf[512];
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
71 struct dirent *entry;
72 int min_diff = 9999;
73 cherokee_boolean_t found = false;
74
e9aa9a9 @alobbs There were a few syscalls where EINTR was not being handled properly.
alobbs authored
75 dir = cherokee_opendir (directory);
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
76 if (dir == NULL)
77 goto go_out;
78
e9aa9a9 @alobbs There were a few syscalls where EINTR was not being handled properly.
alobbs authored
79 for (;;) {
f032773 @alobbs Fixes an invalid memory access
alobbs authored
80 re = cherokee_readdir (dir, (struct dirent *)entry_buf, &entry);
81 if ((re != 0) || (entry == NULL))
82 break;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
83
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
84 if (!strncmp (entry->d_name, ".", 1)) continue;
85 if (!strncmp (entry->d_name, "..", 2)) continue;
86
87 dis = distance ((char *)request, entry->d_name);
88 if (dis < min_diff) {
89 min_diff = dis;
90 found = true;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
91
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
92 cherokee_buffer_clean (output);
93 cherokee_buffer_add (output, entry->d_name, strlen(entry->d_name));
94 }
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
95
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
96 }
e9aa9a9 @alobbs There were a few syscalls where EINTR was not being handled properly.
alobbs authored
97
98 cherokee_closedir (dir);
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
99
100 go_out:
101 return (found) ? ret_ok : ret_error;
102 }
103
104
105 static ret_t
106 get_nearest_name (cherokee_connection_t *conn,
107 cherokee_buffer_t *local_dir,
108 cherokee_buffer_t *request,
109 cherokee_buffer_t *output)
110 {
111 ret_t ret;
112 char *rest;
113 cherokee_thread_t *thread = CONN_THREAD(conn);
114 cherokee_buffer_t *req = THREAD_TMP_BUF1(thread);
115
116 /* Build the local request path
117 */
118 rest = strrchr (request->buf, '/');
119 if (rest == NULL) {
120 return ret_error;
121 }
122 rest++;
123
124 cherokee_buffer_clean (req);
125 cherokee_buffer_add_buffer (req, local_dir);
126 cherokee_buffer_add (req, request->buf, rest - request->buf);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
127
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
128 /* Copy the new filename to the output buffer
129 */
130 ret = get_nearest_from_directory (req->buf, rest, output);
131 if (unlikely (ret != ret_ok)) {
132 return ret_error;
133 }
134
135 /* Prepend the rest of the old request to the new filename
136 */
137 cherokee_buffer_prepend (output, request->buf, rest - request->buf);
138 return ret_ok;
139 }
140
141
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
142 static ret_t
cefda56 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1766 5dc97367-9…
alobbs authored
143 error_nn_init (cherokee_handler_error_t *hdl)
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
144 {
cefda56 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1766 5dc97367-9…
alobbs authored
145 ret_t ret;
146 cherokee_connection_t *conn = HANDLER_CONN(hdl);
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
147
148 cherokee_buffer_clean (&conn->redirect);
149
150 ret = get_nearest_name (conn, &conn->local_directory, &conn->request, &conn->redirect);
151 if (unlikely (ret != ret_ok)) {
152 conn->error_code = http_not_found;
cefda56 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1766 5dc97367-9…
alobbs authored
153 return ret_error;
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
154 }
155
cefda56 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1766 5dc97367-9…
alobbs authored
156 conn->error_code = http_moved_temporarily;
157 return ret_error;
158 }
159
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
160 static ret_t
cefda56 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1766 5dc97367-9…
alobbs authored
161 error_nn_add_headers (cherokee_handler_error_t *hdl, cherokee_buffer_t *buffer)
162 {
163 UNUSED(hdl);
164 UNUSED(buffer);
165
166 return ret_ok;
167 }
168
169 static ret_t
170 error_nn_step (cherokee_handler_error_t *hdl, cherokee_buffer_t *buffer)
171 {
172 UNUSED(hdl);
173 UNUSED(buffer);
174
175 return ret_eof;
176 }
177
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
178 static ret_t
cefda56 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1766 5dc97367-9…
alobbs authored
179 error_nn_free (cherokee_handler_error_t *hdl)
180 {
181 UNUSED(hdl);
182
183 return ret_ok;
184 }
185
186
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
187 ret_t
188 cherokee_handler_error_nn_new (cherokee_handler_t **hdl,
cefda56 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1766 5dc97367-9…
alobbs authored
189 cherokee_connection_t *conn,
190 cherokee_module_props_t *props)
191 {
192 CHEROKEE_NEW_STRUCT (n, handler_error_nn);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
193
cefda56 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1766 5dc97367-9…
alobbs authored
194 cherokee_handler_init_base (HANDLER(n), conn, HANDLER_PROPS(props), PLUGIN_INFO_HANDLER_PTR(error_nn));
195 HANDLER(n)->support = hsupport_error | hsupport_length;
196
197 MODULE(n)->init = (handler_func_init_t) error_nn_init;
198 MODULE(n)->free = (module_func_free_t) error_nn_free;
199 HANDLER(n)->step = (handler_func_step_t) error_nn_step;
200 HANDLER(n)->add_headers = (handler_func_add_headers_t) error_nn_add_headers;
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
201
cefda56 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1766 5dc97367-9…
alobbs authored
202 *hdl = HANDLER(n);
203 return ret_ok;
853183c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1734 5dc97367-9…
alobbs authored
204 }
205
cefda56 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1766 5dc97367-9…
alobbs authored
206
Something went wrong with that request. Please try again.