1999-06-11 10:21:16 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-06-11 10:21:16 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-06-11 10:21:16 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-06-11 10:21:16 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:40:37 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-06-11 10:21:16 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.Date;
|
1999-06-19 08:05:10 +04:00
|
|
|
import java.util.StringTokenizer;
|
1999-06-11 10:21:16 +04:00
|
|
|
/*
|
|
|
|
ScriptFile class reads off the script file and sets up the list
|
|
|
|
of delimiters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ScriptFile {
|
|
|
|
|
|
|
|
public static final String URLMAP = new String("docs/urlmap");
|
|
|
|
|
|
|
|
public ScriptFile(Connection c, String req, PrintWriter stream ) {
|
|
|
|
con = c;
|
|
|
|
request = req;
|
|
|
|
out = stream;
|
|
|
|
if (out == null)
|
|
|
|
out = new PrintWriter(System.out);
|
|
|
|
file = URLMAP;
|
|
|
|
try {
|
|
|
|
Parse();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
out.println("HTTP/1.1 500 Server Error\n\n");
|
|
|
|
out.println("Failed with this exception-");
|
|
|
|
out.println(e);
|
|
|
|
}
|
|
|
|
out.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ScriptFile(String f) {
|
|
|
|
file = f;
|
|
|
|
out = new PrintWriter(System.out);
|
|
|
|
try {
|
|
|
|
Parse();
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException e)
|
|
|
|
{
|
|
|
|
out.println("HTTP/1.1 404 File Not Found\n\n");
|
|
|
|
out.println("File not found!");
|
|
|
|
out.println(e);
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
out.println("HTTP/1.1 500 Server Error\n\n");
|
|
|
|
out.println("Failed with this exception-");
|
|
|
|
out.println(e);
|
|
|
|
}
|
|
|
|
out.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Parse () throws IOException {
|
|
|
|
|
1999-06-19 08:05:10 +04:00
|
|
|
if ((request == null) ||
|
|
|
|
(request.length() == 0) ||
|
1999-10-23 01:21:30 +04:00
|
|
|
request.startsWith("GET / ") || request.startsWith("get / "))
|
1999-06-11 10:21:16 +04:00
|
|
|
{
|
|
|
|
WriteDefaultResponse();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-10-23 01:21:30 +04:00
|
|
|
if (request.startsWith("GET /?") || request.startsWith("get /?"))
|
1999-06-19 08:05:10 +04:00
|
|
|
{
|
|
|
|
WriteSpecialResponse(request.substring(request.indexOf('?')+1));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-06-11 10:21:16 +04:00
|
|
|
boolean outDirty = false;
|
|
|
|
if (file != null)
|
|
|
|
{
|
1999-10-13 14:24:12 +04:00
|
|
|
BufferedReader in = new BufferedReader(
|
|
|
|
new InputStreamReader(
|
|
|
|
new FileInputStream(file)));
|
1999-06-11 10:21:16 +04:00
|
|
|
|
|
|
|
String s = new String();
|
|
|
|
while((s = in.readLine())!= null)
|
|
|
|
{
|
|
|
|
s.trim();
|
|
|
|
/* Skip comments */
|
|
|
|
if ((s.length() == 0) || (s.charAt(0) == '#'))
|
|
|
|
continue;
|
|
|
|
if (s.startsWith("START")) {
|
|
|
|
// Check to see if this was in the requested URL
|
|
|
|
String filename = new String ("GET " + s.substring(6));
|
2000-06-28 02:10:27 +04:00
|
|
|
String otherfilename = new String("POST " + s.substring(6));
|
|
|
|
if (request.startsWith(filename) ||
|
|
|
|
request.startsWith(otherfilename))
|
1999-06-11 10:21:16 +04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else// Else skipto past the END
|
|
|
|
{
|
|
|
|
while (!s.startsWith("END"))
|
|
|
|
{
|
|
|
|
s = in.readLine();
|
|
|
|
if (s != null)
|
|
|
|
{
|
|
|
|
s.trim();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-04-08 03:14:52 +04:00
|
|
|
else if (s.startsWith("ECHO")) {
|
|
|
|
outDirty = true;
|
|
|
|
boolean parameter = false;
|
|
|
|
try {
|
|
|
|
String header = new String(s.substring(5));
|
|
|
|
String req= new String(con.request);
|
|
|
|
int t = req.indexOf(header);
|
|
|
|
if (t != -1) {
|
|
|
|
out.println(req.substring(
|
|
|
|
t, req.indexOf("\n", t)));
|
|
|
|
parameter = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
out.println("Error: " + header +
|
|
|
|
" not specified in request!");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (StringIndexOutOfBoundsException e) {}
|
|
|
|
if (!parameter)
|
|
|
|
out.println(con.request);
|
|
|
|
}
|
1999-06-11 10:21:16 +04:00
|
|
|
else if (s.startsWith("INCLUDE")) {
|
|
|
|
outDirty = true;
|
|
|
|
WriteOutFile("docs/" + s.substring(8));
|
|
|
|
}
|
|
|
|
else if (s.startsWith("CRLF")) {
|
|
|
|
outDirty = true;
|
|
|
|
out.println();
|
|
|
|
}
|
|
|
|
else if (s.startsWith("END")) {
|
|
|
|
// ignore should never have appeared here though!
|
|
|
|
continue;
|
|
|
|
}
|
1999-06-24 06:29:20 +04:00
|
|
|
else {// Not a recognizable line... just print it as is...
|
|
|
|
outDirty = true;
|
|
|
|
out.println(s);
|
|
|
|
}
|
|
|
|
|
1999-06-11 10:21:16 +04:00
|
|
|
}
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
if (outDirty)
|
|
|
|
{
|
|
|
|
out.flush();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
WriteDefaultResponse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String args[]) {
|
|
|
|
if (args.length >= 1) {
|
|
|
|
ScriptFile sf = new ScriptFile(args[0]);
|
|
|
|
/* Detect change stuff;
|
|
|
|
File f = new File(args[0]);
|
|
|
|
long lastMod = f.lastModified();
|
|
|
|
while (f.lastModified() == lastMod) {
|
|
|
|
}
|
|
|
|
sf.Parse();
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void WriteOutFile(String filename) throws IOException {
|
1999-10-13 14:24:12 +04:00
|
|
|
BufferedReader incl = new BufferedReader(
|
|
|
|
new InputStreamReader(
|
|
|
|
new FileInputStream(filename)));
|
1999-06-11 10:21:16 +04:00
|
|
|
// This doesn't have to be line wise... change later TODO
|
|
|
|
String s;
|
|
|
|
while ((s = incl.readLine()) != null)
|
|
|
|
{
|
|
|
|
out.println(s);
|
|
|
|
}
|
|
|
|
incl.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void WriteDefaultResponse() throws IOException {
|
1999-06-19 08:05:10 +04:00
|
|
|
WriteOutFile("docs/generic.res");
|
|
|
|
out.println("Date: " + (new Date()).toString());
|
|
|
|
if (file != null)
|
|
|
|
{
|
|
|
|
File f = new File(file);
|
|
|
|
out.println("Last-modified: " + (new Date(f.lastModified())).toString());
|
|
|
|
}
|
|
|
|
out.println("\n"); // prints 2
|
|
|
|
if (con != null)
|
|
|
|
{
|
|
|
|
out.println("<HTML><BODY>");
|
|
|
|
out.println("<H3>Rec'd. the following request-</H3>");
|
|
|
|
out.println("<PRE>");
|
|
|
|
out.println(con.request);
|
|
|
|
out.println("</PRE>");
|
|
|
|
out.println("From- " + con.client);
|
|
|
|
out.println("</BODY></HTML>");
|
|
|
|
}
|
1999-06-11 10:21:16 +04:00
|
|
|
}
|
|
|
|
|
1999-06-19 08:05:10 +04:00
|
|
|
protected void WriteSpecialResponse(String specialcase) throws IOException {
|
2000-04-13 23:20:54 +04:00
|
|
|
out.println("HTTP/1.1 200 OK");
|
|
|
|
out.println("Server: HTTP Test Server/1.1");
|
|
|
|
out.println("Content-Type: text/plain");
|
|
|
|
|
1999-06-19 08:05:10 +04:00
|
|
|
StringTokenizer st = new StringTokenizer(specialcase, " &");
|
|
|
|
while (st.hasMoreTokens()) {
|
|
|
|
String pair = st.nextToken();
|
2000-04-13 23:20:54 +04:00
|
|
|
if (pair.startsWith("Length=") || pair.startsWith("length="))
|
1999-06-19 08:05:10 +04:00
|
|
|
{
|
|
|
|
|
2000-04-13 23:20:54 +04:00
|
|
|
int len = Integer.valueOf(pair.substring(
|
|
|
|
pair.indexOf('=')+1), 10).intValue();
|
1999-06-19 08:05:10 +04:00
|
|
|
|
|
|
|
out.println("Date: " + (new Date()).toString());
|
|
|
|
out.println("Content-Length: " + len);
|
|
|
|
out.println("\n");
|
2000-04-13 23:20:54 +04:00
|
|
|
for (int i = 0; i<len; i++) {
|
|
|
|
out.print ((char)i%10);
|
|
|
|
if ((i>0) && ((i+1)%80) == 0)
|
1999-06-19 08:05:10 +04:00
|
|
|
out.print('\n');
|
|
|
|
out.flush();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
1999-06-11 10:21:16 +04:00
|
|
|
String file = null;
|
|
|
|
// The string associated with this script occurence
|
|
|
|
|
|
|
|
String request;
|
|
|
|
PrintWriter out;
|
|
|
|
Connection con;
|
|
|
|
}
|