001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
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: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.coyote.tomcat3;
018:
019: import java.io.IOException;
020:
021: import org.apache.coyote.ActionCode;
022: import org.apache.tomcat.util.buf.ByteChunk;
023: import org.apache.tomcat.util.buf.MessageBytes;
024:
025: /** The Request to connect with Coyote.
026: * This class handles the I/O requirements and transferring the request
027: * line and Mime headers between Coyote and Tomcat.
028: *
029: * @author Bill Barker
030: * @author Costin Manolache
031: */
032: public class Tomcat3Request extends org.apache.tomcat.core.Request {
033:
034: org.apache.coyote.Request coyoteRequest = null;
035:
036: // For SSL attributes we need to call an ActionHook to get
037: // info from the protocol handler.
038: // SSLSupport sslSupport=null;
039:
040: ByteChunk readChunk = new ByteChunk(8096);
041: int pos = -1;
042: int end = -1;
043: byte[] readBuffer = null;
044:
045: public Tomcat3Request() {
046: super ();
047: remoteAddrMB.recycle();
048: remoteHostMB.recycle();
049: }
050:
051: public void recycle() {
052: super .recycle();
053: if (coyoteRequest != null)
054: coyoteRequest.recycle();
055:
056: remoteAddrMB.recycle();
057: remoteHostMB.recycle();
058: readChunk.recycle();
059:
060: readBuffer = null;
061: pos = -1;
062: end = -1;
063: }
064:
065: public org.apache.coyote.Request getCoyoteRequest() {
066: return coyoteRequest;
067: }
068:
069: /** Attach the Coyote Request to this Request.
070: * This is currently set pre-request to allow copying the request
071: * attributes to the Tomcat attributes.
072: */
073: public void setCoyoteRequest(org.apache.coyote.Request cReq) {
074: coyoteRequest = cReq;
075:
076: // The CoyoteRequest/Tomcat3Request are bound togheter, they
077: // don't change. That means we can use the same field ( which
078: // doesn't change as well.
079: schemeMB = coyoteRequest.scheme();
080: methodMB = coyoteRequest.method();
081: uriMB = coyoteRequest.requestURI();
082: queryMB = coyoteRequest.query();
083: protoMB = coyoteRequest.protocol();
084:
085: headers = coyoteRequest.getMimeHeaders();
086: scookies.setHeaders(headers);
087: params.setHeaders(headers);
088: params.setQuery(queryMB);
089:
090: remoteAddrMB = coyoteRequest.remoteAddr();
091: remoteHostMB = coyoteRequest.remoteHost();
092: serverNameMB = coyoteRequest.serverName();
093:
094: }
095:
096: /** Read a single character from the request body.
097: */
098: public int doRead() throws IOException {
099: if (available == 0)
100: return -1;
101: // #3745
102: // if available == -1: unknown length, we'll read until end of stream.
103: if (available != -1)
104: available--;
105: if (pos >= end) {
106: if (readBytes() < 0)
107: return -1;
108: }
109: return readBuffer[pos++] & 0xFF;
110: }
111:
112: /** Read a chunk from the request body.
113: */
114: public int doRead(byte[] b, int off, int len) throws IOException {
115: if (available == 0)
116: return -1;
117: // if available == -1: unknown length, we'll read until end of stream.
118: if (pos >= end) {
119: if (readBytes() <= 0)
120: return -1;
121: }
122: int rd = -1;
123: if ((end - pos) > len) {
124: rd = len;
125: } else {
126: rd = end - pos;
127: }
128:
129: System.arraycopy(readBuffer, pos, b, off, rd);
130: pos += rd;
131: if (available != -1)
132: available -= rd;
133:
134: return rd;
135: }
136:
137: /**
138: * Read bytes to the read chunk buffer.
139: */
140: protected int readBytes() throws IOException {
141:
142: int result = coyoteRequest.doRead(readChunk);
143: if (result > 0) {
144: readBuffer = readChunk.getBytes();
145: end = readChunk.getEnd();
146: pos = readChunk.getStart();
147: } else if (result < 0) {
148: throw new IOException("Read bytes failed " + result);
149: }
150: return result;
151:
152: }
153:
154: // -------------------- override special methods
155:
156: public MessageBytes remoteAddr() {
157: if (remoteAddrMB.isNull()) {
158: coyoteRequest.action(
159: ActionCode.ACTION_REQ_HOST_ADDR_ATTRIBUTE,
160: coyoteRequest);
161: }
162: return remoteAddrMB;
163: }
164:
165: public MessageBytes remoteHost() {
166: if (remoteHostMB.isNull()) {
167: coyoteRequest.action(ActionCode.ACTION_REQ_HOST_ATTRIBUTE,
168: coyoteRequest);
169: }
170: return remoteHostMB;
171: }
172:
173: public String getLocalHost() {
174: return localHost;
175: }
176:
177: public MessageBytes serverName() {
178: // That's set by protocol in advance, it's needed for mapping anyway,
179: // no need to do lazy eval.
180: return coyoteRequest.serverName();
181: }
182:
183: public int getServerPort() {
184: return coyoteRequest.getServerPort();
185: }
186:
187: public void setServerPort(int i) {
188: coyoteRequest.setServerPort(i);
189: }
190:
191: public void setRemoteUser(String s) {
192: super .setRemoteUser(s);
193: coyoteRequest.getRemoteUser().setString(s);
194: }
195:
196: public String getRemoteUser() {
197: String s = coyoteRequest.getRemoteUser().toString();
198: if (s == null)
199: s = super .getRemoteUser();
200: return s;
201: }
202:
203: public String getAuthType() {
204: return coyoteRequest.getAuthType().toString();
205: }
206:
207: public void setAuthType(String s) {
208: coyoteRequest.getAuthType().setString(s);
209: }
210:
211: public String getJvmRoute() {
212: return coyoteRequest.instanceId().toString();
213: }
214:
215: public void setJvmRoute(String s) {
216: coyoteRequest.instanceId().setString(s);
217: }
218:
219: public boolean isSecure() {
220: return "https".equalsIgnoreCase(coyoteRequest.scheme()
221: .toString());
222: }
223: }
|