|
1 /* |
|
2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package sun.net.httpserver; |
|
27 |
|
28 import java.io.*; |
|
29 import javax.net.ssl.*; |
|
30 import java.nio.channels.*; |
|
31 import java.lang.System.Logger; |
|
32 import java.lang.System.Logger.Level; |
|
33 import com.sun.net.httpserver.*; |
|
34 import com.sun.net.httpserver.spi.*; |
|
35 |
|
36 /** |
|
37 * encapsulates all the connection specific state for a HTTP/S connection |
|
38 * one of these is hung from the selector attachment and is used to locate |
|
39 * everything from that. |
|
40 */ |
|
41 class HttpConnection { |
|
42 |
|
43 HttpContextImpl context; |
|
44 SSLEngine engine; |
|
45 SSLContext sslContext; |
|
46 SSLStreams sslStreams; |
|
47 |
|
48 /* high level streams returned to application */ |
|
49 InputStream i; |
|
50 |
|
51 /* low level stream that sits directly over channel */ |
|
52 InputStream raw; |
|
53 OutputStream rawout; |
|
54 |
|
55 SocketChannel chan; |
|
56 SelectionKey selectionKey; |
|
57 String protocol; |
|
58 long time; |
|
59 volatile long creationTime; // time this connection was created |
|
60 volatile long rspStartedTime; // time we started writing the response |
|
61 int remaining; |
|
62 boolean closed = false; |
|
63 Logger logger; |
|
64 |
|
65 public enum State {IDLE, REQUEST, RESPONSE}; |
|
66 volatile State state; |
|
67 |
|
68 public String toString() { |
|
69 String s = null; |
|
70 if (chan != null) { |
|
71 s = chan.toString(); |
|
72 } |
|
73 return s; |
|
74 } |
|
75 |
|
76 HttpConnection () { |
|
77 } |
|
78 |
|
79 void setChannel (SocketChannel c) { |
|
80 chan = c; |
|
81 } |
|
82 |
|
83 void setContext (HttpContextImpl ctx) { |
|
84 context = ctx; |
|
85 } |
|
86 |
|
87 State getState() { |
|
88 return state; |
|
89 } |
|
90 |
|
91 void setState (State s) { |
|
92 state = s; |
|
93 } |
|
94 |
|
95 void setParameters ( |
|
96 InputStream in, OutputStream rawout, SocketChannel chan, |
|
97 SSLEngine engine, SSLStreams sslStreams, SSLContext sslContext, String protocol, |
|
98 HttpContextImpl context, InputStream raw |
|
99 ) |
|
100 { |
|
101 this.context = context; |
|
102 this.i = in; |
|
103 this.rawout = rawout; |
|
104 this.raw = raw; |
|
105 this.protocol = protocol; |
|
106 this.engine = engine; |
|
107 this.chan = chan; |
|
108 this.sslContext = sslContext; |
|
109 this.sslStreams = sslStreams; |
|
110 this.logger = context.getLogger(); |
|
111 } |
|
112 |
|
113 SocketChannel getChannel () { |
|
114 return chan; |
|
115 } |
|
116 |
|
117 synchronized void close () { |
|
118 if (closed) { |
|
119 return; |
|
120 } |
|
121 closed = true; |
|
122 if (logger != null && chan != null) { |
|
123 logger.log (Level.TRACE, "Closing connection: " + chan.toString()); |
|
124 } |
|
125 |
|
126 if (!chan.isOpen()) { |
|
127 ServerImpl.dprint ("Channel already closed"); |
|
128 return; |
|
129 } |
|
130 try { |
|
131 /* need to ensure temporary selectors are closed */ |
|
132 if (raw != null) { |
|
133 raw.close(); |
|
134 } |
|
135 } catch (IOException e) { |
|
136 ServerImpl.dprint (e); |
|
137 } |
|
138 try { |
|
139 if (rawout != null) { |
|
140 rawout.close(); |
|
141 } |
|
142 } catch (IOException e) { |
|
143 ServerImpl.dprint (e); |
|
144 } |
|
145 try { |
|
146 if (sslStreams != null) { |
|
147 sslStreams.close(); |
|
148 } |
|
149 } catch (IOException e) { |
|
150 ServerImpl.dprint (e); |
|
151 } |
|
152 try { |
|
153 chan.close(); |
|
154 } catch (IOException e) { |
|
155 ServerImpl.dprint (e); |
|
156 } |
|
157 } |
|
158 |
|
159 /* remaining is the number of bytes left on the lowest level inputstream |
|
160 * after the exchange is finished |
|
161 */ |
|
162 void setRemaining (int r) { |
|
163 remaining = r; |
|
164 } |
|
165 |
|
166 int getRemaining () { |
|
167 return remaining; |
|
168 } |
|
169 |
|
170 SelectionKey getSelectionKey () { |
|
171 return selectionKey; |
|
172 } |
|
173 |
|
174 InputStream getInputStream () { |
|
175 return i; |
|
176 } |
|
177 |
|
178 OutputStream getRawOutputStream () { |
|
179 return rawout; |
|
180 } |
|
181 |
|
182 String getProtocol () { |
|
183 return protocol; |
|
184 } |
|
185 |
|
186 SSLEngine getSSLEngine () { |
|
187 return engine; |
|
188 } |
|
189 |
|
190 SSLContext getSSLContext () { |
|
191 return sslContext; |
|
192 } |
|
193 |
|
194 HttpContextImpl getHttpContext () { |
|
195 return context; |
|
196 } |
|
197 } |