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