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