Fix warnings. No bug. r+sr=jst
[mozilla-central.git] / tools / testserver / ScriptFile.java
blob50c304cfdb50e42211ca5fd0e93417570a6bb985
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 import java.io.*;
39 import java.util.Date;
40 import java.util.StringTokenizer;
42 ScriptFile class reads off the script file and sets up the list
43 of delimiters.
46 class ScriptFile {
48 public static final String URLMAP = new String("docs/urlmap");
50 public ScriptFile(Connection c, String req, PrintWriter stream ) {
51 con = c;
52 request = req;
53 out = stream;
54 if (out == null)
55 out = new PrintWriter(System.out);
56 file = URLMAP;
57 try {
58 Parse();
60 catch (IOException e)
62 out.println("HTTP/1.1 500 Server Error\n\n");
63 out.println("Failed with this exception-");
64 out.println(e);
66 out.flush();
69 public ScriptFile(String f) {
70 file = f;
71 out = new PrintWriter(System.out);
72 try {
73 Parse();
75 catch (FileNotFoundException e)
77 out.println("HTTP/1.1 404 File Not Found\n\n");
78 out.println("File not found!");
79 out.println(e);
81 catch (IOException e)
83 out.println("HTTP/1.1 500 Server Error\n\n");
84 out.println("Failed with this exception-");
85 out.println(e);
87 out.flush();
90 public void Parse () throws IOException {
92 if ((request == null) ||
93 (request.length() == 0) ||
94 request.startsWith("GET / ") || request.startsWith("get / "))
96 WriteDefaultResponse();
97 return;
100 if (request.startsWith("GET /?") || request.startsWith("get /?"))
102 WriteSpecialResponse(request.substring(request.indexOf('?')+1));
103 return;
106 if (request.startsWith("HEAD /") || request.startsWith("head /"))
108 WriteHeadResponse();
109 return;
112 boolean outDirty = false;
113 if (file != null)
115 BufferedReader in = new BufferedReader(
116 new InputStreamReader(
117 new FileInputStream(file)));
119 String s = new String();
120 while((s = in.readLine())!= null)
122 s.trim();
123 /* Skip comments */
124 if ((s.length() == 0) || (s.charAt(0) == '#'))
125 continue;
126 if (s.startsWith("START")) {
127 // Check to see if this was in the requested URL
128 String filename = new String ("GET " + s.substring(6));
129 String otherfilename = new String("POST " + s.substring(6));
130 if (request.startsWith(filename) ||
131 request.startsWith(otherfilename))
133 continue;
135 else// Else skipto past the END
137 while (!s.startsWith("END"))
139 s = in.readLine();
140 if (s != null)
142 s.trim();
144 else
145 break;
149 else if (s.startsWith("ECHO")) {
150 outDirty = true;
151 boolean parameter = false;
152 try {
153 String header = new String(s.substring(5));
154 String req= new String(con.request);
155 int t = req.indexOf(header);
156 if (t != -1) {
157 out.println(req.substring(
158 t, req.indexOf("\n", t)));
159 parameter = true;
161 else {
162 out.println("Error: " + header +
163 " not specified in request!");
167 catch (StringIndexOutOfBoundsException e) {}
168 if (!parameter)
169 out.println(con.request);
171 else if (s.startsWith("INCLUDE")) {
172 outDirty = true;
173 WriteOutFile("docs/" + s.substring(8));
175 else if (s.startsWith("CRLF")) {
176 outDirty = true;
177 out.println();
179 else if (s.startsWith("END")) {
180 // ignore should never have appeared here though!
181 continue;
183 else {// Not a recognizable line... just print it as is...
184 outDirty = true;
185 out.println(s);
189 in.close();
191 if (outDirty)
193 out.flush();
195 else
196 WriteDefaultResponse();
200 public static void main(String args[]) {
201 if (args.length >= 1) {
202 ScriptFile sf = new ScriptFile(args[0]);
203 /* Detect change stuff;
204 File f = new File(args[0]);
205 long lastMod = f.lastModified();
206 while (f.lastModified() == lastMod) {
208 sf.Parse();
213 protected void WriteOutFile(String filename) throws IOException {
214 BufferedReader incl = new BufferedReader(
215 new InputStreamReader(
216 new FileInputStream(filename)));
217 // This doesn't have to be line wise... change later TODO
218 String s;
219 while ((s = incl.readLine()) != null)
221 out.println(s);
223 incl.close();
226 protected void WriteDefaultResponse() throws IOException {
227 WriteOutFile("docs/generic.res");
228 out.println("Date: " + (new Date()).toString());
229 if (file != null)
231 File f = new File(file);
232 out.println("Last-modified: " + (new Date(f.lastModified())).toString());
234 out.println("\n"); // prints 2
235 if (con != null)
237 out.println("<HTML><BODY>");
238 out.println("<H3>Rec'd. the following request-</H3>");
239 out.println("<PRE>");
240 out.println(con.request);
241 out.println("</PRE>");
242 out.println("From- " + con.client);
243 out.println("</BODY></HTML>");
247 protected void WriteSpecialResponse(String specialcase) throws IOException {
248 out.println("HTTP/1.1 200 OK");
249 out.println("Server: HTTP Test Server/1.1");
250 out.println("Content-Type: text/plain");
252 StringTokenizer st = new StringTokenizer(specialcase, " &");
253 while (st.hasMoreTokens()) {
254 String pair = st.nextToken();
255 if (pair.startsWith("Length=") || pair.startsWith("length="))
258 int len = Integer.valueOf(pair.substring(
259 pair.indexOf('=')+1), 10).intValue();
261 out.println("Date: " + (new Date()).toString());
262 out.println("Content-Length: " + len);
263 out.println("\n");
264 for (int i = 0; i<len; i++) {
265 out.print ((char)i%10);
266 if ((i>0) && ((i+1)%80) == 0)
267 out.print('\n');
268 out.flush();
270 return;
276 protected void WriteHeadResponse() {
277 out.println("HTTP/1.1 200 OK");
278 out.println("Server: HTTP Test Server/1.1");
279 out.println("Content-Type: text/plain");
280 out.println("Content-Length: 1000" ); // bogus
281 out.println("Date: " + (new Date()).toString());
282 out.println(); // also test without it
283 out.flush();
286 String file = null;
287 // The string associated with this script occurence
289 String request;
290 PrintWriter out;
291 Connection con;