⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ngx_http_dav_module.c

📁 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) Igor Sysoev */#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>#define NGX_HTTP_DAV_COPY_BLOCK      65536#define NGX_HTTP_DAV_OFF             2#define NGX_HTTP_DAV_NO_DEPTH        -3#define NGX_HTTP_DAV_INVALID_DEPTH   -2#define NGX_HTTP_DAV_INFINITY_DEPTH  -1typedef struct {    ngx_uint_t  methods;    ngx_flag_t  create_full_put_path;    ngx_uint_t  access;} ngx_http_dav_loc_conf_t;typedef struct {    ngx_str_t   path;    size_t      len;} ngx_http_dav_copy_ctx_t;static ngx_int_t ngx_http_dav_handler(ngx_http_request_t *r);static void ngx_http_dav_put_handler(ngx_http_request_t *r);static ngx_int_t ngx_http_dav_delete_handler(ngx_http_request_t *r);static ngx_int_t ngx_http_dav_no_init(void *ctx, void *prev);static ngx_int_t ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path);static ngx_int_t ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path);static ngx_int_t ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path);static ngx_int_t ngx_http_dav_mkcol_handler(ngx_http_request_t *r,    ngx_http_dav_loc_conf_t *dlcf);static ngx_int_t ngx_http_dav_copy_move_handler(ngx_http_request_t *r);static ngx_int_t ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path);static ngx_int_t ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx,    ngx_str_t *path);static ngx_int_t ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, ngx_str_t *path);static ngx_int_t ngx_http_dav_delete_path(ngx_http_request_t *r,    ngx_str_t *path, ngx_uint_t dir);static ngx_int_t ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt);static ngx_int_t ngx_http_dav_error(ngx_log_t *log, ngx_err_t err,    ngx_int_t not_found, char *failed, u_char *path);static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r, u_char *path);static void *ngx_http_dav_create_loc_conf(ngx_conf_t *cf);static char *ngx_http_dav_merge_loc_conf(ngx_conf_t *cf,    void *parent, void *child);static ngx_int_t ngx_http_dav_init(ngx_conf_t *cf);static ngx_conf_bitmask_t  ngx_http_dav_methods_mask[] = {    { ngx_string("off"), NGX_HTTP_DAV_OFF },    { ngx_string("put"), NGX_HTTP_PUT },    { ngx_string("delete"), NGX_HTTP_DELETE },    { ngx_string("mkcol"), NGX_HTTP_MKCOL },    { ngx_string("copy"), NGX_HTTP_COPY },    { ngx_string("move"), NGX_HTTP_MOVE },    { ngx_null_string, 0 }};static ngx_command_t  ngx_http_dav_commands[] = {    { ngx_string("dav_methods"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,      ngx_conf_set_bitmask_slot,      NGX_HTTP_LOC_CONF_OFFSET,      offsetof(ngx_http_dav_loc_conf_t, methods),      &ngx_http_dav_methods_mask },    { ngx_string("create_full_put_path"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,      ngx_conf_set_flag_slot,      NGX_HTTP_LOC_CONF_OFFSET,      offsetof(ngx_http_dav_loc_conf_t, create_full_put_path),      NULL },    { ngx_string("dav_access"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,      ngx_conf_set_access_slot,      NGX_HTTP_LOC_CONF_OFFSET,      offsetof(ngx_http_dav_loc_conf_t, access),      NULL },      ngx_null_command};static ngx_http_module_t  ngx_http_dav_module_ctx = {    NULL,                                  /* preconfiguration */    ngx_http_dav_init,                     /* postconfiguration */    NULL,                                  /* create main configuration */    NULL,                                  /* init main configuration */    NULL,                                  /* create server configuration */    NULL,                                  /* merge server configuration */    ngx_http_dav_create_loc_conf,          /* create location configuration */    ngx_http_dav_merge_loc_conf            /* merge location configuration */};ngx_module_t  ngx_http_dav_module = {    NGX_MODULE_V1,    &ngx_http_dav_module_ctx,              /* module context */    ngx_http_dav_commands,                 /* module directives */    NGX_HTTP_MODULE,                       /* module type */    NULL,                                  /* init master */    NULL,                                  /* init module */    NULL,                                  /* init process */    NULL,                                  /* init thread */    NULL,                                  /* exit thread */    NULL,                                  /* exit process */    NULL,                                  /* exit master */    NGX_MODULE_V1_PADDING};static ngx_int_tngx_http_dav_handler(ngx_http_request_t *r){    ngx_int_t                 rc;    ngx_http_dav_loc_conf_t  *dlcf;    /* TODO: Win32 */    if (r->zero_in_uri) {        return NGX_DECLINED;    }    dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);    if (!(r->method & dlcf->methods)) {        return NGX_DECLINED;    }    switch (r->method) {    case NGX_HTTP_PUT:        if (r->uri.data[r->uri.len - 1] == '/') {            return NGX_HTTP_BAD_REQUEST;        }        r->request_body_in_file_only = 1;        r->request_body_in_persistent_file = 1;        r->request_body_in_clean_file = 1;        r->request_body_file_group_access = 1;        r->request_body_file_log_level = 0;        rc = ngx_http_read_client_request_body(r, ngx_http_dav_put_handler);        if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {            return rc;        }        return NGX_DONE;    case NGX_HTTP_DELETE:        return ngx_http_dav_delete_handler(r);    case NGX_HTTP_MKCOL:        return ngx_http_dav_mkcol_handler(r, dlcf);    case NGX_HTTP_COPY:        return ngx_http_dav_copy_move_handler(r);    case NGX_HTTP_MOVE:        return ngx_http_dav_copy_move_handler(r);    }    return NGX_DECLINED;}static voidngx_http_dav_put_handler(ngx_http_request_t *r){    char                     *failed;    u_char                   *name;    size_t                    root;    time_t                    date;    ngx_err_t                 err;    ngx_str_t                *temp, path;    ngx_uint_t                status, not_found;    ngx_file_info_t           fi;    ngx_http_dav_loc_conf_t  *dlcf;    ngx_http_map_uri_to_path(r, &path, &root, 0);    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "http put filename: \"%s\"", path.data);    temp = &r->request_body->temp_file->file.name;    if (ngx_file_info(path.data, &fi) == -1) {        status = NGX_HTTP_CREATED;    } else {        status = NGX_HTTP_NO_CONTENT;        if (ngx_is_dir(&fi)) {            ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_EISDIR,                          "\"%s\" could not be created", path.data);            if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) {                ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,                              ngx_delete_file_n " \"%s\" failed",                              temp->data);            }            ngx_http_finalize_request(r, NGX_HTTP_CONFLICT);            return;        }    }    dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);#if !(NGX_WIN32)    if (ngx_change_file_access(temp->data, dlcf->access) == NGX_FILE_ERROR) {        err = ngx_errno;        not_found = NGX_HTTP_INTERNAL_SERVER_ERROR;        failed = ngx_change_file_access_n;        name = temp->data;        goto failed;    }#endif    if (r->headers_in.date) {        date = ngx_http_parse_time(r->headers_in.date->value.data,                                   r->headers_in.date->value.len);        if (date != NGX_ERROR) {            if (ngx_set_file_time(temp->data,                                  r->request_body->temp_file->file.fd, date)                != NGX_OK)            {                err = ngx_errno;                not_found = NGX_HTTP_INTERNAL_SERVER_ERROR;                failed = ngx_set_file_time_n;                name = temp->data;                goto failed;            }        }    }    not_found = NGX_HTTP_CONFLICT;    failed = ngx_rename_file_n;    name = path.data;    if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) {        goto ok;    }    err = ngx_errno;    if (err == NGX_ENOENT) {        if (dlcf->create_full_put_path) {            err = ngx_create_full_path(path.data, ngx_dir_access(dlcf->access));            if (err == 0) {                if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) {                    goto ok;                }                err = ngx_errno;            }        }    }#if (NGX_WIN32)    if (err == NGX_EEXIST) {        if (ngx_win32_rename_file(temp, &path, r->pool) != NGX_ERROR) {            if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) {                goto ok;            }        }        err = ngx_errno;    }#endiffailed:    if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) {        ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,                      ngx_delete_file_n " \"%s\" failed",                      temp->data);    }    ngx_http_finalize_request(r,                              ngx_http_dav_error(r->connection->log, err,                                                 not_found, failed, name));    return;ok:    if (status == NGX_HTTP_CREATED) {        if (ngx_http_dav_location(r, path.data) != NGX_OK) {            ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);            return;        }        r->headers_out.content_length_n = 0;    }    r->headers_out.status = status;    r->header_only = 1;    ngx_http_finalize_request(r, ngx_http_send_header(r));    return;}static ngx_int_tngx_http_dav_delete_handler(ngx_http_request_t *r){    size_t           root;    ngx_int_t        rc, depth;    ngx_uint_t       dir;    ngx_str_t        path;    ngx_file_info_t  fi;    if (r->headers_in.content_length_n > 0) {        return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;    }    rc = ngx_http_discard_body(r);    if (rc != NGX_OK && rc != NGX_AGAIN) {        return rc;    }    ngx_http_map_uri_to_path(r, &path, &root, 0);    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "http delete filename: \"%s\"", path.data);    if (ngx_file_info(path.data, &fi) == -1) {        return ngx_http_dav_error(r->connection->log, ngx_errno,                                  NGX_HTTP_NOT_FOUND, ngx_file_info_n,                                  path.data);    }    if (ngx_is_dir(&fi)) {        if (r->uri.data[r->uri.len - 1] != '/') {            /* TODO: 301 */            return NGX_HTTP_BAD_REQUEST;        }        depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH);        if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) {            return NGX_HTTP_BAD_REQUEST;        }        path.len -= 2;  /* omit "/\0" */        dir = 1;    } else {        depth = ngx_http_dav_depth(r, 0);        if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) {            return NGX_HTTP_BAD_REQUEST;        }        dir = 0;    }    rc = ngx_http_dav_delete_path(r, &path, dir);    if (rc == NGX_OK) {        return NGX_HTTP_NO_CONTENT;    }    return rc;}static ngx_int_tngx_http_dav_no_init(void *ctx, void *prev){    return NGX_OK;}static ngx_int_tngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path){    return NGX_OK;}static ngx_int_tngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path){    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,                   "http delete dir: \"%s\"", path->data);    if (ngx_delete_dir(path->data) == NGX_FILE_ERROR) {        /* TODO: add to 207 */        (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_dir_n,                                  path->data);    }    return NGX_OK;}static ngx_int_tngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path){    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,                   "http delete file: \"%s\"", path->data);    if (ngx_delete_file(path->data) == NGX_FILE_ERROR) {        /* TODO: add to 207 */        (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_file_n,                                  path->data);    }    return NGX_OK;}static ngx_int_tngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf){    size_t     root;    ngx_int_t  rc;    ngx_str_t  path;    if (r->headers_in.content_length_n > 0) {        return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;    }    rc = ngx_http_discard_body(r);    if (rc != NGX_OK && rc != NGX_AGAIN) {        return rc;    }    ngx_http_map_uri_to_path(r, &path, &root, 0);    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "http mkcol path: \"%s\"", path.data);    if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access))        != NGX_FILE_ERROR)    {        if (ngx_http_dav_location(r, path.data) != NGX_OK) {            return NGX_HTTP_INTERNAL_SERVER_ERROR;        }        return NGX_HTTP_CREATED;    }    return ngx_http_dav_error(r->connection->log, ngx_errno,                              NGX_HTTP_BAD_REQUEST, ngx_create_dir_n,                              path.data);}static ngx_int_tngx_http_dav_copy_move_handler(ngx_http_request_t *r){    u_char                   *p, *host, *last, ch;    size_t                    root;    ngx_err_t                 err;    ngx_int_t                 rc, depth;    ngx_uint_t                overwrite, slash;    ngx_str_t                 path, uri;    ngx_tree_ctx_t            tree;    ngx_file_info_t           fi;    ngx_table_elt_t          *dest, *over;    ngx_http_dav_copy_ctx_t   copy;    if (r->headers_in.content_length_n > 0) {        return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;    }    dest = r->headers_in.destination;    if (dest == NULL) {        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                      "client sent no \"Destination\" header");        return NGX_HTTP_BAD_REQUEST;    }    if (dest->value.len < sizeof("http://") - 1 + r->server_name.len + 1) {        goto invalid_destination;    }#if (NGX_HTTP_SSL)    if (r->connection->ssl) {        if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1)            != 0)        {            goto invalid_destination;        }        host = dest->value.data + sizeof("https://") - 1;    } else#endif    {        if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1)            != 0)        {            goto invalid_destination;        }        host = dest->value.data + sizeof("http://") - 1;    }    if (ngx_strncmp(host, r->server_name.data, r->server_name.len) != 0) {        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                      "Destination URI \"%V\" is handled by "                      "different repository than the source URI",                      &dest->value);        return NGX_HTTP_BAD_REQUEST;    }    last = dest->value.data + dest->value.len;    for (p = host + r->server_name.len; p < last; p++) {        if (*p == '/') {            goto destination_done;        }    }invalid_destination:    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                  "client sent invalid \"Destination\" header: \"%V\"",                  &dest->value);    return NGX_HTTP_BAD_REQUEST;destination_done:    depth = ngx_http_dav_depth(r, 0);    if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) {        return NGX_HTTP_BAD_REQUEST;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -