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