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
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.
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 ***** */
39 import java
.util
.Date
;
40 import java
.util
.StringTokenizer
;
42 ScriptFile class reads off the script file and sets up the list
48 public static final String URLMAP
= new String("docs/urlmap");
50 public ScriptFile(Connection c
, String req
, PrintWriter stream
) {
55 out
= new PrintWriter(System
.out
);
62 out
.println("HTTP/1.1 500 Server Error\n\n");
63 out
.println("Failed with this exception-");
69 public ScriptFile(String f
) {
71 out
= new PrintWriter(System
.out
);
75 catch (FileNotFoundException e
)
77 out
.println("HTTP/1.1 404 File Not Found\n\n");
78 out
.println("File not found!");
83 out
.println("HTTP/1.1 500 Server Error\n\n");
84 out
.println("Failed with this exception-");
90 public void Parse () throws IOException
{
92 if ((request
== null) ||
93 (request
.length() == 0) ||
94 request
.startsWith("GET / ") || request
.startsWith("get / "))
96 WriteDefaultResponse();
100 if (request
.startsWith("GET /?") || request
.startsWith("get /?"))
102 WriteSpecialResponse(request
.substring(request
.indexOf('?')+1));
106 if (request
.startsWith("HEAD /") || request
.startsWith("head /"))
112 boolean outDirty
= false;
115 BufferedReader in
= new BufferedReader(
116 new InputStreamReader(
117 new FileInputStream(file
)));
119 String s
= new String();
120 while((s
= in
.readLine())!= null)
124 if ((s
.length() == 0) || (s
.charAt(0) == '#'))
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
))
135 else// Else skipto past the END
137 while (!s
.startsWith("END"))
149 else if (s
.startsWith("ECHO")) {
151 boolean parameter
= false;
153 String header
= new String(s
.substring(5));
154 String req
= new String(con
.request
);
155 int t
= req
.indexOf(header
);
157 out
.println(req
.substring(
158 t
, req
.indexOf("\n", t
)));
162 out
.println("Error: " + header
+
163 " not specified in request!");
167 catch (StringIndexOutOfBoundsException e
) {}
169 out
.println(con
.request
);
171 else if (s
.startsWith("INCLUDE")) {
173 WriteOutFile("docs/" + s
.substring(8));
175 else if (s
.startsWith("CRLF")) {
179 else if (s
.startsWith("END")) {
180 // ignore should never have appeared here though!
183 else {// Not a recognizable line... just print it as is...
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) {
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
219 while ((s
= incl
.readLine()) != null)
226 protected void WriteDefaultResponse() throws IOException
{
227 WriteOutFile("docs/generic.res");
228 out
.println("Date: " + (new Date()).toString());
231 File f
= new File(file
);
232 out
.println("Last-modified: " + (new Date(f
.lastModified())).toString());
234 out
.println("\n"); // prints 2
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
);
264 for (int i
= 0; i
<len
; i
++) {
265 out
.print ((char)i
%10);
266 if ((i
>0) && ((i
+1)%80) == 0)
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
287 // The string associated with this script occurence