-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle a fragment in a client-side URI properly (#4789)
Motivation: When a user sends a request whose path contains a fragment (e.g. `#foo`), Armeria behaves inconsistently depending on whether a user specified an absolute URI in `:path` or not. On an absolute URI, we rely on `URI` for parsing, which takes a fragment into account. Otherwise, we use `PathAndQuery`, which doesn't treat a fragment as a fragment and just normalizes `#` into `%2A` as a part of path and query. Modifications: - Evolve `PathAndQuery` into `RequestTarget` that is capable of parsing and normalizing a `:path` header. - `RequestTarget` now understands a fragment as well as an absolute URI. - Added `RequestTargetForm` - When normalizing a client-side path, `RequestTarget` doesn't clean up consecutive slashes anymore, e.g. `foo///bar` is *not* normalized into `foo/bar` on the client side. - Replaced `path`, `query` and `fragment` fields and parameters with `RequestTarget` where applicable, including: - `RequestContext` implementations - `AbstractRequestContextBuilder` and its subtypes - `UserClient` and its subtypes - `RoutingContext` implementations - Removed `RoutingStatus.INVALID_PATH` because `RequestTarget` always ensures that the path is valid now. - Split the path cache metrics into client-side and server-side ones - Old meter name: `armeria.server.parsed.path.cache` - New meter names: - `armeria.path.cache{type=client}` - `armeria.path.cache{type=server}` - `HttpClientDelegate` now makes sure `ctx.request() == req` to prevent the loophole where a decorator can send a request with an invalid path. - A user must call `ctx.updateRequest()` to validate the request first. - Renamed `PathParsingBenchmark` to `RequestTargetBenchmark` - Added client-side benchmarks - Fixed the incorrect JVM system property name Result: - Armeria client now handles the fragment part of URI consistently. - (Defect) Closed the loophole that allowed a decorator to send a different request than `ctx.request()`. - A decorator now must call `ctx.updateRequest()` when it replaces the current request. - (Improvement) Armeria client doesn't normalize consecutive slashes in a client request path anymore, giving a user freedom to send such a request. - Note: Please make sure that your server or service handles consecutive slashes (e.g. `foo//bar`) properly before an upgrade. Armeria server always cleans up such a path for you, so you don't need to worry. - (Breaking) `RoutingStatus.INVALID_PATH` has been removed because Armeria doesn't leak a request with an invalid state into router. - (Breaking) The signatures of `UserClient.execute()` have been changed. - (Breaking) The names of path cache meters have been changed. - Old meter name: `armeria.server.parsed.path.cache` - New meter names: - `armeria.path.cache{type=client}` - `armeria.path.cache{type=server}`
- Loading branch information
Showing
53 changed files
with
2,612 additions
and
1,941 deletions.
There are no files selected for viewing
79 changes: 0 additions & 79 deletions
79
benchmarks/jmh/src/jmh/java/com/linecorp/armeria/internal/common/PathParsingBenchmark.java
This file was deleted.
Oops, something went wrong.
119 changes: 119 additions & 0 deletions
119
benchmarks/jmh/src/jmh/java/com/linecorp/armeria/internal/common/RequestTargetBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2017 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.internal.common; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import com.linecorp.armeria.common.RequestTarget; | ||
|
||
/** | ||
* Microbenchmarks for {@link RequestTarget}. | ||
*/ | ||
@State(Scope.Thread) | ||
public class RequestTargetBenchmark { | ||
|
||
private static final String NO_CACHE_JVM_OPTS = "-Dcom.linecorp.armeria.parsedPathCacheSpec=off"; | ||
|
||
private String path1; | ||
private String path2; | ||
|
||
@Setup(Level.Invocation) | ||
@SuppressWarnings("StringOperationCanBeSimplified") | ||
public void setUp() { | ||
// Create a new String for paths every time to avoid constant folding. | ||
path1 = new String("/armeria/services/hello-world"); | ||
path2 = new String("/armeria/services/goodbye-world"); | ||
} | ||
|
||
@Benchmark | ||
public RequestTarget serverCached() { | ||
return doServer(); | ||
} | ||
|
||
@Benchmark | ||
@Fork(jvmArgsAppend = NO_CACHE_JVM_OPTS) | ||
public RequestTarget serverUncached() { | ||
return doServer(); | ||
} | ||
|
||
private RequestTarget doServer() { | ||
final RequestTarget parsed = RequestTarget.forServer(path1); | ||
RequestTargetCache.putForServer(path1, parsed); | ||
return parsed; | ||
} | ||
|
||
@Benchmark | ||
public RequestTarget serverCachedAndUncached(Blackhole bh) { | ||
return doServerCachedAndUncached(bh); | ||
} | ||
|
||
@Benchmark | ||
@Fork(jvmArgsAppend = NO_CACHE_JVM_OPTS) | ||
public RequestTarget serverUncachedAndUncached(Blackhole bh) { | ||
return doServerCachedAndUncached(bh); | ||
} | ||
|
||
private RequestTarget doServerCachedAndUncached(Blackhole bh) { | ||
final RequestTarget parsed = RequestTarget.forServer(path1); | ||
RequestTargetCache.putForServer(path1, parsed); | ||
final RequestTarget parsed2 = RequestTarget.forServer(path2); | ||
bh.consume(parsed2); | ||
return parsed; | ||
} | ||
|
||
@Benchmark | ||
public RequestTarget clientCached() { | ||
return doServer(); | ||
} | ||
|
||
@Benchmark | ||
@Fork(jvmArgsAppend = NO_CACHE_JVM_OPTS) | ||
public RequestTarget clientUncached() { | ||
return doClient(); | ||
} | ||
|
||
private RequestTarget doClient() { | ||
final RequestTarget parsed = RequestTarget.forClient(path1); | ||
RequestTargetCache.putForClient(path1, parsed); | ||
return parsed; | ||
} | ||
|
||
@Benchmark | ||
public RequestTarget clientCachedAndUncached(Blackhole bh) { | ||
return doClientCachedAndUncached(bh); | ||
} | ||
|
||
@Benchmark | ||
@Fork(jvmArgsAppend = NO_CACHE_JVM_OPTS) | ||
public RequestTarget clientUncachedAndUncached(Blackhole bh) { | ||
return doClientCachedAndUncached(bh); | ||
} | ||
|
||
private RequestTarget doClientCachedAndUncached(Blackhole bh) { | ||
final RequestTarget parsed = RequestTarget.forClient(path1); | ||
RequestTargetCache.putForClient(path1, parsed); | ||
final RequestTarget parsed2 = RequestTarget.forClient(path2); | ||
bh.consume(parsed2); | ||
return parsed; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.