Jake Slack | 03928ae | 2014-05-13 18:41:56 -0700 | [diff] [blame] | 1 | // |
| 2 | // ======================================================================== |
| 3 | // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
| 4 | // ------------------------------------------------------------------------ |
| 5 | // All rights reserved. This program and the accompanying materials |
| 6 | // are made available under the terms of the Eclipse Public License v1.0 |
| 7 | // and Apache License v2.0 which accompanies this distribution. |
| 8 | // |
| 9 | // The Eclipse Public License is available at |
| 10 | // http://www.eclipse.org/legal/epl-v10.html |
| 11 | // |
| 12 | // The Apache License v2.0 is available at |
| 13 | // http://www.opensource.org/licenses/apache2.0.php |
| 14 | // |
| 15 | // You may elect to redistribute this code under either of these licenses. |
| 16 | // ======================================================================== |
| 17 | // |
| 18 | |
| 19 | package org.eclipse.jetty.client; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.InterruptedIOException; |
| 23 | import java.net.Socket; |
| 24 | import javax.net.SocketFactory; |
| 25 | |
| 26 | import org.eclipse.jetty.io.Connection; |
| 27 | import org.eclipse.jetty.io.EndPoint; |
| 28 | import org.eclipse.jetty.io.bio.SocketEndPoint; |
| 29 | import org.eclipse.jetty.util.component.AbstractLifeCycle; |
| 30 | import org.eclipse.jetty.util.log.Log; |
| 31 | import org.eclipse.jetty.util.log.Logger; |
| 32 | |
| 33 | class SocketConnector extends AbstractLifeCycle implements HttpClient.Connector |
| 34 | { |
| 35 | private static final Logger LOG = Log.getLogger(SocketConnector.class); |
| 36 | |
| 37 | /** |
| 38 | * |
| 39 | */ |
| 40 | private final HttpClient _httpClient; |
| 41 | |
| 42 | /** |
| 43 | * @param httpClient |
| 44 | */ |
| 45 | SocketConnector(HttpClient httpClient) |
| 46 | { |
| 47 | _httpClient = httpClient; |
| 48 | } |
| 49 | |
| 50 | public void startConnection(final HttpDestination destination) throws IOException |
| 51 | { |
| 52 | Socket socket= destination.isSecure() |
| 53 | ? destination.getSslContextFactory().newSslSocket() |
| 54 | : SocketFactory.getDefault().createSocket(); |
| 55 | |
| 56 | socket.setSoTimeout(0); |
| 57 | socket.setTcpNoDelay(true); |
| 58 | |
| 59 | Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress(); |
| 60 | socket.connect(address.toSocketAddress(), _httpClient.getConnectTimeout()); |
| 61 | |
| 62 | final EndPoint endpoint=new SocketEndPoint(socket); |
| 63 | |
| 64 | final AbstractHttpConnection connection=new BlockingHttpConnection(_httpClient.getRequestBuffers(),_httpClient.getResponseBuffers(),endpoint); |
| 65 | connection.setDestination(destination); |
| 66 | destination.onNewConnection(connection); |
| 67 | _httpClient.getThreadPool().dispatch(new Runnable() |
| 68 | { |
| 69 | public void run() |
| 70 | { |
| 71 | try |
| 72 | { |
| 73 | Connection con = connection; |
| 74 | while(true) |
| 75 | { |
| 76 | final Connection next = con.handle(); |
| 77 | if (next!=con) |
| 78 | { |
| 79 | con=next; |
| 80 | continue; |
| 81 | } |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | catch (IOException e) |
| 86 | { |
| 87 | if (e instanceof InterruptedIOException) |
| 88 | LOG.ignore(e); |
| 89 | else |
| 90 | { |
| 91 | LOG.debug(e); |
| 92 | destination.onException(e); |
| 93 | } |
| 94 | } |
| 95 | finally |
| 96 | { |
| 97 | try |
| 98 | { |
| 99 | destination.returnConnection(connection,true); |
| 100 | } |
| 101 | catch (IOException e) |
| 102 | { |
| 103 | LOG.debug(e); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | } |
| 110 | } |