|
1 /* |
|
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any 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.util.logging.Logger; |
|
32 import com.sun.net.httpserver.*; |
|
33 import com.sun.net.httpserver.spi.*; |
|
34 |
|
35 /** |
|
36 * encapsulates all the connection specific state for a HTTP/S connection |
|
37 * one of these is hung from the selector attachment and is used to locate |
|
38 * everything from that. |
|
39 */ |
|
40 class HttpConnection { |
|
41 |
|
42 HttpContextImpl context; |
|
43 SSLEngine engine; |
|
44 SSLContext sslContext; |
|
45 SSLStreams sslStreams; |
|
46 |
|
47 /* high level streams returned to application */ |
|
48 InputStream i; |
|
49 |
|
50 /* low level stream that sits directly over channel */ |
|
51 InputStream raw; |
|
52 OutputStream rawout; |
|
53 |
|
54 SocketChannel chan; |
|
55 SelectionKey selectionKey; |
|
56 String protocol; |
|
57 long time; |
|
58 int remaining; |
|
59 boolean closed = false; |
|
60 Logger logger; |
|
61 |
|
62 public String toString() { |
|
63 String s = null; |
|
64 if (chan != null) { |
|
65 s = chan.toString(); |
|
66 } |
|
67 return s; |
|
68 } |
|
69 |
|
70 HttpConnection () { |
|
71 } |
|
72 |
|
73 void setChannel (SocketChannel c) { |
|
74 chan = c; |
|
75 } |
|
76 |
|
77 void setContext (HttpContextImpl ctx) { |
|
78 context = ctx; |
|
79 } |
|
80 |
|
81 void setParameters ( |
|
82 InputStream in, OutputStream rawout, SocketChannel chan, |
|
83 SSLEngine engine, SSLStreams sslStreams, SSLContext sslContext, String protocol, |
|
84 HttpContextImpl context, InputStream raw |
|
85 ) |
|
86 { |
|
87 this.context = context; |
|
88 this.i = in; |
|
89 this.rawout = rawout; |
|
90 this.raw = raw; |
|
91 this.protocol = protocol; |
|
92 this.engine = engine; |
|
93 this.chan = chan; |
|
94 this.sslContext = sslContext; |
|
95 this.sslStreams = sslStreams; |
|
96 this.logger = context.getLogger(); |
|
97 } |
|
98 |
|
99 SocketChannel getChannel () { |
|
100 return chan; |
|
101 } |
|
102 |
|
103 synchronized void close () { |
|
104 if (closed) { |
|
105 return; |
|
106 } |
|
107 closed = true; |
|
108 if (logger != null && chan != null) { |
|
109 logger.finest ("Closing connection: " + chan.toString()); |
|
110 } |
|
111 |
|
112 if (!chan.isOpen()) { |
|
113 ServerImpl.dprint ("Channel already closed"); |
|
114 return; |
|
115 } |
|
116 try { |
|
117 /* need to ensure temporary selectors are closed */ |
|
118 if (raw != null) { |
|
119 raw.close(); |
|
120 } |
|
121 } catch (IOException e) { |
|
122 ServerImpl.dprint (e); |
|
123 } |
|
124 try { |
|
125 if (rawout != null) { |
|
126 rawout.close(); |
|
127 } |
|
128 } catch (IOException e) { |
|
129 ServerImpl.dprint (e); |
|
130 } |
|
131 try { |
|
132 if (sslStreams != null) { |
|
133 sslStreams.close(); |
|
134 } |
|
135 } catch (IOException e) { |
|
136 ServerImpl.dprint (e); |
|
137 } |
|
138 try { |
|
139 chan.close(); |
|
140 } catch (IOException e) { |
|
141 ServerImpl.dprint (e); |
|
142 } |
|
143 } |
|
144 |
|
145 /* remaining is the number of bytes left on the lowest level inputstream |
|
146 * after the exchange is finished |
|
147 */ |
|
148 void setRemaining (int r) { |
|
149 remaining = r; |
|
150 } |
|
151 |
|
152 int getRemaining () { |
|
153 return remaining; |
|
154 } |
|
155 |
|
156 SelectionKey getSelectionKey () { |
|
157 return selectionKey; |
|
158 } |
|
159 |
|
160 InputStream getInputStream () { |
|
161 return i; |
|
162 } |
|
163 |
|
164 OutputStream getRawOutputStream () { |
|
165 return rawout; |
|
166 } |
|
167 |
|
168 String getProtocol () { |
|
169 return protocol; |
|
170 } |
|
171 |
|
172 SSLEngine getSSLEngine () { |
|
173 return engine; |
|
174 } |
|
175 |
|
176 SSLContext getSSLContext () { |
|
177 return sslContext; |
|
178 } |
|
179 |
|
180 HttpContextImpl getHttpContext () { |
|
181 return context; |
|
182 } |
|
183 } |