1 29 30 package com.caucho.servlets.ssi; 31 32 import com.caucho.util.ByteBuffer; 33 import com.caucho.vfs.Path; 34 import com.caucho.vfs.ReadStream; 35 import com.caucho.vfs.Vfs; 36 import com.caucho.vfs.WriteStream; 37 38 import javax.servlet.ServletException ; 39 import javax.servlet.http.HttpServlet ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import java.io.IOException ; 43 import java.util.ArrayList ; 44 import java.util.HashMap ; 45 46 49 public class SSIServlet extends HttpServlet { 50 public void doGet(HttpServletRequest request, 51 HttpServletResponse response) 52 throws ServletException , IOException 53 { 54 String servletPath = request.getServletPath(); 55 String pathInfo = request.getPathInfo(); 56 57 String fullPath; 58 59 if (pathInfo != null) 60 fullPath = servletPath + pathInfo; 61 else 62 fullPath = servletPath; 63 64 66 String realPath = request.getRealPath(servletPath); 67 68 Path path = Vfs.lookup().lookup(realPath); 69 70 Statement stmt = new Parser().parse(path); 71 72 WriteStream out = Vfs.openWrite(response.getOutputStream()); 73 74 try { 75 stmt.apply(out, request, response); 76 } finally { 77 out.close(); 78 } 79 } 80 81 static class Parser { 82 private int _line; 83 84 Statement parse(Path path) 85 throws IOException 86 { 87 ReadStream is = path.openRead(); 88 89 try { 90 ArrayList <Statement> statements = new ArrayList <Statement>(); 91 92 parse(is, statements); 93 94 return new BlockStatement(statements); 95 } finally { 96 is.close(); 97 } 98 } 99 100 103 private void parse(ReadStream is, ArrayList <Statement> statements) 104 throws IOException 105 { 106 ByteBuffer bb = new ByteBuffer(); 107 int ch; 108 109 while ((ch = is.read()) >= 0) { 110 if (ch != '<') { 111 if (ch == '\n') 112 _line++; 113 114 bb.append(ch); 115 } 116 else if ((ch = is.read()) != '!') { 117 bb.append('<'); 118 119 is.unread(); 120 } 121 else if ((ch = is.read()) != '-') { 122 bb.append('<'); 123 bb.append('!'); 124 125 is.unread(); 126 } 127 else if ((ch = is.read()) != '-') { 128 bb.append('<'); 129 bb.append('!'); 130 bb.append('-'); 131 132 is.unread(); 133 } 134 else if ((ch = is.read()) != '#') { 135 bb.append('<'); 136 bb.append('!'); 137 bb.append('-'); 138 bb.append('-'); 139 140 is.unread(); 141 } 142 else { 143 if (bb.getLength() > 0) { 144 TextStatement text; 145 146 text = new TextStatement(bb.getBuffer(), 0, bb.getLength()); 147 148 statements.add(text); 149 bb.clear(); 150 } 151 152 statements.add(parseCommand(is)); 153 } 154 } 155 156 if (bb.getLength() > 0) { 157 statements.add(new TextStatement(bb.getBuffer(), 0, bb.getLength())); 158 bb.clear(); 159 } 160 } 161 162 private Statement parseCommand(ReadStream is) 163 throws IOException 164 { 165 StringBuilder sb = new StringBuilder (); 166 167 int ch; 168 169 while (Character.isLetterOrDigit((ch = is.read()))) { 170 sb.append((char) ch); 171 } 172 173 String cmd = sb.toString(); 174 175 HashMap <String ,String > attr = parseAttributes(is); 176 177 if ((ch = is.read()) != '-') { 178 } 179 else if ((ch = is.read()) != '-') { 180 } 181 else if ((ch = is.read()) != '>') { 182 } 183 184 if ("config".equals(cmd)) 185 return ConfigStatement.create(attr, is.getPath()); 186 else if ("echo".equals(cmd)) 187 return EchoStatement.create(attr, is.getPath()); 188 else if ("include".equals(cmd)) 189 return IncludeStatement.create(attr, is.getPath()); 190 else if ("set".equals(cmd)) 191 return SetStatement.create(attr, is.getPath()); 192 else 193 return new ErrorStatement("['" + cmd + "' is an unknown command.]"); 194 } 195 196 private HashMap <String ,String > parseAttributes(ReadStream is) 197 throws IOException 198 { 199 HashMap <String ,String > attr = new HashMap <String ,String >(); 200 201 while (true) { 202 int ch; 203 204 while (Character.isWhitespace((ch = is.read()))) { 205 } 206 207 StringBuilder key = new StringBuilder (); 208 209 for (; Character.isLetterOrDigit(ch); ch = is.read()) { 210 key.append((char) ch); 211 } 212 213 for (; Character.isWhitespace(ch); ch = is.read()) { 214 } 215 216 if (ch != '=') 217 return attr; 218 219 for (ch = is.read(); Character.isWhitespace(ch); ch = is.read()) { 220 } 221 222 StringBuilder value = new StringBuilder (); 223 224 if (ch == '\'' || ch == '"') { 225 int end = ch; 226 227 for (ch = is.read(); ch > 0 && ch != end; ch = is.read()) { 228 if (ch == '\\') { 229 ch = is.read(); 230 231 if (ch == '\'' || ch == '\"') 232 value.append((char) ch); 233 else { 234 value.append('\\'); 235 is.unread(); 236 } 237 } 238 else 239 value.append((char) ch); 240 } 241 } 242 else { 243 for (; ch > 0 && ! Character.isWhitespace(ch); ch = is.read()) { 244 value.append((char) ch); 245 } 246 } 247 248 attr.put(key.toString(), value.toString()); 249 } 250 } 251 } 252 } 253 | Popular Tags |