Skip to content

Commit

Permalink
Added tag for version 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vkholodkov committed Oct 11, 2008
1 parent aaa500a commit 9431767
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 106 deletions.
5 changes: 3 additions & 2 deletions config
@@ -1,6 +1,7 @@
USE_MD5=YES
USE_SHA1=YES
USE_ZLIB=YES
ngx_addon_name=ngx_http_upload_module
HTTP_MODULES="$HTTP_MODULES ngx_http_upload_module ngx_http_unzip_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_upload_module.c $ngx_addon_dir/ngx_http_unzip_filter_module.c"
HTTP_MODULES="$HTTP_MODULES ngx_http_upload_module ngx_upload_unzip_filter_module ngx_upload_discard_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_upload_module.c $ngx_addon_dir/ngx_upload_unzip_filter_module.c $ngx_addon_dir/ngx_upload_discard_filter_module.c"
HTTP_INCS="$HTTP_INCS $ngx_addon_dir"
56 changes: 56 additions & 0 deletions nginx-unzip.conf
@@ -0,0 +1,56 @@

worker_processes 20;

error_log logs/error.log notice;

working_directory /usr/local/nginx;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

server {
listen 80;
client_max_body_size 100m;

# Upload form should be submitted to this location
location /upload {
# Pass altered request body to this location
upload_pass /test;

# Store files to this directory
# The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
upload_store /tmp 1;

# Allow uploaded files to be read only by user
upload_store_access user:r;

# Set specified fields in request body
upload_set_form_field "${upload_field_name}${upload_archive_elm}_name" $upload_file_name;
upload_set_form_field "${upload_field_name}${upload_archive_elm}_content_type" $upload_content_type;
upload_set_form_field "${upload_field_name}${upload_archive_elm}_path" $upload_tmp_path;
upload_set_form_field "${upload_field_name}${upload_archive_elm}_archive_path" $upload_archive_path;

# Inform backend about hash and size of a file
upload_aggregate_form_field "${upload_field_name}${upload_archive_elm}_md5" $upload_file_md5;
upload_aggregate_form_field "${upload_field_name}${upload_archive_elm}_size" $upload_file_size;

upload_pass_form_field "^submit$|^description$";

upload_cleanup 400-599;

upload_filter application/zip {
upload_unzip on;
}
}

# Pass altered request body to a backend
location /test {
proxy_pass http://localhost:8080;
}
}
}
4 changes: 4 additions & 0 deletions nginx.conf
Expand Up @@ -39,6 +39,10 @@ http {
upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;

upload_pass_form_field "^submit$|^description$";

upload_filter "application/zip" {
unzip on;
}
}

