Welcome! Log In Create A New Profile

Advanced

Re: The addition before sub

Witold Filipczyk
June 06, 2013 02:28PM
W dniu 06.06.2013 o 19:56 Maxim Dounin <mdounin@mdounin.ru> pisze:

> Hello!
>
> On Thu, Jun 06, 2013 at 05:43:14PM +0200, Witold Filipczyk wrote:
>
>> W dniu 06.06.2013 o 15:57 Maxim Dounin <mdounin@mdounin.ru> pisze:
>>
>> >Hello!
>> >
>> >On Thu, Jun 06, 2013 at 03:41:53PM +0200, Witold Filipczyk wrote:
>> >
>> >>Hello,
>> >>I want to inject something before </body> and before </head>, but
>> >>even if there is no </body>.
>> >>So used add_after_body /after_body;
>> >>/after_body returns the text </body>, so there is at least one
>> >></body> in the page.
>> >>I wanted sub_filter to replace </body> to SOMETHING</body>, but the
>> >>sub_filter is run before the addition_filter.
>> >>Changing the order in modules doesn't help, because the sub_filter
>> >>doesn't replace the text from subrequests.
>> >
>> >Works fine here:
>> >
>> > location / {
>> > add_after_body /after.html;
>> > sub_filter 'foo' 'bar';
>> > }
>> >
>> >$ cat main.html
>> >this is main.html: foo
>> >$ cat after.html
>> >this is in after.html: foo
>> >$ fetch -qo - 'http://localhost:8080/main.html'
>> >this is main.html: bar
>> >this is in after.html: bar
>> >
>> >The explanation is simple: addition filter uses _subrequests_ to
>> >add text, and these subrequests are in turn processed by the whole
>> >filter chain, including the sub filter. Hence order of sub and
>> >addition filters doesn't matter.
>> >
>> >Most likely it doesn't work for you because /after_body have some
>> >default mime type not matched by sub_filter_types, see
>> >http://nginx.org/r/sub_filter_types.
>>
>> OK. It works, but:
>>
>> server {
>> listen 8000;
>> sub_filter 'foo' 'bar';
>> sub_filter_once on;
>>
>> location / {
>> root /;
>> add_after_body /after.html;
>> }
>>
>> location = /after.html {
>> root /;
>> }
>> }
>>
>> /after.html contains "lalala foo"
>> /body.html contains "foo"
>>
>> http://localhost:8000/body.html:
>> bar lalala bar
>>
>> I expected:
>> bar lalala foo
>
> Any reasons for the expectation?

I wanted to replace given substring once.
add_after_body and sub_filter occurs at the "server" level.
There is too many locations.
Thanks for the reply.

I did a workaround:
In ngx_http_sub_body_filter:

ngx_http_sub_ctx_t *ctx_main = ngx_http_get_module_ctx(r->main,
ngx_http_sub_filter_module);

if (ctx_main && ctx_main->once) {
ctx->once = ctx_main->once;
}

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

The addition before sub

Witold Filipczyk 772 June 06, 2013 09:44AM

Re: The addition before sub

Maxim Dounin 305 June 06, 2013 09:58AM

Re: The addition before sub

Witold Filipczyk 348 June 06, 2013 11:44AM

Re: The addition before sub

Maxim Dounin 384 June 06, 2013 01:58PM

Re: The addition before sub

Witold Filipczyk 426 June 06, 2013 02:28PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 150
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready