1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/SocketHttpServerConnection.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,281 @@ 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 server-side HTTP connection that can be bound to a 1.48 + * network Socket in order to receive and transmit data. 1.49 + * <p> 1.50 + * The following parameters can be used to customize the behavior of this 1.51 + * class: 1.52 + * <ul> 1.53 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li> 1.54 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li> 1.55 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li> 1.56 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li> 1.57 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li> 1.58 + * </ul> 1.59 + * 1.60 + * @since 4.0 1.61 + */ 1.62 +public class SocketHttpServerConnection extends 1.63 + AbstractHttpServerConnection implements HttpInetConnection { 1.64 + 1.65 + private volatile boolean open; 1.66 + private volatile Socket socket = null; 1.67 + 1.68 + public SocketHttpServerConnection() { 1.69 + super(); 1.70 + } 1.71 + 1.72 + protected void assertNotOpen() { 1.73 + if (this.open) { 1.74 + throw new IllegalStateException("Connection is already open"); 1.75 + } 1.76 + } 1.77 + 1.78 + protected void assertOpen() { 1.79 + if (!this.open) { 1.80 + throw new IllegalStateException("Connection is not open"); 1.81 + } 1.82 + } 1.83 + 1.84 + /** 1.85 + * @deprecated Use {@link #createSessionInputBuffer(Socket, int, HttpParams)} 1.86 + */ 1.87 + protected SessionInputBuffer createHttpDataReceiver( 1.88 + final Socket socket, 1.89 + int buffersize, 1.90 + final HttpParams params) throws IOException { 1.91 + return createSessionInputBuffer(socket, buffersize, params); 1.92 + } 1.93 + 1.94 + /** 1.95 + * @deprecated Use {@link #createSessionOutputBuffer(Socket, int, HttpParams)} 1.96 + */ 1.97 + protected SessionOutputBuffer createHttpDataTransmitter( 1.98 + final Socket socket, 1.99 + int buffersize, 1.100 + final HttpParams params) throws IOException { 1.101 + return createSessionOutputBuffer(socket, buffersize, params); 1.102 + } 1.103 + 1.104 + /** 1.105 + * Creates an instance of {@link SocketInputBuffer} to be used for 1.106 + * receiving data from the given {@link Socket}. 1.107 + * <p> 1.108 + * This method can be overridden in a super class in order to provide 1.109 + * a custom implementation of {@link SessionInputBuffer} interface. 1.110 + * 1.111 + * @see SocketInputBuffer#SocketInputBuffer(Socket, int, HttpParams) 1.112 + * 1.113 + * @param socket the socket. 1.114 + * @param buffersize the buffer size. 1.115 + * @param params HTTP parameters. 1.116 + * @return session input buffer. 1.117 + * @throws IOException in case of an I/O error. 1.118 + */ 1.119 + protected SessionInputBuffer createSessionInputBuffer( 1.120 + final Socket socket, 1.121 + int buffersize, 1.122 + final HttpParams params) throws IOException { 1.123 + return new SocketInputBuffer(socket, buffersize, params); 1.124 + } 1.125 + 1.126 + /** 1.127 + * Creates an instance of {@link SessionOutputBuffer} to be used for 1.128 + * sending data to the given {@link Socket}. 1.129 + * <p> 1.130 + * This method can be overridden in a super class in order to provide 1.131 + * a custom implementation of {@link SocketOutputBuffer} interface. 1.132 + * 1.133 + * @see SocketOutputBuffer#SocketOutputBuffer(Socket, int, HttpParams) 1.134 + * 1.135 + * @param socket the socket. 1.136 + * @param buffersize the buffer size. 1.137 + * @param params HTTP parameters. 1.138 + * @return session output buffer. 1.139 + * @throws IOException in case of an I/O error. 1.140 + */ 1.141 + protected SessionOutputBuffer createSessionOutputBuffer( 1.142 + final Socket socket, 1.143 + int buffersize, 1.144 + final HttpParams params) throws IOException { 1.145 + return new SocketOutputBuffer(socket, buffersize, params); 1.146 + } 1.147 + 1.148 + /** 1.149 + * Binds this connection to the given {@link Socket}. This socket will be 1.150 + * used by the connection to send and receive data. 1.151 + * <p> 1.152 + * This method will invoke {@link #createSessionInputBuffer(Socket, int, HttpParams)} 1.153 + * and {@link #createSessionOutputBuffer(Socket, int, HttpParams)} methods 1.154 + * to create session input / output buffers bound to this socket and then 1.155 + * will invoke {@link #init(SessionInputBuffer, SessionOutputBuffer, HttpParams)} 1.156 + * method to pass references to those buffers to the underlying HTTP message 1.157 + * parser and formatter. 1.158 + * <p> 1.159 + * After this method's execution the connection status will be reported 1.160 + * as open and the {@link #isOpen()} will return <code>true</code>. 1.161 + * 1.162 + * @param socket the socket. 1.163 + * @param params HTTP parameters. 1.164 + * @throws IOException in case of an I/O error. 1.165 + */ 1.166 + protected void bind(final Socket socket, final HttpParams params) throws IOException { 1.167 + if (socket == null) { 1.168 + throw new IllegalArgumentException("Socket may not be null"); 1.169 + } 1.170 + if (params == null) { 1.171 + throw new IllegalArgumentException("HTTP parameters may not be null"); 1.172 + } 1.173 + this.socket = socket; 1.174 + 1.175 + int buffersize = HttpConnectionParams.getSocketBufferSize(params); 1.176 + 1.177 + init( 1.178 + createHttpDataReceiver(socket, buffersize, params), 1.179 + createHttpDataTransmitter(socket, buffersize, params), 1.180 + params); 1.181 + 1.182 + this.open = true; 1.183 + } 1.184 + 1.185 + protected Socket getSocket() { 1.186 + return this.socket; 1.187 + } 1.188 + 1.189 + public boolean isOpen() { 1.190 + return this.open; 1.191 + } 1.192 + 1.193 + public InetAddress getLocalAddress() { 1.194 + if (this.socket != null) { 1.195 + return this.socket.getLocalAddress(); 1.196 + } else { 1.197 + return null; 1.198 + } 1.199 + } 1.200 + 1.201 + public int getLocalPort() { 1.202 + if (this.socket != null) { 1.203 + return this.socket.getLocalPort(); 1.204 + } else { 1.205 + return -1; 1.206 + } 1.207 + } 1.208 + 1.209 + public InetAddress getRemoteAddress() { 1.210 + if (this.socket != null) { 1.211 + return this.socket.getInetAddress(); 1.212 + } else { 1.213 + return null; 1.214 + } 1.215 + } 1.216 + 1.217 + public int getRemotePort() { 1.218 + if (this.socket != null) { 1.219 + return this.socket.getPort(); 1.220 + } else { 1.221 + return -1; 1.222 + } 1.223 + } 1.224 + 1.225 + public void setSocketTimeout(int timeout) { 1.226 + assertOpen(); 1.227 + if (this.socket != null) { 1.228 + try { 1.229 + this.socket.setSoTimeout(timeout); 1.230 + } catch (SocketException ignore) { 1.231 + // It is not quite clear from the Sun's documentation if there are any 1.232 + // other legitimate cases for a socket exception to be thrown when setting 1.233 + // SO_TIMEOUT besides the socket being already closed 1.234 + } 1.235 + } 1.236 + } 1.237 + 1.238 + public int getSocketTimeout() { 1.239 + if (this.socket != null) { 1.240 + try { 1.241 + return this.socket.getSoTimeout(); 1.242 + } catch (SocketException ignore) { 1.243 + return -1; 1.244 + } 1.245 + } else { 1.246 + return -1; 1.247 + } 1.248 + } 1.249 + 1.250 + public void shutdown() throws IOException { 1.251 + this.open = false; 1.252 + Socket tmpsocket = this.socket; 1.253 + if (tmpsocket != null) { 1.254 + tmpsocket.close(); 1.255 + } 1.256 + } 1.257 + 1.258 + public void close() throws IOException { 1.259 + if (!this.open) { 1.260 + return; 1.261 + } 1.262 + this.open = false; 1.263 + this.open = false; 1.264 + Socket sock = this.socket; 1.265 + try { 1.266 + doFlush(); 1.267 + try { 1.268 + try { 1.269 + sock.shutdownOutput(); 1.270 + } catch (IOException ignore) { 1.271 + } 1.272 + try { 1.273 + sock.shutdownInput(); 1.274 + } catch (IOException ignore) { 1.275 + } 1.276 + } catch (UnsupportedOperationException ignore) { 1.277 + // if one isn't supported, the other one isn't either 1.278 + } 1.279 + } finally { 1.280 + sock.close(); 1.281 + } 1.282 + } 1.283 + 1.284 +}