Mercurial > hg > nginx-vendor-0-7
comparison src/http/modules/ngx_http_dav_module.c @ 286:5bef04fc3fd5 NGINX_0_5_13
nginx 0.5.13
*) Feature: the COPY and MOVE methods.
*) Bugfix: the ngx_http_realip_module set garbage for requests passed
via keep-alive connection.
*) Bugfix: nginx did not work on big-endian 64-bit Linux.
Thanks to Andrei Nigmatulin.
*) Bugfix: now when IMAP/POP3 proxy receives too long command it closes
the connection right away, but not after timeout.
*) Bugfix: if the "epoll" method was used and a client closed a
connection prematurely, then nginx closed the connection after a
send timeout only.
*) Bugfix: nginx could not be built on platforms different from i386,
amd64, sparc and ppc; bug appeared in 0.5.8.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Mon, 19 Feb 2007 00:00:00 +0300 |
parents | 675a39fd14cd |
children | 2ceaee987f37 |
comparison
equal
deleted
inserted
replaced
285:0e505c8b6528 | 286:5bef04fc3fd5 |
---|---|
7 #include <ngx_config.h> | 7 #include <ngx_config.h> |
8 #include <ngx_core.h> | 8 #include <ngx_core.h> |
9 #include <ngx_http.h> | 9 #include <ngx_http.h> |
10 | 10 |
11 | 11 |
12 #define NGX_HTTP_DAV_OFF 2 | 12 #define NGX_HTTP_DAV_COPY_BLOCK 65536 |
13 | |
14 #define NGX_HTTP_DAV_OFF 2 | |
15 | |
16 | |
17 #define NGX_HTTP_DAV_NO_DEPTH -3 | |
18 #define NGX_HTTP_DAV_INVALID_DEPTH -2 | |
19 #define NGX_HTTP_DAV_INFINITY_DEPTH -1 | |
20 | |
13 | 21 |
14 typedef struct { | 22 typedef struct { |
15 ngx_uint_t methods; | 23 ngx_uint_t methods; |
16 ngx_flag_t create_full_put_path; | 24 ngx_flag_t create_full_put_path; |
17 ngx_uint_t access; | 25 ngx_uint_t access; |
18 } ngx_http_dav_loc_conf_t; | 26 } ngx_http_dav_loc_conf_t; |
19 | 27 |
20 | 28 |
29 typedef struct { | |
30 ngx_str_t path; | |
31 size_t len; | |
32 } ngx_http_dav_copy_ctx_t; | |
33 | |
34 | |
21 static ngx_int_t ngx_http_dav_handler(ngx_http_request_t *r); | 35 static ngx_int_t ngx_http_dav_handler(ngx_http_request_t *r); |
36 | |
37 static void ngx_http_dav_put_handler(ngx_http_request_t *r); | |
38 | |
39 static ngx_int_t ngx_http_dav_delete_handler(ngx_http_request_t *r); | |
22 static ngx_int_t ngx_http_dav_no_init(void *ctx, void *prev); | 40 static ngx_int_t ngx_http_dav_no_init(void *ctx, void *prev); |
23 static ngx_int_t ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path); | 41 static ngx_int_t ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path); |
24 static ngx_int_t ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path); | 42 static ngx_int_t ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path); |
25 static ngx_int_t ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path); | 43 static ngx_int_t ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path); |
26 static void ngx_http_dav_put_handler(ngx_http_request_t *r); | 44 |
27 static ngx_int_t ngx_http_dav_error(ngx_http_request_t *, ngx_err_t err, | 45 static ngx_int_t ngx_http_dav_mkcol_handler(ngx_http_request_t *r, |
46 ngx_http_dav_loc_conf_t *dlcf); | |
47 | |
48 static ngx_int_t ngx_http_dav_copy_move_handler(ngx_http_request_t *r); | |
49 static ngx_int_t ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path); | |
50 static ngx_int_t ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, | |
51 ngx_str_t *path); | |
52 static ngx_int_t ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, ngx_str_t *path); | |
53 | |
54 static ngx_int_t ngx_http_dav_delete_path(ngx_http_request_t *r, | |
55 ngx_str_t *path, ngx_uint_t dir); | |
56 static ngx_int_t ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt); | |
57 static ngx_int_t ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, | |
28 ngx_int_t not_found, char *failed, u_char *path); | 58 ngx_int_t not_found, char *failed, u_char *path); |
29 static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r, u_char *path); | 59 static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r, u_char *path); |
30 static char *ngx_http_dav_access(ngx_conf_t *cf, ngx_command_t *cmd, | 60 static char *ngx_http_dav_access(ngx_conf_t *cf, ngx_command_t *cmd, |
31 void *conf); | 61 void *conf); |
32 static void *ngx_http_dav_create_loc_conf(ngx_conf_t *cf); | 62 static void *ngx_http_dav_create_loc_conf(ngx_conf_t *cf); |
38 static ngx_conf_bitmask_t ngx_http_dav_methods_mask[] = { | 68 static ngx_conf_bitmask_t ngx_http_dav_methods_mask[] = { |
39 { ngx_string("off"), NGX_HTTP_DAV_OFF }, | 69 { ngx_string("off"), NGX_HTTP_DAV_OFF }, |
40 { ngx_string("put"), NGX_HTTP_PUT }, | 70 { ngx_string("put"), NGX_HTTP_PUT }, |
41 { ngx_string("delete"), NGX_HTTP_DELETE }, | 71 { ngx_string("delete"), NGX_HTTP_DELETE }, |
42 { ngx_string("mkcol"), NGX_HTTP_MKCOL }, | 72 { ngx_string("mkcol"), NGX_HTTP_MKCOL }, |
73 { ngx_string("copy"), NGX_HTTP_COPY }, | |
74 { ngx_string("move"), NGX_HTTP_MOVE }, | |
43 { ngx_null_string, 0 } | 75 { ngx_null_string, 0 } |
44 }; | 76 }; |
45 | 77 |
46 | 78 |
47 static ngx_command_t ngx_http_dav_commands[] = { | 79 static ngx_command_t ngx_http_dav_commands[] = { |
103 | 135 |
104 | 136 |
105 static ngx_int_t | 137 static ngx_int_t |
106 ngx_http_dav_handler(ngx_http_request_t *r) | 138 ngx_http_dav_handler(ngx_http_request_t *r) |
107 { | 139 { |
108 char *failed; | |
109 size_t root; | |
110 ngx_int_t rc; | 140 ngx_int_t rc; |
111 ngx_str_t path; | |
112 ngx_tree_ctx_t tree; | |
113 ngx_file_info_t fi; | |
114 ngx_table_elt_t *depth; | |
115 ngx_http_dav_loc_conf_t *dlcf; | 141 ngx_http_dav_loc_conf_t *dlcf; |
116 | 142 |
117 /* TODO: Win32 */ | 143 /* TODO: Win32 */ |
118 if (r->zero_in_uri) { | 144 if (r->zero_in_uri) { |
119 return NGX_DECLINED; | 145 return NGX_DECLINED; |
147 | 173 |
148 return NGX_DONE; | 174 return NGX_DONE; |
149 | 175 |
150 case NGX_HTTP_DELETE: | 176 case NGX_HTTP_DELETE: |
151 | 177 |
152 if (r->headers_in.content_length_n > 0) { | 178 return ngx_http_dav_delete_handler(r); |
153 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | |
154 } | |
155 | |
156 rc = ngx_http_discard_body(r); | |
157 | |
158 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
159 return rc; | |
160 } | |
161 | |
162 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
163 | |
164 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
165 "http delete filename: \"%s\"", path.data); | |
166 | |
167 if (ngx_file_info(path.data, &fi) != -1) { | |
168 | |
169 if (ngx_is_dir(&fi)) { | |
170 | |
171 if (r->uri.data[r->uri.len - 1] != '/') { | |
172 return NGX_HTTP_BAD_REQUEST; | |
173 } | |
174 | |
175 depth = r->headers_in.depth; | |
176 | |
177 if (depth | |
178 && (depth->value.len != sizeof("infinity") - 1 | |
179 || ngx_strcmp(depth->value.data, "infinity") != 0)) | |
180 { | |
181 return NGX_HTTP_BAD_REQUEST; | |
182 } | |
183 | |
184 path.len -= 2; | |
185 | |
186 tree.init_handler = ngx_http_dav_no_init; | |
187 tree.file_handler = ngx_http_dav_delete_file; | |
188 tree.pre_tree_handler = ngx_http_dav_noop; | |
189 tree.post_tree_handler = ngx_http_dav_delete_dir; | |
190 tree.spec_handler = ngx_http_dav_delete_file; | |
191 tree.data = NULL; | |
192 tree.alloc = 0; | |
193 tree.log = r->connection->log; | |
194 | |
195 if (ngx_walk_tree(&tree, &path) == NGX_OK) { | |
196 | |
197 if (ngx_delete_dir(path.data) != NGX_FILE_ERROR) { | |
198 return NGX_HTTP_NO_CONTENT; | |
199 } | |
200 | |
201 } | |
202 | |
203 failed = ngx_delete_dir_n; | |
204 | |
205 } else { | |
206 | |
207 if (r->uri.data[r->uri.len - 1] == '/') { | |
208 return NGX_HTTP_BAD_REQUEST; | |
209 } | |
210 | |
211 depth = r->headers_in.depth; | |
212 | |
213 if (depth | |
214 && depth->value.len == 1 | |
215 && depth->value.data[0] == '1') | |
216 { | |
217 return NGX_HTTP_BAD_REQUEST; | |
218 } | |
219 | |
220 if (ngx_delete_file(path.data) != NGX_FILE_ERROR) { | |
221 return NGX_HTTP_NO_CONTENT; | |
222 } | |
223 | |
224 failed = ngx_delete_file_n; | |
225 } | |
226 | |
227 } else { | |
228 failed = ngx_file_info_n; | |
229 } | |
230 | |
231 return ngx_http_dav_error(r, ngx_errno, NGX_HTTP_NOT_FOUND, failed, | |
232 path.data); | |
233 | 179 |
234 case NGX_HTTP_MKCOL: | 180 case NGX_HTTP_MKCOL: |
235 | 181 |
236 if (r->uri.data[r->uri.len - 1] != '/') { | 182 return ngx_http_dav_mkcol_handler(r, dlcf); |
237 return NGX_DECLINED; | 183 |
238 } | 184 case NGX_HTTP_COPY: |
239 | 185 |
240 if (r->headers_in.content_length_n > 0) { | 186 return ngx_http_dav_copy_move_handler(r); |
241 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | 187 |
242 } | 188 case NGX_HTTP_MOVE: |
243 | 189 |
244 rc = ngx_http_discard_body(r); | 190 return ngx_http_dav_copy_move_handler(r); |
245 | |
246 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
247 return rc; | |
248 } | |
249 | |
250 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
251 | |
252 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
253 "http mkcol path: \"%s\"", path.data); | |
254 | |
255 if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access)) | |
256 != NGX_FILE_ERROR) | |
257 { | |
258 if (ngx_http_dav_location(r, path.data) != NGX_OK) { | |
259 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
260 } | |
261 | |
262 return NGX_HTTP_CREATED; | |
263 } | |
264 | |
265 return ngx_http_dav_error(r, ngx_errno, NGX_HTTP_CONFLICT, | |
266 ngx_create_dir_n, path.data); | |
267 } | 191 } |
268 | 192 |
269 return NGX_DECLINED; | 193 return NGX_DECLINED; |
270 } | |
271 | |
272 | |
273 static ngx_int_t | |
274 ngx_http_dav_no_init(void *ctx, void *prev) | |
275 { | |
276 return NGX_OK; | |
277 } | |
278 | |
279 | |
280 static ngx_int_t | |
281 ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
282 { | |
283 return NGX_OK; | |
284 } | |
285 | |
286 | |
287 static ngx_int_t | |
288 ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
289 { | |
290 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
291 "http delete dir: \"%s\"", path->data); | |
292 | |
293 ngx_delete_dir(path->data); | |
294 | |
295 return NGX_OK; | |
296 } | |
297 | |
298 | |
299 static ngx_int_t | |
300 ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
301 { | |
302 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
303 "http delete file: \"%s\"", path->data); | |
304 | |
305 ngx_delete_file(path->data); | |
306 | |
307 return NGX_OK; | |
308 } | 194 } |
309 | 195 |
310 | 196 |
311 static void | 197 static void |
312 ngx_http_dav_put_handler(ngx_http_request_t *r) | 198 ngx_http_dav_put_handler(ngx_http_request_t *r) |
315 u_char *name; | 201 u_char *name; |
316 size_t root; | 202 size_t root; |
317 time_t date; | 203 time_t date; |
318 ngx_err_t err; | 204 ngx_err_t err; |
319 ngx_str_t *temp, path; | 205 ngx_str_t *temp, path; |
320 ngx_uint_t status; | 206 ngx_uint_t status, not_found; |
321 ngx_file_info_t fi; | 207 ngx_file_info_t fi; |
322 ngx_http_dav_loc_conf_t *dlcf; | 208 ngx_http_dav_loc_conf_t *dlcf; |
323 | 209 |
324 ngx_http_map_uri_to_path(r, &path, &root, 0); | 210 ngx_http_map_uri_to_path(r, &path, &root, 0); |
325 | 211 |
355 | 241 |
356 if (ngx_change_file_access(temp->data, dlcf->access) | 242 if (ngx_change_file_access(temp->data, dlcf->access) |
357 == NGX_FILE_ERROR) | 243 == NGX_FILE_ERROR) |
358 { | 244 { |
359 err = ngx_errno; | 245 err = ngx_errno; |
246 not_found = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
360 failed = ngx_change_file_access_n; | 247 failed = ngx_change_file_access_n; |
361 name = temp->data; | 248 name = temp->data; |
362 | 249 |
363 goto failed; | 250 goto failed; |
364 } | 251 } |
373 if (ngx_set_file_time(temp->data, | 260 if (ngx_set_file_time(temp->data, |
374 r->request_body->temp_file->file.fd, date) | 261 r->request_body->temp_file->file.fd, date) |
375 != NGX_OK) | 262 != NGX_OK) |
376 { | 263 { |
377 err = ngx_errno; | 264 err = ngx_errno; |
265 not_found = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
378 failed = ngx_set_file_time_n; | 266 failed = ngx_set_file_time_n; |
379 name = temp->data; | 267 name = temp->data; |
380 | 268 |
381 goto failed; | 269 goto failed; |
382 } | 270 } |
383 } | 271 } |
384 } | 272 } |
385 | 273 |
274 not_found = NGX_HTTP_CONFLICT; | |
386 failed = ngx_rename_file_n; | 275 failed = ngx_rename_file_n; |
387 name = path.data; | 276 name = path.data; |
388 | 277 |
389 if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) { | 278 if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) { |
390 goto ok; | 279 goto ok; |
429 ngx_delete_file_n " \"%s\" failed", | 318 ngx_delete_file_n " \"%s\" failed", |
430 temp->data); | 319 temp->data); |
431 } | 320 } |
432 | 321 |
433 ngx_http_finalize_request(r, | 322 ngx_http_finalize_request(r, |
434 ngx_http_dav_error(r, err, NGX_HTTP_CONFLICT, failed, name)); | 323 ngx_http_dav_error(r->connection->log, err, |
324 not_found, failed, name)); | |
435 | 325 |
436 return; | 326 return; |
437 | 327 |
438 ok: | 328 ok: |
439 | 329 |
453 return; | 343 return; |
454 } | 344 } |
455 | 345 |
456 | 346 |
457 static ngx_int_t | 347 static ngx_int_t |
458 ngx_http_dav_error(ngx_http_request_t *r, ngx_err_t err, ngx_int_t not_found, | 348 ngx_http_dav_delete_handler(ngx_http_request_t *r) |
349 { | |
350 size_t root; | |
351 ngx_int_t rc, depth; | |
352 ngx_uint_t dir; | |
353 ngx_str_t path; | |
354 ngx_file_info_t fi; | |
355 | |
356 if (r->headers_in.content_length_n > 0) { | |
357 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | |
358 } | |
359 | |
360 rc = ngx_http_discard_body(r); | |
361 | |
362 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
363 return rc; | |
364 } | |
365 | |
366 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
367 | |
368 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
369 "http delete filename: \"%s\"", path.data); | |
370 | |
371 if (ngx_file_info(path.data, &fi) == -1) { | |
372 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
373 NGX_HTTP_NOT_FOUND, ngx_file_info_n, | |
374 path.data); | |
375 } | |
376 | |
377 if (ngx_is_dir(&fi)) { | |
378 | |
379 if (r->uri.data[r->uri.len - 1] != '/') { | |
380 /* TODO: 301 */ | |
381 return NGX_HTTP_BAD_REQUEST; | |
382 } | |
383 | |
384 depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH); | |
385 | |
386 if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
387 return NGX_HTTP_BAD_REQUEST; | |
388 } | |
389 | |
390 path.len -= 2; /* omit "/\0" */ | |
391 | |
392 dir = 1; | |
393 | |
394 } else { | |
395 | |
396 depth = ngx_http_dav_depth(r, 0); | |
397 | |
398 if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
399 return NGX_HTTP_BAD_REQUEST; | |
400 } | |
401 | |
402 dir = 0; | |
403 } | |
404 | |
405 rc = ngx_http_dav_delete_path(r, &path, dir); | |
406 | |
407 if (rc == NGX_OK) { | |
408 return NGX_HTTP_NO_CONTENT; | |
409 } | |
410 | |
411 return rc; | |
412 } | |
413 | |
414 | |
415 static ngx_int_t | |
416 ngx_http_dav_no_init(void *ctx, void *prev) | |
417 { | |
418 return NGX_OK; | |
419 } | |
420 | |
421 | |
422 static ngx_int_t | |
423 ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
424 { | |
425 return NGX_OK; | |
426 } | |
427 | |
428 | |
429 static ngx_int_t | |
430 ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
431 { | |
432 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
433 "http delete dir: \"%s\"", path->data); | |
434 | |
435 if (ngx_delete_dir(path->data) == NGX_FILE_ERROR) { | |
436 | |
437 /* TODO: add to 207 */ | |
438 | |
439 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_dir_n, | |
440 path->data); | |
441 } | |
442 | |
443 return NGX_OK; | |
444 } | |
445 | |
446 | |
447 static ngx_int_t | |
448 ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
449 { | |
450 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
451 "http delete file: \"%s\"", path->data); | |
452 | |
453 if (ngx_delete_file(path->data) == NGX_FILE_ERROR) { | |
454 | |
455 /* TODO: add to 207 */ | |
456 | |
457 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_file_n, | |
458 path->data); | |
459 } | |
460 | |
461 return NGX_OK; | |
462 } | |
463 | |
464 | |
465 static ngx_int_t | |
466 ngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf) | |
467 { | |
468 size_t root; | |
469 ngx_int_t rc; | |
470 ngx_str_t path; | |
471 | |
472 if (r->headers_in.content_length_n > 0) { | |
473 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | |
474 } | |
475 | |
476 rc = ngx_http_discard_body(r); | |
477 | |
478 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
479 return rc; | |
480 } | |
481 | |
482 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
483 | |
484 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
485 "http mkcol path: \"%s\"", path.data); | |
486 | |
487 if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access)) | |
488 != NGX_FILE_ERROR) | |
489 { | |
490 if (ngx_http_dav_location(r, path.data) != NGX_OK) { | |
491 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
492 } | |
493 | |
494 return NGX_HTTP_CREATED; | |
495 } | |
496 | |
497 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
498 NGX_HTTP_BAD_REQUEST, ngx_create_dir_n, | |
499 path.data); | |
500 } | |
501 | |
502 | |
503 static ngx_int_t | |
504 ngx_http_dav_copy_move_handler(ngx_http_request_t *r) | |
505 { | |
506 u_char *p, *host, *last, ch; | |
507 size_t root; | |
508 ngx_err_t err; | |
509 ngx_int_t rc, depth; | |
510 ngx_uint_t overwrite, slash; | |
511 ngx_str_t path, uri; | |
512 ngx_tree_ctx_t tree; | |
513 ngx_file_info_t fi; | |
514 ngx_table_elt_t *dest, *over; | |
515 ngx_http_dav_copy_ctx_t copy; | |
516 | |
517 if (r->headers_in.content_length_n > 0) { | |
518 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | |
519 } | |
520 | |
521 dest = r->headers_in.destination; | |
522 | |
523 if (dest == NULL) { | |
524 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
525 "client sent no \"Destination\" header"); | |
526 return NGX_HTTP_BAD_REQUEST; | |
527 } | |
528 | |
529 if (dest->value.len < sizeof("http://") - 1 + r->server_name.len + 1) { | |
530 goto invalid_destination; | |
531 } | |
532 | |
533 #if (NGX_HTTP_SSL) | |
534 | |
535 if (r->connection->ssl) { | |
536 if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1) | |
537 != 0) | |
538 { | |
539 goto invalid_destination; | |
540 } | |
541 | |
542 host = dest->value.data + sizeof("https://") - 1; | |
543 | |
544 } else | |
545 #endif | |
546 { | |
547 if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1) | |
548 != 0) | |
549 { | |
550 goto invalid_destination; | |
551 } | |
552 | |
553 host = dest->value.data + sizeof("http://") - 1; | |
554 } | |
555 | |
556 if (ngx_strncmp(host, r->server_name.data, r->server_name.len) != 0) { | |
557 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
558 "Destination URI \"%V\" is handled by " | |
559 "different repository than the source URI", | |
560 &dest->value); | |
561 | |
562 return NGX_HTTP_BAD_REQUEST; | |
563 } | |
564 | |
565 last = dest->value.data + dest->value.len; | |
566 | |
567 for (p = host + r->server_name.len; p < last; p++) { | |
568 if (*p == '/') { | |
569 goto destination_done; | |
570 } | |
571 } | |
572 | |
573 invalid_destination: | |
574 | |
575 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
576 "client sent invalid \"Destination\" header: \"%V\"", | |
577 &dest->value); | |
578 | |
579 return NGX_HTTP_BAD_REQUEST; | |
580 | |
581 destination_done: | |
582 | |
583 depth = ngx_http_dav_depth(r, 0); | |
584 | |
585 if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
586 return NGX_HTTP_BAD_REQUEST; | |
587 } | |
588 | |
589 over = r->headers_in.overwrite; | |
590 | |
591 if (over) { | |
592 if (over->value.len == 1) { | |
593 ch = over->value.data[0]; | |
594 | |
595 if (ch == 'T' || ch == 't') { | |
596 overwrite = 1; | |
597 goto overwrite_done; | |
598 } | |
599 | |
600 if (ch == 'F' || ch == 'f') { | |
601 overwrite = 0; | |
602 goto overwrite_done; | |
603 } | |
604 | |
605 } | |
606 | |
607 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
608 "client sent invalid \"Overwrite\" header: \"%V\"", | |
609 &over->value); | |
610 | |
611 return NGX_HTTP_BAD_REQUEST; | |
612 } | |
613 | |
614 overwrite = 1; | |
615 | |
616 overwrite_done: | |
617 | |
618 rc = ngx_http_discard_body(r); | |
619 | |
620 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
621 return rc; | |
622 } | |
623 | |
624 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
625 | |
626 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
627 "http copy from: \"%s\"", path.data); | |
628 | |
629 uri = r->uri; | |
630 | |
631 r->uri.len = last - p; | |
632 r->uri.data = p; | |
633 | |
634 ngx_http_map_uri_to_path(r, ©.path, &root, 0); | |
635 | |
636 r->uri = uri; | |
637 | |
638 copy.path.len--; /* omit "\0" */ | |
639 | |
640 if (copy.path.data[copy.path.len - 1] == '/') { | |
641 slash = 1; | |
642 copy.path.len--; | |
643 copy.path.data[copy.path.len] = '\0'; | |
644 | |
645 } else { | |
646 slash = 0; | |
647 } | |
648 | |
649 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
650 "http copy to: \"%s\"", copy.path.data); | |
651 | |
652 if (ngx_file_info(copy.path.data, &fi) == -1) { | |
653 err = ngx_errno; | |
654 | |
655 if (err != NGX_ENOENT) { | |
656 return ngx_http_dav_error(r->connection->log, err, | |
657 NGX_HTTP_NOT_FOUND, ngx_file_info_n, | |
658 copy.path.data); | |
659 } | |
660 | |
661 /* destination does not exist */ | |
662 | |
663 } else { | |
664 | |
665 /* destination exists */ | |
666 | |
667 if (ngx_is_dir(&fi) && !slash) { | |
668 return NGX_HTTP_CONFLICT; | |
669 } | |
670 | |
671 if (!overwrite) { | |
672 return NGX_HTTP_PRECONDITION_FAILED; | |
673 } | |
674 | |
675 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
676 "http delete: \"%s\"", copy.path.data); | |
677 | |
678 rc = ngx_http_dav_delete_path(r, ©.path, ngx_is_dir(&fi)); | |
679 | |
680 if (rc != NGX_OK) { | |
681 return rc; | |
682 } | |
683 } | |
684 | |
685 if (ngx_file_info(path.data, &fi) == -1) { | |
686 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
687 NGX_HTTP_NOT_FOUND, ngx_file_info_n, | |
688 path.data); | |
689 } | |
690 | |
691 | |
692 if (ngx_is_dir(&fi)) { | |
693 | |
694 if (r->uri.data[r->uri.len - 1] != '/') { | |
695 /* TODO: 301 */ | |
696 return NGX_HTTP_BAD_REQUEST; | |
697 } | |
698 | |
699 path.len -= 2; /* omit "/\0" */ | |
700 | |
701 if (r->method == NGX_HTTP_MOVE) { | |
702 if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) { | |
703 return NGX_HTTP_CREATED; | |
704 } | |
705 } | |
706 | |
707 if (ngx_create_dir(copy.path.data, ngx_file_access(&fi)) | |
708 == NGX_FILE_ERROR) | |
709 { | |
710 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
711 NGX_HTTP_NOT_FOUND, | |
712 ngx_create_dir_n, copy.path.data); | |
713 } | |
714 | |
715 copy.len = path.len; | |
716 | |
717 tree.init_handler = ngx_http_dav_no_init; | |
718 tree.file_handler = ngx_http_dav_copy_file; | |
719 tree.pre_tree_handler = ngx_http_dav_copy_dir; | |
720 tree.post_tree_handler = ngx_http_dav_copy_dir_time; | |
721 tree.spec_handler = ngx_http_dav_noop; | |
722 tree.data = © | |
723 tree.alloc = 0; | |
724 tree.log = r->connection->log; | |
725 | |
726 if (ngx_walk_tree(&tree, &path) == NGX_OK) { | |
727 | |
728 if (r->method == NGX_HTTP_MOVE) { | |
729 rc = ngx_http_dav_delete_path(r, &path, 1); | |
730 | |
731 if (rc != NGX_OK) { | |
732 return rc; | |
733 } | |
734 } | |
735 | |
736 return NGX_HTTP_CREATED; | |
737 } | |
738 | |
739 } else { | |
740 | |
741 if (dest->value.data[dest->value.len - 1] == '/') { | |
742 return NGX_HTTP_BAD_REQUEST; | |
743 } | |
744 | |
745 if (r->method == NGX_HTTP_MOVE) { | |
746 if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) { | |
747 return NGX_HTTP_NO_CONTENT; | |
748 } | |
749 } | |
750 | |
751 tree.data = © | |
752 tree.log = r->connection->log; | |
753 | |
754 if (ngx_http_dav_copy_file(&tree, &path) != NGX_FILE_ERROR) { | |
755 | |
756 if (r->method == NGX_HTTP_MOVE) { | |
757 rc = ngx_http_dav_delete_path(r, &path, 0); | |
758 | |
759 if (rc != NGX_OK) { | |
760 return rc; | |
761 } | |
762 } | |
763 | |
764 return NGX_HTTP_NO_CONTENT; | |
765 } | |
766 } | |
767 | |
768 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
769 } | |
770 | |
771 | |
772 static ngx_int_t | |
773 ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
774 { | |
775 u_char *p, *dir; | |
776 size_t len; | |
777 ngx_http_dav_copy_ctx_t *copy; | |
778 | |
779 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
780 "http copy dir: \"%s\"", path->data); | |
781 | |
782 copy = ctx->data; | |
783 | |
784 len = copy->path.len + path->len; | |
785 | |
786 dir = ngx_alloc(len + 1, ctx->log); | |
787 if (dir == NULL) { | |
788 return NGX_ABORT; | |
789 } | |
790 | |
791 p = ngx_cpymem(dir, copy->path.data, copy->path.len); | |
792 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
793 | |
794 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
795 "http copy dir to: \"%s\"", dir); | |
796 | |
797 if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) { | |
798 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n, | |
799 dir); | |
800 } | |
801 | |
802 ngx_free(dir); | |
803 | |
804 return NGX_OK; | |
805 } | |
806 | |
807 | |
808 static ngx_int_t | |
809 ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
810 { | |
811 u_char *p, *dir; | |
812 size_t len; | |
813 ngx_http_dav_copy_ctx_t *copy; | |
814 #if (WIN32) | |
815 ngx_fd_t fd; | |
816 #endif | |
817 | |
818 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
819 "http copy dir time: \"%s\"", path->data); | |
820 | |
821 copy = ctx->data; | |
822 | |
823 len = copy->path.len + path->len; | |
824 | |
825 dir = ngx_alloc(len + 1, ctx->log); | |
826 if (dir == NULL) { | |
827 return NGX_ABORT; | |
828 } | |
829 | |
830 p = ngx_cpymem(dir, copy->path.data, copy->path.len); | |
831 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
832 | |
833 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
834 "http copy dir time to: \"%s\"", dir); | |
835 | |
836 #if (WIN32) | |
837 | |
838 fd = ngx_open_file(dir, NGX_FILE_RDWR, NGX_FILE_OPEN, 0); | |
839 | |
840 if (fd == NGX_INVALID_FILE) { | |
841 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir); | |
842 goto failed; | |
843 } | |
844 | |
845 if (ngx_set_file_time(NULL, fd, ctx->mtime) != NGX_OK) { | |
846 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
847 ngx_set_file_time_n " \"%s\" failed", dir); | |
848 } | |
849 | |
850 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
851 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
852 ngx_close_file_n " \"%s\" failed", dir); | |
853 } | |
854 | |
855 failed: | |
856 | |
857 #else | |
858 | |
859 if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) { | |
860 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
861 ngx_set_file_time_n " \"%s\" failed", dir); | |
862 } | |
863 | |
864 #endif | |
865 | |
866 ngx_free(dir); | |
867 | |
868 return NGX_OK; | |
869 } | |
870 | |
871 | |
872 static ngx_int_t | |
873 ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
874 { | |
875 u_char *p, *file; | |
876 size_t len; | |
877 off_t size; | |
878 ssize_t n; | |
879 ngx_fd_t fd, copy_fd; | |
880 ngx_http_dav_copy_ctx_t *copy; | |
881 u_char buf[NGX_HTTP_DAV_COPY_BLOCK]; | |
882 | |
883 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
884 "http copy file: \"%s\"", path->data); | |
885 | |
886 copy = ctx->data; | |
887 | |
888 len = copy->path.len + path->len; | |
889 | |
890 file = ngx_alloc(len + 1, ctx->log); | |
891 if (file == NULL) { | |
892 return NGX_ABORT; | |
893 } | |
894 | |
895 p = ngx_cpymem(file, copy->path.data, copy->path.len); | |
896 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
897 | |
898 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
899 "http copy file to: \"%s\"", file); | |
900 | |
901 fd = ngx_open_file(path->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); | |
902 | |
903 if (fd == NGX_INVALID_FILE) { | |
904 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, | |
905 path->data); | |
906 goto failed; | |
907 } | |
908 | |
909 copy_fd = ngx_open_file(file, NGX_FILE_WRONLY, NGX_FILE_CREATE_OR_OPEN, | |
910 ctx->access); | |
911 | |
912 if (copy_fd == NGX_INVALID_FILE) { | |
913 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, | |
914 file); | |
915 goto copy_failed; | |
916 } | |
917 | |
918 for (size = ctx->size; size > 0; size -= n) { | |
919 | |
920 n = ngx_read_fd(fd, buf, NGX_HTTP_DAV_COPY_BLOCK); | |
921 | |
922 if (n == NGX_FILE_ERROR) { | |
923 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
924 ngx_read_fd_n " \"%s\" failed", path->data); | |
925 break; | |
926 } | |
927 | |
928 if (ngx_write_fd(copy_fd, buf, n) == NGX_FILE_ERROR) { | |
929 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
930 ngx_write_fd_n " \"%s\" failed", file); | |
931 } | |
932 } | |
933 | |
934 if (ngx_set_file_time(file, copy_fd, ctx->mtime) != NGX_OK) { | |
935 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
936 ngx_set_file_time_n " \"%s\" failed", file); | |
937 } | |
938 | |
939 if (ngx_close_file(copy_fd) == NGX_FILE_ERROR) { | |
940 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
941 ngx_close_file_n " \"%s\" failed", file); | |
942 } | |
943 | |
944 copy_failed: | |
945 | |
946 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
947 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
948 ngx_close_file_n " \"%s\" failed", path->data); | |
949 } | |
950 | |
951 failed: | |
952 | |
953 ngx_free(file); | |
954 | |
955 return NGX_OK; | |
956 } | |
957 | |
958 | |
959 static ngx_int_t | |
960 ngx_http_dav_delete_path(ngx_http_request_t *r, ngx_str_t *path, ngx_uint_t dir) | |
961 { | |
962 char *failed; | |
963 ngx_tree_ctx_t tree; | |
964 | |
965 if (dir) { | |
966 | |
967 tree.init_handler = ngx_http_dav_no_init; | |
968 tree.file_handler = ngx_http_dav_delete_file; | |
969 tree.pre_tree_handler = ngx_http_dav_noop; | |
970 tree.post_tree_handler = ngx_http_dav_delete_dir; | |
971 tree.spec_handler = ngx_http_dav_delete_file; | |
972 tree.data = NULL; | |
973 tree.alloc = 0; | |
974 tree.log = r->connection->log; | |
975 | |
976 /* todo: 207 */ | |
977 | |
978 if (ngx_walk_tree(&tree, path) != NGX_OK) { | |
979 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
980 } | |
981 | |
982 if (ngx_delete_dir(path->data) != NGX_FILE_ERROR) { | |
983 return NGX_OK; | |
984 } | |
985 | |
986 failed = ngx_delete_dir_n; | |
987 | |
988 } else { | |
989 | |
990 if (ngx_delete_file(path->data) != NGX_FILE_ERROR) { | |
991 return NGX_OK; | |
992 } | |
993 | |
994 failed = ngx_delete_file_n; | |
995 } | |
996 | |
997 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
998 NGX_HTTP_NOT_FOUND, failed, path->data); | |
999 } | |
1000 | |
1001 | |
1002 static ngx_int_t | |
1003 ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt) | |
1004 { | |
1005 ngx_table_elt_t *depth; | |
1006 | |
1007 depth = r->headers_in.depth; | |
1008 | |
1009 if (depth == NULL) { | |
1010 return dflt; | |
1011 } | |
1012 | |
1013 if (depth->value.len == 1) { | |
1014 | |
1015 if (depth->value.data[0] == '0') { | |
1016 return 0; | |
1017 } | |
1018 | |
1019 if (depth->value.data[0] == '1') { | |
1020 return 1; | |
1021 } | |
1022 | |
1023 } else { | |
1024 | |
1025 if (depth->value.len == sizeof("infinity") - 1 | |
1026 && ngx_strcmp(depth->value.data, "infinity") == 0) | |
1027 { | |
1028 return NGX_HTTP_DAV_INFINITY_DEPTH; | |
1029 } | |
1030 } | |
1031 | |
1032 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1033 "client sent invalid \"Depth\" header: \"%V\"", | |
1034 &depth->value); | |
1035 | |
1036 return NGX_HTTP_DAV_INVALID_DEPTH; | |
1037 } | |
1038 | |
1039 | |
1040 static ngx_int_t | |
1041 ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found, | |
459 char *failed, u_char *path) | 1042 char *failed, u_char *path) |
460 { | 1043 { |
461 ngx_int_t rc; | 1044 ngx_int_t rc; |
462 ngx_uint_t level; | 1045 ngx_uint_t level; |
463 | 1046 |
480 } else { | 1063 } else { |
481 level = NGX_LOG_CRIT; | 1064 level = NGX_LOG_CRIT; |
482 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | 1065 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; |
483 } | 1066 } |
484 | 1067 |
485 ngx_log_error(level, r->connection->log, err, | 1068 ngx_log_error(level, log, err, "%s \"%s\" failed", failed, path); |
486 "%s \"%s\" failed", failed, path); | |
487 | 1069 |
488 return rc; | 1070 return rc; |
489 } | 1071 } |
490 | 1072 |
491 | 1073 |