Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 85 additions & 3 deletions ngx_http_concat_module.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/*
* Copyright (C) 2010-2012 Alibaba Group Holding Limited
* Copyright (C) 2010-2013 Alibaba Group Holding Limited
*/


Expand All @@ -14,6 +14,7 @@ typedef struct {
ngx_uint_t max_files;
ngx_flag_t unique;
ngx_str_t delimiter;
ngx_str_t error_body;
ngx_flag_t ignore_file_error;

ngx_hash_t types;
Expand All @@ -23,6 +24,8 @@ typedef struct {

static ngx_int_t ngx_http_concat_add_path(ngx_http_request_t *r,
ngx_array_t *uris, size_t max, ngx_str_t *path, u_char *p, u_char *v);
static ngx_int_t ngx_http_concat_insert_error_body(ngx_http_request_t *r,
ngx_chain_t *c, ngx_chain_t ***last, ngx_str_t *error_body, off_t *length);
static ngx_int_t ngx_http_concat_init(ngx_conf_t *cf);
static void *ngx_http_concat_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_concat_merge_loc_conf(ngx_conf_t *cf, void *parent,
Expand Down Expand Up @@ -80,6 +83,13 @@ static ngx_command_t ngx_http_concat_commands[] = {
offsetof(ngx_http_concat_loc_conf_t, ignore_file_error),
NULL },

{ ngx_string("concat_file_error_body"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_concat_loc_conf_t, error_body),
NULL },

ngx_null_command
};

Expand Down Expand Up @@ -314,7 +324,19 @@ ngx_http_concat_handler(ngx_http_request_t *r)
if (clcf->ignore_file_error
&& (rc == NGX_HTTP_NOT_FOUND || rc == NGX_HTTP_FORBIDDEN))
{
continue;
rc = ngx_http_concat_insert_error_body(r, &out, &last_out,
&clcf->error_body,
&length);
if (rc == NGX_AGAIN) {
continue;

} else if (rc == NGX_OK) {
goto delimiter;

} else {
// rc == NGX_ERROR
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}

return rc;
Expand All @@ -324,7 +346,18 @@ ngx_http_concat_handler(ngx_http_request_t *r)
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
"\"%V\" is not a regular file", filename);
if (clcf->ignore_file_error) {
continue;
rc = ngx_http_concat_insert_error_body(r, &out, &last_out,
&clcf->error_body,
&length);
if (rc == NGX_AGAIN) {
continue;

} else if (rc == NGX_OK) {
goto delimiter;

} else {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}

return NGX_HTTP_NOT_FOUND;
Expand Down Expand Up @@ -383,6 +416,8 @@ ngx_http_concat_handler(ngx_http_request_t *r)
cl->next = NULL;
}

delimiter:

if (i + 1 == uris.nelts || clcf->delimiter.len == 0) {
continue;
}
Expand Down Expand Up @@ -480,6 +515,52 @@ ngx_http_concat_add_path(ngx_http_request_t *r, ngx_array_t *uris,
}


ngx_int_t
ngx_http_concat_insert_error_body(ngx_http_request_t *r, ngx_chain_t *c,
ngx_chain_t ***last, ngx_str_t *error_body, off_t *length)
{
ngx_buf_t *b;
ngx_chain_t *cl;

if (error_body->len == 0) {
return NGX_AGAIN;
}

b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
return NGX_ERROR;
}

b->memory = 1;
b->pos = error_body->data;
b->last = error_body->data + error_body->len;
b->start = b->pos;
b->end = b->last;

if (*last == NULL) {
c->buf = b;
*last = &c->next;
c->next = NULL;

} else {
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}

cl->buf = b;

**last = cl;
*last = &cl->next;
cl->next = NULL;
}

*length = *length + error_body->len;

return NGX_OK;
}


static void *
ngx_http_concat_create_loc_conf(ngx_conf_t *cf)
{
Expand Down Expand Up @@ -514,6 +595,7 @@ ngx_http_concat_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)

ngx_conf_merge_value(conf->enable, prev->enable, 0);
ngx_conf_merge_str_value(conf->delimiter, prev->delimiter, "");
ngx_conf_merge_str_value(conf->error_body, prev->error_body, "");
ngx_conf_merge_value(conf->ignore_file_error, prev->ignore_file_error, 0);
ngx_conf_merge_uint_value(conf->max_files, prev->max_files, 10);
ngx_conf_merge_value(conf->unique, prev->unique, 1);
Expand Down