Skip to main content
summaryrefslogtreecommitdiffstats
blob: fbcb9c28dcf29aa98b68893a06f09d6b151bd55a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// ========================================================================
// Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.client;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.Socket;

import javax.net.SocketFactory;

import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.bio.SocketEndPoint;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

class SocketConnector extends AbstractLifeCycle implements HttpClient.Connector
{
    private static final Logger LOG = Log.getLogger(SocketConnector.class);

    /**
     *
     */
    private final HttpClient _httpClient;

    /**
     * @param httpClient
     */
    SocketConnector(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    public void startConnection(final HttpDestination destination) throws IOException
    {
        Socket socket= destination.isSecure()
            ?_httpClient.getSslContextFactory().newSslSocket()
            :SocketFactory.getDefault().createSocket();

        socket.setSoTimeout(0);
        socket.setTcpNoDelay(true);

        Address address = destination.isProxied() ? destination.getProxy() : destination.getAddress();
        socket.connect(address.toSocketAddress(), _httpClient.getConnectTimeout());

        final EndPoint endpoint=new SocketEndPoint(socket);

        final AbstractHttpConnection connection=new BlockingHttpConnection(_httpClient.getRequestBuffers(),_httpClient.getResponseBuffers(),endpoint);
        connection.setDestination(destination);
        destination.onNewConnection(connection);
        _httpClient.getThreadPool().dispatch(new Runnable()
        {
            public void run()
            {
                try
                {
                    Connection con = connection;
                    while(true)
                    {
                        final Connection next = con.handle();
                        if (next!=con)
                        {
                            con=next;
                            continue;
                        }
                        break;
                    }
                }
                catch (IOException e)
                {
                    if (e instanceof InterruptedIOException)
                        LOG.ignore(e);
                    else
                    {
                        LOG.debug(e);
                        destination.onException(e);
                    }
                }
                finally
                {
                    try
                    {
                        destination.returnConnection(connection,true);
                    }
                    catch (IOException e)
                    {
                        LOG.debug(e);
                    }
                }
            }
        });

    }
}

Back to the top