From e0adace11d3b19b7ad0d8d82650af5da4820b155 Mon Sep 17 00:00:00 2001 From: dinic Date: Mon, 25 Mar 2013 23:03:30 +0800 Subject: [PATCH 1/2] merger concat brance --- ngx_http_concat_module.c | 88 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 3 deletions(-) diff --git a/ngx_http_concat_module.c b/ngx_http_concat_module.c index 80e427c..7b3e105 100644 --- a/ngx_http_concat_module.c +++ b/ngx_http_concat_module.c @@ -1,6 +1,6 @@ /* - * Copyright (C) 2010-2012 Alibaba Group Holding Limited + * Copyright (C) 2010-2013 Alibaba Group Holding Limited */ @@ -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; @@ -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, @@ -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 }; @@ -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; @@ -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; @@ -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; } @@ -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) { @@ -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); From a78ded1d6edd1064170f37838ac38754cfd5185a Mon Sep 17 00:00:00 2001 From: dinic Date: Mon, 27 Oct 2014 14:13:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9js=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84MIME-type=20issue=EF=BC=9A=20https://github.com/alibab?= =?UTF-8?q?a/nginx-http-concat/issues/21?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ngx_http_concat_module.c | 94 ++++------------------------------------ 1 file changed, 9 insertions(+), 85 deletions(-) diff --git a/ngx_http_concat_module.c b/ngx_http_concat_module.c index 7b3e105..fe307a2 100644 --- a/ngx_http_concat_module.c +++ b/ngx_http_concat_module.c @@ -1,6 +1,6 @@ /* - * Copyright (C) 2010-2013 Alibaba Group Holding Limited + * Copyright (C) 2010-2012 Alibaba Group Holding Limited */ @@ -14,7 +14,6 @@ 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; @@ -24,8 +23,6 @@ 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, @@ -33,7 +30,13 @@ static char *ngx_http_concat_merge_loc_conf(ngx_conf_t *cf, void *parent, static ngx_str_t ngx_http_concat_default_types[] = { + +#if defined(nginx_version) && nginx_version >= 1005004 + ngx_string("application/javascript"), +#else ngx_string("application/x-javascript"), +#endif + ngx_string("text/css"), ngx_null_string }; @@ -83,13 +86,6 @@ 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 }; @@ -324,19 +320,7 @@ ngx_http_concat_handler(ngx_http_request_t *r) if (clcf->ignore_file_error && (rc == NGX_HTTP_NOT_FOUND || rc == NGX_HTTP_FORBIDDEN)) { - 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; - } + continue; } return rc; @@ -346,18 +330,7 @@ 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) { - 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; - } + continue; } return NGX_HTTP_NOT_FOUND; @@ -416,8 +389,6 @@ ngx_http_concat_handler(ngx_http_request_t *r) cl->next = NULL; } -delimiter: - if (i + 1 == uris.nelts || clcf->delimiter.len == 0) { continue; } @@ -515,52 +486,6 @@ 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) { @@ -595,7 +520,6 @@ 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);