# Pass altered request body to a backend
Expand Down
55 changes: 52 additions & 3 deletions ngx_http_upload.h
Expand Up @@ -128,6 +128,9 @@ typedef struct ngx_http_upload_loc_conf_s {
ngx_array_t *content_filters;
ngx_array_t *content_type_map;

ngx_str_t archive_elm_separator;
ngx_str_t archive_path_separator;

unsigned int md5:1;
unsigned int sha1:1;
unsigned int crc32:1;
Expand Down Expand Up @@ -160,6 +163,7 @@ typedef struct ngx_http_upload_ctx_s {
ngx_str_t field_name;
ngx_str_t file_name;
ngx_str_t content_type;
ngx_str_t archive_elm;
ngx_str_t archive_path;

ngx_buf_t *output_buffer;
Expand Down Expand Up @@ -197,9 +201,54 @@ ngx_module_t ngx_http_upload_module;

ngx_int_t ngx_upload_set_exten(ngx_http_upload_ctx_t *u, ngx_str_t *file_name, ngx_str_t *exten);
ngx_int_t ngx_upload_resolve_content_type(ngx_http_upload_ctx_t *u, ngx_str_t *exten, ngx_str_t *content_type);
ngx_int_t ngx_upload_set_file_name(ngx_http_upload_ctx_t *ctx, ngx_str_t *file_name);
ngx_int_t ngx_upload_set_content_type(ngx_http_upload_ctx_t *ctx, ngx_str_t *content_type);
ngx_int_t ngx_upload_set_archive_path(ngx_http_upload_ctx_t *ctx, ngx_str_t *archive_path);

#define ngx_upload_set_file_name(ctx, fn) \
do{ \
(ctx)->file_name.data = (fn)->data; \
(ctx)->file_name.len = (fn)->len; \
}while(0); \

#define ngx_upload_get_file_name(ctx, fn) \
do{ \
(fn)->data = (ctx)->file_name.data; \
(fn)->len = (ctx)->file_name.len; \
}while(0); \

#define ngx_upload_set_content_type(ctx, ct) \
do{ \
(ctx)->content_type.data = (ct)->data; \
(ctx)->content_type.len = (ct)->len; \
}while(0); \

#define ngx_upload_get_content_type(ctx, ct) \
do{ \
(ct)->data = (ctx)->content_type.data; \
(ct)->len = (ctx)->content_type.len; \
}while(0); \

#define ngx_upload_set_archive_elm(ctx, ae) \
do{ \
(ctx)->archive_elm.data = (ae)->data; \
(ctx)->archive_elm.len = (ae)->len; \
}while(0); \

#define ngx_upload_get_archive_elm(ctx, ae) \
do{ \
(ae)->data = (ctx)->archive_elm.data; \
(ae)->len = (ctx)->archive_elm.len; \
}while(0); \

#define ngx_upload_set_archive_path(ctx, ap) \
do{ \
(ctx)->archive_path.data = (ap)->data; \
(ctx)->archive_path.len = (ap)->len; \
}while(0); \

#define ngx_upload_get_archive_path(ctx, ap) \
do{ \
(ap)->data = (ctx)->archive_path.data; \
(ap)->len = (ctx)->archive_path.len; \
}while(0); \

ngx_upload_field_filter_t*
ngx_upload_get_next_field_filter(ngx_http_upload_ctx_t *ctx);
Expand Down
111 changes: 57 additions & 54 deletions ngx_http_upload_module.c
Expand Up @@ -263,6 +263,26 @@ static ngx_command_t ngx_http_upload_commands[] = { /* {{{ */
0,
NULL},

/*
* Specifies a separator for archive element tokens
*/
{ ngx_string("upload_archive_elm_separator"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_upload_loc_conf_t, archive_elm_separator),
NULL},

/*
* Specifies a separator for archive path tokens
*/
{ ngx_string("upload_archive_path_separator"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_upload_loc_conf_t, archive_path_separator),
NULL},

ngx_null_command
}; /* }}} */

Expand Down Expand Up @@ -313,6 +333,10 @@ static ngx_http_variable_t ngx_http_upload_variables[] = { /* {{{ */
(uintptr_t) offsetof(ngx_http_upload_ctx_t, output_file.name),
NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },

{ ngx_string("upload_archive_elm"), NULL, ngx_http_upload_variable,
(uintptr_t) offsetof(ngx_http_upload_ctx_t, archive_elm),
NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },

{ ngx_string("upload_archive_path"), NULL, ngx_http_upload_variable,
(uintptr_t) offsetof(ngx_http_upload_ctx_t, archive_path),
NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
Expand Down Expand Up @@ -798,6 +822,7 @@ ngx_http_upload_append_str(ngx_http_upload_ctx_t *u, ngx_buf_t *b, ngx_chain_t *
b->temporary = 1;
b->in_file = 0;
b->last_buf = 0;
b->flush = 0;

b->last_in_chain = 0;
b->last_buf = 0;
Expand Down Expand Up @@ -922,35 +947,38 @@ static void ngx_http_upload_field_abort(ngx_http_upload_ctx_t *u) { /* {{{ */
}
} /* }}} */

static ngx_int_t ngx_http_upload_field_process_chain(ngx_http_upload_ctx_t *u, ngx_chain_t *chain) { /* {{{ */
static ngx_int_t /* {{{ ngx_http_upload_field_process_chain */
ngx_http_upload_field_process_chain(ngx_http_upload_ctx_t *u, ngx_chain_t *chain) {
ngx_buf_t *b;
ngx_chain_t *cl;

for(cl = chain; cl && !cl->buf->last_in_chain; cl = cl->next) {
b = ngx_create_temp_buf(u->request->pool, cl->buf->last - cl->buf->pos);
for(;chain && !chain->buf->last_in_chain; chain = chain->next) {
if(chain->buf->last - chain->buf->pos > 0) {
b = ngx_create_temp_buf(u->request->pool, chain->buf->last - chain->buf->pos);

if (b == NULL) {
return NGX_ERROR;
}
if (b == NULL) {
return NGX_ERROR;
}

cl = ngx_alloc_chain_link(u->request->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl = ngx_alloc_chain_link(u->request->pool);
if (cl == NULL) {
return NGX_ERROR;
}

b->last_in_chain = 0;
b->last_in_chain = 0;

cl->buf = b;
cl->next = NULL;
cl->buf = b;
cl->next = NULL;

b->last = ngx_cpymem(b->last, cl->buf->pos, cl->buf->last - cl->buf->pos);
b->last = ngx_cpymem(b->last, chain->buf->pos, chain->buf->last - chain->buf->pos);

if(u->chain == NULL) {
u->chain = cl;
u->last = cl;
}else{
u->last->next = cl;
u->last = cl;
if(u->chain == NULL) {
u->chain = cl;
u->last = cl;
}else{
u->last->next = cl;
u->last = cl;
}
}
}

Expand All @@ -973,6 +1001,7 @@ ngx_http_upload_create_loc_conf(ngx_conf_t *cf)
conf->max_header_len = NGX_CONF_UNSET_SIZE;

/*
* conf->archive_elm_separator
* conf->field_templates,
* conf->aggregate_field_templates,
* and conf->field_filters are
Expand Down Expand Up @@ -1034,6 +1063,9 @@ ngx_http_upload_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
conf->cleanup_statuses = prev->cleanup_statuses;
}

ngx_conf_merge_str_value(conf->archive_elm_separator, prev->archive_elm_separator, "_");
ngx_conf_merge_str_value(conf->archive_path_separator, prev->archive_path_separator, "!");

return NGX_CONF_OK;
} /* }}} */

Expand Down Expand Up @@ -1546,9 +1578,7 @@ ngx_http_upload_filter_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}

for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->type != NGX_HTTP_MODULE &&
ngx_modules[i] != &ngx_http_upload_module &&
ngx_modules[i] != &ngx_http_core_module) {
if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
continue;
}

Expand Down Expand Up @@ -2155,36 +2185,6 @@ ngx_upload_resolve_content_type(ngx_http_upload_ctx_t *u, ngx_str_t *exten, ngx_
return NGX_OK;
} /* }}} */

ngx_int_t /* {{{ ngx_upload_set_file_name */
ngx_upload_set_file_name(ngx_http_upload_ctx_t *ctx,
ngx_str_t *file_name)
{
ctx->file_name.data = file_name->data;
ctx->file_name.len = file_name->len;

return NGX_OK;
} /* }}} */

ngx_int_t /* {{{ ngx_upload_set_content_type */
ngx_upload_set_content_type(ngx_http_upload_ctx_t *ctx,
ngx_str_t *content_type)
{
ctx->content_type.data = content_type->data;
ctx->content_type.len = content_type->len;

return NGX_OK;
} /* }}} */

ngx_int_t /* {{{ ngx_upload_set_archive_path */
ngx_upload_set_archive_path(ngx_http_upload_ctx_t *ctx,
ngx_str_t *archive_path)
{
ctx->archive_path.data = archive_path->data;
ctx->archive_path.len = archive_path->len;

return NGX_OK;
} /* }}} */

static ngx_int_t /* {{{ ngx_upload_set_content_filter */
ngx_upload_set_content_filter(ngx_http_upload_ctx_t *u, ngx_str_t *content_type)
{
Expand Down Expand Up @@ -2277,13 +2277,16 @@ static void upload_abort_file(ngx_http_upload_ctx_t *upload_ctx) { /* {{{ */

static void upload_flush_output_buffer(ngx_http_upload_ctx_t *upload_ctx) { /* {{{ */
ngx_chain_t chain = { upload_ctx->output_buffer, NULL };
ngx_int_t rc;

if(upload_ctx->output_buffer->pos > upload_ctx->output_buffer->start) {
if(upload_ctx->process_chain_f) {
upload_ctx->output_buffer->last = upload_ctx->output_buffer->pos;
upload_ctx->output_buffer->pos = upload_ctx->output_buffer->start;

if(upload_ctx->process_chain_f(upload_ctx, &chain) != NGX_OK) {
rc = upload_ctx->process_chain_f(upload_ctx, &chain);

if(rc != NGX_OK && rc != NGX_AGAIN) {
upload_ctx->discard_data = 1;
}
}
Expand Down

0 comments on commit 9431767

Please sign in to comment.