1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.client;
14
15 import java.io.IOException;
16 import java.io.InterruptedIOException;
17 import java.net.Socket;
18
19 import javax.net.SocketFactory;
20 import javax.net.ssl.SSLContext;
21
22 import org.eclipse.jetty.io.Connection;
23 import org.eclipse.jetty.io.EndPoint;
24 import org.eclipse.jetty.io.bio.SocketEndPoint;
25 import org.eclipse.jetty.util.component.AbstractLifeCycle;
26 import org.eclipse.jetty.util.log.Log;
27 import org.eclipse.jetty.util.log.Logger;
28
29 class SocketConnector extends AbstractLifeCycle implements HttpClient.Connector
30 {
31 private static final Logger LOG = Log.getLogger(SocketConnector.class);
32
33
34
35
36 private final HttpClient _httpClient;
37
38
39
40
41 SocketConnector(HttpClient httpClient)
42 {
43 _httpClient = httpClient;
44 }
45
46 public void startConnection(final HttpDestination destination) throws IOException
47 {
48 Socket socket= destination.isSecure()
49 ?_httpClient.getSslContextFactory().newSslSocket()
50 :SocketFactory.getDefault().createSocket();
51
52 socket.setSoTimeout(0);
53 socket.setTcpNoDelay(true);
54
55 Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress();
56 socket.connect(address.toSocketAddress(), _httpClient.getConnectTimeout());
57
58 EndPoint endpoint=new SocketEndPoint(socket);
59
60 final HttpConnection connection=new HttpConnection(_httpClient.getRequestBuffers(),_httpClient.getResponseBuffers(),endpoint);
61 connection.setDestination(destination);
62 destination.onNewConnection(connection);
63 _httpClient.getThreadPool().dispatch(new Runnable()
64 {
65 public void run()
66 {
67 try
68 {
69 Connection con = connection;
70 while(true)
71 {
72 final Connection next = con.handle();
73 if (next!=con)
74 {
75 con=next;
76 continue;
77 }
78 break;
79 }
80 }
81 catch (IOException e)
82 {
83 if (e instanceof InterruptedIOException)
84 LOG.ignore(e);
85 else
86 {
87 LOG.debug(e);
88 destination.onException(e);
89 }
90 }
91 finally
92 {
93 try
94 {
95 destination.returnConnection(connection,true);
96 }
97 catch (IOException e)
98 {
99 LOG.debug(e);
100 }
101 }
102 }
103 });
104
105 }
106 }