001: //========================================================================
002: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: //------------------------------------------------------------------------
004: //Licensed under the Apache License, Version 2.0 (the "License");
005: //you may not use this file except in compliance with the License.
006: //You may obtain a copy of the License at
007: //http://www.apache.org/licenses/LICENSE-2.0
008: //Unless required by applicable law or agreed to in writing, software
009: //distributed under the License is distributed on an "AS IS" BASIS,
010: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: //See the License for the specific language governing permissions and
012: //limitations under the License.
013: //========================================================================
014:
015: package org.mortbay.jetty.ajp;
016:
017: import java.io.IOException;
018: import java.io.PrintWriter;
019:
020: import javax.servlet.ServletInputStream;
021: import javax.servlet.ServletOutputStream;
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServletResponse;
024:
025: import org.mortbay.io.Buffer;
026: import org.mortbay.io.EndPoint;
027: import org.mortbay.jetty.Connector;
028: import org.mortbay.jetty.HttpConnection;
029: import org.mortbay.jetty.HttpException;
030: import org.mortbay.jetty.HttpFields;
031: import org.mortbay.jetty.Request;
032: import org.mortbay.jetty.Response;
033: import org.mortbay.jetty.RetryRequest;
034: import org.mortbay.jetty.Server;
035: import org.mortbay.jetty.EofException;
036: import org.mortbay.jetty.HttpStatus;
037: import org.mortbay.log.Log;
038: import org.mortbay.util.URIUtil;
039: import org.mortbay.util.ajax.Continuation;
040:
041: /**
042: * Connection implementation of the Ajp13 protocol. <p/> XXX Refactor to remove
043: * duplication of HttpConnection
044: *
045: * @author Markus Kobler markus(at)inquisitive-mind.com
046: * @author Greg Wilkins
047: */
048: public class Ajp13Connection extends HttpConnection {
049: private boolean _sslSecure = false;
050:
051: public Ajp13Connection(Connector connector, EndPoint endPoint,
052: Server server) {
053: super (connector, endPoint, server);
054: _request = new Ajp13Request(this );
055: _generator = new Ajp13Generator(_connector, _endp, _connector
056: .getHeaderBufferSize(), _connector
057: .getResponseBufferSize());
058: _parser = new Ajp13Parser(_connector, _endp,
059: new RequestHandler(), (Ajp13Generator) _generator);
060: _generator.setSendServerVersion(server.getSendServerVersion());
061: _server = server;
062:
063: }
064:
065: public boolean isConfidential(Request request) {
066: return _sslSecure;
067: }
068:
069: public boolean isIntegral(Request request) {
070: return _sslSecure;
071: }
072:
073: public ServletInputStream getInputStream() {
074: if (_in == null)
075: _in = new Ajp13Parser.Input((Ajp13Parser) _parser,
076: _connector.getMaxIdleTime());
077: return _in;
078: }
079:
080: private class RequestHandler implements Ajp13Parser.EventHandler {
081: boolean _delayedHandling = false;
082:
083: public void startForwardRequest() throws IOException {
084: _delayedHandling = false;
085: _uri.clear();
086: _sslSecure = false;
087: _request.setTimeStamp(System.currentTimeMillis());
088: _request.setUri(_uri);
089: }
090:
091: public void parsedMethod(Buffer method) throws IOException {
092: _request.setMethod(method.toString());
093: }
094:
095: public void parsedUri(Buffer uri) throws IOException {
096: // TODO avoid this copy.
097: _uri.parse(uri.asArray(), 0, uri.length());
098: }
099:
100: public void parsedProtocol(Buffer protocol) throws IOException {
101: if (protocol != null && protocol.length() > 0) {
102: _request.setProtocol(protocol.toString());
103: }
104: }
105:
106: public void parsedRemoteAddr(Buffer addr) throws IOException {
107: if (addr != null && addr.length() > 0) {
108: ((Ajp13Request) _request)
109: .setRemoteAddr(addr.toString());
110: }
111: }
112:
113: public void parsedRemoteHost(Buffer name) throws IOException {
114: if (name != null && name.length() > 0) {
115: ((Ajp13Request) _request)
116: .setRemoteHost(name.toString());
117: }
118: }
119:
120: public void parsedServerName(Buffer name) throws IOException {
121: if (name != null && name.length() > 0) {
122: _request.setServerName(name.toString());
123: }
124: }
125:
126: public void parsedServerPort(int port) throws IOException {
127: ((Ajp13Request) _request).setServerPort(port);
128: }
129:
130: public void parsedSslSecure(boolean secure) throws IOException {
131: _sslSecure = secure;
132: }
133:
134: public void parsedQueryString(Buffer value) throws IOException {
135: String u = _uri + "?" + value;
136: _uri.parse(u);
137: }
138:
139: public void parsedHeader(Buffer name, Buffer value)
140: throws IOException {
141: _requestFields.add(name, value);
142: }
143:
144: public void parsedRequestAttribute(String key, Buffer value)
145: throws IOException {
146: _request.setAttribute(key, value.toString());
147: }
148:
149: public void headerComplete() throws IOException {
150: if (((Ajp13Parser) _parser).getContentLength() <= 0) {
151: handleRequest();
152: } else {
153: _delayedHandling = true;
154: }
155: }
156:
157: public void messageComplete(long contextLength)
158: throws IOException {
159: }
160:
161: public void content(Buffer ref) throws IOException {
162: if (_delayedHandling) {
163: _delayedHandling = false;
164: handleRequest();
165: }
166: }
167:
168: }
169:
170: }
|