private void updateCharset() { String contentTypeHeader = headers.get(CONTENT_TYPE); Charset contentTypeCharset = extractContentTypeCharsetAttribute(contentTypeHeader); charset = withDefault(contentTypeCharset, withDefault(charset, UTF_8)); if (contentTypeHeader != null && contentTypeHeader.regionMatches(true, 0, "text/", 0, 5) && contentTypeCharset == null) { // add explicit charset to content-type header headers.set(CONTENT_TYPE, contentTypeHeader + "; charset=" + charset.name()); } }
@Test public void testGetHostHeader() { Uri uri = Uri.create("http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs"); String hostHeader = HttpUtils.hostHeader(uri); assertEquals(hostHeader, "stackoverflow.com", "Incorrect hostHeader returned"); }
@Test public void computeOriginForSecuredUriWithNonDefaultPort() { assertEquals(HttpUtils.originHeader(Uri.create("wss://foo.com:444/bar")), "https://foo.com:444"); } }
/** * Creates a new multipart entity containing the given parts. * * @param parts the parts to include. * @param requestHeaders the request headers * @return a MultipartBody */ public static MultipartBody newMultipartBody(List<Part> parts, HttpHeaders requestHeaders) { assertNotNull(parts, "parts"); byte[] boundary; String contentType; String contentTypeHeader = requestHeaders.get(CONTENT_TYPE); if (isNonEmpty(contentTypeHeader)) { int boundaryLocation = contentTypeHeader.indexOf("boundary="); if (boundaryLocation != -1) { // boundary defined in existing Content-Type contentType = contentTypeHeader; boundary = (contentTypeHeader.substring(boundaryLocation + "boundary=".length()).trim()).getBytes(US_ASCII); } else { // generate boundary and append it to existing Content-Type boundary = computeMultipartBoundary(); contentType = patchContentTypeWithBoundaryAttribute(contentTypeHeader, boundary); } } else { boundary = computeMultipartBoundary(); contentType = patchContentTypeWithBoundaryAttribute(HttpHeaderValues.MULTIPART_FORM_DATA, boundary); } List<MultipartPart<? extends Part>> multipartParts = generateMultipartParts(parts, boundary); return new MultipartBody(multipartParts, contentType, boundary); }
public void upgradeProtocol(ChannelPipeline pipeline, String scheme, String host, int port) throws IOException, GeneralSecurityException { if (pipeline.get(HTTP_HANDLER) != null) pipeline.remove(HTTP_HANDLER); if (isSecure(scheme)) if (isSslHandlerConfigured(pipeline)) { pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec()); } else { pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); pipeline.addFirst(SSL_HANDLER, createSslHandler(host, port)); } else pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); if (isWebSocket(scheme)) { pipeline.addAfter(HTTP_PROCESSOR, WS_PROCESSOR, wsProcessor); pipeline.remove(HTTP_PROCESSOR); } }
if (userDefinedAcceptEncoding != null) { headers.set(ACCEPT_ENCODING, filterOutBrotliFromAcceptEncoding(userDefinedAcceptEncoding)); headers.set(ORIGIN, originHeader(uri)); headers.set(HOST, virtualHost != null ? virtualHost : hostHeader(uri));
@Test public void testGetFollowRedirectInRequest() { Request request = Dsl.get("http://stackoverflow.com/questions/1057564").setFollowRedirect(true).build(); DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().build(); boolean followRedirect = HttpUtils.followRedirect(config, request); assertTrue(followRedirect, "Follow redirect must be true as set in the request"); }
/** * Always make sure the channel who got cached support the proper protocol. * It could only occurs when a HttpMethod. CONNECT is used against a proxy * that requires upgrading from http to https. */ public void verifyChannelPipeline(ChannelPipeline pipeline, String scheme) throws IOException, GeneralSecurityException { boolean sslHandlerConfigured = isSslHandlerConfigured(pipeline); if (isSecure(scheme)) { if (!sslHandlerConfigured) pipeline.addFirst(SSL_HANDLER, new SslInitializer(this)); } else if (sslHandlerConfigured) pipeline.remove(SSL_HANDLER); }
private void formUrlEncoding(Charset charset) throws Exception { String key = "key"; String value = "中文"; List<Param> params = new ArrayList<>(); params.add(new Param(key, value)); ByteBuffer ahcBytes = HttpUtils.urlEncodeFormParams(params, charset); String ahcString = toUsAsciiString(ahcBytes); String jdkString = key + "=" + URLEncoder.encode(value, charset.name()); assertEquals(ahcString, jdkString); }
public <T> ListenableFuture<T> sendRequest(final Request request,// final AsyncHandler<T> asyncHandler,// NettyResponseFuture<T> future,// boolean reclaimCache) throws IOException { if (closed.get()) throw new IOException("Closed"); Uri uri = request.getUri(); validateWebSocketRequest(request, uri, asyncHandler); ProxyServer proxyServer = getProxyServer(config, request); boolean resultOfAConnect = future != null && future.getNettyRequest() != null && future.getNettyRequest().getHttpRequest().getMethod() == HttpMethod.CONNECT; boolean useProxy = proxyServer != null && !resultOfAConnect; if (useProxy && useProxyConnect(uri)) // SSL proxy, have to handle CONNECT if (future != null && future.isConnectAllowed()) // CONNECT forced return sendRequestWithCertainForceConnect(request, asyncHandler, future, reclaimCache, uri, proxyServer, true, true); else return sendRequestThroughSslProxy(request, asyncHandler, future, reclaimCache, uri, proxyServer); else return sendRequestWithCertainForceConnect(request, asyncHandler, future, reclaimCache, uri, proxyServer, useProxy, false); }
private static void encodeAndAppendFormParam(StringBuilder sb, String name, String value, Charset charset) { encodeAndAppendFormField(sb, name, charset); if (value != null) { sb.append('='); encodeAndAppendFormField(sb, value, charset); } sb.append('&'); }
public void upgradeProtocol(ChannelPipeline pipeline, String scheme, String host, int port) throws IOException, GeneralSecurityException { if (pipeline.get(HTTP_HANDLER) != null) pipeline.remove(HTTP_HANDLER); if (isSecure(scheme)) if (isSslHandlerConfigured(pipeline)) { pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec()); } else { pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); pipeline.addFirst(SSL_HANDLER, createSslHandler(host, port)); } else pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); if (isWebSocket(scheme)) { pipeline.addAfter(HTTP_PROCESSOR, WS_PROCESSOR, wsProcessor); pipeline.remove(HTTP_PROCESSOR); } }
@Test public void testGetFollowRedirectInConfig() { Request request = Dsl.get("http://stackoverflow.com/questions/1057564").build(); DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().setFollowRedirect(true).build(); boolean followRedirect = HttpUtils.followRedirect(config, request); assertTrue(followRedirect, "Follow redirect should be equal to value specified in config when not specified in request"); }
public void verifyChannelPipeline(ChannelPipeline pipeline, String scheme) throws IOException, GeneralSecurityException { boolean sslHandlerConfigured = isSslHandlerConfigured(pipeline); if (isSecure(scheme)) { if (!sslHandlerConfigured) pipeline.addFirst(SSL_HANDLER, new SslInitializer(this)); } else if (sslHandlerConfigured) pipeline.remove(SSL_HANDLER); }
nettyBody = new NettyByteBufferBody(urlEncodeFormParams(request.getFormParams(), bodyCharset), contentTypeOverride);
public <T> ListenableFuture<T> sendRequest(final Request request,// final AsyncHandler<T> asyncHandler,// NettyResponseFuture<T> future,// boolean reclaimCache) throws IOException { if (closed.get()) throw new IOException("Closed"); Uri uri = request.getUri(); validateWebSocketRequest(request, uri, asyncHandler); ProxyServer proxyServer = getProxyServer(config, request); boolean resultOfAConnect = future != null && future.getNettyRequest() != null && future.getNettyRequest().getHttpRequest().getMethod() == HttpMethod.CONNECT; boolean useProxy = proxyServer != null && !resultOfAConnect; if (useProxy && useProxyConnect(uri)) // SSL proxy, have to handle CONNECT if (future != null && future.isConnectAllowed()) // CONNECT forced return sendRequestWithCertainForceConnect(request, asyncHandler, future, reclaimCache, uri, proxyServer, true, true); else return sendRequestThroughSslProxy(request, asyncHandler, future, reclaimCache, uri, proxyServer); else return sendRequestWithCertainForceConnect(request, asyncHandler, future, reclaimCache, uri, proxyServer, useProxy, false); }
boolean connect = method == HttpMethod.CONNECT; boolean allowConnectionPooling = config.isAllowPoolingConnections() && (!HttpUtils.isSecure(uri) || config.isAllowPoolingSslConnections()); boolean webSocket = isWebSocket(uri.getScheme()); if (!connect && webSocket) { .set(ORIGIN, "http://" + uri.getHost() + ":" + (uri.getPort() == -1 ? isSecure(uri.getScheme()) ? 443 : 80 : uri.getPort()))//
@Test public void testExtractCharsetWithDoubleQuotes() { Charset charset = HttpUtils.extractContentTypeCharsetAttribute("text/html; charset=\"iso-8859-1\""); assertEquals(charset, ISO_8859_1); }
@Test public void computeOriginForSecuredUriWithDefaultPort() { assertEquals(HttpUtils.originHeader(Uri.create("wss://foo.com:443/bar")), "https://foo.com"); }
@Test public void testDefaultFollowRedirect() { Request request = Dsl.get("http://stackoverflow.com/questions/1057564").setVirtualHost("example.com").build(); DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().build(); boolean followRedirect = HttpUtils.followRedirect(config, request); assertFalse(followRedirect, "Default value of redirect should be false"); }