Skip to content

Commit

Permalink
Properly proceed in filter processing when request is replaced (#7174)
Browse files Browse the repository at this point in the history
When a filter calls chain.proceed with a new request, that request should be used for further processing.
Replacing the request is still not fully working even with this change, so #5491 is not fully fixed. RoutingInBoundHandler still does not consider the new request, because most of the request processing happens before the filter is applied. In the test case of this patch, this manifests as the response text still being "initial" even though the request has been replaced.
  • Loading branch information
yawkat committed Apr 5, 2022
1 parent 989e0dc commit fe0bcce
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
@@ -0,0 +1,98 @@
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.HttpResponse
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.AutoCleanup
import spock.lang.PendingFeature
import spock.lang.Shared
import spock.lang.Specification

class FilterReplaceRequestSpec extends Specification {

@Shared
@AutoCleanup
EmbeddedServer server = ApplicationContext.run(EmbeddedServer, ['spec.name': 'FilterReplaceRequestSpec'])

@Shared
@AutoCleanup
HttpClient client = server.applicationContext.createBean(HttpClient, server.URI)

def 'test replaced http request is handled by next filter'() {
when:
client.toBlocking().exchange("/initial", String)
Filter1 filter1 = server.applicationContext.getBean(Filter1)
Filter2 filter2 = server.applicationContext.getBean(Filter2)

then:
filter1.filteredRequest.path == "/initial"
filter2.filteredRequest.path == "/filter1"
}

@PendingFeature
def 'last filter http request is used to match route'() {
when:
HttpResponse<String> resp = client.toBlocking().retrieve("/initial", String)

then:
resp.body() == "filter2"
}

@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
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
chain.proceed(HttpRequest.GET("/filter2"))
}
}

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

@Get("/initial")
String initial() {
"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

0 comments on commit fe0bcce

Please sign in to comment.