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