@ -23,11 +23,12 @@
# define NGX_HTTP_TRIM_TAG_TEXTAREA 4
# define NGX_HTTP_TRIM_TAG_TEXTAREA 4
typedef struct {
typedef struct {
ngx_flag_t trim_enable ;
ngx_flag_t js_enable ;
ngx_flag_t css_enable ;
ngx_hash_t types ;
ngx_array_t * types_keys ;
ngx_hash_t types ;
ngx_array_t * types_keys ;
ngx_http_complex_value_t * js ;
ngx_http_complex_value_t * css ;
ngx_http_complex_value_t * trim ;
} ngx_http_trim_loc_conf_t ;
} ngx_http_trim_loc_conf_t ;
@ -45,6 +46,9 @@ typedef struct {
ngx_int_t saved ;
ngx_int_t saved ;
ngx_int_t count ;
ngx_int_t count ;
ngx_uint_t state ;
ngx_uint_t state ;
unsigned js_enable : 1 ;
unsigned css_enable : 1 ;
} ngx_http_trim_ctx_t ;
} ngx_http_trim_ctx_t ;
@ -167,24 +171,24 @@ static ngx_int_t ngx_http_trim_filter_init(ngx_conf_t *cf);
static ngx_command_t ngx_http_trim_commands [ ] = {
static ngx_command_t ngx_http_trim_commands [ ] = {
{ ngx_string ( " trim " ) ,
{ ngx_string ( " trim " ) ,
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_FLAG ,
ngx_conf_set_flag _slot ,
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 ,
ngx_http_set_complex_value _slot ,
NGX_HTTP_LOC_CONF_OFFSET ,
NGX_HTTP_LOC_CONF_OFFSET ,
offsetof ( ngx_http_trim_loc_conf_t , trim_enable ) ,
offsetof ( ngx_http_trim_loc_conf_t , trim ) ,
NULL } ,
NULL } ,
{ ngx_string ( " trim_js " ) ,
{ ngx_string ( " trim_js " ) ,
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_FLAG ,
ngx_conf_set_flag _slot ,
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 ,
ngx_http_set_complex_value _slot ,
NGX_HTTP_LOC_CONF_OFFSET ,
NGX_HTTP_LOC_CONF_OFFSET ,
offsetof ( ngx_http_trim_loc_conf_t , js_enable ) ,
offsetof ( ngx_http_trim_loc_conf_t , js ) ,
NULL } ,
NULL } ,
{ ngx_string ( " trim_css " ) ,
{ ngx_string ( " trim_css " ) ,
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_FLAG ,
ngx_conf_set_flag _slot ,
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 ,
ngx_http_set_complex_value _slot ,
NGX_HTTP_LOC_CONF_OFFSET ,
NGX_HTTP_LOC_CONF_OFFSET ,
offsetof ( ngx_http_trim_loc_conf_t , css_enable ) ,
offsetof ( ngx_http_trim_loc_conf_t , css ) ,
NULL } ,
NULL } ,
{ ngx_string ( " trim_types " ) ,
{ ngx_string ( " trim_types " ) ,
@ -258,7 +262,7 @@ ngx_http_trim_header_filter(ngx_http_request_t *r)
conf = ngx_http_get_module_loc_conf ( r , ngx_http_trim_filter_module ) ;
conf = ngx_http_get_module_loc_conf ( r , ngx_http_trim_filter_module ) ;
if ( ! conf - > trim_enable
if ( ! conf - > trim
| | r - > headers_out . status ! = NGX_HTTP_OK
| | r - > headers_out . status ! = NGX_HTTP_OK
| | ( r - > method & NGX_HTTP_HEAD )
| | ( r - > method & NGX_HTTP_HEAD )
| | r - > headers_out . content_length_n = = 0
| | r - > headers_out . content_length_n = = 0
@ -279,11 +283,45 @@ ngx_http_trim_header_filter(ngx_http_request_t *r)
return ngx_http_next_header_filter ( r ) ;
return ngx_http_next_header_filter ( r ) ;
}
}
if ( ngx_http_complex_value ( r , conf - > trim , & flag ) ! = NGX_OK ) {
return NGX_ERROR ;
}
if ( ! ( flag . len = = sizeof ( " on " ) - 1
& & ngx_strncmp ( flag . data , " on " , sizeof ( " on " ) - 1 ) = = 0 ) )
{
return ngx_http_next_header_filter ( r ) ;
}
ctx = ngx_pcalloc ( r - > pool , sizeof ( ngx_http_trim_ctx_t ) ) ;
ctx = ngx_pcalloc ( r - > pool , sizeof ( ngx_http_trim_ctx_t ) ) ;
if ( ctx = = NULL ) {
if ( ctx = = NULL ) {
return NGX_ERROR ;
return NGX_ERROR ;
}
}
if ( conf - > js ) {
if ( ngx_http_complex_value ( r , conf - > js , & flag ) ! = NGX_OK ) {
return NGX_ERROR ;
}
if ( flag . len = = sizeof ( " on " ) - 1
& & ngx_strncmp ( flag . data , " on " , sizeof ( " on " ) - 1 ) = = 0 )
{
ctx - > js_enable = 1 ;
}
}
if ( conf - > css ) {
if ( ngx_http_complex_value ( r , conf - > css , & flag ) ! = NGX_OK ) {
return NGX_ERROR ;
}
if ( flag . len = = sizeof ( " on " ) - 1
& & ngx_strncmp ( flag . data , " on " , sizeof ( " on " ) - 1 ) = = 0 )
{
ctx - > css_enable = 1 ;
}
}
ctx - > prev = ' ' ;
ctx - > prev = ' ' ;
ngx_http_set_ctx ( r , ctx , ngx_http_trim_filter_module ) ;
ngx_http_set_ctx ( r , ctx , ngx_http_trim_filter_module ) ;
@ -551,7 +589,7 @@ ngx_http_trim_parse(ngx_http_request_t *r, ngx_buf_t *buf,
ctx - > state = trim_state_tag_textarea_end ;
ctx - > state = trim_state_tag_textarea_end ;
} else if ( ctx - > tag = = NGX_HTTP_TRIM_TAG_SCRIPT ) {
} else if ( ctx - > tag = = NGX_HTTP_TRIM_TAG_SCRIPT ) {
if ( conf - > js_enable
if ( ctx - > js_enable
& & ctx - > looked = = ngx_http_trim_script_js . len )
& & ctx - > looked = = ngx_http_trim_script_js . len )
{
{
ctx - > state = trim_state_tag_script_js_text ;
ctx - > state = trim_state_tag_script_js_text ;
@ -561,7 +599,7 @@ ngx_http_trim_parse(ngx_http_request_t *r, ngx_buf_t *buf,
}
}
} else if ( ctx - > tag = = NGX_HTTP_TRIM_TAG_STYLE ) {
} else if ( ctx - > tag = = NGX_HTTP_TRIM_TAG_STYLE ) {
if ( conf - > css_enable
if ( ctx - > css_enable
& & ctx - > looked = = ngx_http_trim_style_css . len )
& & ctx - > looked = = ngx_http_trim_style_css . len )
{
{
ctx - > state = trim_state_tag_style_css_text ;
ctx - > state = trim_state_tag_style_css_text ;
@ -782,7 +820,7 @@ ngx_http_trim_parse(ngx_http_request_t *r, ngx_buf_t *buf,
look = ngx_http_trim_script . data [ ctx - > looked + + ] ; /* <script> */
look = ngx_http_trim_script . data [ ctx - > looked + + ] ; /* <script> */
if ( ch = = look ) {
if ( ch = = look ) {
if ( ctx - > looked = = ngx_http_trim_script . len ) {
if ( ctx - > looked = = ngx_http_trim_script . len ) {
if ( conf - > js_enable ) {
if ( ctx - > js_enable ) {
ctx - > state = trim_state_tag_script_js_text ;
ctx - > state = trim_state_tag_script_js_text ;
} else {
} else {
@ -1166,7 +1204,7 @@ ngx_http_trim_parse(ngx_http_request_t *r, ngx_buf_t *buf,
look = ngx_http_trim_style . data [ ctx - > looked + + ] ; /* <style> */
look = ngx_http_trim_style . data [ ctx - > looked + + ] ; /* <style> */
if ( ch = = look ) {
if ( ch = = look ) {
if ( ctx - > looked = = ngx_http_trim_style . len ) {
if ( ctx - > looked = = ngx_http_trim_style . len ) {
if ( conf - > css_enable ) {
if ( ctx - > css_enable ) {
ctx - > state = trim_state_tag_style_css_text ;
ctx - > state = trim_state_tag_style_css_text ;
} else {
} else {
@ -1796,7 +1834,7 @@ ngx_http_trim_parse(ngx_http_request_t *r, ngx_buf_t *buf,
ctx - > state = trim_state_tag_textarea_end ;
ctx - > state = trim_state_tag_textarea_end ;
} else if ( ctx - > tag = = NGX_HTTP_TRIM_TAG_SCRIPT ) {
} else if ( ctx - > tag = = NGX_HTTP_TRIM_TAG_SCRIPT ) {
if ( conf - > js_enable
if ( ctx - > js_enable
& & ctx - > looked = = ngx_http_trim_script_js . len )
& & ctx - > looked = = ngx_http_trim_script_js . len )
{
{
ctx - > state = trim_state_tag_script_js_text ;
ctx - > state = trim_state_tag_script_js_text ;
@ -1806,7 +1844,7 @@ ngx_http_trim_parse(ngx_http_request_t *r, ngx_buf_t *buf,
}
}
} else if ( ctx - > tag = = NGX_HTTP_TRIM_TAG_STYLE ) {
} else if ( ctx - > tag = = NGX_HTTP_TRIM_TAG_STYLE ) {
if ( conf - > css_enable
if ( ctx - > css_enable
& & ctx - > looked = = ngx_http_trim_style_css . len )
& & ctx - > looked = = ngx_http_trim_style_css . len )
{
{
ctx - > state = trim_state_tag_style_css_text ;
ctx - > state = trim_state_tag_style_css_text ;
@ -1847,7 +1885,7 @@ ngx_http_trim_parse(ngx_http_request_t *r, ngx_buf_t *buf,
break ;
break ;
}
}
if ( conf - > js_enable & & ctx - > tag = = NGX_HTTP_TRIM_TAG_SCRIPT ) {
if ( ctx - > js_enable & & ctx - > tag = = NGX_HTTP_TRIM_TAG_SCRIPT ) {
if ( ctx - > looked ! = ngx_http_trim_script_js . len ) {
if ( ctx - > looked ! = ngx_http_trim_script_js . len ) {
look = ngx_http_trim_script_js . data [ ctx - > looked + + ] ;
look = ngx_http_trim_script_js . data [ ctx - > looked + + ] ;
if ( ch ! = look ) {
if ( ch ! = look ) {
@ -1856,7 +1894,7 @@ ngx_http_trim_parse(ngx_http_request_t *r, ngx_buf_t *buf,
}
}
}
}
if ( conf - > css_enable & & ctx - > tag = = NGX_HTTP_TRIM_TAG_STYLE ) {
if ( ctx - > css_enable & & ctx - > tag = = NGX_HTTP_TRIM_TAG_STYLE ) {
if ( ctx - > looked ! = ngx_http_trim_style_css . len ) {
if ( ctx - > looked ! = ngx_http_trim_style_css . len ) {
look = ngx_http_trim_style_css . data [ ctx - > looked + + ] ;
look = ngx_http_trim_style_css . data [ ctx - > looked + + ] ;
if ( ch ! = look ) {
if ( ch ! = look ) {
@ -1876,7 +1914,7 @@ ngx_http_trim_parse(ngx_http_request_t *r, ngx_buf_t *buf,
break ;
break ;
}
}
if ( conf - > js_enable & & ctx - > tag = = NGX_HTTP_TRIM_TAG_SCRIPT ) {
if ( ctx - > js_enable & & ctx - > tag = = NGX_HTTP_TRIM_TAG_SCRIPT ) {
if ( ctx - > looked ! = ngx_http_trim_script_js . len ) {
if ( ctx - > looked ! = ngx_http_trim_script_js . len ) {
look = ngx_http_trim_script_js . data [ ctx - > looked + + ] ;
look = ngx_http_trim_script_js . data [ ctx - > looked + + ] ;
if ( ch ! = look ) {
if ( ch ! = look ) {
@ -1885,7 +1923,7 @@ ngx_http_trim_parse(ngx_http_request_t *r, ngx_buf_t *buf,
}
}
}
}
if ( conf - > css_enable & & ctx - > tag = = NGX_HTTP_TRIM_TAG_STYLE ) {
if ( ctx - > css_enable & & ctx - > tag = = NGX_HTTP_TRIM_TAG_STYLE ) {
if ( ctx - > looked ! = ngx_http_trim_style_css . len ) {
if ( ctx - > looked ! = ngx_http_trim_style_css . len ) {
look = ngx_http_trim_style_css . data [ ctx - > looked + + ] ;
look = ngx_http_trim_style_css . data [ ctx - > looked + + ] ;
if ( ch ! = look ) {
if ( ch ! = look ) {
@ -1924,12 +1962,11 @@ ngx_http_trim_create_loc_conf(ngx_conf_t *cf)
*
*
* conf - > types = { NULL } ;
* conf - > types = { NULL } ;
* conf - > types_keys = NULL ;
* conf - > types_keys = NULL ;
* conf - > trim = NULL ;
* conf - > js = NULL ;
* conf - > css = NULL ;
*/
*/
conf - > trim_enable = NGX_CONF_UNSET ;
conf - > js_enable = NGX_CONF_UNSET ;
conf - > css_enable = NGX_CONF_UNSET ;
return conf ;
return conf ;
}
}
@ -1940,10 +1977,6 @@ ngx_http_trim_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_http_trim_loc_conf_t * prev = parent ;
ngx_http_trim_loc_conf_t * prev = parent ;
ngx_http_trim_loc_conf_t * conf = child ;
ngx_http_trim_loc_conf_t * conf = child ;
ngx_conf_merge_value ( conf - > js_enable , prev - > js_enable , 0 ) ;
ngx_conf_merge_value ( conf - > css_enable , prev - > css_enable , 0 ) ;
ngx_conf_merge_value ( conf - > trim_enable , prev - > trim_enable , 0 ) ;
if ( ngx_http_merge_types ( cf , & conf - > types_keys , & conf - > types ,
if ( ngx_http_merge_types ( cf , & conf - > types_keys , & conf - > types ,
& prev - > types_keys , & prev - > types ,
& prev - > types_keys , & prev - > types ,
ngx_http_html_default_types )
ngx_http_html_default_types )
@ -1952,6 +1985,18 @@ ngx_http_trim_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
return NGX_CONF_ERROR ;
return NGX_CONF_ERROR ;
}
}
if ( conf - > trim = = NULL ) {
conf - > trim = prev - > trim ;
}
if ( conf - > js = = NULL ) {
conf - > js = prev - > js ;
}
if ( conf - > css = = NULL ) {
conf - > css = prev - > css ;
}
return NGX_CONF_OK ;
return NGX_CONF_OK ;
}
}