Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly proceed in filter processing when request is replaced #7174

Merged
merged 7 commits into from Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,87 @@
package io.micronaut.http.server.netty.filters

import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Requires
import io.micronaut.http.HttpRequest
import io.micronaut.http.MutableHttpResponse
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Filter
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.HttpClient
import io.micronaut.http.filter.HttpServerFilter
import io.micronaut.http.filter.ServerFilterChain
import io.micronaut.runtime.server.EmbeddedServer
import org.reactivestreams.Publisher
import spock.lang.Specification

class FilterReplaceRequestSpec extends Specification {
def 'test replaced http request is handled by next filter'() {
given:
def ctx = ApplicationContext.run(['spec.name': 'FilterReplaceRequestSpec'])
def server = ctx.getBean(EmbeddedServer)
server.start()
def client = ctx.createBean(HttpClient, server.URI)
def filter1 = ctx.getBean(Filter1)
def filter2 = ctx.getBean(Filter2)

when:
def resp = client.toBlocking().exchange("/initial", String)
then:
resp.body() == "initial"
filter1.filteredRequest.path == "/initial"
filter2.filteredRequest.path == "/filter1"

cleanup:
server.stop()
client.stop()
}


@Filter(Filter.MATCH_ALL_PATTERN)
@Requires(property = 'spec.name', value = 'FilterReplaceRequestSpec')
static class Filter1 implements HttpServerFilter {
HttpRequest<?> filteredRequest = null

@Override
int getOrder() {
1
}

@Override
Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
filteredRequest = request
return chain.proceed(HttpRequest.GET("/filter1"))
}
}

@Filter(Filter.MATCH_ALL_PATTERN)
@Requires(property = 'spec.name', value = 'FilterReplaceRequestSpec')
static class Filter2 implements HttpServerFilter {
HttpRequest<?> filteredRequest = null

@Override
int getOrder() {
2
}

@Override
Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
filteredRequest = request
return chain.proceed(HttpRequest.GET("/filter2"))
}
}

@Controller
@Requires(property = 'spec.name', value = 'FilterReplaceRequestSpec')
static class Ctrl {
@Get("/filter2")
def filter2() {
return "filter2"
}

@Get("/initial")
def initial() {
return "initial"
}
}
}
Expand Up @@ -393,9 +393,9 @@ public Publisher<MutableHttpResponse<?>> proceed(io.micronaut.http.HttpRequest<?
}
HttpFilter httpFilter = filters.get(pos);

HttpRequest<?> requestForFilter = requestReference.getAndSet(request);
requestReference.set(request);
try {
return Flux.from((Publisher<MutableHttpResponse<?>>) httpFilter.doFilter(requestForFilter, this))
return Flux.from((Publisher<MutableHttpResponse<?>>) httpFilter.doFilter(request, this))
.flatMap(handleStatusException)
.onErrorResume(onError);
} catch (Throwable t) {
Expand Down