src/http/modules/ngx_http_dav_module.c - nginx-1.7.10

Global variables defined

Data types defined

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>


  8. #define NGX_HTTP_DAV_OFF             2


  9. #define NGX_HTTP_DAV_NO_DEPTH        -3
  10. #define NGX_HTTP_DAV_INVALID_DEPTH   -2
  11. #define NGX_HTTP_DAV_INFINITY_DEPTH  -1


  12. typedef struct {
  13.     ngx_uint_t  methods;
  14.     ngx_uint_t  access;
  15.     ngx_uint_t  min_delete_depth;
  16.     ngx_flag_t  create_full_put_path;
  17. } ngx_http_dav_loc_conf_t;


  18. typedef struct {
  19.     ngx_str_t   path;
  20.     size_t      len;
  21. } ngx_http_dav_copy_ctx_t;


  22. static ngx_int_t ngx_http_dav_handler(ngx_http_request_t *r);

  23. static void ngx_http_dav_put_handler(ngx_http_request_t *r);

  24. static ngx_int_t ngx_http_dav_delete_handler(ngx_http_request_t *r);
  25. static ngx_int_t ngx_http_dav_delete_path(ngx_http_request_t *r,
  26.     ngx_str_t *path, ngx_uint_t dir);
  27. static ngx_int_t ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path);
  28. static ngx_int_t ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path);
  29. static ngx_int_t ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path);

  30. static ngx_int_t ngx_http_dav_mkcol_handler(ngx_http_request_t *r,
  31.     ngx_http_dav_loc_conf_t *dlcf);

  32. static ngx_int_t ngx_http_dav_copy_move_handler(ngx_http_request_t *r);
  33. static ngx_int_t ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path);
  34. static ngx_int_t ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx,
  35.     ngx_str_t *path);
  36. static ngx_int_t ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx,
  37.     ngx_str_t *path);

  38. static ngx_int_t ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt);
  39. static ngx_int_t ngx_http_dav_error(ngx_log_t *log, ngx_err_t err,
  40.     ngx_int_t not_found, char *failed, u_char *path);
  41. static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r, u_char *path);
  42. static void *ngx_http_dav_create_loc_conf(ngx_conf_t *cf);
  43. static char *ngx_http_dav_merge_loc_conf(ngx_conf_t *cf,
  44.     void *parent, void *child);
  45. static ngx_int_t ngx_http_dav_init(ngx_conf_t *cf);


  46. static ngx_conf_bitmask_t  ngx_http_dav_methods_mask[] = {
  47.     { ngx_string("off"), NGX_HTTP_DAV_OFF },
  48.     { ngx_string("put"), NGX_HTTP_PUT },
  49.     { ngx_string("delete"), NGX_HTTP_DELETE },
  50.     { ngx_string("mkcol"), NGX_HTTP_MKCOL },
  51.     { ngx_string("copy"), NGX_HTTP_COPY },
  52.     { ngx_string("move"), NGX_HTTP_MOVE },
  53.     { ngx_null_string, 0 }
  54. };


  55. static ngx_command_t  ngx_http_dav_commands[] = {

  56.     { ngx_string("dav_methods"),
  57.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  58.       ngx_conf_set_bitmask_slot,
  59.       NGX_HTTP_LOC_CONF_OFFSET,
  60.       offsetof(ngx_http_dav_loc_conf_t, methods),
  61.       &ngx_http_dav_methods_mask },

  62.     { ngx_string("create_full_put_path"),
  63.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  64.       ngx_conf_set_flag_slot,
  65.       NGX_HTTP_LOC_CONF_OFFSET,
  66.       offsetof(ngx_http_dav_loc_conf_t, create_full_put_path),
  67.       NULL },

  68.     { ngx_string("min_delete_depth"),
  69.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  70.       ngx_conf_set_num_slot,
  71.       NGX_HTTP_LOC_CONF_OFFSET,
  72.       offsetof(ngx_http_dav_loc_conf_t, min_delete_depth),
  73.       NULL },

  74.     { ngx_string("dav_access"),
  75.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
  76.       ngx_conf_set_access_slot,
  77.       NGX_HTTP_LOC_CONF_OFFSET,
  78.       offsetof(ngx_http_dav_loc_conf_t, access),
  79.       NULL },

  80.       ngx_null_command
  81. };


  82. static ngx_http_module_t  ngx_http_dav_module_ctx = {
  83.     NULL,                                  /* preconfiguration */
  84.     ngx_http_dav_init,                     /* postconfiguration */

  85.     NULL,                                  /* create main configuration */
  86.     NULL,                                  /* init main configuration */

  87.     NULL,                                  /* create server configuration */
  88.     NULL,                                  /* merge server configuration */

  89.     ngx_http_dav_create_loc_conf,          /* create location configuration */
  90.     ngx_http_dav_merge_loc_conf            /* merge location configuration */
  91. };


  92. ngx_module_t  ngx_http_dav_module = {
  93.     NGX_MODULE_V1,
  94.     &ngx_http_dav_module_ctx,              /* module context */
  95.     ngx_http_dav_commands,                 /* module directives */
  96.     NGX_HTTP_MODULE,                       /* module type */
  97.     NULL,                                  /* init master */
  98.     NULL,                                  /* init module */
  99.     NULL,                                  /* init process */
  100.     NULL,                                  /* init thread */
  101.     NULL,                                  /* exit thread */
  102.     NULL,                                  /* exit process */
  103.     NULL,                                  /* exit master */
  104.     NGX_MODULE_V1_PADDING
  105. };


  106. static ngx_int_t
  107. ngx_http_dav_handler(ngx_http_request_t *r)
  108. {
  109.     ngx_int_t                 rc;
  110.     ngx_http_dav_loc_conf_t  *dlcf;

  111.     dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);

  112.     if (!(r->method & dlcf->methods)) {
  113.         return NGX_DECLINED;
  114.     }

  115.     switch (r->method) {

  116.     case NGX_HTTP_PUT:

  117.         if (r->uri.data[r->uri.len - 1] == '/') {
  118.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  119.                           "cannot PUT to a collection");
  120.             return NGX_HTTP_CONFLICT;
  121.         }

  122.         r->request_body_in_file_only = 1;
  123.         r->request_body_in_persistent_file = 1;
  124.         r->request_body_in_clean_file = 1;
  125.         r->request_body_file_group_access = 1;
  126.         r->request_body_file_log_level = 0;

  127.         rc = ngx_http_read_client_request_body(r, ngx_http_dav_put_handler);

  128.         if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  129.             return rc;
  130.         }

  131.         return NGX_DONE;

  132.     case NGX_HTTP_DELETE:

  133.         return ngx_http_dav_delete_handler(r);

  134.     case NGX_HTTP_MKCOL:

  135.         return ngx_http_dav_mkcol_handler(r, dlcf);

  136.     case NGX_HTTP_COPY:

  137.         return ngx_http_dav_copy_move_handler(r);

  138.     case NGX_HTTP_MOVE:

  139.         return ngx_http_dav_copy_move_handler(r);
  140.     }

  141.     return NGX_DECLINED;
  142. }


  143. static void
  144. ngx_http_dav_put_handler(ngx_http_request_t *r)
  145. {
  146.     size_t                    root;
  147.     time_t                    date;
  148.     ngx_str_t                *temp, path;
  149.     ngx_uint_t                status;
  150.     ngx_file_info_t           fi;
  151.     ngx_ext_rename_file_t     ext;
  152.     ngx_http_dav_loc_conf_t  *dlcf;

  153.     if (r->request_body == NULL || r->request_body->temp_file == NULL) {
  154.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  155.         return;
  156.     }

  157.     if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
  158.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  159.         return;
  160.     }

  161.     path.len--;

  162.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  163.                    "http put filename: \"%s\"", path.data);

  164.     temp = &r->request_body->temp_file->file.name;

  165.     if (ngx_file_info(path.data, &fi) == NGX_FILE_ERROR) {
  166.         status = NGX_HTTP_CREATED;

  167.     } else {
  168.         status = NGX_HTTP_NO_CONTENT;

  169.         if (ngx_is_dir(&fi)) {
  170.             ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR,
  171.                           "\"%s\" could not be created", path.data);

  172.             if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) {
  173.                 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
  174.                               ngx_delete_file_n " \"%s\" failed",
  175.                               temp->data);
  176.             }

  177.             ngx_http_finalize_request(r, NGX_HTTP_CONFLICT);
  178.             return;
  179.         }
  180.     }

  181.     dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);

  182.     ext.access = dlcf->access;
  183.     ext.path_access = dlcf->access;
  184.     ext.time = -1;
  185.     ext.create_path = dlcf->create_full_put_path;
  186.     ext.delete_file = 1;
  187.     ext.log = r->connection->log;

  188.     if (r->headers_in.date) {
  189.         date = ngx_http_parse_time(r->headers_in.date->value.data,
  190.                                    r->headers_in.date->value.len);

  191.         if (date != NGX_ERROR) {
  192.             ext.time = date;
  193.             ext.fd = r->request_body->temp_file->file.fd;
  194.         }
  195.     }

  196.     if (ngx_ext_rename_file(temp, &path, &ext) != NGX_OK) {
  197.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  198.         return;
  199.     }

  200.     if (status == NGX_HTTP_CREATED) {
  201.         if (ngx_http_dav_location(r, path.data) != NGX_OK) {
  202.             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  203.             return;
  204.         }

  205.         r->headers_out.content_length_n = 0;
  206.     }

  207.     r->headers_out.status = status;
  208.     r->header_only = 1;

  209.     ngx_http_finalize_request(r, ngx_http_send_header(r));
  210.     return;
  211. }


  212. static ngx_int_t
  213. ngx_http_dav_delete_handler(ngx_http_request_t *r)
  214. {
  215.     size_t                    root;
  216.     ngx_err_t                 err;
  217.     ngx_int_t                 rc, depth;
  218.     ngx_uint_t                i, d, dir;
  219.     ngx_str_t                 path;
  220.     ngx_file_info_t           fi;
  221.     ngx_http_dav_loc_conf_t  *dlcf;

  222.     if (r->headers_in.content_length_n > 0) {
  223.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  224.                       "DELETE with body is unsupported");
  225.         return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
  226.     }

  227.     dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);

  228.     if (dlcf->min_delete_depth) {
  229.         d = 0;

  230.         for (i = 0; i < r->uri.len; /* void */) {
  231.             if (r->uri.data[i++] == '/') {
  232.                 if (++d >= dlcf->min_delete_depth && i < r->uri.len) {
  233.                     goto ok;
  234.                 }
  235.             }
  236.         }

  237.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  238.                       "insufficient URI depth:%i to DELETE", d);
  239.         return NGX_HTTP_CONFLICT;
  240.     }

  241. ok:

  242.     if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
  243.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  244.     }

  245.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  246.                    "http delete filename: \"%s\"", path.data);

  247.     if (ngx_link_info(path.data, &fi) == NGX_FILE_ERROR) {
  248.         err = ngx_errno;

  249.         rc = (err == NGX_ENOTDIR) ? NGX_HTTP_CONFLICT : NGX_HTTP_NOT_FOUND;

  250.         return ngx_http_dav_error(r->connection->log, err,
  251.                                   rc, ngx_link_info_n, path.data);
  252.     }

  253.     if (ngx_is_dir(&fi)) {

  254.         if (r->uri.data[r->uri.len - 1] != '/') {
  255.             ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR,
  256.                           "DELETE \"%s\" failed", path.data);
  257.             return NGX_HTTP_CONFLICT;
  258.         }

  259.         depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH);

  260.         if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) {
  261.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  262.                           "\"Depth\" header must be infinity");
  263.             return NGX_HTTP_BAD_REQUEST;
  264.         }

  265.         path.len -= 2/* omit "/\0" */

  266.         dir = 1;

  267.     } else {

  268.         /*
  269.          * we do not need to test (r->uri.data[r->uri.len - 1] == '/')
  270.          * because ngx_link_info("/file/") returned NGX_ENOTDIR above
  271.          */

  272.         depth = ngx_http_dav_depth(r, 0);

  273.         if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) {
  274.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  275.                           "\"Depth\" header must be 0 or infinity");
  276.             return NGX_HTTP_BAD_REQUEST;
  277.         }

  278.         dir = 0;
  279.     }

  280.     rc = ngx_http_dav_delete_path(r, &path, dir);

  281.     if (rc == NGX_OK) {
  282.         return NGX_HTTP_NO_CONTENT;
  283.     }

  284.     return rc;
  285. }


  286. static ngx_int_t
  287. ngx_http_dav_delete_path(ngx_http_request_t *r, ngx_str_t *path, ngx_uint_t dir)
  288. {
  289.     char            *failed;
  290.     ngx_tree_ctx_t   tree;

  291.     if (dir) {

  292.         tree.init_handler = NULL;
  293.         tree.file_handler = ngx_http_dav_delete_file;
  294.         tree.pre_tree_handler = ngx_http_dav_noop;
  295.         tree.post_tree_handler = ngx_http_dav_delete_dir;
  296.         tree.spec_handler = ngx_http_dav_delete_file;
  297.         tree.data = NULL;
  298.         tree.alloc = 0;
  299.         tree.log = r->connection->log;

  300.         /* TODO: 207 */

  301.         if (ngx_walk_tree(&tree, path) != NGX_OK) {
  302.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  303.         }

  304.         if (ngx_delete_dir(path->data) != NGX_FILE_ERROR) {
  305.             return NGX_OK;
  306.         }

  307.         failed = ngx_delete_dir_n;

  308.     } else {

  309.         if (ngx_delete_file(path->data) != NGX_FILE_ERROR) {
  310.             return NGX_OK;
  311.         }

  312.         failed = ngx_delete_file_n;
  313.     }

  314.     return ngx_http_dav_error(r->connection->log, ngx_errno,
  315.                               NGX_HTTP_NOT_FOUND, failed, path->data);
  316. }


  317. static ngx_int_t
  318. ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  319. {
  320.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
  321.                    "http delete dir: \"%s\"", path->data);

  322.     if (ngx_delete_dir(path->data) == NGX_FILE_ERROR) {

  323.         /* TODO: add to 207 */

  324.         (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_dir_n,
  325.                                   path->data);
  326.     }

  327.     return NGX_OK;
  328. }


  329. static ngx_int_t
  330. ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  331. {
  332.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
  333.                    "http delete file: \"%s\"", path->data);

  334.     if (ngx_delete_file(path->data) == NGX_FILE_ERROR) {

  335.         /* TODO: add to 207 */

  336.         (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_file_n,
  337.                                   path->data);
  338.     }

  339.     return NGX_OK;
  340. }


  341. static ngx_int_t
  342. ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  343. {
  344.     return NGX_OK;
  345. }


  346. static ngx_int_t
  347. ngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf)
  348. {
  349.     u_char    *p;
  350.     size_t     root;
  351.     ngx_str_t  path;

  352.     if (r->headers_in.content_length_n > 0) {
  353.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  354.                       "MKCOL with body is unsupported");
  355.         return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
  356.     }

  357.     if (r->uri.data[r->uri.len - 1] != '/') {
  358.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  359.                       "MKCOL can create a collection only");
  360.         return NGX_HTTP_CONFLICT;
  361.     }

  362.     p = ngx_http_map_uri_to_path(r, &path, &root, 0);
  363.     if (p == NULL) {
  364.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  365.     }

  366.     *(p - 1) = '\0';
  367.     r->uri.len--;

  368.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  369.                    "http mkcol path: \"%s\"", path.data);

  370.     if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access))
  371.         != NGX_FILE_ERROR)
  372.     {
  373.         if (ngx_http_dav_location(r, path.data) != NGX_OK) {
  374.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  375.         }

  376.         return NGX_HTTP_CREATED;
  377.     }

  378.     return ngx_http_dav_error(r->connection->log, ngx_errno,
  379.                               NGX_HTTP_CONFLICT, ngx_create_dir_n, path.data);
  380. }


  381. static ngx_int_t
  382. ngx_http_dav_copy_move_handler(ngx_http_request_t *r)
  383. {
  384.     u_char                   *p, *host, *last, ch;
  385.     size_t                    len, root;
  386.     ngx_err_t                 err;
  387.     ngx_int_t                 rc, depth;
  388.     ngx_uint_t                overwrite, slash, dir, flags;
  389.     ngx_str_t                 path, uri, duri, args;
  390.     ngx_tree_ctx_t            tree;
  391.     ngx_copy_file_t           cf;
  392.     ngx_file_info_t           fi;
  393.     ngx_table_elt_t          *dest, *over;
  394.     ngx_ext_rename_file_t     ext;
  395.     ngx_http_dav_copy_ctx_t   copy;
  396.     ngx_http_dav_loc_conf_t  *dlcf;

  397.     if (r->headers_in.content_length_n > 0) {
  398.         return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
  399.     }

  400.     dest = r->headers_in.destination;

  401.     if (dest == NULL) {
  402.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  403.                       "client sent no \"Destination\" header");
  404.         return NGX_HTTP_BAD_REQUEST;
  405.     }

  406.     p = dest->value.data;
  407.     /* there is always '\0' even after empty header value */
  408.     if (p[0] == '/') {
  409.         last = p + dest->value.len;
  410.         goto destination_done;
  411.     }

  412.     len = r->headers_in.server.len;

  413.     if (len == 0) {
  414.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  415.                       "client sent no \"Host\" header");
  416.         return NGX_HTTP_BAD_REQUEST;
  417.     }

  418. #if (NGX_HTTP_SSL)

  419.     if (r->connection->ssl) {
  420.         if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1)
  421.             != 0)
  422.         {
  423.             goto invalid_destination;
  424.         }

  425.         host = dest->value.data + sizeof("https://") - 1;

  426.     } else
  427. #endif
  428.     {
  429.         if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1)
  430.             != 0)
  431.         {
  432.             goto invalid_destination;
  433.         }

  434.         host = dest->value.data + sizeof("http://") - 1;
  435.     }

  436.     if (ngx_strncmp(host, r->headers_in.server.data, len) != 0) {
  437.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  438.                       "\"Destination\" URI \"%V\" is handled by "
  439.                       "different repository than the source URI",
  440.                       &dest->value);
  441.         return NGX_HTTP_BAD_REQUEST;
  442.     }

  443.     last = dest->value.data + dest->value.len;

  444.     for (p = host + len; p < last; p++) {
  445.         if (*p == '/') {
  446.             goto destination_done;
  447.         }
  448.     }

  449. invalid_destination:

  450.     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  451.                   "client sent invalid \"Destination\" header: \"%V\"",
  452.                   &dest->value);
  453.     return NGX_HTTP_BAD_REQUEST;

  454. destination_done:

  455.     duri.len = last - p;
  456.     duri.data = p;
  457.     flags = NGX_HTTP_LOG_UNSAFE;

  458.     if (ngx_http_parse_unsafe_uri(r, &duri, &args, &flags) != NGX_OK) {
  459.         goto invalid_destination;
  460.     }

  461.     if ((r->uri.data[r->uri.len - 1] == '/' && *(last - 1) != '/')
  462.         || (r->uri.data[r->uri.len - 1] != '/' && *(last - 1) == '/'))
  463.     {
  464.          ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  465.                        "both URI \"%V\" and \"Destination\" URI \"%V\" "
  466.                        "should be either collections or non-collections",
  467.                        &r->uri, &dest->value);
  468.          return NGX_HTTP_CONFLICT;
  469.     }

  470.     depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH);

  471.     if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) {

  472.         if (r->method == NGX_HTTP_COPY) {
  473.             if (depth != 0) {
  474.                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  475.                               "\"Depth\" header must be 0 or infinity");
  476.                 return NGX_HTTP_BAD_REQUEST;
  477.             }

  478.         } else {
  479.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  480.                           "\"Depth\" header must be infinity");
  481.             return NGX_HTTP_BAD_REQUEST;
  482.         }
  483.     }

  484.     over = r->headers_in.overwrite;

  485.     if (over) {
  486.         if (over->value.len == 1) {
  487.             ch = over->value.data[0];

  488.             if (ch == 'T' || ch == 't') {
  489.                 overwrite = 1;
  490.                 goto overwrite_done;
  491.             }

  492.             if (ch == 'F' || ch == 'f') {
  493.                 overwrite = 0;
  494.                 goto overwrite_done;
  495.             }

  496.         }

  497.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  498.                       "client sent invalid \"Overwrite\" header: \"%V\"",
  499.                       &over->value);
  500.         return NGX_HTTP_BAD_REQUEST;
  501.     }

  502.     overwrite = 1;

  503. overwrite_done:

  504.     if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
  505.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  506.     }

  507.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  508.                    "http copy from: \"%s\"", path.data);

  509.     uri = r->uri;
  510.     r->uri = duri;

  511.     if (ngx_http_map_uri_to_path(r, &copy.path, &root, 0) == NULL) {
  512.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  513.     }

  514.     r->uri = uri;

  515.     copy.path.len--;  /* omit "\0" */

  516.     if (copy.path.data[copy.path.len - 1] == '/') {
  517.         slash = 1;
  518.         copy.path.len--;
  519.         copy.path.data[copy.path.len] = '\0';

  520.     } else {
  521.         slash = 0;
  522.     }

  523.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  524.                    "http copy to: \"%s\"", copy.path.data);

  525.     if (ngx_link_info(copy.path.data, &fi) == NGX_FILE_ERROR) {
  526.         err = ngx_errno;

  527.         if (err != NGX_ENOENT) {
  528.             return ngx_http_dav_error(r->connection->log, err,
  529.                                       NGX_HTTP_NOT_FOUND, ngx_link_info_n,
  530.                                       copy.path.data);
  531.         }

  532.         /* destination does not exist */

  533.         overwrite = 0;
  534.         dir = 0;

  535.     } else {

  536.         /* destination exists */

  537.         if (ngx_is_dir(&fi) && !slash) {
  538.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  539.                           "\"%V\" could not be %Ved to collection \"%V\"",
  540.                           &r->uri, &r->method_name, &dest->value);
  541.             return NGX_HTTP_CONFLICT;
  542.         }

  543.         if (!overwrite) {
  544.             ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EEXIST,
  545.                           "\"%s\" could not be created", copy.path.data);
  546.             return NGX_HTTP_PRECONDITION_FAILED;
  547.         }

  548.         dir = ngx_is_dir(&fi);
  549.     }

  550.     if (ngx_link_info(path.data, &fi) == NGX_FILE_ERROR) {
  551.         return ngx_http_dav_error(r->connection->log, ngx_errno,
  552.                                   NGX_HTTP_NOT_FOUND, ngx_link_info_n,
  553.                                   path.data);
  554.     }

  555.     if (ngx_is_dir(&fi)) {

  556.         if (r->uri.data[r->uri.len - 1] != '/') {
  557.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  558.                           "\"%V\" is collection", &r->uri);
  559.             return NGX_HTTP_BAD_REQUEST;
  560.         }

  561.         if (overwrite) {
  562.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  563.                            "http delete: \"%s\"", copy.path.data);

  564.             rc = ngx_http_dav_delete_path(r, &copy.path, dir);

  565.             if (rc != NGX_OK) {
  566.                 return rc;
  567.             }
  568.         }
  569.     }

  570.     if (ngx_is_dir(&fi)) {

  571.         path.len -= 2/* omit "/\0" */

  572.         if (r->method == NGX_HTTP_MOVE) {
  573.             if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) {
  574.                 return NGX_HTTP_CREATED;
  575.             }
  576.         }

  577.         if (ngx_create_dir(copy.path.data, ngx_file_access(&fi))
  578.             == NGX_FILE_ERROR)
  579.         {
  580.             return ngx_http_dav_error(r->connection->log, ngx_errno,
  581.                                       NGX_HTTP_NOT_FOUND,
  582.                                       ngx_create_dir_n, copy.path.data);
  583.         }

  584.         copy.len = path.len;

  585.         tree.init_handler = NULL;
  586.         tree.file_handler = ngx_http_dav_copy_tree_file;
  587.         tree.pre_tree_handler = ngx_http_dav_copy_dir;
  588.         tree.post_tree_handler = ngx_http_dav_copy_dir_time;
  589.         tree.spec_handler = ngx_http_dav_noop;
  590.         tree.data = &copy;
  591.         tree.alloc = 0;
  592.         tree.log = r->connection->log;

  593.         if (ngx_walk_tree(&tree, &path) == NGX_OK) {

  594.             if (r->method == NGX_HTTP_MOVE) {
  595.                 rc = ngx_http_dav_delete_path(r, &path, 1);

  596.                 if (rc != NGX_OK) {
  597.                     return rc;
  598.                 }
  599.             }

  600.             return NGX_HTTP_CREATED;
  601.         }

  602.     } else {

  603.         if (r->method == NGX_HTTP_MOVE) {

  604.             dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);

  605.             ext.access = 0;
  606.             ext.path_access = dlcf->access;
  607.             ext.time = -1;
  608.             ext.create_path = 1;
  609.             ext.delete_file = 0;
  610.             ext.log = r->connection->log;

  611.             if (ngx_ext_rename_file(&path, &copy.path, &ext) == NGX_OK) {
  612.                 return NGX_HTTP_NO_CONTENT;
  613.             }

  614.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  615.         }

  616.         dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);

  617.         cf.size = ngx_file_size(&fi);
  618.         cf.buf_size = 0;
  619.         cf.access = dlcf->access;
  620.         cf.time = ngx_file_mtime(&fi);
  621.         cf.log = r->connection->log;

  622.         if (ngx_copy_file(path.data, copy.path.data, &cf) == NGX_OK) {
  623.             return NGX_HTTP_NO_CONTENT;
  624.         }
  625.     }

  626.     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  627. }


  628. static ngx_int_t
  629. ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  630. {
  631.     u_char                   *p, *dir;
  632.     size_t                    len;
  633.     ngx_http_dav_copy_ctx_t  *copy;

  634.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
  635.                    "http copy dir: \"%s\"", path->data);

  636.     copy = ctx->data;

  637.     len = copy->path.len + path->len;

  638.     dir = ngx_alloc(len + 1, ctx->log);
  639.     if (dir == NULL) {
  640.         return NGX_ABORT;
  641.     }

  642.     p = ngx_cpymem(dir, copy->path.data, copy->path.len);
  643.     (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);

  644.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
  645.                    "http copy dir to: \"%s\"", dir);

  646.     if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) {
  647.         (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n,
  648.                                   dir);
  649.     }

  650.     ngx_free(dir);

  651.     return NGX_OK;
  652. }


  653. static ngx_int_t
  654. ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  655. {
  656.     u_char                   *p, *dir;
  657.     size_t                    len;
  658.     ngx_http_dav_copy_ctx_t  *copy;

  659.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
  660.                    "http copy dir time: \"%s\"", path->data);

  661.     copy = ctx->data;

  662.     len = copy->path.len + path->len;

  663.     dir = ngx_alloc(len + 1, ctx->log);
  664.     if (dir == NULL) {
  665.         return NGX_ABORT;
  666.     }

  667.     p = ngx_cpymem(dir, copy->path.data, copy->path.len);
  668.     (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);

  669.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
  670.                    "http copy dir time to: \"%s\"", dir);

  671. #if (NGX_WIN32)
  672.     {
  673.     ngx_fd_t  fd;

  674.     fd = ngx_open_file(dir, NGX_FILE_RDWR, NGX_FILE_OPEN, 0);

  675.     if (fd == NGX_INVALID_FILE) {
  676.         (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir);
  677.         goto failed;
  678.     }

  679.     if (ngx_set_file_time(NULL, fd, ctx->mtime) != NGX_OK) {
  680.         ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
  681.                       ngx_set_file_time_n " \"%s\" failed", dir);
  682.     }

  683.     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
  684.         ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
  685.                       ngx_close_file_n " \"%s\" failed", dir);
  686.     }
  687.     }

  688. failed:

  689. #else

  690.     if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) {
  691.         ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
  692.                       ngx_set_file_time_n " \"%s\" failed", dir);
  693.     }

  694. #endif

  695.     ngx_free(dir);

  696.     return NGX_OK;
  697. }


  698. static ngx_int_t
  699. ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  700. {
  701.     u_char                   *p, *file;
  702.     size_t                    len;
  703.     ngx_copy_file_t           cf;
  704.     ngx_http_dav_copy_ctx_t  *copy;

  705.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
  706.                    "http copy file: \"%s\"", path->data);

  707.     copy = ctx->data;

  708.     len = copy->path.len + path->len;

  709.     file = ngx_alloc(len + 1, ctx->log);
  710.     if (file == NULL) {
  711.         return NGX_ABORT;
  712.     }

  713.     p = ngx_cpymem(file, copy->path.data, copy->path.len);
  714.     (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);

  715.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
  716.                    "http copy file to: \"%s\"", file);

  717.     cf.size = ctx->size;
  718.     cf.buf_size = 0;
  719.     cf.access = ctx->access;
  720.     cf.time = ctx->mtime;
  721.     cf.log = ctx->log;

  722.     (void) ngx_copy_file(path->data, file, &cf);

  723.     ngx_free(file);

  724.     return NGX_OK;
  725. }


  726. static ngx_int_t
  727. ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt)
  728. {
  729.     ngx_table_elt_t  *depth;

  730.     depth = r->headers_in.depth;

  731.     if (depth == NULL) {
  732.         return dflt;
  733.     }

  734.     if (depth->value.len == 1) {

  735.         if (depth->value.data[0] == '0') {
  736.             return 0;
  737.         }

  738.         if (depth->value.data[0] == '1') {
  739.             return 1;
  740.         }

  741.     } else {

  742.         if (depth->value.len == sizeof("infinity") - 1
  743.             && ngx_strcmp(depth->value.data, "infinity") == 0)
  744.         {
  745.             return NGX_HTTP_DAV_INFINITY_DEPTH;
  746.         }
  747.     }

  748.     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  749.                   "client sent invalid \"Depth\" header: \"%V\"",
  750.                   &depth->value);

  751.     return NGX_HTTP_DAV_INVALID_DEPTH;
  752. }


  753. static ngx_int_t
  754. ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found,
  755.     char *failed, u_char *path)
  756. {
  757.     ngx_int_t   rc;
  758.     ngx_uint_t  level;

  759.     if (err == NGX_ENOENT || err == NGX_ENOTDIR || err == NGX_ENAMETOOLONG) {
  760.         level = NGX_LOG_ERR;
  761.         rc = not_found;

  762.     } else if (err == NGX_EACCES || err == NGX_EPERM) {
  763.         level = NGX_LOG_ERR;
  764.         rc = NGX_HTTP_FORBIDDEN;

  765.     } else if (err == NGX_EEXIST) {
  766.         level = NGX_LOG_ERR;
  767.         rc = NGX_HTTP_NOT_ALLOWED;

  768.     } else if (err == NGX_ENOSPC) {
  769.         level = NGX_LOG_CRIT;
  770.         rc = NGX_HTTP_INSUFFICIENT_STORAGE;

  771.     } else {
  772.         level = NGX_LOG_CRIT;
  773.         rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
  774.     }

  775.     ngx_log_error(level, log, err, "%s \"%s\" failed", failed, path);

  776.     return rc;
  777. }


  778. static ngx_int_t
  779. ngx_http_dav_location(ngx_http_request_t *r, u_char *path)
  780. {
  781.     u_char                    *location;
  782.     ngx_http_core_loc_conf_t  *clcf;

  783.     r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
  784.     if (r->headers_out.location == NULL) {
  785.         return NGX_ERROR;
  786.     }

  787.     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  788.     if (!clcf->alias && clcf->root_lengths == NULL) {
  789.         location = path + clcf->root.len;

  790.     } else {
  791.         location = ngx_pnalloc(r->pool, r->uri.len);
  792.         if (location == NULL) {
  793.             return NGX_ERROR;
  794.         }

  795.         ngx_memcpy(location, r->uri.data, r->uri.len);
  796.     }

  797.     /*
  798.      * we do not need to set the r->headers_out.location->hash and
  799.      * r->headers_out.location->key fields
  800.      */

  801.     r->headers_out.location->value.len = r->uri.len;
  802.     r->headers_out.location->value.data = location;

  803.     return NGX_OK;
  804. }


  805. static void *
  806. ngx_http_dav_create_loc_conf(ngx_conf_t *cf)
  807. {
  808.     ngx_http_dav_loc_conf_t  *conf;

  809.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dav_loc_conf_t));
  810.     if (conf == NULL) {
  811.         return NULL;
  812.     }

  813.     /*
  814.      * set by ngx_pcalloc():
  815.      *
  816.      *     conf->methods = 0;
  817.      */

  818.     conf->min_delete_depth = NGX_CONF_UNSET_UINT;
  819.     conf->access = NGX_CONF_UNSET_UINT;
  820.     conf->create_full_put_path = NGX_CONF_UNSET;

  821.     return conf;
  822. }


  823. static char *
  824. ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  825. {
  826.     ngx_http_dav_loc_conf_t  *prev = parent;
  827.     ngx_http_dav_loc_conf_t  *conf = child;

  828.     ngx_conf_merge_bitmask_value(conf->methods, prev->methods,
  829.                          (NGX_CONF_BITMASK_SET|NGX_HTTP_DAV_OFF));

  830.     ngx_conf_merge_uint_value(conf->min_delete_depth,
  831.                          prev->min_delete_depth, 0);

  832.     ngx_conf_merge_uint_value(conf->access, prev->access, 0600);

  833.     ngx_conf_merge_value(conf->create_full_put_path,
  834.                          prev->create_full_put_path, 0);

  835.     return NGX_CONF_OK;
  836. }


  837. static ngx_int_t
  838. ngx_http_dav_init(ngx_conf_t *cf)
  839. {
  840.     ngx_http_handler_pt        *h;
  841.     ngx_http_core_main_conf_t  *cmcf;

  842.     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

  843.     h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
  844.     if (h == NULL) {
  845.         return NGX_ERROR;
  846.     }

  847.     *h = ngx_http_dav_handler;

  848.     return NGX_OK;
  849. }