1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.mortbay.jetty.ajp;
16
17 import java.io.ByteArrayInputStream;
18 import java.io.IOException;
19 import java.security.cert.Certificate;
20 import java.security.cert.CertificateException;
21 import java.security.cert.CertificateFactory;
22 import java.security.cert.X509Certificate;
23 import java.util.Collection;
24 import java.util.Iterator;
25
26 import javax.servlet.ServletInputStream;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.mortbay.io.Buffer;
30 import org.mortbay.io.EndPoint;
31 import org.mortbay.jetty.Connector;
32 import org.mortbay.jetty.HttpConnection;
33 import org.mortbay.jetty.HttpException;
34 import org.mortbay.jetty.Request;
35 import org.mortbay.jetty.Server;
36
37
38
39
40
41
42
43
44 public class Ajp13Connection extends HttpConnection
45 {
46 private boolean _sslSecure = false;
47
48 public Ajp13Connection(Connector connector, EndPoint endPoint, Server server)
49 {
50 super(connector, endPoint, server,
51 new Ajp13Parser(connector, endPoint),
52 new Ajp13Generator(connector, endPoint, connector.getHeaderBufferSize(), connector.getResponseBufferSize()),
53 new Ajp13Request(null)
54 );
55
56 ((Ajp13Parser)_parser).setEventHandler(new RequestHandler());
57
58 ((Ajp13Parser)_parser).setGenerator((Ajp13Generator)_generator);
59
60 }
61
62 public boolean isConfidential(Request request)
63 {
64 return _sslSecure;
65 }
66
67 public boolean isIntegral(Request request)
68 {
69 return _sslSecure;
70 }
71
72 public ServletInputStream getInputStream()
73 {
74 if (_in == null)
75 _in = new Ajp13Parser.Input((Ajp13Parser) _parser, _connector.getMaxIdleTime());
76 return _in;
77 }
78
79 private class RequestHandler implements Ajp13Parser.EventHandler
80 {
81 boolean _delayedHandling = false;
82
83 public void startForwardRequest() throws IOException
84 {
85 _delayedHandling = false;
86 _uri.clear();
87 _sslSecure = false;
88 _request.setTimeStamp(System.currentTimeMillis());
89 _request.setUri(_uri);
90
91 }
92
93 public void parsedAuthorizationType(Buffer authType) throws IOException
94 {
95 _request.setAuthType(authType.toString());
96 }
97
98 public void parsedRemoteUser(Buffer remoteUser) throws IOException
99 {
100 ((Ajp13Request)_request).setRemoteUser(remoteUser.toString());
101 }
102
103 public void parsedServletPath(Buffer servletPath) throws IOException
104 {
105 _request.setServletPath(servletPath.toString());
106 }
107
108 public void parsedContextPath(Buffer context) throws IOException
109 {
110 _request.setContextPath(context.toString());
111 }
112
113 public void parsedSslCert(Buffer sslCert) throws IOException
114 {
115 try
116 {
117 CertificateFactory cf = CertificateFactory.getInstance("X.509");
118 ByteArrayInputStream bis = new ByteArrayInputStream(sslCert.toString().getBytes());
119
120 Collection certCollection = cf.generateCertificates(bis);
121 X509Certificate[] certificates = new X509Certificate[certCollection.size()];
122
123 int i=0;
124 Iterator iter=certCollection.iterator();
125 while(iter.hasNext())
126 certificates[i++] = (X509Certificate)iter.next();
127
128 _request.setAttribute("javax.servlet.request.X509Certificate", certificates);
129 }
130 catch (Exception e)
131 {
132 org.mortbay.log.Log.warn(e.toString());
133 org.mortbay.log.Log.ignore(e);
134 if (sslCert!=null)
135 _request.setAttribute("javax.servlet.request.X509Certificate", sslCert.toString());
136 }
137 }
138
139 public void parsedSslCipher(Buffer sslCipher) throws IOException
140 {
141 _request.setAttribute("javax.servlet.request.cipher_suite", sslCipher.toString());
142 }
143
144 public void parsedSslSession(Buffer sslSession) throws IOException
145 {
146 _request.setAttribute("javax.servlet.request.ssl_session", sslSession.toString());
147 }
148
149 public void parsedSslKeySize(int keySize) throws IOException
150 {
151 _request.setAttribute("javax.servlet.request.key_size", new Integer(keySize));
152 }
153
154 public void parsedMethod(Buffer method) throws IOException
155 {
156 if (method == null)
157 throw new HttpException(HttpServletResponse.SC_BAD_REQUEST);
158 _request.setMethod(method.toString());
159 }
160
161 public void parsedUri(Buffer uri) throws IOException
162 {
163 _uri.parse(uri.toString());
164 }
165
166 public void parsedProtocol(Buffer protocol) throws IOException
167 {
168 if (protocol != null && protocol.length()>0)
169 {
170 _request.setProtocol(protocol.toString());
171 }
172 }
173
174 public void parsedRemoteAddr(Buffer addr) throws IOException
175 {
176 if (addr != null && addr.length()>0)
177 {
178 ((Ajp13Request) _request).setRemoteAddr(addr.toString());
179 }
180 }
181
182 public void parsedRemoteHost(Buffer name) throws IOException
183 {
184 if (name != null && name.length()>0)
185 {
186 ((Ajp13Request) _request).setRemoteHost(name.toString());
187 }
188 }
189
190 public void parsedServerName(Buffer name) throws IOException
191 {
192 if (name != null && name.length()>0)
193 {
194 _request.setServerName(name.toString());
195 }
196 }
197
198 public void parsedServerPort(int port) throws IOException
199 {
200 ((Ajp13Request) _request).setServerPort(port);
201 }
202
203 public void parsedSslSecure(boolean secure) throws IOException
204 {
205 _sslSecure = secure;
206 }
207
208 public void parsedQueryString(Buffer value) throws IOException
209 {
210 String u = _uri + "?" + value;
211 _uri.parse(u);
212 }
213
214 public void parsedHeader(Buffer name, Buffer value) throws IOException
215 {
216 _requestFields.add(name, value);
217 }
218
219 public void parsedRequestAttribute(String key, Buffer value) throws IOException
220 {
221 _request.setAttribute(key, value.toString());
222 }
223
224 public void parsedRequestAttribute(String key, int value) throws IOException
225 {
226 _request.setAttribute(key, Integer.toString(value));
227 }
228
229 public void headerComplete() throws IOException
230 {
231 if (((Ajp13Parser) _parser).getContentLength() <= 0)
232 {
233 handleRequest();
234 }
235 else
236 {
237 _delayedHandling = true;
238 }
239 }
240
241 public void messageComplete(long contextLength) throws IOException
242 {
243 }
244
245 public void content(Buffer ref) throws IOException
246 {
247 if (_delayedHandling)
248 {
249 _delayedHandling = false;
250 handleRequest();
251 }
252 }
253
254 }
255
256 }