001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.servlets.ssi;
031:
032: import com.caucho.util.Alarm;
033: import com.caucho.util.IntMap;
034: import com.caucho.util.QDate;
035: import com.caucho.vfs.Path;
036:
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: /**
041: * Represents a SSI variable
042: */
043: public class VarExpr extends SSIExpr {
044: private static final int ATTRIBUTE = 0;
045: private static final int HTTP_ = ATTRIBUTE + 1;
046:
047: private static final int SERVER_SOFTWARE = HTTP_ + 1;
048: private static final int SERVER_NAME = SERVER_SOFTWARE + 1;
049: private static final int SERVER_ADDR = SERVER_NAME + 1;
050: private static final int SERVER_PORT = SERVER_ADDR + 1;
051: private static final int REMOTE_ADDR = SERVER_PORT + 1;
052: private static final int REMOTE_PORT = REMOTE_ADDR + 1;
053: private static final int REMOTE_USER = REMOTE_PORT + 1;
054: private static final int AUTH_TYPE = REMOTE_USER + 1;
055: private static final int GATEWAY_INTERFACE = AUTH_TYPE + 1;
056: private static final int SERVER_PROTOCOL = GATEWAY_INTERFACE + 1;
057: private static final int REQUEST_METHOD = SERVER_PROTOCOL + 1;
058: private static final int QUERY_STRING = REQUEST_METHOD + 1;
059: private static final int REQUEST_URI = QUERY_STRING + 1;
060: private static final int SCRIPT_FILENAME = REQUEST_URI + 1;
061: private static final int SCRIPT_NAME = SCRIPT_FILENAME + 1;
062: private static final int PATH_INFO = SCRIPT_NAME + 1;
063: private static final int PATH_TRANSLATED = PATH_INFO + 1;
064: private static final int CONTENT_LENGTH = PATH_TRANSLATED + 1;
065: private static final int CONTENT_TYPE = CONTENT_LENGTH + 1;
066:
067: private static final int DATE_GMT = CONTENT_TYPE + 1;
068: private static final int DATE_LOCAL = DATE_GMT + 1;
069: private static final int DOCUMENT_NAME = DATE_LOCAL + 1;
070: private static final int DOCUMENT_URI = DOCUMENT_NAME + 1;
071: private static final int LAST_MODIFIED = DOCUMENT_URI + 1;
072: private static final int USER_NAME = LAST_MODIFIED + 1;
073:
074: private static final IntMap _varMap = new IntMap();
075:
076: private final int _code;
077:
078: private final String _var;
079:
080: private final Path _path;
081:
082: VarExpr(String var, Path path) {
083: int code = _varMap.get(var.toLowerCase());
084:
085: if (code > 0) {
086: } else if (var.startsWith("HTTP_")) {
087: var = var.substring(5).replace('_', '-').toLowerCase();
088: code = HTTP_;
089: } else
090: code = ATTRIBUTE;
091:
092: _code = code;
093: _var = var;
094:
095: _path = path;
096: }
097:
098: /**
099: * Evaluate as a string.
100: */
101: public String evalString(HttpServletRequest request,
102: HttpServletResponse response) {
103: String fmt;
104: String value = null;
105:
106: switch (_code) {
107: case ATTRIBUTE:
108: value = String.valueOf(request.getAttribute(_var));
109: break;
110:
111: case HTTP_:
112: value = request.getHeader(_var);
113: break;
114:
115: case SERVER_SOFTWARE:
116: value = "Resin/" + com.caucho.Version.VERSION;
117: break;
118:
119: case SERVER_NAME:
120: case SERVER_ADDR:
121: value = request.getServerName();
122: break;
123:
124: case SERVER_PORT:
125: value = String.valueOf(request.getServerPort());
126: break;
127:
128: case REMOTE_ADDR:
129: value = request.getServerName();
130: break;
131:
132: case REMOTE_PORT:
133: value = String.valueOf(request.getServerPort());
134: break;
135:
136: case REMOTE_USER:
137: value = request.getRemoteUser();
138: break;
139:
140: case AUTH_TYPE:
141: value = request.getAuthType();
142: break;
143:
144: case GATEWAY_INTERFACE:
145: value = "CGI/1.1";
146: break;
147:
148: case SERVER_PROTOCOL:
149: value = request.getProtocol();
150: break;
151:
152: case REQUEST_METHOD:
153: value = request.getMethod();
154: break;
155:
156: case QUERY_STRING:
157: value = request.getQueryString();
158: break;
159:
160: case REQUEST_URI:
161: value = request.getRequestURI();
162: break;
163:
164: case PATH_INFO:
165: value = request.getPathInfo();
166: break;
167:
168: case PATH_TRANSLATED:
169: value = request.getRealPath(request.getPathInfo());
170: break;
171:
172: case CONTENT_LENGTH:
173: value = String.valueOf(request.getContentLength());
174: break;
175:
176: case CONTENT_TYPE:
177: value = request.getHeader("Content-Type");
178: break;
179:
180: case DATE_GMT:
181: fmt = (String) request.getAttribute("caucho.ssi.timefmt");
182: if (fmt == null)
183: fmt = "%Y-%m-%d %H:%M:%S";
184: value = QDate.formatGMT(Alarm.getCurrentTime(), fmt);
185: break;
186:
187: case DATE_LOCAL:
188: fmt = (String) request.getAttribute("caucho.ssi.timefmt");
189: if (fmt == null)
190: fmt = "%Y-%m-%d %H:%M:%S";
191:
192: value = QDate.formatLocal(Alarm.getCurrentTime(), fmt);
193: break;
194:
195: case DOCUMENT_NAME:
196: value = _path.getTail();
197: break;
198:
199: case DOCUMENT_URI:
200: value = request.getRequestURI();
201: break;
202:
203: case LAST_MODIFIED:
204: fmt = (String) request.getAttribute("caucho.ssi.timefmt");
205: if (fmt == null)
206: fmt = "%Y-%m-%d %H:%M:%S";
207:
208: value = QDate.formatLocal(_path.getLastModified(), fmt);
209: break;
210:
211: case USER_NAME:
212: default:
213: break;
214: }
215:
216: if (value != null)
217: return value;
218: else
219: return "(null)";
220: }
221:
222: static {
223: _varMap.put("server_software", SERVER_SOFTWARE);
224: _varMap.put("server_name", SERVER_NAME);
225: _varMap.put("server_addr", SERVER_ADDR);
226: _varMap.put("server_port", SERVER_PORT);
227: _varMap.put("remote_addr", REMOTE_ADDR);
228: _varMap.put("remote_port", REMOTE_PORT);
229: _varMap.put("remote_user", REMOTE_USER);
230: _varMap.put("auth_type", AUTH_TYPE);
231: _varMap.put("gateway_interface", GATEWAY_INTERFACE);
232: _varMap.put("server_protocol", SERVER_PROTOCOL);
233: _varMap.put("request_method", REQUEST_METHOD);
234: _varMap.put("query_string", QUERY_STRING);
235: _varMap.put("request_uri", REQUEST_URI);
236: _varMap.put("script_filename", SCRIPT_FILENAME);
237: _varMap.put("script_name", SCRIPT_NAME);
238: _varMap.put("path_info", PATH_INFO);
239: _varMap.put("path_translated", PATH_TRANSLATED);
240: _varMap.put("content_length", CONTENT_LENGTH);
241: _varMap.put("content_type", CONTENT_TYPE);
242:
243: _varMap.put("date_gmt", DATE_GMT);
244: _varMap.put("date_local", DATE_LOCAL);
245: _varMap.put("document_name", DOCUMENT_NAME);
246: _varMap.put("document_uri", DOCUMENT_URI);
247: _varMap.put("last_modified", LAST_MODIFIED);
248: _varMap.put("user_name", USER_NAME);
249: }
250: }
|