1a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath/* 2a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Copyright (C) 2007 The Android Open Source Project 3a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 4a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Licensed under the Apache License, Version 2.0 (the "License"); 5a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * you may not use this file except in compliance with the License. 6a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * You may obtain a copy of the License at 7a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 8a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * http://www.apache.org/licenses/LICENSE-2.0 9a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 10a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Unless required by applicable law or agreed to in writing, software 11a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * distributed under the License is distributed on an "AS IS" BASIS, 12a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * See the License for the specific language governing permissions and 14a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * limitations under the License. 15a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 16a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 17a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathpackage android.net.http; 18a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 19a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathimport android.content.Context; 20a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 21a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathimport java.net.Socket; 22a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathimport java.io.IOException; 23a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 24a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathimport org.apache.http.HttpHost; 25a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathimport org.apache.http.params.BasicHttpParams; 26a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathimport org.apache.http.params.HttpConnectionParams; 27a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 28a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath/** 29a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * A requestConnection connecting to a normal (non secure) http server 30a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 31a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathclass HttpConnection extends Connection { 32a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 33a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath HttpConnection(Context context, HttpHost host, 34a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath RequestFeeder requestFeeder) { 35a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath super(context, host, requestFeeder); 36a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 37a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 38a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath /** 39a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Opens the connection to a http server 40a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 41a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * @return the opened low level connection 42a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * @throws IOException if the connection fails for any reason. 43a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 44a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath @Override 45a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath AndroidHttpClientConnection openConnection(Request req) throws IOException { 46a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 47a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath // Update the certificate info (connection not secure - set to null) 48a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath EventHandler eventHandler = req.getEventHandler(); 49a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath mCertificate = null; 50a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath eventHandler.certificate(mCertificate); 51a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 52a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath AndroidHttpClientConnection conn = new AndroidHttpClientConnection(); 53a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath BasicHttpParams params = new BasicHttpParams(); 54a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath Socket sock = new Socket(mHost.getHostName(), mHost.getPort()); 55a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8192); 56a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath conn.bind(sock, params); 57a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath return conn; 58a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 59a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 60a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath /** 61a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Closes the low level connection. 62a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 63a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * If an exception is thrown then it is assumed that the 64a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * connection will have been closed (to the extent possible) 65a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * anyway and the caller does not need to take any further action. 66a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 67a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 68a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath void closeConnection() { 69a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath try { 70a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath if (mHttpClientConnection != null && mHttpClientConnection.isOpen()) { 71a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath mHttpClientConnection.close(); 72a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 73a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } catch (IOException e) { 74a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath if (HttpLog.LOGV) HttpLog.v( 75a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath "closeConnection(): failed closing connection " + 76a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath mHost); 77a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath e.printStackTrace(); 78a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 79a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 80a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 81a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath /** 82a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Restart a secure connection suspended waiting for user interaction. 83a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 84a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath void restartConnection(boolean abort) { 85a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath // not required for plain http connections 86a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 87a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 88a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath String getScheme() { 89a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath return "http"; 90a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 91a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath} 92