/** * Invoke this API if you want to set the Range header on your {@link Request} based on the last valid bytes * position. * * @param request {@link Request} * @return a {@link Request} with the Range header properly set. */ public Request adjustRequestRange(Request request) { Long ri = resumableIndex.get(request.getUrl()); if (ri != null) { byteTransferred.set(ri); } // The Resumable if (resumableListener != null && resumableListener.length() > 0 && byteTransferred.get() != resumableListener.length()) { byteTransferred.set(resumableListener.length()); } RequestBuilder builder = new RequestBuilder(request); if (request.getHeaders().get(RANGE) == null && byteTransferred.get() != 0) { builder.setHeader(RANGE, "bytes=" + byteTransferred.get() + "-"); } return builder.build(); }
@Override public <T> FilterContext<T> filter(FilterContext<T> ctx) { // pass on the x-expect-get and remove the x-redirect // headers if found in the response ctx.getResponseHeaders().get("x-expect-post"); ctx.getRequest().getHeaders().add("x-expect-post", "true"); ctx.getRequest().getHeaders().remove("x-redirect"); return ctx; } };
@Override public <T> FilterContext<T> filter(FilterContext<T> ctx) { // pass on the x-expect-get and remove the x-redirect // headers if found in the response ctx.getResponseHeaders().get("x-expect-get"); ctx.getRequest().getHeaders().add("x-expect-get", "true"); ctx.getRequest().getHeaders().remove("x-redirect"); return ctx; } };
private <T> NettyResponseFuture<T> newNettyResponseFuture(Request request, AsyncHandler<T> asyncHandler, NettyRequest nettyRequest, ProxyServer proxyServer) { NettyResponseFuture<T> future = new NettyResponseFuture<>( request, asyncHandler, nettyRequest, config.getMaxRequestRetry(), request.getChannelPoolPartitioning(), connectionSemaphore, proxyServer); String expectHeader = request.getHeaders().get(EXPECT); if (HttpHeaderValues.CONTINUE.contentEqualsIgnoreCase(expectHeader)) future.setDontWriteBodyBecauseExpectContinue(true); return future; }
@Test public void testSettingHeadersUsingMapWithStringKeys() { Map<String, List<String>> headers = new HashMap<>(); headers.put("X-Forwarded-For", singletonList("10.0.0.1")); RequestBuilder requestBuilder = new RequestBuilder(); requestBuilder.setHeaders(headers); requestBuilder.setUrl("http://localhost"); Request request = requestBuilder.build(); assertEquals(request.getHeaders().get("X-Forwarded-For"), "10.0.0.1"); } }
public static String perConnectionProxyAuthorizationHeader(Request request, Realm proxyRealm) { String proxyAuthorization = null; if (proxyRealm != null && proxyRealm.isUsePreemptiveAuth()) { switch (proxyRealm.getScheme()) { case NTLM: case KERBEROS: case SPNEGO: List<String> auth = request.getHeaders().getAll(PROXY_AUTHORIZATION); if (getHeaderWithPrefix(auth, "NTLM") == null) { String msg = NtlmEngine.INSTANCE.generateType1Msg(); proxyAuthorization = "NTLM " + msg; } break; default: } } return proxyAuthorization; }
@Override protected ListenableFuture<Response> go(Request request) throws JsonProcessingException { Assert.assertEquals(TARGET_URL, request.getUrl()); Assert.assertEquals( "application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE) ); Assert.assertEquals( StringUtils.format( "[%s,%s]\n", jsonMapper.writeValueAsString(events.get(counter.getAndIncrement())), jsonMapper.writeValueAsString(events.get(counter.getAndIncrement())) ), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() ); return GoHandlers.immediateFuture(okResponse()); } }.times(3)
@Override protected ListenableFuture<Response> go(Request request) throws JsonProcessingException { Assert.assertEquals(TARGET_URL, request.getUrl()); Assert.assertEquals( "application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE) ); Assert.assertEquals( StringUtils.format( "[%s,%s]\n", jsonMapper.writeValueAsString(events.get(0)), jsonMapper.writeValueAsString(events.get(1)) ), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() ); return GoHandlers.immediateFuture(okResponse()); } }.times(1)
private HttpHeaders propagatedHeaders(Request request, Realm realm, boolean keepBody) { HttpHeaders headers = request.getHeaders() .remove(HOST) .remove(CONTENT_LENGTH); if (!keepBody) { headers.remove(CONTENT_TYPE); } if (realm != null && realm.getScheme() == AuthScheme.NTLM) { headers.remove(AUTHORIZATION) .remove(PROXY_AUTHORIZATION); } return headers; } }
@Override protected ListenableFuture<Response> go(Request request) throws IOException { Assert.assertEquals(TARGET_URL, request.getUrl()); Assert.assertEquals( "application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE) ); Assert.assertEquals( HttpHeaders.Values.GZIP, request.getHeaders().get(HttpHeaders.Names.CONTENT_ENCODING) ); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteBuffer data = request.getByteBufferData().slice(); byte[] dataArray = new byte[data.remaining()]; data.get(dataArray); CompressionUtils.gunzip(new ByteArrayInputStream(dataArray), baos); Assert.assertEquals( StringUtils.format( "[%s,%s]\n", jsonMapper.writeValueAsString(events.get(0)), jsonMapper.writeValueAsString(events.get(1)) ), baos.toString(StandardCharsets.UTF_8.name()) ); return GoHandlers.immediateFuture(okResponse()); } }.times(1)
@Override protected ListenableFuture<Response> go(Request request) throws JsonProcessingException { Assert.assertEquals(TARGET_URL, request.getUrl()); Assert.assertEquals( "application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE) ); Assert.assertEquals( StringUtils.format( "[%s,%s]\n", jsonMapper.writeValueAsString(events.get(0)), jsonMapper.writeValueAsString(events.get(1)) ), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() ); return GoHandlers.immediateFuture(okResponse()); } }.times(1)
@Test public void testAdjustRange() { MapResumableProcessor proc = new MapResumableProcessor(); ResumableAsyncHandler handler = new ResumableAsyncHandler(proc); Request request = get("http://test/url").build(); Request newRequest = handler.adjustRequestRange(request); assertEquals(newRequest.getUri(), request.getUri()); String rangeHeader = newRequest.getHeaders().get(RANGE); assertNull(rangeHeader); proc.put("http://test/url", 5000); newRequest = handler.adjustRequestRange(request); assertEquals(newRequest.getUri(), request.getUri()); rangeHeader = newRequest.getHeaders().get(RANGE); assertEquals(rangeHeader, "bytes=5000-"); }
@Override protected ListenableFuture<Response> go(Request request) throws JsonProcessingException { Assert.assertEquals(TARGET_URL, request.getUrl()); Assert.assertEquals( "application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE) ); Assert.assertEquals( "Basic " + StringUtils.encodeBase64String(StringUtils.toUtf8("foo:bar")), request.getHeaders().get(HttpHeaders.Names.AUTHORIZATION) ); Assert.assertEquals( StringUtils.format( "%s\n%s\n", jsonMapper.writeValueAsString(events.get(0)), jsonMapper.writeValueAsString(events.get(1)) ), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() ); return GoHandlers.immediateFuture(okResponse()); } }.times(1)
String authHeader = req.getHeaders().get(AUTHORIZATION); Matcher m = Pattern.compile("oauth_signature=\"(.+?)\"").matcher(authHeader); assertTrue(m.find());
headers.set(PROXY_AUTHORIZATION, request.getHeaders().getAll(PROXY_AUTHORIZATION)); headers.set(request.getHeaders());
String authHeader = req.getHeaders().get(AUTHORIZATION); Matcher m = Pattern.compile("oauth_signature=\"(.+?)\"").matcher(authHeader); assertEquals(m.find(), true);
@Test public void testPostCalculateSignature() throws UnsupportedEncodingException { StaticOAuthSignatureCalculator calc = // new StaticOAuthSignatureCalculator(// new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET), new RequestToken(TOKEN_KEY, TOKEN_SECRET), NONCE, TIMESTAMP); final Request req = post("http://photos.example.net/photos") .addFormParam("file", "vacation.jpg") .addFormParam("size", "original") .setSignatureCalculator(calc) .build(); // From the signature tester, POST should look like: // normalized parameters: // file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original // signature base string: // POST&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal // signature: wPkvxykrw+BTdCcGqKr+3I+PsiM= // header: OAuth // realm="",oauth_version="1.0",oauth_consumer_key="dpf43f3p2l4k3l03",oauth_token="nnch734d00sl2jdk",oauth_timestamp="1191242096",oauth_nonce="kllo9940pd9333jh",oauth_signature_method="HMAC-SHA1",oauth_signature="wPkvxykrw%2BBTdCcGqKr%2B3I%2BPsiM%3D" String authHeader = req.getHeaders().get(AUTHORIZATION); Matcher m = Pattern.compile("oauth_signature=\"(.+?)\"").matcher(authHeader); assertEquals(m.find(), true); String encodedSig = m.group(1); String sig = URLDecoder.decode(encodedSig, "UTF-8"); assertEquals(sig, "wPkvxykrw+BTdCcGqKr+3I+PsiM="); }
HttpHeaders requestHeaders = new DefaultHttpHeaders(false).add(request.getHeaders());
this.localAddress = prototype.getLocalAddress(); this.headers = new DefaultHttpHeaders(validateHeaders); this.headers.add(prototype.getHeaders()); if (isNonEmpty(prototype.getCookies())) { this.cookies = new ArrayList<>(prototype.getCookies());
CharSequence contentTypeOverride = request.getHeaders().contains(CONTENT_TYPE) ? null : HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED; nettyBody = new NettyByteBufferBody(urlEncodeFormParams(request.getFormParams(), bodyCharset), contentTypeOverride); nettyBody = new NettyMultipartBody(request.getBodyParts(), request.getHeaders(), config);