Methods Summary |
---|
private void | assertNotOpen()
if (this.open) {
throw new IllegalStateException("Connection is already open");
}
|
private void | assertOpen()
if (!this.open) {
throw new IllegalStateException("Connection is not open");
}
|
public void | bind(java.net.Socket socket, org.apache.http.params.HttpParams params)Bind socket and set HttpParams to AndroidHttpClientConnection
if (socket == null) {
throw new IllegalArgumentException("Socket may not be null");
}
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
assertNotOpen();
socket.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
socket.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
int linger = HttpConnectionParams.getLinger(params);
if (linger >= 0) {
socket.setSoLinger(linger > 0, linger);
}
this.socket = socket;
int buffersize = HttpConnectionParams.getSocketBufferSize(params);
this.inbuffer = new SocketInputBuffer(socket, buffersize, params);
this.outbuffer = new SocketOutputBuffer(socket, buffersize, params);
maxHeaderCount = params.getIntParameter(
CoreConnectionPNames.MAX_HEADER_COUNT, -1);
maxLineLength = params.getIntParameter(
CoreConnectionPNames.MAX_LINE_LENGTH, -1);
this.requestWriter = new HttpRequestWriter(outbuffer, null, params);
this.metrics = new HttpConnectionMetricsImpl(
inbuffer.getMetrics(),
outbuffer.getMetrics());
this.open = true;
|
public void | close()
if (!this.open) {
return;
}
this.open = false;
doFlush();
try {
try {
this.socket.shutdownOutput();
} catch (IOException ignore) {
}
try {
this.socket.shutdownInput();
} catch (IOException ignore) {
}
} catch (UnsupportedOperationException ignore) {
// if one isn't supported, the other one isn't either
}
this.socket.close();
|
private long | determineLength(Headers headers)
long transferEncoding = headers.getTransferEncoding();
// We use Transfer-Encoding if present and ignore Content-Length.
// RFC2616, 4.4 item number 3
if (transferEncoding < Headers.NO_TRANSFER_ENCODING) {
return transferEncoding;
} else {
long contentlen = headers.getContentLength();
if (contentlen > Headers.NO_CONTENT_LENGTH) {
return contentlen;
} else {
return ContentLengthStrategy.IDENTITY;
}
}
|
protected void | doFlush()
this.outbuffer.flush();
|
public void | flush()
assertOpen();
doFlush();
|
public java.net.InetAddress | getLocalAddress()
if (this.socket != null) {
return this.socket.getLocalAddress();
} else {
return null;
}
|
public int | getLocalPort()
if (this.socket != null) {
return this.socket.getLocalPort();
} else {
return -1;
}
|
public org.apache.http.HttpConnectionMetrics | getMetrics()Returns a collection of connection metrcis
return this.metrics;
|
public java.net.InetAddress | getRemoteAddress()
if (this.socket != null) {
return this.socket.getInetAddress();
} else {
return null;
}
|
public int | getRemotePort()
if (this.socket != null) {
return this.socket.getPort();
} else {
return -1;
}
|
public int | getSocketTimeout()
if (this.socket != null) {
try {
return this.socket.getSoTimeout();
} catch (SocketException ignore) {
return -1;
}
} else {
return -1;
}
|
public boolean | isOpen()
// to make this method useful, we want to check if the socket is connected
return (this.open && this.socket != null && this.socket.isConnected());
|
public boolean | isStale()Checks whether this connection has gone down.
Network connections may get closed during some time of inactivity
for several reasons. The next time a read is attempted on such a
connection it will throw an IOException.
This method tries to alleviate this inconvenience by trying to
find out if a connection is still usable. Implementations may do
that by attempting a read with a very small timeout. Thus this
method may block for a small amount of time before returning a result.
It is therefore an expensive operation.
assertOpen();
try {
this.inbuffer.isDataAvailable(1);
return false;
} catch (IOException ex) {
return true;
}
|
public org.apache.http.StatusLine | parseResponseHeader(Headers headers)Parses the response headers and adds them to the
given {@code headers} object, and returns the response StatusLine
assertOpen();
CharArrayBuffer current = new CharArrayBuffer(64);
if (inbuffer.readLine(current) == -1) {
throw new NoHttpResponseException("The target server failed to respond");
}
// Create the status line from the status string
StatusLine statusline = BasicLineParser.DEFAULT.parseStatusLine(
current, new ParserCursor(0, current.length()));
if (HttpLog.LOGV) HttpLog.v("read: " + statusline);
int statusCode = statusline.getStatusCode();
// Parse header body
CharArrayBuffer previous = null;
int headerNumber = 0;
while(true) {
if (current == null) {
current = new CharArrayBuffer(64);
} else {
// This must be he buffer used to parse the status
current.clear();
}
int l = inbuffer.readLine(current);
if (l == -1 || current.length() < 1) {
break;
}
// Parse the header name and value
// Check for folded headers first
// Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
// discussion on folded headers
char first = current.charAt(0);
if ((first == ' " || first == '\t") && previous != null) {
// we have continuation folded header
// so append value
int start = 0;
int length = current.length();
while (start < length) {
char ch = current.charAt(start);
if (ch != ' " && ch != '\t") {
break;
}
start++;
}
if (maxLineLength > 0 &&
previous.length() + 1 + current.length() - start >
maxLineLength) {
throw new IOException("Maximum line length limit exceeded");
}
previous.append(' ");
previous.append(current, start, current.length() - start);
} else {
if (previous != null) {
headers.parseHeader(previous);
}
headerNumber++;
previous = current;
current = null;
}
if (maxHeaderCount > 0 && headerNumber >= maxHeaderCount) {
throw new IOException("Maximum header count exceeded");
}
}
if (previous != null) {
headers.parseHeader(previous);
}
if (statusCode >= 200) {
this.metrics.incrementResponseCount();
}
return statusline;
|
public org.apache.http.HttpEntity | receiveResponseEntity(Headers headers)Return the next response entity.
assertOpen();
BasicHttpEntity entity = new BasicHttpEntity();
long len = determineLength(headers);
if (len == ContentLengthStrategy.CHUNKED) {
entity.setChunked(true);
entity.setContentLength(-1);
entity.setContent(new ChunkedInputStream(inbuffer));
} else if (len == ContentLengthStrategy.IDENTITY) {
entity.setChunked(false);
entity.setContentLength(-1);
entity.setContent(new IdentityInputStream(inbuffer));
} else {
entity.setChunked(false);
entity.setContentLength(len);
entity.setContent(new ContentLengthInputStream(inbuffer, len));
}
String contentTypeHeader = headers.getContentType();
if (contentTypeHeader != null) {
entity.setContentType(contentTypeHeader);
}
String contentEncodingHeader = headers.getContentEncoding();
if (contentEncodingHeader != null) {
entity.setContentEncoding(contentEncodingHeader);
}
return entity;
|
public void | sendRequestEntity(org.apache.http.HttpEntityEnclosingRequest request)Sends the request entity over the connection.
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
assertOpen();
if (request.getEntity() == null) {
return;
}
this.entityserializer.serialize(
this.outbuffer,
request,
request.getEntity());
|
public void | sendRequestHeader(org.apache.http.HttpRequest request)Sends the request line and all headers over the connection.
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
assertOpen();
this.requestWriter.write(request);
this.metrics.incrementRequestCount();
|
public void | setSocketTimeout(int timeout)
assertOpen();
if (this.socket != null) {
try {
this.socket.setSoTimeout(timeout);
} catch (SocketException ignore) {
// It is not quite clear from the original documentation if there are any
// other legitimate cases for a socket exception to be thrown when setting
// SO_TIMEOUT besides the socket being already closed
}
}
|
public void | shutdown()
this.open = false;
Socket tmpsocket = this.socket;
if (tmpsocket != null) {
tmpsocket.close();
}
|
public java.lang.String | toString()
StringBuilder buffer = new StringBuilder();
buffer.append(getClass().getSimpleName()).append("[");
if (isOpen()) {
buffer.append(getRemotePort());
} else {
buffer.append("closed");
}
buffer.append("]");
return buffer.toString();
|