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

📄 ngx_http_autoindex_module.c

📁 nginx 反向代理0.7.1版本 用于实现反向代理
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) Igor Sysoev */#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>#if 0typedef struct {    ngx_buf_t     *buf;    size_t         size;    ngx_pool_t    *pool;    size_t         alloc_size;    ngx_chain_t  **last_out;} ngx_http_autoindex_ctx_t;#endiftypedef struct {    ngx_str_t      name;    size_t         utf_len;    ngx_uint_t     escape;    ngx_uint_t     dir;    time_t         mtime;    off_t          size;} ngx_http_autoindex_entry_t;typedef struct {    ngx_flag_t     enable;    ngx_flag_t     localtime;    ngx_flag_t     exact_size;} ngx_http_autoindex_loc_conf_t;#define NGX_HTTP_AUTOINDEX_PREALLOCATE  50#define NGX_HTTP_AUTOINDEX_NAME_LEN     50static int ngx_libc_cdecl ngx_http_autoindex_cmp_entries(const void *one,    const void *two);static ngx_int_t ngx_http_autoindex_error(ngx_http_request_t *r,    ngx_dir_t *dir, ngx_str_t *name);static ngx_int_t ngx_http_autoindex_init(ngx_conf_t *cf);static void *ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf);static char *ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf,    void *parent, void *child);static ngx_command_t  ngx_http_autoindex_commands[] = {    { ngx_string("autoindex"),      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_autoindex_loc_conf_t, enable),      NULL },    { ngx_string("autoindex_localtime"),      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_autoindex_loc_conf_t, localtime),      NULL },    { ngx_string("autoindex_exact_size"),      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_autoindex_loc_conf_t, exact_size),      NULL },      ngx_null_command};static ngx_http_module_t  ngx_http_autoindex_module_ctx = {    NULL,                                  /* preconfiguration */    ngx_http_autoindex_init,               /* postconfiguration */    NULL,                                  /* create main configuration */    NULL,                                  /* init main configuration */    NULL,                                  /* create server configuration */    NULL,                                  /* merge server configuration */    ngx_http_autoindex_create_loc_conf,    /* create location configration */    ngx_http_autoindex_merge_loc_conf      /* merge location configration */};ngx_module_t  ngx_http_autoindex_module = {    NGX_MODULE_V1,    &ngx_http_autoindex_module_ctx,        /* module context */    ngx_http_autoindex_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 u_char title[] ="<html>" CRLF"<head><title>Index of ";static u_char header[] ="</title></head>" CRLF"<body bgcolor=\"white\">" CRLF"<h1>Index of ";static u_char tail[] ="</body>" CRLF"</html>" CRLF;static ngx_int_tngx_http_autoindex_handler(ngx_http_request_t *r){    u_char                         *last, *filename, scale;    off_t                           length;    size_t                          len, copy, allocated, root;    ngx_tm_t                        tm;    ngx_err_t                       err;    ngx_buf_t                      *b;    ngx_int_t                       rc, size;    ngx_str_t                       path;    ngx_dir_t                       dir;    ngx_uint_t                      i, level;    ngx_pool_t                     *pool;    ngx_time_t                     *tp;    ngx_chain_t                     out;    ngx_array_t                     entries;    ngx_http_autoindex_entry_t     *entry;    ngx_http_autoindex_loc_conf_t  *alcf;    static char  *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",                               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };    if (r->uri.data[r->uri.len - 1] != '/') {        return NGX_DECLINED;    }    /* TODO: Win32 */    if (r->zero_in_uri) {        return NGX_DECLINED;    }    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {        return NGX_DECLINED;    }    alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module);    if (!alcf->enable) {        return NGX_DECLINED;    }    /* NGX_DIR_MASK_LEN is lesser than NGX_HTTP_AUTOINDEX_PREALLOCATE */    last = ngx_http_map_uri_to_path(r, &path, &root,                                    NGX_HTTP_AUTOINDEX_PREALLOCATE);    if (last == NULL) {        return NGX_HTTP_INTERNAL_SERVER_ERROR;    }    allocated = path.len;    path.len = last - path.data;    if (path.len > 1) {        path.len--;    }    path.data[path.len] = '\0';    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "http autoindex: \"%s\"", path.data);    if (ngx_open_dir(&path, &dir) == NGX_ERROR) {        err = ngx_errno;        if (err == NGX_ENOENT            || err == NGX_ENOTDIR            || err == NGX_ENAMETOOLONG)        {            level = NGX_LOG_ERR;            rc = NGX_HTTP_NOT_FOUND;        } else if (err == NGX_EACCES) {            level = NGX_LOG_ERR;            rc = NGX_HTTP_FORBIDDEN;        } else {            level = NGX_LOG_CRIT;            rc = NGX_HTTP_INTERNAL_SERVER_ERROR;        }        ngx_log_error(level, r->connection->log, err,                      ngx_open_dir_n " \"%s\" failed", path.data);        return rc;    }#if (NGX_SUPPRESS_WARN)    /* MSVC thinks 'entries' may be used without having been initialized */    ngx_memzero(&entries, sizeof(ngx_array_t));#endif    /* TODO: pool should be temporary pool */    pool = r->pool;    if (ngx_array_init(&entries, pool, 40, sizeof(ngx_http_autoindex_entry_t))        != NGX_OK)    {        return ngx_http_autoindex_error(r, &dir, &path);    }    r->headers_out.status = NGX_HTTP_OK;    r->headers_out.content_type_len = sizeof("text/html") - 1;    r->headers_out.content_type.len = sizeof("text/html") - 1;    r->headers_out.content_type.data = (u_char *) "text/html";    rc = ngx_http_send_header(r);    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {        if (ngx_close_dir(&dir) == NGX_ERROR) {            ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,                          ngx_close_dir_n " \"%V\" failed", &path);        }        return rc;    }    filename = path.data;    filename[path.len] = '/';    for ( ;; ) {        ngx_set_errno(0);        if (ngx_read_dir(&dir) == NGX_ERROR) {            err = ngx_errno;            if (err != NGX_ENOMOREFILES) {                ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,                              ngx_read_dir_n " \"%V\" failed", &path);                return ngx_http_autoindex_error(r, &dir, &path);            }            break;        }        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                       "http autoindex file: \"%s\"", ngx_de_name(&dir));        len = ngx_de_namelen(&dir);        if (ngx_de_name(&dir)[0] == '.') {            continue;        }        if (!dir.valid_info) {            /* 1 byte for '/' and 1 byte for terminating '\0' */            if (path.len + 1 + len + 1 > allocated) {                allocated = path.len + 1 + len + 1                                     + NGX_HTTP_AUTOINDEX_PREALLOCATE;                filename = ngx_palloc(pool, allocated);                if (filename == NULL) {                    return ngx_http_autoindex_error(r, &dir, &path);                }                last = ngx_cpystrn(filename, path.data, path.len + 1);                *last++ = '/';            }            ngx_cpystrn(last, ngx_de_name(&dir), len + 1);            if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {                err = ngx_errno;                if (err != NGX_ENOENT) {                    ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,                                  ngx_de_info_n " \"%s\" failed", filename);                    return ngx_http_autoindex_error(r, &dir, &path);                }                if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) {                    ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,                                  ngx_de_link_info_n " \"%s\" failed",                                  filename);                    return ngx_http_autoindex_error(r, &dir, &path);                }            }        }        entry = ngx_array_push(&entries);        if (entry == NULL) {            return ngx_http_autoindex_error(r, &dir, &path);        }        entry->name.len = len;        entry->name.data = ngx_palloc(pool, len + 1);        if (entry->name.data == NULL) {            return ngx_http_autoindex_error(r, &dir, &path);        }        ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1);

⌨️ 快捷键说明

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