]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/NegotiatingServerConnection.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / server / NegotiatingServerConnection.java
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.server;
20
21 import java.io.IOException;
22 import java.util.List;
23 import javax.net.ssl.SSLEngine;
24 import javax.net.ssl.SSLEngineResult;
25
26 import org.eclipse.jetty.io.AbstractConnection;
27 import org.eclipse.jetty.io.Connection;
28 import org.eclipse.jetty.io.EndPoint;
29 import org.eclipse.jetty.server.ConnectionFactory;
30 import org.eclipse.jetty.server.Connector;
31 import org.eclipse.jetty.util.BufferUtil;
32 import org.eclipse.jetty.util.log.Log;
33 import org.eclipse.jetty.util.log.Logger;
34
35 public abstract class NegotiatingServerConnection extends AbstractConnection
36 {
37     private static final Logger LOG = Log.getLogger(NegotiatingServerConnection.class);
38
39     private final Connector connector;
40     private final SSLEngine engine;
41     private final List<String> protocols;
42     private final String defaultProtocol;
43     private String protocol; // No need to be volatile: it is modified and read by the same thread
44
45     protected NegotiatingServerConnection(Connector connector, EndPoint endPoint, SSLEngine engine, List<String> protocols, String defaultProtocol)
46     {
47         super(endPoint, connector.getExecutor());
48         this.connector = connector;
49         this.protocols = protocols;
50         this.defaultProtocol = defaultProtocol;
51         this.engine = engine;
52     }
53
54     protected List<String> getProtocols()
55     {
56         return protocols;
57     }
58
59     protected String getDefaultProtocol()
60     {
61         return defaultProtocol;
62     }
63
64     protected SSLEngine getSSLEngine()
65     {
66         return engine;
67     }
68
69     protected String getProtocol()
70     {
71         return protocol;
72     }
73
74     protected void setProtocol(String protocol)
75     {
76         this.protocol = protocol;
77     }
78
79     @Override
80     public void onOpen()
81     {
82         super.onOpen();
83         fillInterested();
84     }
85
86     @Override
87     public void onFillable()
88     {
89         int filled = fill();
90
91         if (filled == 0)
92         {
93             if (protocol == null)
94             {
95                 if (engine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING)
96                 {
97                     // Here the SSL handshake is finished, but the protocol has not been negotiated.
98                     LOG.debug("{} could not negotiate protocol, SSLEngine: {}", this, engine);
99                     close();
100                 }
101                 else
102                 {
103                     // Here the SSL handshake is not finished yet but we filled 0 bytes,
104                     // so we need to read more.
105                     fillInterested();
106                 }
107             }
108             else
109             {
110                 ConnectionFactory connectionFactory = connector.getConnectionFactory(protocol);
111                 if (connectionFactory == null)
112                 {
113                     LOG.debug("{} application selected protocol '{}', but no correspondent {} has been configured",
114                             this, protocol, ConnectionFactory.class.getName());
115                     close();
116                 }
117                 else
118                 {
119                     EndPoint endPoint = getEndPoint();
120                     Connection oldConnection = endPoint.getConnection();
121                     Connection newConnection = connectionFactory.newConnection(connector, endPoint);
122                     LOG.debug("{} switching from {} to {}", this, oldConnection, newConnection);
123                     oldConnection.onClose();
124                     endPoint.setConnection(newConnection);
125                     getEndPoint().getConnection().onOpen();
126                 }
127             }
128         }
129         else if (filled < 0)
130         {
131             // Something went bad, we need to close.
132             LOG.debug("{} closing on client close", this);
133             close();
134         }
135         else
136         {
137             // Must never happen, since we fill using an empty buffer
138             throw new IllegalStateException();
139         }
140     }
141
142     private int fill()
143     {
144         try
145         {
146             return getEndPoint().fill(BufferUtil.EMPTY_BUFFER);
147         }
148         catch (IOException x)
149         {
150             LOG.debug(x);
151             close();
152             return -1;
153         }
154     }
155
156     @Override
157     public void close()
158     {
159         // Gentler close for SSL.
160         getEndPoint().shutdownOutput();
161         super.close();
162     }
163 }