src/http/modules/ngx_http_dav_module.c - nginx source code

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);
  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.         if (r->headers_in.content_range) {
  123.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  124.                           "PUT with range is unsupported");
  125.             return NGX_HTTP_NOT_IMPLEMENTED;
  126.         }

  127.         r->request_body_in_file_only = 1;
  128.         r->request_body_in_persistent_file = 1;
  129.         r->request_body_in_clean_file = 1;
  130.         r->request_body_file_group_access = 1;
  131.         r->request_body_file_log_level = 0;

  132.         rc = ngx_http_read_client_request_body(r, ngx_http_dav_put_handler);

  133.         if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  134.             return rc;
  135.         }

  136.         return NGX_DONE;

  137.     case NGX_HTTP_DELETE:

  138.         return ngx_http_dav_delete_handler(r);

  139.     case NGX_HTTP_MKCOL:

  140.         return ngx_http_dav_mkcol_handler(r, dlcf);

  141.     case NGX_HTTP_COPY:

  142.         return ngx_http_dav_copy_move_handler(r);

  143.     case NGX_HTTP_MOVE:

  144.         return ngx_http_dav_copy_move_handler(r);
  145.     }

  146.     return NGX_DECLINED;
  147. }


  148. static void
  149. ngx_http_dav_put_handler(ngx_http_request_t *r)
  150. {
  151.     size_t                    root;
  152.     time_t                    date;
  153.     ngx_str_t                *temp, path;
  154.     ngx_uint_t                status;
  155.     ngx_file_info_t           fi;
  156.     ngx_ext_rename_file_t     ext;
  157.     ngx_http_dav_loc_conf_t  *dlcf;

  158.     if (r->request_body == NULL) {
  159.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  160.                       "PUT request body is unavailable");
  161.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  162.         return;
  163.     }

  164.     if (r->request_body->temp_file == NULL) {
  165.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  166.                       "PUT request body must be in a file");
  167.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  168.         return;
  169.     }

  170.     if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
  171.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  172.         return;
  173.     }

  174.     path.len--;

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

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

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

  180.     } else {
  181.         status = NGX_HTTP_NO_CONTENT;

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

  185.             if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) {
  186.                 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
  187.                               ngx_delete_file_n " \"%s\" failed",
  188.                               temp->data);
  189.             }

  190.             ngx_http_finalize_request(r, NGX_HTTP_CONFLICT);
  191.             return;
  192.         }
  193.     }

  194.     dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);

  195.     ext.access = dlcf->access;
  196.     ext.path_access = dlcf->access;
  197.     ext.time = -1;
  198.     ext.create_path = dlcf->create_full_put_path;
  199.     ext.delete_file = 1;
  200.     ext.log = r->connection->log;

  201.     if (r->headers_in.date) {
  202.         date = ngx_parse_http_time(r->headers_in.date->value.data,
  203.                                    r->headers_in.date->value.len);

  204.         if (date != NGX_ERROR) {
  205.             ext.time = date;
  206.             ext.fd = r->request_body->temp_file->file.fd;
  207.         }
  208.     }

  209.     if (ngx_ext_rename_file(temp, &path, &ext) != NGX_OK) {
  210.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  211.         return;
  212.     }

  213.     if (status == NGX_HTTP_CREATED) {
  214.         if (ngx_http_dav_location(r) != NGX_OK) {
  215.             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  216.             return;
  217.         }

  218.         r->headers_out.content_length_n = 0;
  219.     }

  220.     r->headers_out.status = status;
  221.     r->header_only = 1;

  222.     ngx_http_finalize_request(r, ngx_http_send_header(r));
  223.     return;
  224. }


  225. static ngx_int_t
  226. ngx_http_dav_delete_handler(ngx_http_request_t *r)
  227. {
  228.     size_t                    root;
  229.     ngx_err_t                 err;
  230.     ngx_int_t                 rc, depth;
  231.     ngx_uint_t                i, d, dir;
  232.     ngx_str_t                 path;
  233.     ngx_file_info_t           fi;
  234.     ngx_http_dav_loc_conf_t  *dlcf;

  235.     if (r->headers_in.content_length_n > 0 || r->headers_in.chunked) {
  236.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  237.                       "DELETE with body is unsupported");
  238.         return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
  239.     }

  240.     dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);

  241.     if (dlcf->min_delete_depth) {
  242.         d = 0;

  243.         for (i = 0; i < r->uri.len; /* void */) {
  244.             if (r->uri.data[i++] == '/') {
  245.                 if (++d >= dlcf->min_delete_depth && i < r->uri.len) {
  246.                     goto ok;
  247.                 }
  248.             }
  249.         }

  250.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  251.                       "insufficient URI depth:%i to DELETE", d);
  252.         return NGX_HTTP_CONFLICT;
  253.     }

  254. ok:

  255.     if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
  256.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  257.     }

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

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

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

  263.         return ngx_http_dav_error(r->connection->log, err,
  264.                                   rc, ngx_link_info_n, path.data);
  265.     }

  266.     if (ngx_is_dir(&fi)) {

  267.         if (r->uri.data[r->uri.len - 1] != '/') {
  268.             ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR,
  269.                           "DELETE \"%s\" failed", path.data);
  270.             return NGX_HTTP_CONFLICT;
  271.         }

  272.         depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH);

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

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

  279.         dir = 1;

  280.     } else {

  281.         /*
  282.          * we do not need to test (r->uri.data[r->uri.len - 1] == '/')
  283.          * because ngx_link_info("/file/") returned NGX_ENOTDIR above
  284.          */

  285.         depth = ngx_http_dav_depth(r, 0);

  286.         if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) {
  287.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  288.                           "\"Depth\" header must be 0 or infinity");
  289.             return NGX_HTTP_BAD_REQUEST;
  290.         }

  291.         dir = 0;
  292.     }

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

  294.     if (rc == NGX_OK) {
  295.         return NGX_HTTP_NO_CONTENT;
  296.     }

  297.     return rc;
  298. }


  299. static ngx_int_t
  300. ngx_http_dav_delete_path(ngx_http_request_t *r, ngx_str_t *path, ngx_uint_t dir)
  301. {
  302.     char            *failed;
  303.     ngx_tree_ctx_t   tree;

  304.     if (dir) {

  305.         tree.init_handler = NULL;
  306.         tree.file_handler = ngx_http_dav_delete_file;
  307.         tree.pre_tree_handler = ngx_http_dav_noop;
  308.         tree.post_tree_handler = ngx_http_dav_delete_dir;
  309.         tree.spec_handler = ngx_http_dav_delete_file;
  310.         tree.data = NULL;
  311.         tree.alloc = 0;
  312.         tree.log = r->connection->log;

  313.         /* TODO: 207 */

  314.         if (ngx_walk_tree(&tree, path) != NGX_OK) {
  315.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  316.         }

  317.         if (ngx_delete_dir(path->data) != NGX_FILE_ERROR) {
  318.             return NGX_OK;
  319.         }

  320.         failed = ngx_delete_dir_n;

  321.     } else {

  322.         if (ngx_delete_file(path->data) != NGX_FILE_ERROR) {
  323.             return NGX_OK;
  324.         }

  325.         failed = ngx_delete_file_n;
  326.     }

  327.     return ngx_http_dav_error(r->connection->log, ngx_errno,
  328.                               NGX_HTTP_NOT_FOUND, failed, path->data);
  329. }


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

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

  336.         /* TODO: add to 207 */

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

  340.     return NGX_OK;
  341. }


  342. static ngx_int_t
  343. ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  344. {
  345.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
  346.                    "http delete file: \"%s\"", path->data);

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

  348.         /* TODO: add to 207 */

  349.         (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_file_n,
  350.                                   path->data);
  351.     }

  352.     return NGX_OK;
  353. }


  354. static ngx_int_t
  355. ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  356. {
  357.     return NGX_OK;
  358. }


  359. static ngx_int_t
  360. ngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf)
  361. {
  362.     u_char    *p;
  363.     size_t     root;
  364.     ngx_str_t  path;

  365.     if (r->headers_in.content_length_n > 0 || r->headers_in.chunked) {
  366.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  367.                       "MKCOL with body is unsupported");
  368.         return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
  369.     }

  370.     if (r->uri.data[r->uri.len - 1] != '/') {
  371.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  372.                       "MKCOL can create a collection only");
  373.         return NGX_HTTP_CONFLICT;
  374.     }

  375.     p = ngx_http_map_uri_to_path(r, &path, &root, 0);
  376.     if (p == NULL) {
  377.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  378.     }

  379.     *(p - 1) = '\0';

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

  382.     if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access))
  383.         != NGX_FILE_ERROR)
  384.     {
  385.         if (ngx_http_dav_location(r) != NGX_OK) {
  386.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  387.         }

  388.         return NGX_HTTP_CREATED;
  389.     }

  390.     return ngx_http_dav_error(r->connection->log, ngx_errno,
  391.                               NGX_HTTP_CONFLICT, ngx_create_dir_n, path.data);
  392. }


  393. static ngx_int_t
  394. ngx_http_dav_copy_move_handler(ngx_http_request_t *r)
  395. {
  396.     u_char                   *p, *host, *last, ch;
  397.     size_t                    len, root;
  398.     ngx_err_t                 err;
  399.     ngx_int_t                 rc, depth;
  400.     ngx_uint_t                overwrite, slash, dir, flags;
  401.     ngx_str_t                 path, uri, duri, args;
  402.     ngx_tree_ctx_t            tree;
  403.     ngx_copy_file_t           cf;
  404.     ngx_file_info_t           fi;
  405.     ngx_table_elt_t          *dest, *over;
  406.     ngx_ext_rename_file_t     ext;
  407.     ngx_http_dav_copy_ctx_t   copy;
  408.     ngx_http_dav_loc_conf_t  *dlcf;

  409.     if (r->headers_in.content_length_n > 0 || r->headers_in.chunked) {
  410.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  411.                       "COPY and MOVE with body are unsupported");
  412.         return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
  413.     }

  414.     dest = r->headers_in.destination;

  415.     if (dest == NULL) {
  416.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  417.                       "client sent no \"Destination\" header");
  418.         return NGX_HTTP_BAD_REQUEST;
  419.     }

  420.     p = dest->value.data;
  421.     /* there is always '\0' even after empty header value */
  422.     if (p[0] == '/') {
  423.         last = p + dest->value.len;
  424.         goto destination_done;
  425.     }

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

  427.     if (len == 0) {
  428.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  429.                       "client sent no \"Host\" header");
  430.         return NGX_HTTP_BAD_REQUEST;
  431.     }

  432. #if (NGX_HTTP_SSL)

  433.     if (r->connection->ssl) {
  434.         if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1)
  435.             != 0)
  436.         {
  437.             goto invalid_destination;
  438.         }

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

  440.     } else
  441. #endif
  442.     {
  443.         if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1)
  444.             != 0)
  445.         {
  446.             goto invalid_destination;
  447.         }

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

  450.     if (ngx_strncmp(host, r->headers_in.server.data, len) != 0) {
  451.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  452.                       "\"Destination\" URI \"%V\" is handled by "
  453.                       "different repository than the source URI",
  454.                       &dest->value);
  455.         return NGX_HTTP_BAD_REQUEST;
  456.     }

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

  458.     for (p = host + len; p < last; p++) {
  459.         if (*p == '/') {
  460.             goto destination_done;
  461.         }
  462.     }

  463. invalid_destination:

  464.     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  465.                   "client sent invalid \"Destination\" header: \"%V\"",
  466.                   &dest->value);
  467.     return NGX_HTTP_BAD_REQUEST;

  468. destination_done:

  469.     duri.len = last - p;
  470.     duri.data = p;
  471.     flags = NGX_HTTP_LOG_UNSAFE;

  472.     if (ngx_http_parse_unsafe_uri(r, &duri, &args, &flags) != NGX_OK) {
  473.         goto invalid_destination;
  474.     }

  475.     if ((r->uri.data[r->uri.len - 1] == '/' && *(last - 1) != '/')
  476.         || (r->uri.data[r->uri.len - 1] != '/' && *(last - 1) == '/'))
  477.     {
  478.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  479.                       "both URI \"%V\" and \"Destination\" URI \"%V\" "
  480.                       "should be either collections or non-collections",
  481.                       &r->uri, &dest->value);
  482.         return NGX_HTTP_CONFLICT;
  483.     }

  484.     depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH);

  485.     if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) {

  486.         if (r->method == NGX_HTTP_COPY) {
  487.             if (depth != 0) {
  488.                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  489.                               "\"Depth\" header must be 0 or infinity");
  490.                 return NGX_HTTP_BAD_REQUEST;
  491.             }

  492.         } else {
  493.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  494.                           "\"Depth\" header must be infinity");
  495.             return NGX_HTTP_BAD_REQUEST;
  496.         }
  497.     }

  498.     over = r->headers_in.overwrite;

  499.     if (over) {
  500.         if (over->value.len == 1) {
  501.             ch = over->value.data[0];

  502.             if (ch == 'T' || ch == 't') {
  503.                 overwrite = 1;
  504.                 goto overwrite_done;
  505.             }

  506.             if (ch == 'F' || ch == 'f') {
  507.                 overwrite = 0;
  508.                 goto overwrite_done;
  509.             }

  510.         }

  511.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  512.                       "client sent invalid \"Overwrite\" header: \"%V\"",
  513.                       &over->value);
  514.         return NGX_HTTP_BAD_REQUEST;
  515.     }

  516.     overwrite = 1;

  517. overwrite_done:

  518.     if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
  519.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  520.     }

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

  523.     uri = r->uri;
  524.     r->uri = duri;

  525.     if (ngx_http_map_uri_to_path(r, &copy.path, &root, 0) == NULL) {
  526.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  527.     }

  528.     r->uri = uri;

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

  530.     if (copy.path.data[copy.path.len - 1] == '/') {
  531.         slash = 1;
  532.         copy.path.len--;
  533.         copy.path.data[copy.path.len] = '\0';

  534.     } else {
  535.         slash = 0;
  536.     }

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

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

  541.         if (err != NGX_ENOENT) {
  542.             return ngx_http_dav_error(r->connection->log, err,
  543.                                       NGX_HTTP_NOT_FOUND, ngx_link_info_n,
  544.                                       copy.path.data);
  545.         }

  546.         /* destination does not exist */

  547.         overwrite = 0;
  548.         dir = 0;

  549.     } else {

  550.         /* destination exists */

  551.         if (ngx_is_dir(&fi) && !slash) {
  552.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  553.                           "\"%V\" could not be %Ved to collection \"%V\"",
  554.                           &r->uri, &r->method_name, &dest->value);
  555.             return NGX_HTTP_CONFLICT;
  556.         }

  557.         if (!overwrite) {
  558.             ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EEXIST,
  559.                           "\"%s\" could not be created", copy.path.data);
  560.             return NGX_HTTP_PRECONDITION_FAILED;
  561.         }

  562.         dir = ngx_is_dir(&fi);
  563.     }

  564.     if (ngx_link_info(path.data, &fi) == NGX_FILE_ERROR) {
  565.         return ngx_http_dav_error(r->connection->log, ngx_errno,
  566.                                   NGX_HTTP_NOT_FOUND, ngx_link_info_n,
  567.                                   path.data);
  568.     }

  569.     if (ngx_is_dir(&fi)) {

  570.         if (r->uri.data[r->uri.len - 1] != '/') {
  571.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  572.                           "\"%V\" is collection", &r->uri);
  573.             return NGX_HTTP_BAD_REQUEST;
  574.         }

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

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

  579.             if (rc != NGX_OK) {
  580.                 return rc;
  581.             }
  582.         }
  583.     }

  584.     if (ngx_is_dir(&fi)) {

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

  586.         if (r->method == NGX_HTTP_MOVE) {
  587.             if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) {
  588.                 return NGX_HTTP_CREATED;
  589.             }
  590.         }

  591.         if (ngx_create_dir(copy.path.data, ngx_file_access(&fi))
  592.             == NGX_FILE_ERROR)
  593.         {
  594.             return ngx_http_dav_error(r->connection->log, ngx_errno,
  595.                                       NGX_HTTP_NOT_FOUND,
  596.                                       ngx_create_dir_n, copy.path.data);
  597.         }

  598.         copy.len = path.len;

  599.         tree.init_handler = NULL;
  600.         tree.file_handler = ngx_http_dav_copy_tree_file;
  601.         tree.pre_tree_handler = ngx_http_dav_copy_dir;
  602.         tree.post_tree_handler = ngx_http_dav_copy_dir_time;
  603.         tree.spec_handler = ngx_http_dav_noop;
  604.         tree.data = &copy;
  605.         tree.alloc = 0;
  606.         tree.log = r->connection->log;

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

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

  610.                 if (rc != NGX_OK) {
  611.                     return rc;
  612.                 }
  613.             }

  614.             return NGX_HTTP_CREATED;
  615.         }

  616.     } else {

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

  618.             dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);

  619.             ext.access = 0;
  620.             ext.path_access = dlcf->access;
  621.             ext.time = -1;
  622.             ext.create_path = 1;
  623.             ext.delete_file = 0;
  624.             ext.log = r->connection->log;

  625.             if (ngx_ext_rename_file(&path, &copy.path, &ext) == NGX_OK) {
  626.                 return NGX_HTTP_NO_CONTENT;
  627.             }

  628.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  629.         }

  630.         cf.size = ngx_file_size(&fi);
  631.         cf.buf_size = 0;
  632.         cf.access = ngx_file_access(&fi);
  633.         cf.time = ngx_file_mtime(&fi);
  634.         cf.log = r->connection->log;

  635.         if (ngx_copy_file(path.data, copy.path.data, &cf) == NGX_OK) {
  636.             return NGX_HTTP_NO_CONTENT;
  637.         }
  638.     }

  639.     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  640. }


  641. static ngx_int_t
  642. ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  643. {
  644.     u_char                   *p, *dir;
  645.     size_t                    len;
  646.     ngx_http_dav_copy_ctx_t  *copy;

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

  649.     copy = ctx->data;

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

  651.     dir = ngx_alloc(len + 1, ctx->log);
  652.     if (dir == NULL) {
  653.         return NGX_ABORT;
  654.     }

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

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

  659.     if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) {
  660.         (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n,
  661.                                   dir);
  662.     }

  663.     ngx_free(dir);

  664.     return NGX_OK;
  665. }


  666. static ngx_int_t
  667. ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  668. {
  669.     u_char                   *p, *dir;
  670.     size_t                    len;
  671.     ngx_http_dav_copy_ctx_t  *copy;

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

  674.     copy = ctx->data;

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

  676.     dir = ngx_alloc(len + 1, ctx->log);
  677.     if (dir == NULL) {
  678.         return NGX_ABORT;
  679.     }

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

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

  684. #if (NGX_WIN32)
  685.     {
  686.     ngx_fd_t  fd;

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

  688.     if (fd == NGX_INVALID_FILE) {
  689.         (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir);
  690.         goto failed;
  691.     }

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

  696.     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
  697.         ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
  698.                       ngx_close_file_n " \"%s\" failed", dir);
  699.     }
  700.     }

  701. failed:

  702. #else

  703.     if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) {
  704.         ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
  705.                       ngx_set_file_time_n " \"%s\" failed", dir);
  706.     }

  707. #endif

  708.     ngx_free(dir);

  709.     return NGX_OK;
  710. }


  711. static ngx_int_t
  712. ngx_http_dav_copy_tree_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  713. {
  714.     u_char                   *p, *file;
  715.     size_t                    len;
  716.     ngx_copy_file_t           cf;
  717.     ngx_http_dav_copy_ctx_t  *copy;

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

  720.     copy = ctx->data;

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

  722.     file = ngx_alloc(len + 1, ctx->log);
  723.     if (file == NULL) {
  724.         return NGX_ABORT;
  725.     }

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

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

  730.     cf.size = ctx->size;
  731.     cf.buf_size = 0;
  732.     cf.access = ctx->access;
  733.     cf.time = ctx->mtime;
  734.     cf.log = ctx->log;

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

  736.     ngx_free(file);

  737.     return NGX_OK;
  738. }


  739. static ngx_int_t
  740. ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt)
  741. {
  742.     ngx_table_elt_t  *depth;

  743.     depth = r->headers_in.depth;

  744.     if (depth == NULL) {
  745.         return dflt;
  746.     }

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

  748.         if (depth->value.data[0] == '0') {
  749.             return 0;
  750.         }

  751.         if (depth->value.data[0] == '1') {
  752.             return 1;
  753.         }

  754.     } else {

  755.         if (depth->value.len == sizeof("infinity") - 1
  756.             && ngx_strcmp(depth->value.data, "infinity") == 0)
  757.         {
  758.             return NGX_HTTP_DAV_INFINITY_DEPTH;
  759.         }
  760.     }

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

  764.     return NGX_HTTP_DAV_INVALID_DEPTH;
  765. }


  766. static ngx_int_t
  767. ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found,
  768.     char *failed, u_char *path)
  769. {
  770.     ngx_int_t   rc;
  771.     ngx_uint_t  level;

  772.     if (err == NGX_ENOENT || err == NGX_ENOTDIR || err == NGX_ENAMETOOLONG) {
  773.         level = NGX_LOG_ERR;
  774.         rc = not_found;

  775.     } else if (err == NGX_EACCES || err == NGX_EPERM) {
  776.         level = NGX_LOG_ERR;
  777.         rc = NGX_HTTP_FORBIDDEN;

  778.     } else if (err == NGX_EEXIST) {
  779.         level = NGX_LOG_ERR;
  780.         rc = NGX_HTTP_NOT_ALLOWED;

  781.     } else if (err == NGX_ENOSPC) {
  782.         level = NGX_LOG_CRIT;
  783.         rc = NGX_HTTP_INSUFFICIENT_STORAGE;

  784.     } else {
  785.         level = NGX_LOG_CRIT;
  786.         rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
  787.     }

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

  789.     return rc;
  790. }


  791. static ngx_int_t
  792. ngx_http_dav_location(ngx_http_request_t *r)
  793. {
  794.     u_char     *p;
  795.     size_t      len;
  796.     uintptr_t   escape;

  797.     r->headers_out.location = ngx_list_push(&r->headers_out.headers);
  798.     if (r->headers_out.location == NULL) {
  799.         return NGX_ERROR;
  800.     }

  801.     r->headers_out.location->hash = 1;
  802.     r->headers_out.location->next = NULL;
  803.     ngx_str_set(&r->headers_out.location->key, "Location");

  804.     escape = 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len, NGX_ESCAPE_URI);

  805.     if (escape) {
  806.         len = r->uri.len + escape;

  807.         p = ngx_pnalloc(r->pool, len);
  808.         if (p == NULL) {
  809.             ngx_http_clear_location(r);
  810.             return NGX_ERROR;
  811.         }

  812.         r->headers_out.location->value.len = len;
  813.         r->headers_out.location->value.data = p;

  814.         ngx_escape_uri(p, r->uri.data, r->uri.len, NGX_ESCAPE_URI);

  815.     } else {
  816.         r->headers_out.location->value = r->uri;
  817.     }

  818.     return NGX_OK;
  819. }


  820. static void *
  821. ngx_http_dav_create_loc_conf(ngx_conf_t *cf)
  822. {
  823.     ngx_http_dav_loc_conf_t  *conf;

  824.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dav_loc_conf_t));
  825.     if (conf == NULL) {
  826.         return NULL;
  827.     }

  828.     /*
  829.      * set by ngx_pcalloc():
  830.      *
  831.      *     conf->methods = 0;
  832.      */

  833.     conf->min_delete_depth = NGX_CONF_UNSET_UINT;
  834.     conf->access = NGX_CONF_UNSET_UINT;
  835.     conf->create_full_put_path = NGX_CONF_UNSET;

  836.     return conf;
  837. }


  838. static char *
  839. ngx_http_dav_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  840. {
  841.     ngx_http_dav_loc_conf_t  *prev = parent;
  842.     ngx_http_dav_loc_conf_t  *conf = child;

  843.     ngx_conf_merge_bitmask_value(conf->methods, prev->methods,
  844.                          (NGX_CONF_BITMASK_SET|NGX_HTTP_DAV_OFF));

  845.     ngx_conf_merge_uint_value(conf->min_delete_depth,
  846.                          prev->min_delete_depth, 0);

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

  848.     ngx_conf_merge_value(conf->create_full_put_path,
  849.                          prev->create_full_put_path, 0);

  850.     return NGX_CONF_OK;
  851. }


  852. static ngx_int_t
  853. ngx_http_dav_init(ngx_conf_t *cf)
  854. {
  855.     ngx_http_handler_pt        *h;
  856.     ngx_http_core_main_conf_t  *cmcf;

  857.     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

  858.     h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
  859.     if (h == NULL) {
  860.         return NGX_ERROR;
  861.     }

  862.     *h = ngx_http_dav_handler;

  863.     return NGX_OK;
  864. }