Mercurial > hg > nginx
annotate src/http/modules/ngx_http_dav_module.c @ 1818:ae7affa52ec0
style fix
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 03 Jan 2008 17:21:13 +0000 |
parents | 20a2512aacc4 |
children | 0f907513dd3c |
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 { | |
492 size_t root; | |
493 ngx_int_t rc; | |
494 ngx_str_t path; | |
495 | |
496 if (r->headers_in.content_length_n > 0) { | |
497 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | |
498 } | |
499 | |
1370
cc114c85be0f
rename ngx_http_discard_body() to ngx_http_discard_request_body()
Igor Sysoev <igor@sysoev.ru>
parents:
1296
diff
changeset
|
500 rc = ngx_http_discard_request_body(r); |
1116 | 501 |
1374 | 502 if (rc != NGX_OK) { |
1116 | 503 return rc; |
504 } | |
505 | |
506 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
507 | |
508 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
509 "http mkcol path: \"%s\"", path.data); | |
510 | |
511 if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access)) | |
512 != NGX_FILE_ERROR) | |
513 { | |
514 if (ngx_http_dav_location(r, path.data) != NGX_OK) { | |
515 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
516 } | |
517 | |
518 return NGX_HTTP_CREATED; | |
519 } | |
520 | |
521 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
522 NGX_HTTP_BAD_REQUEST, ngx_create_dir_n, | |
523 path.data); | |
524 } | |
525 | |
526 | |
527 static ngx_int_t | |
528 ngx_http_dav_copy_move_handler(ngx_http_request_t *r) | |
529 { | |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
530 u_char *p, *desthost, *last, ch; |
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
531 size_t len, root; |
1116 | 532 ngx_err_t err; |
533 ngx_int_t rc, depth; | |
534 ngx_uint_t overwrite, slash; | |
535 ngx_str_t path, uri; | |
536 ngx_tree_ctx_t tree; | |
537 ngx_file_info_t fi; | |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
538 ngx_table_elt_t *host, *dest, *over; |
1116 | 539 ngx_http_dav_copy_ctx_t copy; |
540 | |
541 if (r->headers_in.content_length_n > 0) { | |
542 return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE; | |
543 } | |
544 | |
545 dest = r->headers_in.destination; | |
546 | |
547 if (dest == NULL) { | |
548 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
549 "client sent no \"Destination\" header"); | |
550 return NGX_HTTP_BAD_REQUEST; | |
551 } | |
552 | |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
553 host = r->headers_in.host; |
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
554 |
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
555 if (host == NULL) { |
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
556 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
|
557 "client sent no \"Host\" header"); |
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
558 return NGX_HTTP_BAD_REQUEST; |
1116 | 559 } |
560 | |
561 #if (NGX_HTTP_SSL) | |
562 | |
563 if (r->connection->ssl) { | |
564 if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1) | |
565 != 0) | |
566 { | |
567 goto invalid_destination; | |
568 } | |
569 | |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
570 desthost = dest->value.data + sizeof("https://") - 1; |
1116 | 571 |
572 } else | |
573 #endif | |
574 { | |
575 if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1) | |
576 != 0) | |
577 { | |
578 goto invalid_destination; | |
579 } | |
580 | |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
581 desthost = dest->value.data + sizeof("http://") - 1; |
1116 | 582 } |
583 | |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
584 len = r->headers_in.host_name_len; |
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
585 |
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
586 if (ngx_strncmp(desthost, host->value.data, len) != 0) { |
1116 | 587 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
588 "Destination URI \"%V\" is handled by " | |
589 "different repository than the source URI", | |
590 &dest->value); | |
591 return NGX_HTTP_BAD_REQUEST; | |
592 } | |
593 | |
594 last = dest->value.data + dest->value.len; | |
595 | |
1804
d457a1576532
several changes in server_name:
Igor Sysoev <igor@sysoev.ru>
parents:
1803
diff
changeset
|
596 for (p = desthost + len; p < last; p++) { |
1116 | 597 if (*p == '/') { |
598 goto destination_done; | |
599 } | |
600 } | |
601 | |
602 invalid_destination: | |
603 | |
604 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
605 "client sent invalid \"Destination\" header: \"%V\"", | |
606 &dest->value); | |
607 return NGX_HTTP_BAD_REQUEST; | |
608 | |
609 destination_done: | |
610 | |
611 depth = ngx_http_dav_depth(r, 0); | |
612 | |
613 if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) { | |
614 return NGX_HTTP_BAD_REQUEST; | |
615 } | |
1174
6be5ee17d80b
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1116
diff
changeset
|
616 |
1116 | 617 over = r->headers_in.overwrite; |
618 | |
619 if (over) { | |
620 if (over->value.len == 1) { | |
621 ch = over->value.data[0]; | |
622 | |
623 if (ch == 'T' || ch == 't') { | |
624 overwrite = 1; | |
625 goto overwrite_done; | |
626 } | |
627 | |
628 if (ch == 'F' || ch == 'f') { | |
629 overwrite = 0; | |
630 goto overwrite_done; | |
631 } | |
632 | |
633 } | |
634 | |
635 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
636 "client sent invalid \"Overwrite\" header: \"%V\"", | |
637 &over->value); | |
638 return NGX_HTTP_BAD_REQUEST; | |
639 } | |
640 | |
641 overwrite = 1; | |
642 | |
643 overwrite_done: | |
644 | |
1370
cc114c85be0f
rename ngx_http_discard_body() to ngx_http_discard_request_body()
Igor Sysoev <igor@sysoev.ru>
parents:
1296
diff
changeset
|
645 rc = ngx_http_discard_request_body(r); |
1116 | 646 |
1374 | 647 if (rc != NGX_OK) { |
1116 | 648 return rc; |
649 } | |
650 | |
651 ngx_http_map_uri_to_path(r, &path, &root, 0); | |
652 | |
653 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
654 "http copy from: \"%s\"", path.data); | |
655 | |
656 uri = r->uri; | |
657 | |
658 r->uri.len = last - p; | |
659 r->uri.data = p; | |
660 | |
661 ngx_http_map_uri_to_path(r, ©.path, &root, 0); | |
662 | |
663 r->uri = uri; | |
664 | |
665 copy.path.len--; /* omit "\0" */ | |
666 | |
667 if (copy.path.data[copy.path.len - 1] == '/') { | |
668 slash = 1; | |
669 copy.path.len--; | |
670 copy.path.data[copy.path.len] = '\0'; | |
671 | |
672 } else { | |
673 slash = 0; | |
674 } | |
675 | |
676 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
677 "http copy to: \"%s\"", copy.path.data); | |
678 | |
679 if (ngx_file_info(copy.path.data, &fi) == -1) { | |
680 err = ngx_errno; | |
681 | |
682 if (err != NGX_ENOENT) { | |
683 return ngx_http_dav_error(r->connection->log, err, | |
684 NGX_HTTP_NOT_FOUND, ngx_file_info_n, | |
685 copy.path.data); | |
686 } | |
687 | |
688 /* destination does not exist */ | |
689 | |
690 } else { | |
691 | |
692 /* destination exists */ | |
693 | |
694 if (ngx_is_dir(&fi) && !slash) { | |
695 return NGX_HTTP_CONFLICT; | |
696 } | |
697 | |
698 if (!overwrite) { | |
699 return NGX_HTTP_PRECONDITION_FAILED; | |
700 } | |
701 | |
702 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
703 "http delete: \"%s\"", copy.path.data); | |
704 | |
705 rc = ngx_http_dav_delete_path(r, ©.path, ngx_is_dir(&fi)); | |
706 | |
707 if (rc != NGX_OK) { | |
708 return rc; | |
709 } | |
710 } | |
711 | |
712 if (ngx_file_info(path.data, &fi) == -1) { | |
713 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
714 NGX_HTTP_NOT_FOUND, ngx_file_info_n, | |
715 path.data); | |
716 } | |
717 | |
718 | |
719 if (ngx_is_dir(&fi)) { | |
720 | |
721 if (r->uri.data[r->uri.len - 1] != '/') { | |
722 /* TODO: 301 */ | |
723 return NGX_HTTP_BAD_REQUEST; | |
724 } | |
725 | |
726 path.len -= 2; /* omit "/\0" */ | |
727 | |
728 if (r->method == NGX_HTTP_MOVE) { | |
729 if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) { | |
730 return NGX_HTTP_CREATED; | |
731 } | |
732 } | |
733 | |
734 if (ngx_create_dir(copy.path.data, ngx_file_access(&fi)) | |
735 == NGX_FILE_ERROR) | |
736 { | |
737 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
738 NGX_HTTP_NOT_FOUND, | |
739 ngx_create_dir_n, copy.path.data); | |
740 } | |
741 | |
742 copy.len = path.len; | |
743 | |
1769
a35bc4007ec3
tree.init_handler is never called
Igor Sysoev <igor@sysoev.ru>
parents:
1749
diff
changeset
|
744 tree.init_handler = NULL; |
1116 | 745 tree.file_handler = ngx_http_dav_copy_file; |
746 tree.pre_tree_handler = ngx_http_dav_copy_dir; | |
747 tree.post_tree_handler = ngx_http_dav_copy_dir_time; | |
748 tree.spec_handler = ngx_http_dav_noop; | |
749 tree.data = © | |
750 tree.alloc = 0; | |
751 tree.log = r->connection->log; | |
752 | |
753 if (ngx_walk_tree(&tree, &path) == NGX_OK) { | |
754 | |
755 if (r->method == NGX_HTTP_MOVE) { | |
756 rc = ngx_http_dav_delete_path(r, &path, 1); | |
757 | |
758 if (rc != NGX_OK) { | |
759 return rc; | |
760 } | |
761 } | |
762 | |
763 return NGX_HTTP_CREATED; | |
764 } | |
765 | |
766 } else { | |
767 | |
768 if (dest->value.data[dest->value.len - 1] == '/') { | |
769 return NGX_HTTP_BAD_REQUEST; | |
770 } | |
771 | |
772 if (r->method == NGX_HTTP_MOVE) { | |
773 if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) { | |
774 return NGX_HTTP_NO_CONTENT; | |
775 } | |
776 } | |
777 | |
778 tree.data = © | |
779 tree.log = r->connection->log; | |
780 | |
781 if (ngx_http_dav_copy_file(&tree, &path) != NGX_FILE_ERROR) { | |
782 | |
783 if (r->method == NGX_HTTP_MOVE) { | |
784 rc = ngx_http_dav_delete_path(r, &path, 0); | |
785 | |
786 if (rc != NGX_OK) { | |
787 return rc; | |
788 } | |
789 } | |
790 | |
791 return NGX_HTTP_NO_CONTENT; | |
792 } | |
793 } | |
794 | |
795 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
796 } | |
797 | |
798 | |
799 static ngx_int_t | |
800 ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
801 { | |
802 u_char *p, *dir; | |
803 size_t len; | |
804 ngx_http_dav_copy_ctx_t *copy; | |
805 | |
806 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
807 "http copy dir: \"%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 to: \"%s\"", dir); | |
823 | |
824 if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) { | |
825 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n, | |
826 dir); | |
827 } | |
828 | |
829 ngx_free(dir); | |
830 | |
831 return NGX_OK; | |
832 } | |
833 | |
834 | |
835 static ngx_int_t | |
836 ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
837 { | |
838 u_char *p, *dir; | |
839 size_t len; | |
840 ngx_http_dav_copy_ctx_t *copy; | |
841 | |
842 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
843 "http copy dir time: \"%s\"", path->data); | |
844 | |
845 copy = ctx->data; | |
846 | |
847 len = copy->path.len + path->len; | |
848 | |
849 dir = ngx_alloc(len + 1, ctx->log); | |
850 if (dir == NULL) { | |
851 return NGX_ABORT; | |
852 } | |
853 | |
854 p = ngx_cpymem(dir, copy->path.data, copy->path.len); | |
855 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
856 | |
857 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
858 "http copy dir time to: \"%s\"", dir); | |
859 | |
1803
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
860 #if (NGX_WIN32) |
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
861 { |
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
862 ngx_fd_t fd; |
1116 | 863 |
864 fd = ngx_open_file(dir, NGX_FILE_RDWR, NGX_FILE_OPEN, 0); | |
865 | |
866 if (fd == NGX_INVALID_FILE) { | |
867 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir); | |
868 goto failed; | |
869 } | |
870 | |
871 if (ngx_set_file_time(NULL, fd, ctx->mtime) != NGX_OK) { | |
872 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
873 ngx_set_file_time_n " \"%s\" failed", dir); | |
874 } | |
875 | |
876 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
877 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
878 ngx_close_file_n " \"%s\" failed", dir); | |
879 } | |
1803
7405719e4848
condition declarations inside blocks update missed in r1705
Igor Sysoev <igor@sysoev.ru>
parents:
1769
diff
changeset
|
880 } |
1116 | 881 |
882 failed: | |
883 | |
884 #else | |
885 | |
886 if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) { | |
887 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
888 ngx_set_file_time_n " \"%s\" failed", dir); | |
889 } | |
890 | |
891 #endif | |
892 | |
893 ngx_free(dir); | |
894 | |
895 return NGX_OK; | |
896 } | |
897 | |
898 | |
899 static ngx_int_t | |
900 ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, ngx_str_t *path) | |
901 { | |
902 u_char *p, *file; | |
903 size_t len; | |
904 off_t size; | |
905 ssize_t n; | |
906 ngx_fd_t fd, copy_fd; | |
907 ngx_http_dav_copy_ctx_t *copy; | |
908 u_char buf[NGX_HTTP_DAV_COPY_BLOCK]; | |
909 | |
910 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
911 "http copy file: \"%s\"", path->data); | |
912 | |
913 copy = ctx->data; | |
914 | |
915 len = copy->path.len + path->len; | |
916 | |
917 file = ngx_alloc(len + 1, ctx->log); | |
918 if (file == NULL) { | |
919 return NGX_ABORT; | |
920 } | |
921 | |
922 p = ngx_cpymem(file, copy->path.data, copy->path.len); | |
923 (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1); | |
924 | |
925 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0, | |
926 "http copy file to: \"%s\"", file); | |
927 | |
928 fd = ngx_open_file(path->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); | |
929 | |
930 if (fd == NGX_INVALID_FILE) { | |
931 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, | |
932 path->data); | |
933 goto failed; | |
934 } | |
935 | |
936 copy_fd = ngx_open_file(file, NGX_FILE_WRONLY, NGX_FILE_CREATE_OR_OPEN, | |
937 ctx->access); | |
938 | |
939 if (copy_fd == NGX_INVALID_FILE) { | |
940 (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, | |
941 file); | |
942 goto copy_failed; | |
943 } | |
944 | |
945 for (size = ctx->size; size > 0; size -= n) { | |
946 | |
947 n = ngx_read_fd(fd, buf, NGX_HTTP_DAV_COPY_BLOCK); | |
948 | |
949 if (n == NGX_FILE_ERROR) { | |
950 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
951 ngx_read_fd_n " \"%s\" failed", path->data); | |
952 break; | |
953 } | |
954 | |
955 if (ngx_write_fd(copy_fd, buf, n) == NGX_FILE_ERROR) { | |
956 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
957 ngx_write_fd_n " \"%s\" failed", file); | |
958 } | |
959 } | |
960 | |
961 if (ngx_set_file_time(file, copy_fd, ctx->mtime) != NGX_OK) { | |
962 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
963 ngx_set_file_time_n " \"%s\" failed", file); | |
964 } | |
965 | |
966 if (ngx_close_file(copy_fd) == NGX_FILE_ERROR) { | |
967 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
968 ngx_close_file_n " \"%s\" failed", file); | |
969 } | |
970 | |
971 copy_failed: | |
972 | |
973 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
974 ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno, | |
975 ngx_close_file_n " \"%s\" failed", path->data); | |
976 } | |
977 | |
978 failed: | |
979 | |
980 ngx_free(file); | |
981 | |
982 return NGX_OK; | |
983 } | |
984 | |
985 | |
986 static ngx_int_t | |
987 ngx_http_dav_delete_path(ngx_http_request_t *r, ngx_str_t *path, ngx_uint_t dir) | |
988 { | |
989 char *failed; | |
990 ngx_tree_ctx_t tree; | |
991 | |
992 if (dir) { | |
993 | |
1769
a35bc4007ec3
tree.init_handler is never called
Igor Sysoev <igor@sysoev.ru>
parents:
1749
diff
changeset
|
994 tree.init_handler = NULL; |
1116 | 995 tree.file_handler = ngx_http_dav_delete_file; |
996 tree.pre_tree_handler = ngx_http_dav_noop; | |
997 tree.post_tree_handler = ngx_http_dav_delete_dir; | |
998 tree.spec_handler = ngx_http_dav_delete_file; | |
999 tree.data = NULL; | |
1000 tree.alloc = 0; | |
1001 tree.log = r->connection->log; | |
1002 | |
1749 | 1003 /* TODO: 207 */ |
1116 | 1004 |
1005 if (ngx_walk_tree(&tree, path) != NGX_OK) { | |
1006 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1007 } | |
1008 | |
1009 if (ngx_delete_dir(path->data) != NGX_FILE_ERROR) { | |
1010 return NGX_OK; | |
1011 } | |
1012 | |
1013 failed = ngx_delete_dir_n; | |
1014 | |
1015 } else { | |
1016 | |
1017 if (ngx_delete_file(path->data) != NGX_FILE_ERROR) { | |
1018 return NGX_OK; | |
1019 } | |
1020 | |
1021 failed = ngx_delete_file_n; | |
1022 } | |
1023 | |
1024 return ngx_http_dav_error(r->connection->log, ngx_errno, | |
1025 NGX_HTTP_NOT_FOUND, failed, path->data); | |
1026 } | |
1027 | |
1028 | |
1029 static ngx_int_t | |
1030 ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt) | |
1031 { | |
1032 ngx_table_elt_t *depth; | |
1174
6be5ee17d80b
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1116
diff
changeset
|
1033 |
1116 | 1034 depth = r->headers_in.depth; |
1035 | |
1036 if (depth == NULL) { | |
1037 return dflt; | |
1038 } | |
1039 | |
1040 if (depth->value.len == 1) { | |
1041 | |
1042 if (depth->value.data[0] == '0') { | |
1043 return 0; | |
1044 } | |
1045 | |
1046 if (depth->value.data[0] == '1') { | |
1047 return 1; | |
1048 } | |
1049 | |
1050 } else { | |
1051 | |
1052 if (depth->value.len == sizeof("infinity") - 1 | |
1053 && ngx_strcmp(depth->value.data, "infinity") == 0) | |
1054 { | |
1055 return NGX_HTTP_DAV_INFINITY_DEPTH; | |
1056 } | |
1057 } | |
1058 | |
1059 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1060 "client sent invalid \"Depth\" header: \"%V\"", | |
1061 &depth->value); | |
1062 | |
1063 return NGX_HTTP_DAV_INVALID_DEPTH; | |
1064 } | |
1065 | |
1066 | |
1067 static ngx_int_t | |
1068 ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found, | |
637 | 1069 char *failed, u_char *path) |
1070 { | |
1071 ngx_int_t rc; | |
1072 ngx_uint_t level; | |
1073 | |
1074 if (err == NGX_ENOENT || err == NGX_ENOTDIR || err == NGX_ENAMETOOLONG) { | |
1075 level = NGX_LOG_ERR; | |
1076 rc = not_found; | |
1077 | |
1078 } else if (err == NGX_EACCES || err == NGX_EPERM) { | |
1079 level = NGX_LOG_ERR; | |
1080 rc = NGX_HTTP_FORBIDDEN; | |
1081 | |
1082 } else if (err == NGX_EEXIST) { | |
1083 level = NGX_LOG_ERR; | |
1084 rc = NGX_HTTP_NOT_ALLOWED; | |
1085 | |
1086 } else if (err == NGX_ENOSPC) { | |
1087 level = NGX_LOG_CRIT; | |
1088 rc = NGX_HTTP_INSUFFICIENT_STORAGE; | |
1089 | |
1090 } else { | |
1091 level = NGX_LOG_CRIT; | |
1092 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1093 } | |
1094 | |
1116 | 1095 ngx_log_error(level, log, err, "%s \"%s\" failed", failed, path); |
637 | 1096 |
1097 return rc; | |
1098 } | |
1099 | |
1100 | |
1101 static ngx_int_t | |
1102 ngx_http_dav_location(ngx_http_request_t *r, u_char *path) | |
1103 { | |
1104 u_char *location; | |
1105 ngx_http_core_loc_conf_t *clcf; | |
1106 | |
1107 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t)); | |
1108 if (r->headers_out.location == NULL) { | |
1109 return NGX_ERROR; | |
1110 } | |
1111 | |
1112 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1113 | |
1114 if (!clcf->alias && clcf->root_lengths == NULL) { | |
1115 location = path + clcf->root.len; | |
1116 | |
1117 } else { | |
1118 location = ngx_palloc(r->pool, r->uri.len); | |
1119 if (location == NULL) { | |
1120 return NGX_ERROR; | |
1121 } | |
1122 | |
1123 ngx_memcpy(location, r->uri.data, r->uri.len); | |
1124 } | |
1125 | |
1126 /* | |
1127 * we do not need to set the r->headers_out.location->hash and | |
1128 * r->headers_out.location->key fields | |
1129 */ | |
1130 | |
1131 r->headers_out.location->value.len = r->uri.len; | |
1132 r->headers_out.location->value.data = location; | |
1133 | |
1134 return NGX_OK; | |
1135 } | |
1136 | |
1137 | |
633 | 1138 static void * |
1139 ngx_http_dav_create_loc_conf(ngx_conf_t *cf) | |
1140 { | |
1141 ngx_http_dav_loc_conf_t *conf; | |
1142 | |
1143 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dav_loc_conf_t)); | |
1144 if (conf == NULL) { | |
1145 return NGX_CONF_ERROR; | |
1146 } | |
1147 | |
1148 /* | |
1149 * set by ngx_pcalloc(): | |
1150 * | |
1151 * conf->methods = 0; | |
1152 */ | |
1153 | |
1816 | 1154 conf->min_delete_depth = NGX_CONF_UNSET; |
1155 conf->access = NGX_CONF_UNSET_UINT; | |
637 | 1156 conf->create_full_put_path = NGX_CONF_UNSET; |
1157 | |
633 | 1158 return conf; |
1159 } | |
1160 | |
1161 | |
1162 static char * | |
1163 ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
1164 { | |
1165 ngx_http_dav_loc_conf_t *prev = parent; | |
1166 ngx_http_dav_loc_conf_t *conf = child; | |
1167 | |
1168 ngx_conf_merge_bitmask_value(conf->methods, prev->methods, | |
637 | 1169 (NGX_CONF_BITMASK_SET|NGX_HTTP_DAV_OFF)); |
1170 | |
1816 | 1171 ngx_conf_merge_uint_value(conf->min_delete_depth, |
1172 prev->min_delete_depth, 0); | |
633 | 1173 |
669 | 1174 ngx_conf_merge_uint_value(conf->access, prev->access, 0600); |
1175 | |
1816 | 1176 ngx_conf_merge_value(conf->create_full_put_path, |
1177 prev->create_full_put_path, 0); | |
1178 | |
633 | 1179 return NGX_CONF_OK; |
1180 } | |
1181 | |
1182 | |
1183 static ngx_int_t | |
681 | 1184 ngx_http_dav_init(ngx_conf_t *cf) |
633 | 1185 { |
1186 ngx_http_handler_pt *h; | |
1187 ngx_http_core_main_conf_t *cmcf; | |
1188 | |
681 | 1189 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
633 | 1190 |
1191 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
1192 if (h == NULL) { | |
1193 return NGX_ERROR; | |
1194 } | |
1195 | |
1196 *h = ngx_http_dav_handler; | |
1197 | |
1198 return NGX_OK; | |
1199 } |