~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Nginx/http/modules/ngx_http_dav_module.c

Version: ~ [ nginx-0.8.20 ] ~ [ nginx-0.7.62 ] ~ [ nginx-0.6.39 ] ~

  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 
 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 
 21 
 22 typedef struct {
 23     ngx_uint_t  methods;
 24     ngx_uint_t  access;
 25     ngx_uint_t  min_delete_depth;
 26     ngx_flag_t  create_full_put_path;
 27 } ngx_http_dav_loc_conf_t;
 28 
 29 
 30 typedef struct {
 31     ngx_str_t   path;
 32     size_t      len;
 33 } ngx_http_dav_copy_ctx_t;
 34 
 35 
 36 static ngx_int_t ngx_http_dav_handler(ngx_http_request_t *r);
 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);
 41 static ngx_int_t ngx_http_dav_delete_path(ngx_http_request_t *r,
 42     ngx_str_t *path, ngx_uint_t dir);
 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);
 45 static ngx_int_t ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path);
 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);
 54 static ngx_int_t ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx,
 55     ngx_str_t *path);
 56 
 57 static ngx_int_t ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt);
 58 static ngx_int_t ngx_http_dav_error(ngx_log_t *log, ngx_err_t err,
 59     ngx_int_t not_found, char *failed, u_char *path);
 60 static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r, u_char *path);
 61 static void *ngx_http_dav_create_loc_conf(ngx_conf_t *cf);
 62 static char *ngx_http_dav_merge_loc_conf(ngx_conf_t *cf,
 63     void *parent, void *child);
 64 static ngx_int_t ngx_http_dav_init(ngx_conf_t *cf);
 65 
 66 
 67 static ngx_conf_bitmask_t  ngx_http_dav_methods_mask[] = {
 68     { ngx_string("off"), NGX_HTTP_DAV_OFF },
 69     { ngx_string("put"), NGX_HTTP_PUT },
 70     { ngx_string("delete"), NGX_HTTP_DELETE },
 71     { ngx_string("mkcol"), NGX_HTTP_MKCOL },
 72     { ngx_string("copy"), NGX_HTTP_COPY },
 73     { ngx_string("move"), NGX_HTTP_MOVE },
 74     { ngx_null_string, 0 }
 75 };
 76 
 77 
 78 static ngx_command_t  ngx_http_dav_commands[] = {
 79 
 80     { ngx_string("dav_methods"),
 81       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
 82       ngx_conf_set_bitmask_slot,
 83       NGX_HTTP_LOC_CONF_OFFSET,
 84       offsetof(ngx_http_dav_loc_conf_t, methods),
 85       &ngx_http_dav_methods_mask },
 86 
 87     { ngx_string("create_full_put_path"),
 88       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
 89       ngx_conf_set_flag_slot,
 90       NGX_HTTP_LOC_CONF_OFFSET,
 91       offsetof(ngx_http_dav_loc_conf_t, create_full_put_path),
 92       NULL },
 93 
 94     { ngx_string("min_delete_depth"),
 95       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
 96       ngx_conf_set_num_slot,
 97       NGX_HTTP_LOC_CONF_OFFSET,
 98       offsetof(ngx_http_dav_loc_conf_t, min_delete_depth),
 99       NULL },
100 
101     { ngx_string("dav_access"),
102       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
103       ngx_conf_set_access_slot,
104       NGX_HTTP_LOC_CONF_OFFSET,
105       offsetof(ngx_http_dav_loc_conf_t, access),
106       NULL },
107 
108       ngx_null_command
109 };
110 
111 
112 static ngx_http_module_t  ngx_http_dav_module_ctx = {
113     NULL,                                  /* preconfiguration */
114     ngx_http_dav_init,                     /* postconfiguration */
115 
116     NULL,                                  /* create main configuration */
117     NULL,                                  /* init main configuration */
118 
119     NULL,                                  /* create server configuration */
120     NULL,                                  /* merge server configuration */
121 
122     ngx_http_dav_create_loc_conf,          /* create location configuration */
123     ngx_http_dav_merge_loc_conf            /* merge location configuration */
124 };
125 
126 
127 ngx_module_t  ngx_http_dav_module = {
128     NGX_MODULE_V1,
129     &ngx_http_dav_module_ctx,              /* module context */
130     ngx_http_dav_commands,                 /* module directives */
131     NGX_HTTP_MODULE,                       /* module type */
132     NULL,                                  /* init master */
133     NULL,                                  /* init module */
134     NULL,                                  /* init process */
135     NULL,                                  /* init thread */
136     NULL,                                  /* exit thread */
137     NULL,                                  /* exit process */
138     NULL,                                  /* exit master */
139     NGX_MODULE_V1_PADDING
140 };
141 
142 
143 static ngx_int_t
144 ngx_http_dav_handler(ngx_http_request_t *r)
145 {
146     ngx_int_t                 rc;
147     ngx_http_dav_loc_conf_t  *dlcf;
148 
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] == '/') {
164             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
165                           "can not PUT to a collection");
166             return NGX_HTTP_CONFLICT;
167         }
168 
169         r->request_body_in_file_only = 1;
170         r->request_body_in_persistent_file = 1;
171         r->request_body_in_clean_file = 1;
172         r->request_body_file_group_access = 1;
173         r->request_body_file_log_level = 0;
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 
185         return ngx_http_dav_delete_handler(r);
186 
187     case NGX_HTTP_MKCOL:
188 
189         return ngx_http_dav_mkcol_handler(r, dlcf);
190 
191     case NGX_HTTP_COPY:
192 
193         return ngx_http_dav_copy_move_handler(r);
194 
195     case NGX_HTTP_MOVE:
196 
197         return ngx_http_dav_copy_move_handler(r);
198     }
199 
200     return NGX_DECLINED;
201 }
202 
203 
204 static void
205 ngx_http_dav_put_handler(ngx_http_request_t *r)
206 {
207     size_t                    root;
208     time_t                    date;
209     ngx_str_t                *temp, path;
210     ngx_uint_t                status;
211     ngx_file_info_t           fi;
212     ngx_ext_rename_file_t     ext;
213     ngx_http_dav_loc_conf_t  *dlcf;
214 
215     ngx_http_map_uri_to_path(r, &path, &root, 0);
216 
217     path.len--;
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) == NGX_FILE_ERROR) {
225         status = NGX_HTTP_CREATED;
226 
227     } else {
228         status = NGX_HTTP_NO_CONTENT;
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);
233 
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;
242         }
243     }
244 
245     dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);
246 
247     ext.access = dlcf->access;
248     ext.path_access = dlcf->access;
249     ext.time = -1;
250     ext.create_path = dlcf->create_full_put_path;
251     ext.delete_file = 1;
252     ext.log = r->connection->log;
253 
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) {
259             ext.time = date;
260             ext.fd = r->request_body->temp_file->file.fd;
261         }
262     }
263 
264     if (ngx_ext_rename_file(temp, &path, &ext) != NGX_OK) {
265         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
266         return;
267     }
268 
269     if (status == NGX_HTTP_CREATED) {
270         if (ngx_http_dav_location(r, path.data) != NGX_OK) {
271             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
272             return;
273         }
274 
275         r->headers_out.content_length_n = 0;
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 
286 static ngx_int_t
287 ngx_http_dav_delete_handler(ngx_http_request_t *r)
288 {
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;
296 
297     if (r->headers_in.content_length_n > 0) {
298         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
299                       "DELETE with body is unsupported");
300         return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
301     }
302 
303     dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);
304 
305     if (dlcf->min_delete_depth) {
306         d = 0;
307 
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;
319     }
320 
321 ok:
322 
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) == NGX_FILE_ERROR) {
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);
335     }
336 
337     if (ngx_is_dir(&fi)) {
338 
339         if (r->uri.data[r->uri.len - 1] != '/') {
340             ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR,
341                           "DELETE \"%s\" failed", path.data);
342             return NGX_HTTP_CONFLICT;
343         }
344 
345         depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH);
346 
347         if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) {
348             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
349                           "\"Depth\" header must be infinity");
350             return NGX_HTTP_BAD_REQUEST;
351         }
352 
353         path.len -= 2;  /* omit "/\0" */
354 
355         dir = 1;
356 
357     } else {
358 
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 
364         depth = ngx_http_dav_depth(r, 0);
365 
366         if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) {
367             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
368                           "\"Depth\" header must be 0 or infinity");
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
386 ngx_http_dav_delete_path(ngx_http_request_t *r, ngx_str_t *path, ngx_uint_t dir)
387 {
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);
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
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
472 ngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf)
473 {
474     u_char    *p;
475     size_t     root;
476     ngx_str_t  path;
477 
478     if (r->headers_in.content_length_n > 0) {
479         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
480                       "MKCOL with body is unsupported");
481         return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
482     }
483 
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;
488     }
489 
490     p = ngx_http_map_uri_to_path(r, &path, &root, 0);
491 
492     *(p - 1) = '\0';
493     r->uri.len--;
494 
495     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
496                    "http mkcol path: \"%s\"", path.data);
497 
498     if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access))
499         != NGX_FILE_ERROR)
500     {
501         if (ngx_http_dav_location(r, path.data) != NGX_OK) {
502             return NGX_HTTP_INTERNAL_SERVER_ERROR;
503         }
504 
505         return NGX_HTTP_CREATED;
506     }
507 
508     return ngx_http_dav_error(r->connection->log, ngx_errno,
509                               NGX_HTTP_CONFLICT, ngx_create_dir_n, path.data);
510 }
511 
512 
513 static ngx_int_t
514 ngx_http_dav_copy_move_handler(ngx_http_request_t *r)
515 {
516     u_char                   *p, *host, *last, ch;
517     size_t                    len, root;
518     ngx_err_t                 err;
519     ngx_int_t                 rc, depth;
520     ngx_uint_t                overwrite, slash, dir, flags;
521     ngx_str_t                 path, uri, duri, args;
522     ngx_tree_ctx_t            tree;
523     ngx_copy_file_t           cf;
524     ngx_file_info_t           fi;
525     ngx_table_elt_t          *dest, *over;
526     ngx_ext_rename_file_t     ext;
527     ngx_http_dav_copy_ctx_t   copy;
528     ngx_http_dav_loc_conf_t  *dlcf;
529 
530     if (r->headers_in.content_length_n > 0) {
531         return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
532     }
533 
534     dest = r->headers_in.destination;
535 
536     if (dest == NULL) {
537         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
538                       "client sent no \"Destination\" header");
539         return NGX_HTTP_BAD_REQUEST;
540     }
541 
542     len = r->headers_in.server.len;
543 
544     if (len == 0) {
545         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
546                       "client sent no \"Host\" header");
547         return NGX_HTTP_BAD_REQUEST;
548     }
549 
550 #if (NGX_HTTP_SSL)
551 
552     if (r->connection->ssl) {
553         if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1)
554             != 0)
555         {
556             goto invalid_destination;
557         }
558 
559         host = dest->value.data + sizeof("https://") - 1;
560 
561     } else
562 #endif
563     {
564         if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1)
565             != 0)
566         {
567             goto invalid_destination;
568         }
569 
570         host = dest->value.data + sizeof("http://") - 1;
571     }
572 
573     if (ngx_strncmp(host, r->headers_in.server.data, len) != 0) {
574         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
575                       "\"Destination\" URI \"%V\" is handled by "
576                       "different repository than the source URI",
577                       &dest->value);
578         return NGX_HTTP_BAD_REQUEST;
579     }
580 
581     last = dest->value.data + dest->value.len;
582 
583     for (p = host + len; p < last; p++) {
584         if (*p == '/') {
585             goto destination_done;
586         }
587     }
588 
589 invalid_destination:
590 
591     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
592                   "client sent invalid \"Destination\" header: \"%V\"",
593                   &dest->value);
594     return NGX_HTTP_BAD_REQUEST;
595 
596 destination_done:
597 
598     duri.len = last - p;
599     duri.data = p;
600     flags = 0;
601 
602     if (ngx_http_parse_unsafe_uri(r, &duri, &args, &flags) != NGX_OK) {
603         goto invalid_destination;
604     }
605 
606     if ((r->uri.data[r->uri.len - 1] == '/' && *(last - 1) != '/')
607         || (r->uri.data[r->uri.len - 1] != '/' && *(last - 1) == '/'))
608     {
609          ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
610                        "both URI \"%V\" and \"Destination\" URI \"%V\" "
611                        "should be either collections or non-collections",
612                        &r->uri, &dest->value);
613          return NGX_HTTP_CONFLICT;
614     }
615 
616     depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH);
617 
618     if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) {
619 
620         if (r->method == NGX_HTTP_COPY) {
621             if (depth != 0) {
622                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
623                               "\"Depth\" header must be 0 or infinity");
624                 return NGX_HTTP_BAD_REQUEST;
625             }
626 
627         } else {
628             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
629                           "\"Depth\" header must be infinity");
630             return NGX_HTTP_BAD_REQUEST;
631         }
632     }
633 
634     over = r->headers_in.overwrite;
635 
636     if (over) {
637         if (over->value.len == 1) {
638             ch = over->value.data[0];
639 
640             if (ch == 'T' || ch == 't') {
641                 overwrite = 1;
642                 goto overwrite_done;
643             }
644 
645             if (ch == 'F' || ch == 'f') {
646                 overwrite = 0;
647                 goto overwrite_done;
648             }
649 
650         }
651 
652         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
653                       "client sent invalid \"Overwrite\" header: \"%V\"",
654                       &over->value);
655         return NGX_HTTP_BAD_REQUEST;
656     }
657 
658     overwrite = 1;
659 
660 overwrite_done:
661 
662     ngx_http_map_uri_to_path(r, &path, &root, 0);
663 
664     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
665                    "http copy from: \"%s\"", path.data);
666 
667     uri = r->uri;
668     r->uri = duri;
669 
670     ngx_http_map_uri_to_path(r, &copy.path, &root, 0);
671 
672     r->uri = uri;
673 
674     copy.path.len--;  /* omit "\0" */
675 
676     if (copy.path.data[copy.path.len - 1] == '/') {
677         slash = 1;
678         copy.path.len--;
679         copy.path.data[copy.path.len] = '\0';
680 
681     } else {
682         slash = 0;
683     }
684 
685     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
686                    "http copy to: \"%s\"", copy.path.data);
687 
688     if (ngx_file_info(copy.path.data, &fi) == NGX_FILE_ERROR) {
689         err = ngx_errno;
690 
691         if (err != NGX_ENOENT) {
692             return ngx_http_dav_error(r->connection->log, err,
693                                       NGX_HTTP_NOT_FOUND, ngx_file_info_n,
694                                       copy.path.data);
695         }
696 
697         /* destination does not exist */
698 
699         overwrite = 0;
700         dir = 0;
701 
702     } else {
703 
704         /* destination exists */
705 
706         if (ngx_is_dir(&fi) && !slash) {
707             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
708                           "\"%V\" could not be %Ved to collection \"%V\"",
709                           &r->uri, &r->method_name, &dest->value);
710             return NGX_HTTP_CONFLICT;
711         }
712 
713         if (!overwrite) {
714             ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EEXIST,
715                           "\"%s\" could not be created", copy.path.data);
716             return NGX_HTTP_PRECONDITION_FAILED;
717         }
718 
719         dir = ngx_is_dir(&fi);
720     }
721 
722     if (ngx_file_info(path.data, &fi) == NGX_FILE_ERROR) {
723         return ngx_http_dav_error(r->connection->log, ngx_errno,
724                                   NGX_HTTP_NOT_FOUND, ngx_file_info_n,
725                                   path.data);
726     }
727 
728     if (ngx_is_dir(&fi)) {
729 
730         if (r->uri.data[r->uri.len - 1] != '/') {
731             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
732                           "\"%V\" is collection", &r->uri);
733             return NGX_HTTP_BAD_REQUEST;
734         }
735 
736         if (overwrite) {
737             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
738                            "http delete: \"%s\"", copy.path.data);
739 
740             rc = ngx_http_dav_delete_path(r, &copy.path, dir);
741 
742             if (rc != NGX_OK) {
743                 return rc;
744             }
745         }
746     }
747 
748     if (ngx_is_dir(&fi)) {
749 
750         path.len -= 2;  /* omit "/\0" */
751 
752         if (r->method == NGX_HTTP_MOVE) {
753             if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) {
754                 return NGX_HTTP_CREATED;
755             }
756         }
757 
758         if (ngx_create_dir(copy.path.data, ngx_file_access(&fi))
759             == NGX_FILE_ERROR)
760         {
761             return ngx_http_dav_error(r->connection->log, ngx_errno,
762                                       NGX_HTTP_NOT_FOUND,
763                                       ngx_create_dir_n, copy.path.data);
764         }
765 
766         copy.len = path.len;
767 
768         tree.init_handler = NULL;
769         tree.file_handler = ngx_http_dav_copy_tree_file;
770         tree.pre_tree_handler = ngx_http_dav_copy_dir;
771         tree.post_tree_handler = ngx_http_dav_copy_dir_time;
772         tree.spec_handler = ngx_http_dav_noop;
773         tree.data = &copy;
774         tree.alloc = 0;
775         tree.log = r->connection->log;
776 
777         if (ngx_walk_tree(&tree, &path) == NGX_OK) {
778 
779             if (r->method == NGX_HTTP_MOVE) {
780                 rc = ngx_http_dav_delete_path(r, &path, 1);
781 
782                 if (rc != NGX_OK) {
783                     return rc;
784                 }
785             }
786 
787             return NGX_HTTP_CREATED;
788         }
789 
790     } else {
791 
792         if (r->method == NGX_HTTP_MOVE) {
793 
794             dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);
795 
796             ext.access = 0;
797             ext.path_access = dlcf->access;
798             ext.time = -1;
799             ext.create_path = 1;
800             ext.delete_file = 0;
801             ext.log = r->connection->log;
802 
803             if (ngx_ext_rename_file(&path, &copy.path, &ext) == NGX_OK) {
804                 return NGX_HTTP_NO_CONTENT;
805             }
806 
807             return NGX_HTTP_INTERNAL_SERVER_ERROR;
808         }
809 
810         dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);
811 
812         cf.size = ngx_file_size(&fi);
813         cf.buf_size = 0;
814         cf.access = dlcf->access;
815         cf.time = ngx_file_mtime(&fi);
816         cf.log = r->connection->log;
817 
818         if (ngx_copy_file(path.data, copy.path.data, &cf) == NGX_OK) {
819             return NGX_HTTP_NO_CONTENT;
820         }
821     }
822 
823     return NGX_HTTP_INTERNAL_SERVER_ERROR;
824 }
825 
826 
827 static ngx_int_t
828 ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path)
829 {
830     u_char                   *p, *dir;
831     size_t                    len;
832     ngx_http_dav_copy_ctx_t  *copy;
833 
834     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
835                    "http copy dir: \"%s\"", path->data);
836 
837     copy = ctx->data;
838 
839     len = copy->path.len + path->len;
840 
841     dir = ngx_alloc(len + 1, ctx->log);
842     if (dir == NULL) {
843         return NGX_ABORT;
844     }
845 
846     p = ngx_cpymem(dir, copy->path.data, copy->path.len);
847     (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);
848 
849     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
850                    "http copy dir to: \"%s\"", dir);
851 
852     if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) {
853         (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n,
854                                   dir);
855     }
856 
857     ngx_free(dir);
858 
859     return NGX_OK;
860 }
861 
862 
863 static ngx_int_t
864 ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path)
865 {
866     u_char                   *p, *dir;
867     size_t                    len;
868     ngx_http_dav_copy_ctx_t  *copy;
869 
870     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
871                    "http copy dir time: \"%s\"", path->data);
872 
873     copy = ctx->data;
874 
875     len = copy->path.len + path->len;
876 
877     dir = ngx_alloc(len + 1, ctx->log);
878     if (dir == NULL) {
879         return NGX_ABORT;
880     }
881 
882     p = ngx_cpymem(dir, copy->path.data, copy->path.len);
883     (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);
884 
885     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
886                    "http copy dir time to: \"%s\"", dir);
887 
888 #if (NGX_WIN32)
889     {
890     ngx_fd_t  fd;
891 
892     fd = ngx_open_file(dir, NGX_FILE_RDWR, NGX_FILE_OPEN, 0);
893 
894     if (fd == NGX_INVALID_FILE) {
895         (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir);
896         goto failed;
897     }
898 
899     if (ngx_set_file_time(NULL, fd, ctx->mtime) != NGX_OK) {
900         ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
901                       ngx_set_file_time_n " \"%s\" failed", dir);
902     }
903 
904     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
905         ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
906                       ngx_close_file_n " \"%s\" failed", dir);
907     }
908     }
909 
910 failed:
911 
912 #else
913 
914     if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) {
915         ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
916                       ngx_set_file_time_n " \"%s\" failed", dir);
917     }
918 
919 #endif
920 
921     ngx_free(dir);
922 
923     return NGX_OK;
924 }
925 
926 
927 static ngx_int_t
928 ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
929 {
930     u_char                   *p, *file;
931     size_t                    len;
932     ngx_copy_file_t           cf;
933     ngx_http_dav_copy_ctx_t  *copy;
934 
935     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
936                    "http copy file: \"%s\"", path->data);
937 
938     copy = ctx->data;
939 
940     len = copy->path.len + path->len;
941 
942     file = ngx_alloc(len + 1, ctx->log);
943     if (file == NULL) {
944         return NGX_ABORT;
945     }
946 
947     p = ngx_cpymem(file, copy->path.data, copy->path.len);
948     (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);
949 
950     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
951                    "http copy file to: \"%s\"", file);
952 
953     cf.size = ctx->size;
954     cf.buf_size = 0;
955     cf.access = ctx->access;
956     cf.time = ctx->mtime;
957     cf.log = ctx->log;
958 
959     (void) ngx_copy_file(path->data, file, &cf);
960 
961     ngx_free(file);
962 
963     return NGX_OK;
964 }
965 
966 
967 static ngx_int_t
968 ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt)
969 {
970     ngx_table_elt_t  *depth;
971 
972     depth = r->headers_in.depth;
973 
974     if (depth == NULL) {
975         return dflt;
976     }
977 
978     if (depth->value.len == 1) {
979 
980         if (depth->value.data[0] == '') {
981             return 0;
982         }
983 
984         if (depth->value.data[0] == '1') {
985             return 1;
986         }
987 
988     } else {
989 
990         if (depth->value.len == sizeof("infinity") - 1
991             && ngx_strcmp(depth->value.data, "infinity") == 0)
992         {
993             return NGX_HTTP_DAV_INFINITY_DEPTH;
994         }
995     }
996 
997     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
998                   "client sent invalid \"Depth\" header: \"%V\"",
999                   &depth->value);
1000 
1001     return NGX_HTTP_DAV_INVALID_DEPTH;
1002 }
1003 
1004 
1005 static ngx_int_t
1006 ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found,
1007     char *failed, u_char *path)
1008 {
1009     ngx_int_t   rc;
1010     ngx_uint_t  level;
1011 
1012     if (err == NGX_ENOENT || err == NGX_ENOTDIR || err == NGX_ENAMETOOLONG) {
1013         level = NGX_LOG_ERR;
1014         rc = not_found;
1015 
1016     } else if (err == NGX_EACCES || err == NGX_EPERM) {
1017         level = NGX_LOG_ERR;
1018         rc = NGX_HTTP_FORBIDDEN;
1019 
1020     } else if (err == NGX_EEXIST) {
1021         level = NGX_LOG_ERR;
1022         rc = NGX_HTTP_NOT_ALLOWED;
1023 
1024     } else if (err == NGX_ENOSPC) {
1025         level = NGX_LOG_CRIT;
1026         rc = NGX_HTTP_INSUFFICIENT_STORAGE;
1027 
1028     } else {
1029         level = NGX_LOG_CRIT;
1030         rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
1031     }
1032 
1033     ngx_log_error(level, log, err, "%s \"%s\" failed", failed, path);
1034 
1035     return rc;
1036 }
1037 
1038 
1039 static ngx_int_t
1040 ngx_http_dav_location(ngx_http_request_t *r, u_char *path)
1041 {
1042     u_char                    *location;
1043     ngx_http_core_loc_conf_t  *clcf;
1044 
1045     r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
1046     if (r->headers_out.location == NULL) {
1047         return NGX_ERROR;
1048     }
1049 
1050     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1051 
1052     if (!clcf->alias && clcf->root_lengths == NULL) {
1053         location = path + clcf->root.len;
1054 
1055     } else {
1056         location = ngx_pnalloc(r->pool, r->uri.len);
1057         if (location == NULL) {
1058             return NGX_ERROR;
1059         }
1060 
1061         ngx_memcpy(location, r->uri.data, r->uri.len);
1062     }
1063 
1064     /*
1065      * we do not need to set the r->headers_out.location->hash and
1066      * r->headers_out.location->key fields
1067      */
1068 
1069     r->headers_out.location->value.len = r->uri.len;
1070     r->headers_out.location->value.data = location;
1071 
1072     return NGX_OK;
1073 }
1074 
1075 
1076 static void *
1077 ngx_http_dav_create_loc_conf(ngx_conf_t *cf)
1078 {
1079     ngx_http_dav_loc_conf_t  *conf;
1080 
1081     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dav_loc_conf_t));
1082     if (conf == NULL) {
1083         return NULL;
1084     }
1085 
1086     /*
1087      * set by ngx_pcalloc():
1088      *
1089      *     conf->methods = 0;
1090      */
1091 
1092     conf->min_delete_depth = NGX_CONF_UNSET_UINT;
1093     conf->access = NGX_CONF_UNSET_UINT;
1094     conf->create_full_put_path = NGX_CONF_UNSET;
1095 
1096     return conf;
1097 }
1098 
1099 
1100 static char *
1101 ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
1102 {
1103     ngx_http_dav_loc_conf_t  *prev = parent;
1104     ngx_http_dav_loc_conf_t  *conf = child;
1105 
1106     ngx_conf_merge_bitmask_value(conf->methods, prev->methods,
1107                          (NGX_CONF_BITMASK_SET|NGX_HTTP_DAV_OFF));
1108 
1109     ngx_conf_merge_uint_value(conf->min_delete_depth,
1110                          prev->min_delete_depth, 0);
1111 
1112     ngx_conf_merge_uint_value(conf->access, prev->access, 0600);
1113 
1114     ngx_conf_merge_value(conf->create_full_put_path,
1115                          prev->create_full_put_path, 0);
1116 
1117     return NGX_CONF_OK;
1118 }
1119 
1120 
1121 static ngx_int_t
1122 ngx_http_dav_init(ngx_conf_t *cf)
1123 {
1124     ngx_http_handler_pt        *h;
1125     ngx_http_core_main_conf_t  *cmcf;
1126 
1127     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1128 
1129     h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
1130     if (h == NULL) {
1131         return NGX_ERROR;
1132     }
1133 
1134     *h = ngx_http_dav_handler;
1135 
1136     return NGX_OK;
1137 }
1138 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.