< prev index next >
src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 21,35 ****
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
! package jdk.incubator.http.internal.common;
! import jdk.incubator.http.HttpHeaders;
import sun.net.NetProperties;
import sun.net.util.IPAddressUtil;
import javax.net.ssl.SSLParameters;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
--- 21,36 ----
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
! package jdk.internal.net.http.common;
! import java.net.http.HttpHeaders;
import sun.net.NetProperties;
import sun.net.util.IPAddressUtil;
+ import sun.net.www.HeaderParser;
import javax.net.ssl.SSLParameters;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
*** 46,61 ****
--- 47,66 ----
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.Collection;
+ import java.util.Collections;
import java.util.List;
import java.util.Set;
+ import java.util.TreeSet;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
+ import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.function.Supplier;
+ import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Collectors.joining;
/**
*** 91,117 ****
public static final int BUFSIZE = getIntegerNetProperty(
"jdk.httpclient.bufsize", DEFAULT_BUFSIZE
);
! private static final Set<String> DISALLOWED_HEADERS_SET = Set.of(
! "authorization", "connection", "cookie", "content-length",
! "date", "expect", "from", "host", "origin", "proxy-authorization",
! "referer", "user-agent", "upgrade", "via", "warning");
public static final Predicate<String>
! ALLOWED_HEADERS = header -> !Utils.DISALLOWED_HEADERS_SET.contains(header);
public static ByteBuffer getBuffer() {
return ByteBuffer.allocate(BUFSIZE);
}
public static Throwable getCompletionCause(Throwable x) {
if (!(x instanceof CompletionException)
&& !(x instanceof ExecutionException)) return x;
final Throwable cause = x.getCause();
! return cause == null ? x : cause;
}
public static IOException getIOException(Throwable t) {
if (t instanceof IOException) {
return (IOException) t;
--- 96,206 ----
public static final int BUFSIZE = getIntegerNetProperty(
"jdk.httpclient.bufsize", DEFAULT_BUFSIZE
);
! private static final Set<String> DISALLOWED_HEADERS_SET;
! static {
! // A case insensitive TreeSet of strings.
! TreeSet<String> treeSet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
! treeSet.addAll(Set.of("connection", "content-length",
! "date", "expect", "from", "host", "origin",
! "referer", "upgrade",
! "via", "warning"));
! DISALLOWED_HEADERS_SET = Collections.unmodifiableSet(treeSet);
! }
public static final Predicate<String>
! ALLOWED_HEADERS = header -> !DISALLOWED_HEADERS_SET.contains(header);
!
! private static final Predicate<String> IS_PROXY_HEADER = (k) ->
! k != null && k.length() > 6 && "proxy-".equalsIgnoreCase(k.substring(0,6));
! private static final Predicate<String> NO_PROXY_HEADER =
! IS_PROXY_HEADER.negate();
! private static final Predicate<String> ALL_HEADERS = (s) -> true;
!
! private static final Set<String> PROXY_AUTH_DISABLED_SCHEMES;
! private static final Set<String> PROXY_AUTH_TUNNEL_DISABLED_SCHEMES;
! static {
! String proxyAuthDisabled =
! getNetProperty("jdk.http.auth.proxying.disabledSchemes");
! String proxyAuthTunnelDisabled =
! getNetProperty("jdk.http.auth.tunneling.disabledSchemes");
! PROXY_AUTH_DISABLED_SCHEMES =
! proxyAuthDisabled == null ? Set.of() :
! Stream.of(proxyAuthDisabled.split(","))
! .map(String::trim)
! .filter((s) -> !s.isEmpty())
! .collect(Collectors.toUnmodifiableSet());
! PROXY_AUTH_TUNNEL_DISABLED_SCHEMES =
! proxyAuthTunnelDisabled == null ? Set.of() :
! Stream.of(proxyAuthTunnelDisabled.split(","))
! .map(String::trim)
! .filter((s) -> !s.isEmpty())
! .collect(Collectors.toUnmodifiableSet());
! }
!
! private static final String WSPACES = " \t\r\n";
! private static final boolean isAllowedForProxy(String name,
! List<String> value,
! Set<String> disabledSchemes,
! Predicate<String> allowedKeys) {
! if (!allowedKeys.test(name)) return false;
! if (disabledSchemes.isEmpty()) return true;
! if (name.equalsIgnoreCase("proxy-authorization")) {
! if (value.isEmpty()) return false;
! for (String scheme : disabledSchemes) {
! int slen = scheme.length();
! for (String v : value) {
! int vlen = v.length();
! if (vlen == slen) {
! if (v.equalsIgnoreCase(scheme)) {
! return false;
! }
! } else if (vlen > slen) {
! if (v.substring(0,slen).equalsIgnoreCase(scheme)) {
! int c = v.codePointAt(slen);
! if (WSPACES.indexOf(c) > -1
! || Character.isSpaceChar(c)
! || Character.isWhitespace(c)) {
! return false;
! }
! }
! }
! }
! }
! }
! return true;
! }
!
! public static final BiPredicate<String, List<String>> PROXY_TUNNEL_FILTER =
! (s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_TUNNEL_DISABLED_SCHEMES,
! IS_PROXY_HEADER);
! public static final BiPredicate<String, List<String>> PROXY_FILTER =
! (s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_DISABLED_SCHEMES,
! ALL_HEADERS);
! public static final BiPredicate<String, List<String>> NO_PROXY_HEADERS_FILTER =
! (n,v) -> Utils.NO_PROXY_HEADER.test(n);
!
!
! public static boolean proxyHasDisabledSchemes(boolean tunnel) {
! return tunnel ? ! PROXY_AUTH_TUNNEL_DISABLED_SCHEMES.isEmpty()
! : ! PROXY_AUTH_DISABLED_SCHEMES.isEmpty();
! }
public static ByteBuffer getBuffer() {
return ByteBuffer.allocate(BUFSIZE);
}
public static Throwable getCompletionCause(Throwable x) {
if (!(x instanceof CompletionException)
&& !(x instanceof ExecutionException)) return x;
final Throwable cause = x.getCause();
! if (cause == null) {
! throw new InternalError("Unexpected null cause", x);
! }
! return cause;
}
public static IOException getIOException(Throwable t) {
if (t instanceof IOException) {
return (IOException) t;
*** 462,488 ****
public static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer.allocate(0);
public static final ByteBuffer[] EMPTY_BB_ARRAY = new ByteBuffer[0];
public static final List<ByteBuffer> EMPTY_BB_LIST = List.of();
public static final ByteBufferReference[] EMPTY_BBR_ARRAY = new ByteBufferReference[0];
! public static ByteBuffer slice(ByteBuffer buffer, int amount) {
ByteBuffer newb = buffer.slice();
! newb.limit(amount);
! buffer.position(buffer.position() + amount);
return newb;
}
/**
* Get the Charset from the Content-encoding header. Defaults to
* UTF_8
*/
public static Charset charsetFrom(HttpHeaders headers) {
! String encoding = headers.firstValue("Content-encoding")
! .orElse("UTF_8");
try {
! return Charset.forName(encoding);
! } catch (IllegalArgumentException e) {
return StandardCharsets.UTF_8;
}
}
public static UncheckedIOException unchecked(IOException e) {
--- 551,601 ----
public static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer.allocate(0);
public static final ByteBuffer[] EMPTY_BB_ARRAY = new ByteBuffer[0];
public static final List<ByteBuffer> EMPTY_BB_LIST = List.of();
public static final ByteBufferReference[] EMPTY_BBR_ARRAY = new ByteBufferReference[0];
! /**
! * Returns a slice of size {@code amount} from the given buffer. If the
! * buffer contains more data than {@code amount}, then the slice's capacity
! * ( and, but not just, its limit ) is set to {@code amount}. If the buffer
! * does not contain more data than {@code amount}, then the slice's capacity
! * will be the same as the given buffer's capacity.
! */
! public static ByteBuffer sliceWithLimitedCapacity(ByteBuffer buffer, int amount) {
! final int index = buffer.position() + amount;
! final int limit = buffer.limit();
! if (index != limit) {
! // additional data in the buffer
! buffer.limit(index); // ensures that the slice does not go beyond
! } else {
! // no additional data in the buffer
! buffer.limit(buffer.capacity()); // allows the slice full capacity
! }
!
ByteBuffer newb = buffer.slice();
! buffer.position(index);
! buffer.limit(limit); // restore the original buffer's limit
! newb.limit(amount); // slices limit to amount (capacity may be greater)
return newb;
}
/**
* Get the Charset from the Content-encoding header. Defaults to
* UTF_8
*/
public static Charset charsetFrom(HttpHeaders headers) {
! String type = headers.firstValue("Content-type")
! .orElse("text/html; charset=utf-8");
! int i = type.indexOf(";");
! if (i >= 0) type = type.substring(i+1);
try {
! HeaderParser parser = new HeaderParser(type);
! String value = parser.findValue("charset");
! if (value == null) return StandardCharsets.UTF_8;
! return Charset.forName(value);
! } catch (Throwable x) {
! Log.logTrace("Can't find charset in \"{0}\" ({1})", type, x);
return StandardCharsets.UTF_8;
}
}
public static UncheckedIOException unchecked(IOException e) {
< prev index next >