📄 protocol.java
字号:
/* * @(#)Protocol.java 1.72 01/08/29 * Copyright (c)s 1999,2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package com.sun.midp.io.j2me.http;/** * A class representing a http connection. An http connection consists of * stream connection as well as input and output streams for read/write data to * and from a web server. This version supports HTTP1.1 persistent connections * allowing connects to be shared from a connection pool. This pool and the * maximum number of connections can be configured for a particular platform. * Proxy connections are also allowed through this interface. * * <p> The actual connection to the web server does not take place until the * application needs an, (1) input stream, (2) flush data, (3)request some * header info or closes the connection (with outstanding data in the * output stream). Because of this issue the state transition must allow for * some flexability to move backwards for WRITE state conditions. * * <p> Persistent connections are provided through the use of a connection * pool that tracks the connect status. There are maximum threshold values * defined and these values can be overiden using property key/value pars. * The connection pool provides a synchronized interface for managing the * maximum configurable connections. Persistent connections will only be * supported for HTTP1.1 connections - otherwise the connections will be * closed and disgarded after its done (HTTP1.0 behavior). * * <p> This class extends the ConnectionBaseAdapter where Connector type * objects (like this) use various features. Output and Input streams are * created and managed in the adapter class. * * <p> The reading and writing of data through the input and output streams * are configured, buffered and managed depending on the ability of a * platform to read/write on those streams. * */import java.io.IOException;import java.io.InputStream;import java.io.InterruptedIOException;import java.io.OutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.ByteArrayOutputStream;import java.util.Enumeration;import java.util.Hashtable;import javax.microedition.io.StreamConnection;import javax.microedition.io.HttpConnection;import javax.microedition.io.Connector;import javax.microedition.io.Connection;import javax.microedition.io.ConnectionNotFoundException;import com.sun.midp.io.InternalConnector;import com.sun.midp.io.HttpUrl;import com.sun.cldc.io.DateParser;import com.sun.midp.io.j2me.http.StreamConnectionPool;import com.sun.midp.io.j2me.http.StreamConnectionElement;import com.sun.midp.Configuration;import com.sun.midp.io.ConnectionBaseAdapter;import com.sun.midp.io.Properties;/** * This class implements the necessary functionality * for an HTTP connection. * */public class Protocol extends ConnectionBaseAdapter implements HttpConnection { /** Saved copy of URL string to be processed. */ protected String saved_url; /** Protocol for this class or subclass. */ protected String protocol; /** Default port number for this protocol. */ protected int default_port; /** Parsed Url. */ protected HttpUrl url; /** url.host + ":" + url.port. */ protected String hostAndPort; /** Numeric code returned from HTTP response header. */ protected int responseCode; /** Message string from HTTP response header. */ protected String responseMsg; /** Collection of request headers as name/value pairs. */ protected Properties reqProperties; /** Collection of response headers as name/value pairs. */ protected Properties headerFields; /** HTTP method type for the current request. */ protected String method; /** * Type of read or write connection desired from the generic connection * framework. */ protected int mode; /** Reconnect int variable to limit the number of interations. */ protected int reconnects; /** Maximum number of reconnect tries for a session. */ private static final int MAX_NUMBER_OF_RECONNECTS = 2; /* * In/Out Streams used to buffer input and output. */ /** Input stream associated with the current HTTP connection. */ private InputStream in; /** Output stream associated with the current HTTP connection. */ private OutputStream out; /* * The streams from the underlying socket connection. */ /** Low level socket connection used for the HTTP requests. */ private StreamConnection streamConnection; /** Low level socket output stream. */ private DataOutputStream streamOutput; /** Low level socket input stream. */ private DataInputStream streamInput; /** * A shared temporary buffer. */ private StringBuffer stringbuffer; /** The "host:port" value to use for HTTP proxied requests. */ private String http_proxy; /** HTTP version string to use with all outgoing HTTP requests. */ protected static final String HTTP_VERSION = "HTTP/1.1"; /** Persistent connection poo.l */ private static StreamConnectionPool connectionPool; /** Stream connection container. */ private StreamConnectionElement connectionElement = null; /** HTTP version string set with all incoming HTTP responses. */ private String httpVer = null; /** Used when appl calls setRequestProperty("Connection", "close"). */ private boolean ConnectionCloseFlag; /** * Total number of bytes in the current chunk or content-length when * data is sent as one big chunk. */ private int chunksize; /** * Number of bytes read from the stream for non-chunked data or * the bytes read from the current chunk. */ private int totalbytesread; /** True if Transfer-Encoding: chunkedIn. */ private boolean chunkedIn; /** True if Transfer-Encoding: chunkedOut. */ private boolean chunkedOut; /** True if eof seen. */ private boolean eof; /** Internal stream buffer to minimize the number of TCP socket reads. */ private byte[] readbuf; /** Number of bytes left in internal input stream buffer. */ private int bytesleft; /** Number of bytes read from the internal input stream buffer. */ private int bytesread; /** Buffered data output for content length calculation. */ private ByteArrayOutputStream byteArrayOutput; /** Maximum number of persistent connections. */ private int maxNumberOfPersistentConnections = 1; /** Default size for input buffer. */ private final int HTTP_BUFFER_SIZE = 256; /** Default size for output buffer. */ private final int HTTP_OUTPUT_BUFFER_SIZE = 2048; /** Initial state for http state transition. */ private static final int INIT_STATE = 0; /** Write state for http state transition. */ private static final int WRITE_STATE = 1; /** Open state for http state transition. */ private static final int OPEN_STATE = 2; /** Chunk state for http state transition. */ private static final int CHUNK_STATE = 3; /** Read state for http state transition. */ private static final int READ_STATE = 4; /** Close state for http state transition. */ private static final int CLOSE_STATE = 5; /** Close state for http state transition. */ private static final int FLUSH_STATE = 6; /** Done state for http state transition. */ private static final int CONNECT_STATE = 7; /** The http current state flag. */ private int state = INIT_STATE; /** Collection of "Proxy-" headers as name/value pairs. */ private Properties proxyHeaders = new Properties(); /** Last handshake error. */ private byte handshakeError; /** * Create a new instance of this class and intialize variables. * Initially an http connection is unconnected to the network. */ public Protocol() { reqProperties = new Properties(); headerFields = new Properties(); stringbuffer = new StringBuffer(32); method = GET; responseCode = -1; protocol = "http"; default_port = 80; maxNumberOfPersistentConnections = 1; /* * Initialize state */ state = INIT_STATE; /* * bug#4455443 - allows for configuration options to shut off * the persistent connection feature for http */ String flag = Configuration.getProperty( "com.sun.midp.io.http.force_non_persistent"); if ((flag != null) && (flag.equals("true"))) { ConnectionCloseFlag = true; } /* * Get the maximum number of persistent connections * from the configuration file. */ String num = Configuration.getProperty( "com.sun.midp.io.http.max_persistent_connections"); if (num != null) { try { maxNumberOfPersistentConnections = Integer.parseInt(num); } catch (NumberFormatException nfe) { } if (maxNumberOfPersistentConnections <= 0) { maxNumberOfPersistentConnections = 1; } } readbuf = new byte[HTTP_BUFFER_SIZE]; } /** * Provides the connect() method that sets up the connection, but * does not actually connect to the server until there's something * to do. * * @param name The URL for the connection, without the * without the protcol part. * @param mode The access mode * @param timeouts A flag to indicate that the called wants * timeout exceptions * * @exception IllegalArgumentException If a parameter is invalid. * @exception ConnectionNotFoundException If the connection cannot be * found. * @exception IOException If some other kind of I/O error occurs. */ protected void connect(String name, int mode, boolean timeouts) throws IOException, IllegalArgumentException, ConnectionNotFoundException { if (mode != Connector.READ && mode != Connector.WRITE && mode != Connector.READ_WRITE) { throw new IllegalArgumentException("illegal mode: " + mode); } this.saved_url = name; this.mode = mode; url = new HttpUrl(protocol, name); if (url.port == -1) { url.port = default_port; } if (url.host == null) { // avoid NullPointerExceptions url.host = ""; } hostAndPort = url.host + ":" + url.port; } /** * Open the input stream if it has not already been opened. * * @exception IOException is thrown if it has already been opened. * @return input stream for the current connection */ public InputStream openInputStream() throws IOException { if (in != null) { throw new IOException("input stream already open"); } /* Check that the connection was opened for reading */ if (mode != Connector.READ && mode != Connector.READ_WRITE) { throw new IOException("write-only connection"); } /* * Send a request to the web server if there wasn't one * sent already - now moving to READ state */ sendRequest(READ_STATE); /* * Initialize and set the current input stream variables */ bytesleft = 0; chunksize = 0; bytesread = 0; totalbytesread = 0; chunkedIn = false; eof = false; /* * Determine if this is a chunked datatransfer and setup */ String te = (String)getHeaderField("transfer-encoding"); if (te != null && te.equals("chunked")) { chunkedIn = true; chunksize = readChunkSize(); } else { // treat non chunked data of known length as one big chunk chunksize = (int)getLength(); } if (chunksize == 0) { eof = true; } /* * Call into parent to create input stream passed back to the user */ in = super.openInputStream(); return in; } /** * Open the output stream if it has not already been opened. * * @exception IOException is thrown if it has already been opened. * @return output stream for the current connection */ public OutputStream openOutputStream() throws IOException { if (mode != Connector.WRITE && mode != Connector.READ_WRITE) { throw new IOException("read-only connection"); } /* * Create a byte array output stream for output buffering * once the user calls flush() this gets written to stream */ byteArrayOutput = new ByteArrayOutputStream(HTTP_OUTPUT_BUFFER_SIZE); /* * call into parent to create output stream passed back to the user */ out = super.openOutputStream(); return out; } /** * Reads up to <code>len</code> bytes of data from the input stream into * an array of bytes. * This method reads NonChunked http connection input streams. * This method can only be called after the InputStream setup is complete. * * @param b the buffer into which the data is read. * @param off the start offset in array <code>b</code> * at which the data is written. * @param len the maximum number of bytes to read. * @return the total number of bytes read into the buffer, or * <code>-1</code> if there is no more data because the end of * the stream has been reached. * @exception IOException if an I/O error occurs. */ protected int readBytes(byte b[], int off, int len) throws IOException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -