📄 commonshttpsender.java
字号:
* @param host host name * @param nonProxyHosts string containing the list of non proxy hosts * * @return true/false */ protected boolean isHostInNonProxyList(String host, String nonProxyHosts) { if ((nonProxyHosts == null) || (host == null)) { return false; } /* * The http.nonProxyHosts system property is a list enclosed in * double quotes with items separated by a vertical bar. */ StringTokenizer tokenizer = new StringTokenizer(nonProxyHosts, "|\""); while (tokenizer.hasMoreTokens()) { String pattern = tokenizer.nextToken(); if (log.isDebugEnabled()) { log.debug(Messages.getMessage("match00", new String[]{"HTTPSender", host, pattern})); } if (match(pattern, host, false)) { return true; } } return false; } /** * Matches a string against a pattern. The pattern contains two special * characters: * '*' which means zero or more characters, * * @param pattern the (non-null) pattern to match against * @param str the (non-null) string that must be matched against the * pattern * @param isCaseSensitive * * @return <code>true</code> when the string matches against the pattern, * <code>false</code> otherwise. */ protected static boolean match(String pattern, String str, boolean isCaseSensitive) { char[] patArr = pattern.toCharArray(); char[] strArr = str.toCharArray(); int patIdxStart = 0; int patIdxEnd = patArr.length - 1; int strIdxStart = 0; int strIdxEnd = strArr.length - 1; char ch; boolean containsStar = false; for (int i = 0; i < patArr.length; i++) { if (patArr[i] == '*') { containsStar = true; break; } } if (!containsStar) { // No '*'s, so we make a shortcut if (patIdxEnd != strIdxEnd) { return false; // Pattern and string do not have the same size } for (int i = 0; i <= patIdxEnd; i++) { ch = patArr[i]; if (isCaseSensitive && (ch != strArr[i])) { return false; // Character mismatch } if (!isCaseSensitive && (Character.toUpperCase(ch) != Character.toUpperCase(strArr[i]))) { return false; // Character mismatch } } return true; // String matches against pattern } if (patIdxEnd == 0) { return true; // Pattern contains only '*', which matches anything } // Process characters before first star while ((ch = patArr[patIdxStart]) != '*' && (strIdxStart <= strIdxEnd)) { if (isCaseSensitive && (ch != strArr[strIdxStart])) { return false; // Character mismatch } if (!isCaseSensitive && (Character.toUpperCase(ch) != Character.toUpperCase(strArr[strIdxStart]))) { return false; // Character mismatch } patIdxStart++; strIdxStart++; } if (strIdxStart > strIdxEnd) { // All characters in the string are used. Check if only '*'s are // left in the pattern. If so, we succeeded. Otherwise failure. for (int i = patIdxStart; i <= patIdxEnd; i++) { if (patArr[i] != '*') { return false; } } return true; } // Process characters after last star while ((ch = patArr[patIdxEnd]) != '*' && (strIdxStart <= strIdxEnd)) { if (isCaseSensitive && (ch != strArr[strIdxEnd])) { return false; // Character mismatch } if (!isCaseSensitive && (Character.toUpperCase(ch) != Character.toUpperCase(strArr[strIdxEnd]))) { return false; // Character mismatch } patIdxEnd--; strIdxEnd--; } if (strIdxStart > strIdxEnd) { // All characters in the string are used. Check if only '*'s are // left in the pattern. If so, we succeeded. Otherwise failure. for (int i = patIdxStart; i <= patIdxEnd; i++) { if (patArr[i] != '*') { return false; } } return true; } // process pattern between stars. padIdxStart and patIdxEnd point // always to a '*'. while ((patIdxStart != patIdxEnd) && (strIdxStart <= strIdxEnd)) { int patIdxTmp = -1; for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { if (patArr[i] == '*') { patIdxTmp = i; break; } } if (patIdxTmp == patIdxStart + 1) { // Two stars next to each other, skip the first one. patIdxStart++; continue; } // Find the pattern between padIdxStart & padIdxTmp in str between // strIdxStart & strIdxEnd int patLength = (patIdxTmp - patIdxStart - 1); int strLength = (strIdxEnd - strIdxStart + 1); int foundIdx = -1; strLoop: for (int i = 0; i <= strLength - patLength; i++) { for (int j = 0; j < patLength; j++) { ch = patArr[patIdxStart + j + 1]; if (isCaseSensitive && (ch != strArr[strIdxStart + i + j])) { continue strLoop; } if (!isCaseSensitive && (Character .toUpperCase(ch) != Character .toUpperCase(strArr[strIdxStart + i + j]))) { continue strLoop; } } foundIdx = strIdxStart + i; break; } if (foundIdx == -1) { return false; } patIdxStart = patIdxTmp; strIdxStart = foundIdx + patLength; } // All characters in the string are used. Check if only '*'s are left // in the pattern. If so, we succeeded. Otherwise failure. for (int i = patIdxStart; i <= patIdxEnd; i++) { if (patArr[i] != '*') { return false; } } return true; } private static String getHeader(HttpMethodBase method, String headerName) { Header header = method.getResponseHeader(headerName); return (header == null) ? null : header.getValue().trim(); } private InputStream createConnectionReleasingInputStream(final HttpMethodBase method) throws IOException { return new FilterInputStream(method.getResponseBodyAsStream()) { public void close() throws IOException { try { super.close(); } finally { method.releaseConnection(); } } }; } private static class MessageRequestEntity implements RequestEntity { private HttpMethodBase method; private Message message; boolean httpChunkStream = true; //Use HTTP chunking or not. public MessageRequestEntity(HttpMethodBase method, Message message) { this.message = message; this.method = method; } public MessageRequestEntity(HttpMethodBase method, Message message, boolean httpChunkStream) { this.message = message; this.method = method; this.httpChunkStream = httpChunkStream; } public boolean isRepeatable() { return true; } public void writeRequest(OutputStream out) throws IOException { try { this.message.writeTo(out); } catch (SOAPException e) { throw new IOException(e.getMessage()); } } protected boolean isContentLengthNeeded() { return this.method.getParams().getVersion() == HttpVersion.HTTP_1_0 || !httpChunkStream; } public long getContentLength() { if (isContentLengthNeeded()) { try { return message.getContentLength(); } catch (Exception e) { } } return -1; /* -1 for chunked */ } public String getContentType() { return null; // a separate header is added } } private static class GzipMessageRequestEntity extends MessageRequestEntity { public GzipMessageRequestEntity(HttpMethodBase method, Message message) { super(method, message); } public GzipMessageRequestEntity(HttpMethodBase method, Message message, boolean httpChunkStream) { super(method, message, httpChunkStream); } public void writeRequest(OutputStream out) throws IOException { if (cachedStream != null) { cachedStream.writeTo(out); } else { GZIPOutputStream gzStream = new GZIPOutputStream(out); super.writeRequest(gzStream); gzStream.finish(); } } public long getContentLength() { if(isContentLengthNeeded()) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { writeRequest(baos); cachedStream = baos; return baos.size(); } catch (IOException e) { // fall through to doing chunked. } } return -1; // do chunked } private ByteArrayOutputStream cachedStream; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -