Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_dav_module.c @ 1316:f5bb5521d02c
bump version
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 17 Jul 2007 10:15:07 +0000 |
parents | 9fcfca54b208 |
children | cc114c85be0f |
rev | line source |
---|---|
633 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
1116 | 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 | |
633 | 21 |
22 typedef struct { | |
23 ngx_uint_t methods; | |
637 | 24 ngx_flag_t create_full_put_path; |
669 | 25 ngx_uint_t access; |
633 | 26 } ngx_http_dav_loc_conf_t; |
27 | |
28 | |
1116 | 29 typedef struct { |
30 ngx_str_t path; | |
31 size_t len; | |
32 } ngx_http_dav_copy_ctx_t; | |
33 | |
34 | |
633 | 35 static ngx_int_t ngx_http_dav_handler(ngx_http_request_t *r); |
1116 | 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); | |
1058
01f818c6f7de
add size, access, and mtime to ngx_walk_tree()
Igor Sysoev <igor@sysoev.ru>
parents:
957
diff
changeset
|
40 static ngx_int_t ngx_http_dav_no_init(void *ctx, void *prev); |
957 | 41 static ngx_int_t ngx_http_dav_noop(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); | |
43 static ngx_int_t ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path); | |
1116 | 44 |
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, | |
637 | 58 ngx_int_t not_found, char *failed, u_char *path); |
59 static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r, u_char *path); | |
633 | 60 static void *ngx_http_dav_create_loc_conf(ngx_conf_t *cf); |
61 static char *ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, | |
62 void *parent, void *child); | |
681 | 63 static ngx_int_t ngx_http_dav_init(ngx_conf_t *cf); |
633 | 64 |
65 | |
66 static ngx_conf_bitmask_t ngx_http_dav_methods_mask[] = { | |
67 { ngx_string("off"), NGX_HTTP_DAV_OFF }, | |
68 { ngx_string("put"), NGX_HTTP_PUT }, | |
69 { ngx_string("delete"), NGX_HTTP_DELETE }, | |
637 | 70 { ngx_string("mkcol"), NGX_HTTP_MKCOL }, |
1116 | 71 { ngx_string("copy"), NGX_HTTP_COPY }, |
72 { ngx_string("move"), NGX_HTTP_MOVE }, | |
633 | 73 { ngx_null_string, 0 } |
74 }; | |
75 | |
76 | |
77 static ngx_command_t ngx_http_dav_commands[] = { | |
78 | |
79 { ngx_string("dav_methods"), | |
80 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
81 ngx_conf_set_bitmask_slot, | |
82 NGX_HTTP_LOC_CONF_OFFSET, | |
83 offsetof(ngx_http_dav_loc_conf_t, methods), | |
84 &ngx_http_dav_methods_mask }, | |
85 | |
637 | 86 { ngx_string("create_full_put_path"), |
87 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
88 ngx_conf_set_flag_slot, | |
89 NGX_HTTP_LOC_CONF_OFFSET, | |
90 offsetof(ngx_http_dav_loc_conf_t, create_full_put_path), | |
91 NULL }, | |
92 | |
669 | 93 { ngx_string("dav_access"), |
675 | 94 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123, |
1296
9fcfca54b208
dav_access uses ngx_conf_set_access_slot()
Igor Sysoev <igor@sysoev.ru>
parents:
1289
diff
changeset
|
95 ngx_conf_set_access_slot, |
669 | 96 NGX_HTTP_LOC_CONF_OFFSET, |
1296
9fcfca54b208
dav_access uses ngx_conf_set_access_slot()
Igor Sysoev <igor@sysoev.ru>
parents:
1289
diff
changeset
|
97 offsetof(ngx_http_dav_loc_conf_t, access), |
669 | 98 NULL }, |
99 | |
633 | 100 ngx_null_command |
101 }; | |
102 | |
103 | |
667 | 104 static ngx_http_module_t ngx_http_dav_module_ctx = { |
633 | 105 NULL, /* preconfiguration */ |
681 | 106 ngx_http_dav_init, /* postconfiguration */ |
633 | 107 |
108 NULL, /* create main configuration */ | |
109 NULL, /* init main configuration */ | |
110 | |
111 NULL, /* create server configuration */ | |
112 NULL, /* merge server configuration */ | |
113 | |
114 ngx_http_dav_create_loc_conf, /* create location configuration */ | |
115 ngx_http_dav_merge_loc_conf /* merge location configuration */ | |
116 }; | |
117 | |
118 | |
119 ngx_module_t ngx_http_dav_module = { | |
120 NGX_MODULE_V1, | |
121 &ngx_http_dav_module_ctx, /* module context */ | |
122 ngx_http_dav_commands, /* module directives */ | |
123 NGX_HTTP_MODULE, /* module type */ | |
124 NULL, /* init master */ | |
681 | 125 NULL, /* init module */ |
633 | 126 NULL, /* init process */ |
127 NULL, /* init thread */ | |
128 NULL, /* exit thread */ | |
129 NULL, /* exit process */ | |
130 NULL, /* exit master */ | |
131 NGX_MODULE_V1_PADDING | |
132 }; | |
133 | |
134 | |
135 static ngx_int_t | |
136 ngx_http_dav_handler(ngx_http_request_t *r) | |
137 { | |
138 ngx_int_t rc; | |
139 ngx_http_dav_loc_conf_t *dlcf; | |
140 | |
141 /* TODO: Win32 */ | |
142 if (r->zero_in_uri) { | |
143 return NGX_DECLINED; | |
144 } | |
145 | |
146 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); | |
147 | |
148 if (!(r->method & dlcf->methods)) { | |
149 return NGX_DECLINED; | |
150 } | |
151 | |
152 switch (r->method) { | |
153 | |
154 case NGX_HTTP_PUT: | |
155 | |
156 if (r->uri.data[r->uri.len - 1] == '/') { | |
1060
8e5c032e7855
the PUTing to collection must return NGX_HTTP_BAD_REQUEST
Igor Sysoev <igor@sysoev.ru>
parents:
1058
diff
changeset
|
157 return NGX_HTTP_BAD_REQUEST; |
633 | 158 } |
159 | |
160 r->request_body_in_file_only = 1; | |
161 r->request_body_in_persistent_file = 1; | |
1075
4d203f76b757
undo "client_body_in_file_only any"
Igor Sysoev <igor@sysoev.ru>
parents:
1060
diff
changeset
|
162 r->request_body_in_clean_file = 1; |
633 | 163 r->request_body_file_group_access = 1; |
637 | 164 r->request_body_file_log_level = 0; |
633 | 165 |
166 rc = ngx_http_read_client_request_body(r, ngx_http_dav_put_handler); | |
167 | |
168 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
169 return rc; | |
170 } | |
171 | |
172 return NGX_DONE; | |
173 | |
174 case NGX_HTTP_DELETE: | |
175 | |
1116 | 176 return ngx_http_dav_delete_handler(r); |
637 | 177 |
178 case NGX_HTTP_MKCOL: | |
179 | |
1116 | 180 return ngx_http_dav_mkcol_handler(r, dlcf); |
637 | 181 |
1116 | 182 case NGX_HTTP_COPY: |
637 | 183 |
1116 | 184 return ngx_http_dav_copy_move_handler(r); |
637 | 185 |
1116 | 186 case NGX_HTTP_MOVE: |
637 | 187 |
1116 | 188 return ngx_http_dav_copy_move_handler(r); |
633 | 189 } |
190 | |
191 return NGX_DECLINED; | |
192 } | |
193 | |
194 | |
195 static void | |
196 ngx_http_dav_put_handler(ngx_http_request_t *r) | |
197 { | |
669 | 198 char *failed; |
199 u_char *name; | |
773 | 200 size_t root; |
681 | 201 time_t date; |
637 | 202 ngx_err_t err; |
203 ngx_str_t *temp, path; | |
1116 | 204 ngx_uint_t status, not_found; |
637 | 205 ngx_file_info_t fi; |
206 ngx_http_dav_loc_conf_t *dlcf; | |
633 | 207 |
773 | 208 ngx_http_map_uri_to_path(r, &path, &root, 0); |
633 | 209 |
210 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
211 "http put filename: \"%s\"", path.data); | |
212 | |
213 temp = &r->request_body->temp_file->file.name; | |
214 | |
215 if (ngx_file_info(path.data, &fi) == -1) { | |
216 status = NGX_HTTP_CREATED; | |
217 | |
218 } else { | |
219 status = NGX_HTTP_NO_CONTENT; | |
663 | 220 |
221 if (ngx_is_dir(&fi)) { | |
222 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR, | |
223 "\"%s\" could not be created", path.data); | |
633 | 224 |
663 | 225 if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) { |
226 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
227 ngx_delete_file_n " \"%s\" failed", | |
228 temp->data); | |
229 } | |
230 | |
231 ngx_http_finalize_request(r, NGX_HTTP_CONFLICT); | |
232 return; | |
661 | 233 } |
637 | 234 } |
235 | |
669 | 236 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module); |
237 | |
238 #if !(NGX_WIN32) | |
239 | |
1289 | 240 if (ngx_change_file_access(temp->data, dlcf->access) == NGX_FILE_ERROR) { |
669 | 241 err = ngx_errno; |
1116 | 242 not_found = NGX_HTTP_INTERNAL_SERVER_ERROR; |
669 | 243 failed = ngx_change_file_access_n; |
244 name = temp->data; | |
245 | |
246 goto failed; | |
247 } | |
248 | |
249 #endif | |
250 | |
681 | 251 if (r->headers_in.date) { |
252 date = ngx_http_parse_time(r->headers_in.date->value.data, | |
253 r->headers_in.date->value.len); | |
254 | |
255 if (date != NGX_ERROR) { | |
256 if (ngx_set_file_time(temp->data, | |
257 r->request_body->temp_file->file.fd, date) | |
258 != NGX_OK) | |
259 { | |
260 err = ngx_errno; | |
1116 | 261 not_found = NGX_HTTP_INTERNAL_SERVER_ERROR; |
681 | 262 failed = ngx_set_file_time_n; |
263 name = temp->data; | |
264 | |
265 goto failed; | |
266 } | |
267 } | |
268 } | |
269 | |
1116 | 270 not_found = NGX_HTTP_CONFLICT; |
669 | 271 failed = ngx_rename_file_n; |
272 name = path.data; | |
273 | |
633 | 274 if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) { |
275 goto ok; | |
276 } | |
277 | |
278 err = ngx_errno; | |
279 | |
637 | 280 if (err == NGX_ENOENT) { |
281 | |
282 if (dlcf->create_full_put_path) { | |
1089
b2e6eb425770
use ngx_dir_access() to enable search bits for directory
Igor Sysoev <igor@sysoev.ru>
parents:
1075
diff
changeset
|
283 err = ngx_create_full_path(path.data, ngx_dir_access(dlcf->access)); |
637 | 284 |
285 if (err == 0) { | |
286 if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) { | |
287 goto ok; | |
288 } | |
289 | |
290 err = ngx_errno; | |
291 } | |
292 } | |
293 } | |
294 | |
633 | 295 #if (NGX_WIN32) |
296 | |
297 if (err == NGX_EEXIST) { | |
298 if (ngx_win32_rename_file(temp, &path, r->pool) != NGX_ERROR) { | |
299 | |
300 if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) { | |
301 goto ok; | |
302 } | |
303 } | |
304 | |
305 err = ngx_errno; | |
306 } | |
307 | |
681 | 308 #endif |
669 | 309 |
310 failed: | |
311 | |
661 | 312 if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) { |
313 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
314 ngx_delete_file_n " \"%s\" failed", | |
315 temp->data); | |
316 } | |
317 | |
669 | 318 ngx_http_finalize_request(r, |
1116 | 319 ngx_http_dav_error(r->connection->log, err, |
320 not_found, failed, name)); | |
669 | 321 |
633 | 322 return; |
323 | |
324 ok: | |
325 | |
326 if (status == NGX_HTTP_CREATED) { | |
637 | 327 if (ngx_http_dav_location(r, path.data) != NGX_OK) { |
633 | 328 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
329 return; | |
330 } | |
639 | 331 |
332 r->headers_out.content_length_n = 0; | |
633 | 333 } |
334 | |
335 r->headers_out.status = status; | |
336 r->header_only = 1; | |
337 | |
338 ngx_http_finalize_request(r, ngx_http_send_header(r)); | |
339 return; | |
340 } | |
341 | |
342 | |
637 | 343 static ngx_int_t |
1116 | 344 ngx_http_dav_delete_handler(ngx_http_request_t *r) |
345 { | |
346 size_t root; | |
347 ngx_int_t rc, depth; | |
348 ngx_uint_t dir; | |
349 ngx_str_t path; | |
350 ngx_file_info_t fi; | |
351 | |
352 if (r->headers_in.content_length_n > 0) { | |
353 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | |
354 } | |
355 | |
356 rc = ngx_http_discard_body(r); | |
357 | |
358 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
359 return rc; | |
360 } | |
361 | |
362 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
363 | |
364 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
365 "http delete filename: \"%s\"", path.data); | |
366 | |
367 if (ngx_file_info(path.data, &fi) == -1) { | |
368 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
369 NGX_HTTP_NOT_FOUND, ngx_file_info_n, | |
370 path.data); | |
371 } | |
372 | |
373 if (ngx_is_dir(&fi)) { | |
374 | |
375 if (r->uri.data[r->uri.len - 1] != '/') { | |
376 /* TODO: 301 */ | |
377 return NGX_HTTP_BAD_REQUEST; | |
378 } | |
379 | |
380 depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH); | |
381 | |
382 if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
383 return NGX_HTTP_BAD_REQUEST; | |
384 } | |
385 | |
386 path.len -= 2; /* omit "/\0" */ | |
387 | |
388 dir = 1; | |
389 | |
390 } else { | |
391 | |
392 depth = ngx_http_dav_depth(r, 0); | |
393 | |
394 if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
395 return NGX_HTTP_BAD_REQUEST; | |
396 } | |
397 | |
398 dir = 0; | |
399 } | |
400 | |
401 rc = ngx_http_dav_delete_path(r, &path, dir); | |
402 | |
403 if (rc == NGX_OK) { | |
404 return NGX_HTTP_NO_CONTENT; | |
405 } | |
406 | |
407 return rc; | |
408 } | |
409 | |
410 | |
411 static ngx_int_t | |
412 ngx_http_dav_no_init(void *ctx, void *prev) | |
413 { | |
414 return NGX_OK; | |
415 } | |
416 | |
417 | |
418 static ngx_int_t | |
419 ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
420 { | |
421 return NGX_OK; | |
422 } | |
423 | |
424 | |
425 static ngx_int_t | |
426 ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
427 { | |
428 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
429 "http delete dir: \"%s\"", path->data); | |
430 | |
431 if (ngx_delete_dir(path->data) == NGX_FILE_ERROR) { | |
432 | |
433 /* TODO: add to 207 */ | |
434 | |
435 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_dir_n, | |
436 path->data); | |
437 } | |
438 | |
439 return NGX_OK; | |
440 } | |
441 | |
442 | |
443 static ngx_int_t | |
444 ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
445 { | |
446 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
447 "http delete file: \"%s\"", path->data); | |
448 | |
449 if (ngx_delete_file(path->data) == NGX_FILE_ERROR) { | |
450 | |
451 /* TODO: add to 207 */ | |
452 | |
453 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_file_n, | |
454 path->data); | |
455 } | |
456 | |
457 return NGX_OK; | |
458 } | |
459 | |
460 | |
461 static ngx_int_t | |
462 ngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf) | |
463 { | |
464 size_t root; | |
465 ngx_int_t rc; | |
466 ngx_str_t path; | |
467 | |
468 if (r->headers_in.content_length_n > 0) { | |
469 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | |
470 } | |
471 | |
472 rc = ngx_http_discard_body(r); | |
473 | |
474 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
475 return rc; | |
476 } | |
477 | |
478 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
479 | |
480 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
481 "http mkcol path: \"%s\"", path.data); | |
482 | |
483 if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access)) | |
484 != NGX_FILE_ERROR) | |
485 { | |
486 if (ngx_http_dav_location(r, path.data) != NGX_OK) { | |
487 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
488 } | |
489 | |
490 return NGX_HTTP_CREATED; | |
491 } | |
492 | |
493 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
494 NGX_HTTP_BAD_REQUEST, ngx_create_dir_n, | |
495 path.data); | |
496 } | |
497 | |
498 | |
499 static ngx_int_t | |
500 ngx_http_dav_copy_move_handler(ngx_http_request_t *r) | |
501 { | |
502 u_char *p, *host, *last, ch; | |
503 size_t root; | |
504 ngx_err_t err; | |
505 ngx_int_t rc, depth; | |
506 ngx_uint_t overwrite, slash; | |
507 ngx_str_t path, uri; | |
508 ngx_tree_ctx_t tree; | |
509 ngx_file_info_t fi; | |
510 ngx_table_elt_t *dest, *over; | |
511 ngx_http_dav_copy_ctx_t copy; | |
512 | |
513 if (r->headers_in.content_length_n > 0) { | |
514 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | |
515 } | |
516 | |
517 dest = r->headers_in.destination; | |
518 | |
519 if (dest == NULL) { | |
520 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
521 "client sent no \"Destination\" header"); | |
522 return NGX_HTTP_BAD_REQUEST; | |
523 } | |
524 | |
525 if (dest->value.len < sizeof("http://") - 1 + r->server_name.len + 1) { | |
526 goto invalid_destination; | |
527 } | |
528 | |
529 #if (NGX_HTTP_SSL) | |
530 | |
531 if (r->connection->ssl) { | |
532 if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1) | |
533 != 0) | |
534 { | |
535 goto invalid_destination; | |
536 } | |
537 | |
538 host = dest->value.data + sizeof("https://") - 1; | |
539 | |
540 } else | |
541 #endif | |
542 { | |
543 if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1) | |
544 != 0) | |
545 { | |
546 goto invalid_destination; | |
547 } | |
548 | |
549 host = dest->value.data + sizeof("http://") - 1; | |
550 } | |
551 | |
552 if (ngx_strncmp(host, r->server_name.data, r->server_name.len) != 0) { | |
553 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
554 "Destination URI \"%V\" is handled by " | |
555 "different repository than the source URI", | |
556 &dest->value); | |
557 | |
558 return NGX_HTTP_BAD_REQUEST; | |
559 } | |
560 | |
561 last = dest->value.data + dest->value.len; | |
562 | |
563 for (p = host + r->server_name.len; p < last; p++) { | |
564 if (*p == '/') { | |
565 goto destination_done; | |
566 } | |
567 } | |
568 | |
569 invalid_destination: | |
570 | |
571 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
572 "client sent invalid \"Destination\" header: \"%V\"", | |
573 &dest->value); | |
574 | |
575 return NGX_HTTP_BAD_REQUEST; | |
576 | |
577 destination_done: | |
578 | |
579 depth = ngx_http_dav_depth(r, 0); | |
580 | |
581 if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
582 return NGX_HTTP_BAD_REQUEST; | |
583 } | |
1174
6be5ee17d80b
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1116
diff
changeset
|
584 |
1116 | 585 over = r->headers_in.overwrite; |
586 | |
587 if (over) { | |
588 if (over->value.len == 1) { | |
589 ch = over->value.data[0]; | |
590 | |
591 if (ch == 'T' || ch == 't') { | |
592 overwrite = 1; | |
593 goto overwrite_done; | |
594 } | |
595 | |
596 if (ch == 'F' || ch == 'f') { | |
597 overwrite = 0; | |
598 goto overwrite_done; | |
599 } | |
600 | |
601 } | |
602 | |
603 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
604 "client sent invalid \"Overwrite\" header: \"%V\"", | |
605 &over->value); | |
606 | |
607 return NGX_HTTP_BAD_REQUEST; | |
608 } | |
609 | |
610 overwrite = 1; | |
611 | |
612 overwrite_done: | |
613 | |
614 rc = ngx_http_discard_body(r); | |
615 | |
616 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
617 return rc; | |
618 } | |
619 | |
620 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
621 | |
622 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
623 "http copy from: \"%s\"", path.data); | |
624 | |
625 uri = r->uri; | |
626 | |
627 r->uri.len = last - p; | |
628 r->uri.data = p; | |
629 | |
630 ngx_http_map_uri_to_path(r, ©.path, &root, 0); | |
631 | |
632 r->uri = uri; | |
633 | |
634 copy.path.len--; /* omit "\0" */ | |
635 | |
636 if (copy.path.data[copy.path.len - 1] == '/') { | |
637 slash = 1; | |
638 copy.path.len--; | |
639 copy.path.data[copy.path.len] = '\0'; | |
640 | |
641 } else { | |
642 slash = 0; | |
643 } | |
644 | |
645 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
646 "http copy to: \"%s\"", copy.path.data); | |
647 | |
648 if (ngx_file_info(copy.path.data, &fi) == -1) { | |
649 err = ngx_errno; | |
650 | |
651 if (err != NGX_ENOENT) { | |
652 return ngx_http_dav_error(r->connection->log, err, | |
653 NGX_HTTP_NOT_FOUND, ngx_file_info_n, | |
654 copy.path.data); | |
655 } | |
656 | |
657 /* destination does not exist */ | |
658 | |
659 } else { | |
660 | |
661 /* destination exists */ | |
662 | |
663 if (ngx_is_dir(&fi) && !slash) { | |
664 return NGX_HTTP_CONFLICT; | |
665 } | |
666 | |
667 if (!overwrite) { | |
668 return NGX_HTTP_PRECONDITION_FAILED; | |
669 } | |
670 | |
671 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
672 "http delete: \"%s\"", copy.path.data); | |
673 | |
674 rc = ngx_http_dav_delete_path(r, ©.path, ngx_is_dir(&fi)); | |
675 | |
676 if (rc != NGX_OK) { | |
677 return rc; | |
678 } | |
679 } | |
680 | |
681 if (ngx_file_info(path.data, &fi) == -1) { | |
682 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
683 NGX_HTTP_NOT_FOUND, ngx_file_info_n, | |
684 path.data); | |
685 } | |
686 | |
687 | |
688 if (ngx_is_dir(&fi)) { | |
689 | |
690 if (r->uri.data[r->uri.len - 1] != '/') { | |
691 /* TODO: 301 */ | |
692 return NGX_HTTP_BAD_REQUEST; | |
693 } | |
694 | |
695 path.len -= 2; /* omit "/\0" */ | |
696 | |
697 if (r->method == NGX_HTTP_MOVE) { | |
698 if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) { | |
699 return NGX_HTTP_CREATED; | |
700 } | |
701 } | |
702 | |
703 if (ngx_create_dir(copy.path.data, ngx_file_access(&fi)) | |
704 == NGX_FILE_ERROR) | |
705 { | |
706 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
707 NGX_HTTP_NOT_FOUND, | |
708 ngx_create_dir_n, copy.path.data); | |
709 } | |
710 | |
711 copy.len = path.len; | |
712 | |
713 tree.init_handler = ngx_http_dav_no_init; | |
714 tree.file_handler = ngx_http_dav_copy_file; | |
715 tree.pre_tree_handler = ngx_http_dav_copy_dir; | |
716 tree.post_tree_handler = ngx_http_dav_copy_dir_time; | |
717 tree.spec_handler = ngx_http_dav_noop; | |
718 tree.data = © | |
719 tree.alloc = 0; | |
720 tree.log = r->connection->log; | |
721 | |
722 if (ngx_walk_tree(&tree, &path) == NGX_OK) { | |
723 | |
724 if (r->method == NGX_HTTP_MOVE) { | |
725 rc = ngx_http_dav_delete_path(r, &path, 1); | |
726 | |
727 if (rc != NGX_OK) { | |
728 return rc; | |
729 } | |
730 } | |
731 | |
732 return NGX_HTTP_CREATED; | |
733 } | |
734 | |
735 } else { | |
736 | |
737 if (dest->value.data[dest->value.len - 1] == '/') { | |
738 return NGX_HTTP_BAD_REQUEST; | |
739 } | |
740 | |
741 if (r->method == NGX_HTTP_MOVE) { | |
742 if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) { | |
743 return NGX_HTTP_NO_CONTENT; | |
744 } | |
745 } | |
746 | |
747 tree.data = © | |
748 tree.log = r->connection->log; | |
749 | |
750 if (ngx_http_dav_copy_file(&tree, &path) != NGX_FILE_ERROR) { | |
751 | |
752 if (r->method == NGX_HTTP_MOVE) { | |
753 rc = ngx_http_dav_delete_path(r, &path, 0); | |
754 | |
755 if (rc != NGX_OK) { | |
756 return rc; | |
757 } | |
758 } | |
759 | |
760 return NGX_HTTP_NO_CONTENT; | |
761 } | |
762 } | |
763 | |
764 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
765 } | |
766 | |
767 | |
768 static ngx_int_t | |
769 ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
770 { | |
771 u_char *p, *dir; | |
772 size_t len; | |
773 ngx_http_dav_copy_ctx_t *copy; | |
774 | |
775 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
776 "http copy dir: \"%s\"", path->data); | |
777 | |
778 copy = ctx->data; | |
779 | |
780 len = copy->path.len + path->len; | |
781 | |
782 dir = ngx_alloc(len + 1, ctx->log); | |
783 if (dir == NULL) { | |
784 return NGX_ABORT; | |
785 } | |
786 | |
787 p = ngx_cpymem(dir, copy->path.data, copy->path.len); | |
788 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
789 | |
790 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
791 "http copy dir to: \"%s\"", dir); | |
792 | |
793 if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) { | |
794 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n, | |
795 dir); | |
796 } | |
797 | |
798 ngx_free(dir); | |
799 | |
800 return NGX_OK; | |
801 } | |
802 | |
803 | |
804 static ngx_int_t | |
805 ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
806 { | |
807 u_char *p, *dir; | |
808 size_t len; | |
809 ngx_http_dav_copy_ctx_t *copy; | |
810 #if (WIN32) | |
811 ngx_fd_t fd; | |
812 #endif | |
813 | |
814 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
815 "http copy dir time: \"%s\"", path->data); | |
816 | |
817 copy = ctx->data; | |
818 | |
819 len = copy->path.len + path->len; | |
820 | |
821 dir = ngx_alloc(len + 1, ctx->log); | |
822 if (dir == NULL) { | |
823 return NGX_ABORT; | |
824 } | |
825 | |
826 p = ngx_cpymem(dir, copy->path.data, copy->path.len); | |
827 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
828 | |
829 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
830 "http copy dir time to: \"%s\"", dir); | |
831 | |
832 #if (WIN32) | |
833 | |
834 fd = ngx_open_file(dir, NGX_FILE_RDWR, NGX_FILE_OPEN, 0); | |
835 | |
836 if (fd == NGX_INVALID_FILE) { | |
837 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir); | |
838 goto failed; | |
839 } | |
840 | |
841 if (ngx_set_file_time(NULL, fd, ctx->mtime) != NGX_OK) { | |
842 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
843 ngx_set_file_time_n " \"%s\" failed", dir); | |
844 } | |
845 | |
846 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
847 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
848 ngx_close_file_n " \"%s\" failed", dir); | |
849 } | |
850 | |
851 failed: | |
852 | |
853 #else | |
854 | |
855 if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) { | |
856 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
857 ngx_set_file_time_n " \"%s\" failed", dir); | |
858 } | |
859 | |
860 #endif | |
861 | |
862 ngx_free(dir); | |
863 | |
864 return NGX_OK; | |
865 } | |
866 | |
867 | |
868 static ngx_int_t | |
869 ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
870 { | |
871 u_char *p, *file; | |
872 size_t len; | |
873 off_t size; | |
874 ssize_t n; | |
875 ngx_fd_t fd, copy_fd; | |
876 ngx_http_dav_copy_ctx_t *copy; | |
877 u_char buf[NGX_HTTP_DAV_COPY_BLOCK]; | |
878 | |
879 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
880 "http copy file: \"%s\"", path->data); | |
881 | |
882 copy = ctx->data; | |
883 | |
884 len = copy->path.len + path->len; | |
885 | |
886 file = ngx_alloc(len + 1, ctx->log); | |
887 if (file == NULL) { | |
888 return NGX_ABORT; | |
889 } | |
890 | |
891 p = ngx_cpymem(file, copy->path.data, copy->path.len); | |
892 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
893 | |
894 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
895 "http copy file to: \"%s\"", file); | |
896 | |
897 fd = ngx_open_file(path->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); | |
898 | |
899 if (fd == NGX_INVALID_FILE) { | |
900 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, | |
901 path->data); | |
902 goto failed; | |
903 } | |
904 | |
905 copy_fd = ngx_open_file(file, NGX_FILE_WRONLY, NGX_FILE_CREATE_OR_OPEN, | |
906 ctx->access); | |
907 | |
908 if (copy_fd == NGX_INVALID_FILE) { | |
909 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, | |
910 file); | |
911 goto copy_failed; | |
912 } | |
913 | |
914 for (size = ctx->size; size > 0; size -= n) { | |
915 | |
916 n = ngx_read_fd(fd, buf, NGX_HTTP_DAV_COPY_BLOCK); | |
917 | |
918 if (n == NGX_FILE_ERROR) { | |
919 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
920 ngx_read_fd_n " \"%s\" failed", path->data); | |
921 break; | |
922 } | |
923 | |
924 if (ngx_write_fd(copy_fd, buf, n) == NGX_FILE_ERROR) { | |
925 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
926 ngx_write_fd_n " \"%s\" failed", file); | |
927 } | |
928 } | |
929 | |
930 if (ngx_set_file_time(file, copy_fd, ctx->mtime) != NGX_OK) { | |
931 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
932 ngx_set_file_time_n " \"%s\" failed", file); | |
933 } | |
934 | |
935 if (ngx_close_file(copy_fd) == NGX_FILE_ERROR) { | |
936 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
937 ngx_close_file_n " \"%s\" failed", file); | |
938 } | |
939 | |
940 copy_failed: | |
941 | |
942 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
943 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
944 ngx_close_file_n " \"%s\" failed", path->data); | |
945 } | |
946 | |
947 failed: | |
948 | |
949 ngx_free(file); | |
950 | |
951 return NGX_OK; | |
952 } | |
953 | |
954 | |
955 static ngx_int_t | |
956 ngx_http_dav_delete_path(ngx_http_request_t *r, ngx_str_t *path, ngx_uint_t dir) | |
957 { | |
958 char *failed; | |
959 ngx_tree_ctx_t tree; | |
960 | |
961 if (dir) { | |
962 | |
963 tree.init_handler = ngx_http_dav_no_init; | |
964 tree.file_handler = ngx_http_dav_delete_file; | |
965 tree.pre_tree_handler = ngx_http_dav_noop; | |
966 tree.post_tree_handler = ngx_http_dav_delete_dir; | |
967 tree.spec_handler = ngx_http_dav_delete_file; | |
968 tree.data = NULL; | |
969 tree.alloc = 0; | |
970 tree.log = r->connection->log; | |
971 | |
972 /* todo: 207 */ | |
973 | |
974 if (ngx_walk_tree(&tree, path) != NGX_OK) { | |
975 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
976 } | |
977 | |
978 if (ngx_delete_dir(path->data) != NGX_FILE_ERROR) { | |
979 return NGX_OK; | |
980 } | |
981 | |
982 failed = ngx_delete_dir_n; | |
983 | |
984 } else { | |
985 | |
986 if (ngx_delete_file(path->data) != NGX_FILE_ERROR) { | |
987 return NGX_OK; | |
988 } | |
989 | |
990 failed = ngx_delete_file_n; | |
991 } | |
992 | |
993 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
994 NGX_HTTP_NOT_FOUND, failed, path->data); | |
995 } | |
996 | |
997 | |
998 static ngx_int_t | |
999 ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt) | |
1000 { | |
1001 ngx_table_elt_t *depth; | |
1174
6be5ee17d80b
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1116
diff
changeset
|
1002 |
1116 | 1003 depth = r->headers_in.depth; |
1004 | |
1005 if (depth == NULL) { | |
1006 return dflt; | |
1007 } | |
1008 | |
1009 if (depth->value.len == 1) { | |
1010 | |
1011 if (depth->value.data[0] == '0') { | |
1012 return 0; | |
1013 } | |
1014 | |
1015 if (depth->value.data[0] == '1') { | |
1016 return 1; | |
1017 } | |
1018 | |
1019 } else { | |
1020 | |
1021 if (depth->value.len == sizeof("infinity") - 1 | |
1022 && ngx_strcmp(depth->value.data, "infinity") == 0) | |
1023 { | |
1024 return NGX_HTTP_DAV_INFINITY_DEPTH; | |
1025 } | |
1026 } | |
1027 | |
1028 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1029 "client sent invalid \"Depth\" header: \"%V\"", | |
1030 &depth->value); | |
1031 | |
1032 return NGX_HTTP_DAV_INVALID_DEPTH; | |
1033 } | |
1034 | |
1035 | |
1036 static ngx_int_t | |
1037 ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found, | |
637 | 1038 char *failed, u_char *path) |
1039 { | |
1040 ngx_int_t rc; | |
1041 ngx_uint_t level; | |
1042 | |
1043 if (err == NGX_ENOENT || err == NGX_ENOTDIR || err == NGX_ENAMETOOLONG) { | |
1044 level = NGX_LOG_ERR; | |
1045 rc = not_found; | |
1046 | |
1047 } else if (err == NGX_EACCES || err == NGX_EPERM) { | |
1048 level = NGX_LOG_ERR; | |
1049 rc = NGX_HTTP_FORBIDDEN; | |
1050 | |
1051 } else if (err == NGX_EEXIST) { | |
1052 level = NGX_LOG_ERR; | |
1053 rc = NGX_HTTP_NOT_ALLOWED; | |
1054 | |
1055 } else if (err == NGX_ENOSPC) { | |
1056 level = NGX_LOG_CRIT; | |
1057 rc = NGX_HTTP_INSUFFICIENT_STORAGE; | |
1058 | |
1059 } else { | |
1060 level = NGX_LOG_CRIT; | |
1061 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1062 } | |
1063 | |
1116 | 1064 ngx_log_error(level, log, err, "%s \"%s\" failed", failed, path); |
637 | 1065 |
1066 return rc; | |
1067 } | |
1068 | |
1069 | |
1070 static ngx_int_t | |
1071 ngx_http_dav_location(ngx_http_request_t *r, u_char *path) | |
1072 { | |
1073 u_char *location; | |
1074 ngx_http_core_loc_conf_t *clcf; | |
1075 | |
1076 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t)); | |
1077 if (r->headers_out.location == NULL) { | |
1078 return NGX_ERROR; | |
1079 } | |
1080 | |
1081 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1082 | |
1083 if (!clcf->alias && clcf->root_lengths == NULL) { | |
1084 location = path + clcf->root.len; | |
1085 | |
1086 } else { | |
1087 location = ngx_palloc(r->pool, r->uri.len); | |
1088 if (location == NULL) { | |
1089 return NGX_ERROR; | |
1090 } | |
1091 | |
1092 ngx_memcpy(location, r->uri.data, r->uri.len); | |
1093 } | |
1094 | |
1095 /* | |
1096 * we do not need to set the r->headers_out.location->hash and | |
1097 * r->headers_out.location->key fields | |
1098 */ | |
1099 | |
1100 r->headers_out.location->value.len = r->uri.len; | |
1101 r->headers_out.location->value.data = location; | |
1102 | |
1103 return NGX_OK; | |
1104 } | |
1105 | |
1106 | |
633 | 1107 static void * |
1108 ngx_http_dav_create_loc_conf(ngx_conf_t *cf) | |
1109 { | |
1110 ngx_http_dav_loc_conf_t *conf; | |
1111 | |
1112 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dav_loc_conf_t)); | |
1113 if (conf == NULL) { | |
1114 return NGX_CONF_ERROR; | |
1115 } | |
1116 | |
1117 /* | |
1118 * set by ngx_pcalloc(): | |
1119 * | |
1120 * conf->methods = 0; | |
1121 */ | |
1122 | |
637 | 1123 conf->create_full_put_path = NGX_CONF_UNSET; |
669 | 1124 conf->access = NGX_CONF_UNSET_UINT; |
637 | 1125 |
633 | 1126 return conf; |
1127 } | |
1128 | |
1129 | |
1130 static char * | |
1131 ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
1132 { | |
1133 ngx_http_dav_loc_conf_t *prev = parent; | |
1134 ngx_http_dav_loc_conf_t *conf = child; | |
1135 | |
1136 ngx_conf_merge_bitmask_value(conf->methods, prev->methods, | |
637 | 1137 (NGX_CONF_BITMASK_SET|NGX_HTTP_DAV_OFF)); |
1138 | |
1139 ngx_conf_merge_value(conf->create_full_put_path, prev->create_full_put_path, | |
1140 0); | |
633 | 1141 |
669 | 1142 ngx_conf_merge_uint_value(conf->access, prev->access, 0600); |
1143 | |
633 | 1144 return NGX_CONF_OK; |
1145 } | |
1146 | |
1147 | |
1148 static ngx_int_t | |
681 | 1149 ngx_http_dav_init(ngx_conf_t *cf) |
633 | 1150 { |
1151 ngx_http_handler_pt *h; | |
1152 ngx_http_core_main_conf_t *cmcf; | |
1153 | |
681 | 1154 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
633 | 1155 |
1156 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
1157 if (h == NULL) { | |
1158 return NGX_ERROR; | |
1159 } | |
1160 | |
1161 *h = ngx_http_dav_handler; | |
1162 | |
1163 return NGX_OK; | |
1164 } |