1   //========================================================================
2   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
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   * Connection implementation of the Ajp13 protocol. <p/> XXX Refactor to remove
39   * duplication of HttpConnection
40   * 
41   * @author Markus Kobler markus(at)inquisitive-mind.com
42   * @author Greg Wilkins
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          _request = new Ajp13Request(this);
52          _generator = new Ajp13Generator(_connector, _endp, _connector.getHeaderBufferSize(), _connector.getResponseBufferSize());
53          _parser = new Ajp13Parser(_connector, _endp, new RequestHandler(), (Ajp13Generator) _generator);
54          _generator.setSendServerVersion(server.getSendServerVersion());
55          _server = server;
56  
57      }
58  
59      public boolean isConfidential(Request request)
60      {
61          return _sslSecure;
62      }
63  
64      public boolean isIntegral(Request request)
65      {
66          return _sslSecure;
67      }
68  
69      public ServletInputStream getInputStream()
70      {
71          if (_in == null)
72              _in = new Ajp13Parser.Input((Ajp13Parser) _parser, _connector.getMaxIdleTime());
73          return _in;
74      }
75  
76      private class RequestHandler implements Ajp13Parser.EventHandler
77      {
78          boolean _delayedHandling = false;
79  
80          public void startForwardRequest() throws IOException
81          {
82              _delayedHandling = false;
83              _uri.clear();
84              _sslSecure = false;
85              _request.setTimeStamp(System.currentTimeMillis());
86              _request.setUri(_uri);
87              
88          }
89  
90          public void parsedAuthorizationType(Buffer authType) throws IOException
91          {
92              _request.setAuthType(authType.toString());
93          }
94  
95          public void parsedRemoteUser(Buffer remoteUser) throws IOException
96          {
97              ((Ajp13Request)_request).setRemoteUser(remoteUser.toString());
98          }
99  
100         public void parsedServletPath(Buffer servletPath) throws IOException
101         {
102             _request.setServletPath(servletPath.toString());
103         }
104 
105         public void parsedContextPath(Buffer context) throws IOException
106         {
107             _request.setContextPath(context.toString());
108         }
109 
110         public void parsedSslCert(Buffer sslCert) throws IOException
111         {
112             try 
113             {
114                 CertificateFactory cf = CertificateFactory.getInstance("X.509");
115                 ByteArrayInputStream bis = new ByteArrayInputStream(sslCert.toString().getBytes());
116 
117                 Collection certCollection = cf.generateCertificates(bis);
118                 X509Certificate[] certificates = new X509Certificate[certCollection.size()];
119 
120                 int i=0;
121                 Iterator iter=certCollection.iterator();
122                 while(iter.hasNext())
123                     certificates[i++] = (X509Certificate)iter.next();
124 
125                 _request.setAttribute("javax.servlet.request.X509Certificate", certificates);
126             } 
127             catch (Exception e) 
128             {
129                 org.mortbay.log.Log.warn(e.toString());
130                 org.mortbay.log.Log.ignore(e);
131                 if (sslCert!=null)
132                     _request.setAttribute("javax.servlet.request.X509Certificate", sslCert.toString());
133             }
134         }
135 
136         public void parsedSslCipher(Buffer sslCipher) throws IOException
137         {
138             _request.setAttribute("javax.servlet.request.cipher_suite", sslCipher.toString());
139         }
140 
141         public void parsedSslSession(Buffer sslSession) throws IOException
142         {
143             _request.setAttribute("javax.servlet.request.ssl_session", sslSession.toString());
144         }
145 
146         public void parsedMethod(Buffer method) throws IOException
147         {
148             if (method == null)
149                 throw new HttpException(HttpServletResponse.SC_BAD_REQUEST);
150             _request.setMethod(method.toString());
151         }
152 
153         public void parsedUri(Buffer uri) throws IOException
154         {
155             _uri.parse(uri.toString());
156         }
157 
158         public void parsedProtocol(Buffer protocol) throws IOException
159         {
160             if (protocol != null && protocol.length()>0)
161             {
162                 _request.setProtocol(protocol.toString());
163             }
164         }
165 
166         public void parsedRemoteAddr(Buffer addr) throws IOException
167         {
168             if (addr != null && addr.length()>0)
169             {
170                 ((Ajp13Request) _request).setRemoteAddr(addr.toString());
171             }
172         }
173 
174         public void parsedRemoteHost(Buffer name) throws IOException
175         {
176             if (name != null && name.length()>0)
177             {
178                 ((Ajp13Request) _request).setRemoteHost(name.toString());
179             }
180         }
181 
182         public void parsedServerName(Buffer name) throws IOException
183         {
184             if (name != null && name.length()>0)
185             {
186                 _request.setServerName(name.toString());
187             }
188         }
189 
190         public void parsedServerPort(int port) throws IOException
191         {
192             ((Ajp13Request) _request).setServerPort(port);
193         }
194 
195         public void parsedSslSecure(boolean secure) throws IOException
196         {
197             _sslSecure = secure;
198         }
199 
200         public void parsedQueryString(Buffer value) throws IOException
201         {
202             String u = _uri + "?" + value;
203             _uri.parse(u);
204         }
205 
206         public void parsedHeader(Buffer name, Buffer value) throws IOException
207         {
208             _requestFields.add(name, value);
209         }
210 
211         public void parsedRequestAttribute(String key, Buffer value) throws IOException
212         {
213             _request.setAttribute(key, value.toString());
214         }
215         
216         public void parsedRequestAttribute(String key, int value) throws IOException
217         {
218             _request.setAttribute(key, Integer.toString(value));
219         }
220 
221         public void headerComplete() throws IOException
222         {
223             if (((Ajp13Parser) _parser).getContentLength() <= 0)
224             {
225                 handleRequest();
226             }
227             else
228             {
229                 _delayedHandling = true;
230             }
231         }
232 
233         public void messageComplete(long contextLength) throws IOException
234         {
235         }
236 
237         public void content(Buffer ref) throws IOException
238         {
239             if (_delayedHandling)
240             {
241                 _delayedHandling = false;
242                 handleRequest();
243             }
244         }
245 
246     }
247 
248 }