1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/SocketHttpClientConnection.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,263 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl; 1.32 + 1.33 +import java.io.IOException; 1.34 +import java.net.InetAddress; 1.35 +import java.net.Socket; 1.36 +import java.net.SocketException; 1.37 + 1.38 +import ch.boye.httpclientandroidlib.HttpInetConnection; 1.39 +import ch.boye.httpclientandroidlib.impl.io.SocketInputBuffer; 1.40 +import ch.boye.httpclientandroidlib.impl.io.SocketOutputBuffer; 1.41 +import ch.boye.httpclientandroidlib.io.SessionInputBuffer; 1.42 +import ch.boye.httpclientandroidlib.io.SessionOutputBuffer; 1.43 +import ch.boye.httpclientandroidlib.params.HttpConnectionParams; 1.44 +import ch.boye.httpclientandroidlib.params.HttpParams; 1.45 + 1.46 +/** 1.47 + * Implementation of a client-side HTTP connection that can be bound to an 1.48 + * arbitrary {@link Socket} for receiving data from and transmitting data to 1.49 + * a remote server. 1.50 + * <p> 1.51 + * The following parameters can be used to customize the behavior of this 1.52 + * class: 1.53 + * <ul> 1.54 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li> 1.55 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li> 1.56 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li> 1.57 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li> 1.58 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li> 1.59 + * </ul> 1.60 + * 1.61 + * @since 4.0 1.62 + */ 1.63 +public class SocketHttpClientConnection 1.64 + extends AbstractHttpClientConnection implements HttpInetConnection { 1.65 + 1.66 + private volatile boolean open; 1.67 + private volatile Socket socket = null; 1.68 + 1.69 + public SocketHttpClientConnection() { 1.70 + super(); 1.71 + } 1.72 + 1.73 + protected void assertNotOpen() { 1.74 + if (this.open) { 1.75 + throw new IllegalStateException("Connection is already open"); 1.76 + } 1.77 + } 1.78 + 1.79 + protected void assertOpen() { 1.80 + if (!this.open) { 1.81 + throw new IllegalStateException("Connection is not open"); 1.82 + } 1.83 + } 1.84 + 1.85 + /** 1.86 + * Creates an instance of {@link SocketInputBuffer} to be used for 1.87 + * receiving data from the given {@link Socket}. 1.88 + * <p> 1.89 + * This method can be overridden in a super class in order to provide 1.90 + * a custom implementation of {@link SessionInputBuffer} interface. 1.91 + * 1.92 + * @see SocketInputBuffer#SocketInputBuffer(Socket, int, HttpParams) 1.93 + * 1.94 + * @param socket the socket. 1.95 + * @param buffersize the buffer size. 1.96 + * @param params HTTP parameters. 1.97 + * @return session input buffer. 1.98 + * @throws IOException in case of an I/O error. 1.99 + */ 1.100 + protected SessionInputBuffer createSessionInputBuffer( 1.101 + final Socket socket, 1.102 + int buffersize, 1.103 + final HttpParams params) throws IOException { 1.104 + return new SocketInputBuffer(socket, buffersize, params); 1.105 + } 1.106 + 1.107 + /** 1.108 + * Creates an instance of {@link SessionOutputBuffer} to be used for 1.109 + * sending data to the given {@link Socket}. 1.110 + * <p> 1.111 + * This method can be overridden in a super class in order to provide 1.112 + * a custom implementation of {@link SocketOutputBuffer} interface. 1.113 + * 1.114 + * @see SocketOutputBuffer#SocketOutputBuffer(Socket, int, HttpParams) 1.115 + * 1.116 + * @param socket the socket. 1.117 + * @param buffersize the buffer size. 1.118 + * @param params HTTP parameters. 1.119 + * @return session output buffer. 1.120 + * @throws IOException in case of an I/O error. 1.121 + */ 1.122 + protected SessionOutputBuffer createSessionOutputBuffer( 1.123 + final Socket socket, 1.124 + int buffersize, 1.125 + final HttpParams params) throws IOException { 1.126 + return new SocketOutputBuffer(socket, buffersize, params); 1.127 + } 1.128 + 1.129 + /** 1.130 + * Binds this connection to the given {@link Socket}. This socket will be 1.131 + * used by the connection to send and receive data. 1.132 + * <p> 1.133 + * This method will invoke {@link #createSessionInputBuffer(Socket, int, HttpParams)} 1.134 + * and {@link #createSessionOutputBuffer(Socket, int, HttpParams)} methods 1.135 + * to create session input / output buffers bound to this socket and then 1.136 + * will invoke {@link #init(SessionInputBuffer, SessionOutputBuffer, HttpParams)} 1.137 + * method to pass references to those buffers to the underlying HTTP message 1.138 + * parser and formatter. 1.139 + * <p> 1.140 + * After this method's execution the connection status will be reported 1.141 + * as open and the {@link #isOpen()} will return <code>true</code>. 1.142 + * 1.143 + * @param socket the socket. 1.144 + * @param params HTTP parameters. 1.145 + * @throws IOException in case of an I/O error. 1.146 + */ 1.147 + protected void bind( 1.148 + final Socket socket, 1.149 + final HttpParams params) throws IOException { 1.150 + if (socket == null) { 1.151 + throw new IllegalArgumentException("Socket may not be null"); 1.152 + } 1.153 + if (params == null) { 1.154 + throw new IllegalArgumentException("HTTP parameters may not be null"); 1.155 + } 1.156 + this.socket = socket; 1.157 + 1.158 + int buffersize = HttpConnectionParams.getSocketBufferSize(params); 1.159 + 1.160 + init( 1.161 + createSessionInputBuffer(socket, buffersize, params), 1.162 + createSessionOutputBuffer(socket, buffersize, params), 1.163 + params); 1.164 + 1.165 + this.open = true; 1.166 + } 1.167 + 1.168 + public boolean isOpen() { 1.169 + return this.open; 1.170 + } 1.171 + 1.172 + protected Socket getSocket() { 1.173 + return this.socket; 1.174 + } 1.175 + 1.176 + public InetAddress getLocalAddress() { 1.177 + if (this.socket != null) { 1.178 + return this.socket.getLocalAddress(); 1.179 + } else { 1.180 + return null; 1.181 + } 1.182 + } 1.183 + 1.184 + public int getLocalPort() { 1.185 + if (this.socket != null) { 1.186 + return this.socket.getLocalPort(); 1.187 + } else { 1.188 + return -1; 1.189 + } 1.190 + } 1.191 + 1.192 + public InetAddress getRemoteAddress() { 1.193 + if (this.socket != null) { 1.194 + return this.socket.getInetAddress(); 1.195 + } else { 1.196 + return null; 1.197 + } 1.198 + } 1.199 + 1.200 + public int getRemotePort() { 1.201 + if (this.socket != null) { 1.202 + return this.socket.getPort(); 1.203 + } else { 1.204 + return -1; 1.205 + } 1.206 + } 1.207 + 1.208 + public void setSocketTimeout(int timeout) { 1.209 + assertOpen(); 1.210 + if (this.socket != null) { 1.211 + try { 1.212 + this.socket.setSoTimeout(timeout); 1.213 + } catch (SocketException ignore) { 1.214 + // It is not quite clear from the Sun's documentation if there are any 1.215 + // other legitimate cases for a socket exception to be thrown when setting 1.216 + // SO_TIMEOUT besides the socket being already closed 1.217 + } 1.218 + } 1.219 + } 1.220 + 1.221 + public int getSocketTimeout() { 1.222 + if (this.socket != null) { 1.223 + try { 1.224 + return this.socket.getSoTimeout(); 1.225 + } catch (SocketException ignore) { 1.226 + return -1; 1.227 + } 1.228 + } else { 1.229 + return -1; 1.230 + } 1.231 + } 1.232 + 1.233 + public void shutdown() throws IOException { 1.234 + this.open = false; 1.235 + Socket tmpsocket = this.socket; 1.236 + if (tmpsocket != null) { 1.237 + tmpsocket.close(); 1.238 + } 1.239 + } 1.240 + 1.241 + public void close() throws IOException { 1.242 + if (!this.open) { 1.243 + return; 1.244 + } 1.245 + this.open = false; 1.246 + Socket sock = this.socket; 1.247 + try { 1.248 + doFlush(); 1.249 + try { 1.250 + try { 1.251 + sock.shutdownOutput(); 1.252 + } catch (IOException ignore) { 1.253 + } 1.254 + try { 1.255 + sock.shutdownInput(); 1.256 + } catch (IOException ignore) { 1.257 + } 1.258 + } catch (UnsupportedOperationException ignore) { 1.259 + // if one isn't supported, the other one isn't either 1.260 + } 1.261 + } finally { 1.262 + sock.close(); 1.263 + } 1.264 + } 1.265 + 1.266 +}