@VisibleForTesting LiveHttpRequest.Builder makeAStyxRequestFrom(HttpRequest request, Observable<ByteBuf> content) { Url url = url(unwiseCharEncoder.encode(request.uri())) .build(); LiveHttpRequest.Builder requestBuilder = new LiveHttpRequest.Builder() .method(toStyxMethod(request.method())) .url(url) .version(toStyxVersion(request.protocolVersion())) .id(uniqueIdSupplier.get()) .body(new ByteStream(toPublisher(content.map(Buffers::fromByteBuf)))); stream(request.headers().spliterator(), false) .forEach(entry -> requestBuilder.addHeader(entry.getKey(), entry.getValue())); return requestBuilder; }
/** * Create a builder from a URI string. This will be parsed using the java standard library. * * @param value a URI string * @return a new builder */ public static Builder url(String value) { URI uri = URI.create(value); return new Builder() .scheme(uri.getScheme()) .authority(uri.getAuthority()) .path(uri.getRawPath()) .rawQuery(uri.getRawQuery()) .fragment(uri.getFragment()); }
@Test public void convertsToStreamingHttpRequest() throws Exception { HttpRequest fullRequest = new HttpRequest.Builder(POST, "/foo/bar").body("foobar", UTF_8) .version(HTTP_1_1) .header("HeaderName", "HeaderValue") .cookies(requestCookie("CookieName", "CookieValue")) .build(); LiveHttpRequest streaming = fullRequest.stream(); assertThat(streaming.method(), is(HttpMethod.POST)); assertThat(streaming.url(), is(url("/foo/bar").build())); assertThat(streaming.version(), is(HTTP_1_1)); assertThat(streaming.headers(), containsInAnyOrder( header("Content-Length", "6"), header("HeaderName", "HeaderValue"), header("Cookie", "CookieName=CookieValue"))); assertThat(streaming.cookies(), contains(requestCookie("CookieName", "CookieValue"))); StepVerifier.create(streaming.aggregate(0x10000).map(it -> it.bodyAs(UTF_8))) .expectNext("foobar") .verifyComplete(); }
/** * Rewrites the URL of the request according to the rewrite rules. * * @param request a request * @return a rewritten request */ public HttpRequest rewrite(HttpRequest request) { String path = request.path(); String newPath = newPath(path); if (!Objects.equals(newPath, path)) { Url newUrl = request.url().newBuilder().path(newPath).build(); return request.newBuilder() .url(newUrl) .build(); } return request; }
@Test public void decodesToFullHttpRequest() throws Exception { LiveHttpRequest streamingRequest = post("/foo/bar", body("foo", "bar")) .version(HTTP_1_0) .header("HeaderName", "HeaderValue") .cookies(requestCookie("CookieName", "CookieValue")) .build(); HttpRequest full = Mono.from(streamingRequest.aggregate(0x1000)).block(); assertThat(full.method(), is(POST)); assertThat(full.url(), is(url("/foo/bar").build())); assertThat(full.version(), is(HTTP_1_0)); assertThat(full.headers(), containsInAnyOrder( header("HeaderName", "HeaderValue"), header("Cookie", "CookieName=CookieValue"))); assertThat(full.cookies(), contains(requestCookie("CookieName", "CookieValue"))); assertThat(full.body(), is(bytes("foobar"))); }
/** * Rewrites the URL of the request according to the rewrite rules. * * @param request a request * @return a rewritten request */ public LiveHttpRequest rewrite(LiveHttpRequest request) { String path = request.path(); String newPath = newPath(path); if (!Objects.equals(newPath, path)) { Url newUrl = request.url().newBuilder().path(newPath).build(); return request.newBuilder() .url(newUrl) .build(); } return request; }
/** * Create a builder from a URI string. This will be parsed using the java standard library. * * @param value a URI string * @return a new builder */ public static Builder url(String value) { URI uri = URI.create(value); return new Builder() .scheme(uri.getScheme()) .authority(uri.getAuthority()) .path(uri.getRawPath()) .rawQuery(uri.getRawQuery()) .fragment(uri.getFragment()); }
/** * Return a new {@link Builder} that will inherit properties from this object. * This allows a new {@link Url} to be made that will be identical to this one except for the properties * overridden by the builder methods. * * @return new builder based on this object */ public Builder newBuilder() { return new Builder(this); }
@Test public void spaceIsAlsoPlusIsAlsoHex20() { String ENCODED_SPACE = "/customercare/subscribe.html%20sessid=nXF5jQ8rTW3bAbh6djb2hYJE3D.web-app-02"; String path = "/customercare/subscribe.html+sessid=nXF5jQ8rTW3bAbh6djb2hYJE3D.web-app-02"; assertThat(url(path).build().encodedUri(), is(path)); assertThat(url(ENCODED_SPACE).build().encodedUri(), is(ENCODED_SPACE)); }