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 17package android.net.http; 18 19import android.content.Context; 20 21import java.net.Socket; 22import java.io.IOException; 23 24import org.apache.http.HttpClientConnection; 25import org.apache.http.HttpHost; 26import org.apache.http.impl.DefaultHttpClientConnection; 27import org.apache.http.params.BasicHttpParams; 28import org.apache.http.params.HttpConnectionParams; 29 30/** 31 * A requestConnection connecting to a normal (non secure) http server 32 * 33 * {@hide} 34 */ 35class HttpConnection extends Connection { 36 37 HttpConnection(Context context, HttpHost host, 38 RequestFeeder requestFeeder) { 39 super(context, host, requestFeeder); 40 } 41 42 /** 43 * Opens the connection to a http server 44 * 45 * @return the opened low level connection 46 * @throws IOException if the connection fails for any reason. 47 */ 48 @Override 49 AndroidHttpClientConnection openConnection(Request req) throws IOException { 50 51 // Update the certificate info (connection not secure - set to null) 52 EventHandler eventHandler = req.getEventHandler(); 53 mCertificate = null; 54 eventHandler.certificate(mCertificate); 55 56 AndroidHttpClientConnection conn = new AndroidHttpClientConnection(); 57 BasicHttpParams params = new BasicHttpParams(); 58 Socket sock = new Socket(mHost.getHostName(), mHost.getPort()); 59 params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8192); 60 conn.bind(sock, params); 61 return conn; 62 } 63 64 /** 65 * Closes the low level connection. 66 * 67 * If an exception is thrown then it is assumed that the 68 * connection will have been closed (to the extent possible) 69 * anyway and the caller does not need to take any further action. 70 * 71 */ 72 void closeConnection() { 73 try { 74 if (mHttpClientConnection != null && mHttpClientConnection.isOpen()) { 75 mHttpClientConnection.close(); 76 } 77 } catch (IOException e) { 78 if (HttpLog.LOGV) HttpLog.v( 79 "closeConnection(): failed closing connection " + 80 mHost); 81 e.printStackTrace(); 82 } 83 } 84 85 /** 86 * Restart a secure connection suspended waiting for user interaction. 87 */ 88 void restartConnection(boolean abort) { 89 // not required for plain http connections 90 } 91 92 String getScheme() { 93 return "http"; 94 } 95} 96