📄 protocol.java
字号:
public String getType() { try { connect(); } catch (IOException x) { return null; } try { return getHeaderField("content-type"); } catch (IOException e) { return null; } } public String getEncoding() { try { connect(); } catch (IOException x) { return null; } try { return getHeaderField("content-encoding"); } catch (Exception e) { return null; } } public long getExpiration() throws IOException { return getHeaderFieldDate("expires", 0); } public long getDate() throws IOException { return getHeaderFieldDate("date", 0); } public long getLastModified() throws IOException { return getHeaderFieldDate("last-modified", 0); } public String getHeaderField(String name) throws IOException { ensureOpen(); try { connect(); } catch (IOException x) { return null; } return (String)headerFields.get(toLowerCase(name)); } public String getHeaderField(int index) throws IOException { ensureOpen(); try { connect(); } catch (IOException x) { return null; } if (headerFieldValues == null) { makeHeaderFieldValues(); } if (index >= headerFieldValues.length) return null; return headerFieldValues[index]; } public String getHeaderFieldKey(int index) throws IOException { ensureOpen(); try { connect(); } catch (IOException x) { return null; } if (headerFieldNames == null) { makeHeaderFields(); } if (index >= headerFieldNames.length) return null; return headerFieldNames[index]; } private void makeHeaderFields() { int i = 0; headerFieldNames = new String [ headerFields.size() ]; for (Enumeration e = headerFields.keys(); e.hasMoreElements(); headerFieldNames[i++] = (String)e.nextElement()); } private void makeHeaderFieldValues() { int i = 0; headerFieldValues = new String [ headerFields.size() ]; for (Enumeration e = headerFields.keys(); e.hasMoreElements(); headerFieldValues[i++] = (String) headerFields.get(e.nextElement())); } public int getHeaderFieldInt(String name, int def) throws IOException { ensureOpen(); try { connect(); } catch (IOException x) { return def; } try { return Integer.parseInt(getHeaderField(name)); } catch (Throwable t) { } return def; } public long getHeaderFieldDate(String name, long def) throws IOException { ensureOpen(); try { connect(); } catch (IOException x) { return def; } try { return DateParser.parse(getHeaderField(name)); } catch (Throwable t) { } return def; } protected StreamConnection connectSocket() throws IOException { // Check for illegal empty string for host if (host.equals("")) { throw new IllegalArgumentException("Host not recognized: "+host); } // Open socket connection. StreamConnection hsc = null; if (proxyHost == null) { hsc = connectionPool.get(protocol, host, port); } else { hsc = connectionPool.get(protocol, proxyHost, proxyPort); } if (hsc != null) { return hsc; } if (proxyHost == null) { hsc = new HttpStreamConnection(host, port); } else { hsc = new HttpStreamConnection(proxyHost, proxyPort); } return hsc; } protected void connect() throws IOException { if (connected) { return; } streamOutput = streamConnection.openDataOutputStream(); // HTTP 1.1 requests must contain content length for proxies if ((getRequestProperty("Content-Length") == null) || (getRequestProperty("Content-Length").equals("0"))) { reqProperties.put("Content-Length", "" + (privateOut == null ? 0 : privateOut.size())); } String reqLine; if (proxyHost == null) { reqLine = method + " " + (getFile() == null ? "/" : getFile()) + (getRef() == null ? "" : "#" + getRef()) + (getQuery() == null ? "" : "?" + getQuery()) + " " + httpVersion + "\r\n"; } else { reqLine = method + " http://" + host + ":" + port + (getFile() == null ? "/" : getFile()) + (getRef() == null ? "" : "#" + getRef()) + (getQuery() == null ? "" : "?" + getQuery()) + " " + httpVersion + "\r\n"; } // DEBUG: System.out.print("Request: " + reqLine); streamOutput.write((reqLine).getBytes()); // HTTP 1/1 requests require the Host header to distinguish virtual // host locations. reqProperties.put("Host", host + ":" + port); Enumeration reqKeys = reqProperties.keys(); while (reqKeys.hasMoreElements()) { String key = (String)reqKeys.nextElement(); String reqPropLine = key + ": " + reqProperties.get(key) + "\r\n"; streamOutput.write((reqPropLine).getBytes()); } streamOutput.write("\r\n".getBytes()); if (privateOut != null) { streamOutput.write(privateOut.toByteArray()); resetOutput(); // ***Bug 4485901*** streamOutput.write("\r\n".getBytes()); /* DEBUG: System.out.println("Request: " + new String(out.toByteArray())); */ } streamOutput.flush(); streamInput = streamConnection.openDataInputStream(); readResponseMessage(); readHeaders(); connected = true; } protected void resetOutput() { privateOut.reset(); } protected void readResponseMessage() throws IOException { String line = readLine(streamInput); int httpEnd, codeEnd; responseCode = -1; responseMsg = null; responseProtocol = null; // DEBUG: System.out.println ("Response: " + line);malformed: { if (line == null) break malformed; httpEnd = line.indexOf(' '); if (httpEnd < 0) break malformed; responseProtocol = line.substring(0, httpEnd); if (!responseProtocol.startsWith("HTTP")) break malformed; if (line.length() <= httpEnd) break malformed; codeEnd = line.substring(httpEnd + 1).indexOf(' '); if (codeEnd < 0) break malformed; codeEnd += (httpEnd + 1); if (line.length() <= codeEnd) break malformed; try { responseCode = Integer.parseInt(line.substring(httpEnd + 1, codeEnd)); } catch (NumberFormatException nfe) { break malformed; } responseMsg = line.substring(codeEnd + 1); return; } disconnect(); throw new InterruptedIOException("malformed response message"); } protected void readHeaders() throws IOException { String line, key = null, value = null; int index; for (;;) { line = readLine(streamInput); // DEBUG: System.out.println ("Response: " + line); if (line == null || line.equals("")) break; /* * There can be multiline values. The line starts with a space * in that case. */ if (line.charAt(0) == ' ' || line.charAt(0) == '\t') { index = 0; // Replace multiple spaces with a single space while (line.charAt(index) == ' ' || line.charAt(index) == '\t') { ++index; } value += " " + line.substring(index); } else { if (key != null && value != null) { headerFields.put(toLowerCase(key), value); key = null; value = null; } index = line.indexOf(':'); if (index < 0) throw new IOException("malformed header field"); key = line.substring(0, index); if (key.length() == 0) throw new IOException("malformed header field"); if (line.length() <= index + 2) value = ""; else value = line.substring(index + 2); } } if (key != null && value != null) { headerFields.put(toLowerCase(key), value); } } /* * Uses the shared stringbuffer to read a line * terminated by <cr><lf> and return it as string. */ protected String readLine(InputStream in) { int c; stringbuffer.setLength(0); for (;;) { try { c = in.read(); if (c < 0) { return null; } if (c == '\r') { continue; } } catch (IOException ioe) { return null; } if (c == '\n') { break; } stringbuffer.append((char)c); } return stringbuffer.toString(); } protected void disconnect() throws IOException { if (streamConnection == null) return; if (streamConnection != null) { String connectionField = (String) headerFields.get("connection"); if (privateIn == null || privateIn.available() > 0 || connectionField != null && (connectionField.equalsIgnoreCase("close") || (responseProtocol != null && responseProtocol.equalsIgnoreCase("HTTP/1.0") && !connectionField.equalsIgnoreCase("keep-alive")))) { if (streamConnection instanceof StreamConnectionElement) { connectionPool.remove( (StreamConnectionElement) streamConnection); streamConnection = null; } else { disconnectSocket(); } } if (streamConnection != null) { if (streamConnection instanceof StreamConnectionElement) { // we got this connection from the pool connectionPool.returnForReuse( (StreamConnectionElement) streamConnection); streamConnection = null; } else { // save the connection for reuse if (!connectionPool.add(protocol, host, port, (HttpStreamConnection)streamConnection, streamOutput, streamInput)) { // pool full, disconnect disconnectSocket(); } } } } privateIn = null; privateOut = null; responseCode = -1; responseMsg = null; connected = false; responseProtocol = null; } protected void disconnectSocket() throws IOException { if (streamConnection != null) { streamConnection.close(); if (! (streamConnection instanceof StreamConnectionElement)) { streamInput.close(); streamOutput.close(); } streamInput = null; streamOutput = null; streamConnection = null; } } protected synchronized void parseURL() throws IOException { try { URL loc = new URL(url.startsWith("//") ? protocol+":"+url : url); host = loc.getHost(); if (-1 == (port = loc.getPort())) { port = loc.getProtocol().equals("http") ? 80 : 443; } if (null == (file = loc.getPath())) { file = ""; } if (null == (query = loc.getQuery())) { query = ""; } if (null == (ref = loc.getRef())) { ref = ""; } } catch (MalformedURLException ex) { throw new IllegalArgumentException("Malformed URL: "+url); } } // The proxy value, if any, is specified as a host:port // string. Use the convenience routines to parse it. protected synchronized void parseProxy(String proxyVal) { if (proxyVal != null) { try { URL proxyURL = new URL(proxyVal.startsWith("http://") ? proxyVal : "http://" + proxyVal); proxyHost = proxyURL.getHost(); // null is ok. if (-1 == (proxyPort = proxyURL.getPort())) { proxyPort = 80; } } catch (MalformedURLException ex) { throw new IllegalArgumentException("Malformed URL: "+proxyVal); } } return; } private String toLowerCase(String string) { // Uses the shared stringbuffer to create a lower case string. stringbuffer.setLength(0); for (int i = 0; i < string.length(); i++) { stringbuffer.append(Character.toLowerCase(string.charAt(i))); } return stringbuffer.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -