mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-25 13:51:41 +00:00
Added the test server for testing anamalous responses from HTTP servers. For more details read http://lxr.mozilla.org/mozilla/source/netwerk/testserver/docs/help.html
This commit is contained in:
parent
6cfa4268fc
commit
a941eb6b56
98
netwerk/testserver/Connection.java
Normal file
98
netwerk/testserver/Connection.java
Normal file
@ -0,0 +1,98 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class Connection extends Thread {
|
||||
|
||||
public static final boolean DEBUG = true;
|
||||
public Socket client;
|
||||
protected BufferedReader in;
|
||||
protected PrintWriter out;
|
||||
|
||||
final static String SERVER = "HTTP Test Server/1.1";
|
||||
|
||||
public Connection(Socket client_socket) {
|
||||
client = client_socket;
|
||||
|
||||
if (DEBUG)
|
||||
{
|
||||
System.out.println(
|
||||
"---------------------------------------------------------");
|
||||
System.out.println(client);
|
||||
}
|
||||
try {
|
||||
in = new BufferedReader(new InputStreamReader(
|
||||
client.getInputStream()));
|
||||
out = new PrintWriter(client.getOutputStream());
|
||||
this.start();
|
||||
}
|
||||
catch (IOException e) {
|
||||
try {
|
||||
client.close();
|
||||
}
|
||||
catch (IOException another) {
|
||||
}
|
||||
System.out.println("Exception while getting socket streams."
|
||||
+ e);
|
||||
}
|
||||
}
|
||||
|
||||
final public void run() {
|
||||
|
||||
String line = null;
|
||||
String firstline = null;
|
||||
request = new StringBuffer();
|
||||
|
||||
int len = 0;
|
||||
try {
|
||||
while(true) {
|
||||
line = in.readLine();
|
||||
if (line == null)
|
||||
break;
|
||||
if (firstline == null)
|
||||
firstline = new String(line);
|
||||
len = line.length();
|
||||
if (len == 0)
|
||||
break;
|
||||
request.append(line);
|
||||
request.append("\n");
|
||||
if (DEBUG)
|
||||
System.out.println(line);
|
||||
}
|
||||
// This will appropriately write all the stuff based
|
||||
// on the first line and the script file
|
||||
ScriptFile sf = new ScriptFile(this, firstline, out);
|
||||
out.flush();
|
||||
// mark for garbage collect
|
||||
sf = null;
|
||||
} // try
|
||||
catch (IOException e) {
|
||||
System.out.println("Some problem while communicating. " + e);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
client.close();
|
||||
}
|
||||
catch (IOException e2) {
|
||||
}
|
||||
}
|
||||
}
|
||||
StringBuffer request;
|
||||
}
|
190
netwerk/testserver/ScriptFile.java
Normal file
190
netwerk/testserver/ScriptFile.java
Normal file
@ -0,0 +1,190 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Date;
|
||||
/*
|
||||
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 {
|
||||
|
||||
if ((request == null) || (request.length() == 0) || request.startsWith("GET / "))
|
||||
{
|
||||
WriteDefaultResponse();
|
||||
return;
|
||||
}
|
||||
|
||||
boolean outDirty = false;
|
||||
if (file != null)
|
||||
{
|
||||
DataInputStream in =
|
||||
new DataInputStream(
|
||||
new BufferedInputStream(
|
||||
new FileInputStream(file)));
|
||||
|
||||
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));
|
||||
if (request.startsWith(filename))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else// Else skipto past the END
|
||||
{
|
||||
while (!s.startsWith("END"))
|
||||
{
|
||||
s = in.readLine();
|
||||
if (s != null)
|
||||
{
|
||||
s.trim();
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
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 {
|
||||
DataInputStream incl =
|
||||
new DataInputStream(
|
||||
new BufferedInputStream(
|
||||
new FileInputStream(filename)));
|
||||
// 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 {
|
||||
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("<H3>Rec'd. the following request-</H3>");
|
||||
out.println("<PRE>");
|
||||
out.println(con.request);
|
||||
out.println("</PRE>");
|
||||
out.println("From- " + con.client);
|
||||
}
|
||||
}
|
||||
|
||||
String file = null;
|
||||
// The string associated with this script occurence
|
||||
|
||||
String request;
|
||||
PrintWriter out;
|
||||
Connection con;
|
||||
}
|
76
netwerk/testserver/TestServer.java
Normal file
76
netwerk/testserver/TestServer.java
Normal file
@ -0,0 +1,76 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class TestServer extends Thread {
|
||||
|
||||
final static int DEFAULT_PORT = 4321;
|
||||
|
||||
protected int port;
|
||||
protected ServerSocket listen_socket;
|
||||
|
||||
public static void fail(Exception e, String msg) {
|
||||
System.err.println(msg + ":" + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
public TestServer() {
|
||||
this(DEFAULT_PORT);
|
||||
}
|
||||
|
||||
public TestServer(int port) {
|
||||
this.port = port;
|
||||
try {
|
||||
listen_socket = new ServerSocket(port);
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail(e, "Exception creating server socket");
|
||||
}
|
||||
System.out.println("Server: listening on port " + port);
|
||||
this.start();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
Socket client_socket = listen_socket.accept();
|
||||
Connection c = new Connection(client_socket);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail(e, "Exception while listening");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
int port = DEFAULT_PORT;
|
||||
if (args.length == 1) {
|
||||
try {
|
||||
port = Integer.parseInt(args[0]);
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
port = DEFAULT_PORT;
|
||||
}
|
||||
}
|
||||
new TestServer(port);
|
||||
}
|
||||
|
||||
}
|
||||
|
6
netwerk/testserver/docs/bar.html
Normal file
6
netwerk/testserver/docs/bar.html
Normal file
@ -0,0 +1,6 @@
|
||||
<HTML>
|
||||
<BODY>
|
||||
<BR>--------<BR>
|
||||
This is bar.html
|
||||
</BODY>
|
||||
</HTML>
|
5
netwerk/testserver/docs/foo.html
Normal file
5
netwerk/testserver/docs/foo.html
Normal file
@ -0,0 +1,5 @@
|
||||
<HTML>
|
||||
<BODY>
|
||||
This is foo.html
|
||||
</BODY>
|
||||
</HTML>
|
2
netwerk/testserver/docs/generic.res
Normal file
2
netwerk/testserver/docs/generic.res
Normal file
@ -0,0 +1,2 @@
|
||||
HTTP/1.1 200 OK
|
||||
Server: HTTP Test Server/1.1
|
58
netwerk/testserver/docs/help.html
Normal file
58
netwerk/testserver/docs/help.html
Normal file
@ -0,0 +1,58 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Help file for HTTP TestServer/1.1</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h3> Help file for HTTP TestServer/1.1</h3>
|
||||
The HTTP TestServer is a simple server written to test some special HTTP
|
||||
behaviour. It is based on a script file (docs/urlmap) and can be easily
|
||||
extended to add special response situations. <P>
|
||||
|
||||
To compile this you will need any version of JDK >1.1.2. Just compile them
|
||||
with command- <PRE>javac *.java</PRE>
|
||||
And then run the server with command- <PRE>java TestServer [port]</PRE>
|
||||
The port is optional and if not specified defaults to 4321.
|
||||
Assuming that the server is running on http://foo:4321/
|
||||
Use the following URLs to retrieve special responses. In order to create
|
||||
your own special case- telnet to foo, and edit the docs/urlmap file to
|
||||
handle special cases and then add info here about it.
|
||||
<P>
|
||||
<center><table WIDTH="80%" >
|
||||
<tr>
|
||||
<td><a href="/help">http://foo:4321/help</a></td>
|
||||
<td>This help file.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><a href="/">http://foo:4321/</a></td>
|
||||
<td>Default echo of the server. Prints the request out.</td>
|
||||
</tr>
|
||||
</table></center>
|
||||
The subsequent ones may be combined to generate multiple results. NOTE: This
|
||||
hasn't been fixed as yet. So it wont work for now. But if it did...
|
||||
You can try something fancy like- http://foo:4321/multi&close&both
|
||||
|
||||
<center><table WIDTH="80%" >
|
||||
|
||||
<tr>
|
||||
<td><a href="/multi">http://foo:4321/multi</a></td>
|
||||
<td>Returns a multipart message. </td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><a href="/close">http://foo:4321/close</a></td>
|
||||
<td>Returns "Connection: close" in a response header.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><a href="/both">http://foo:4321/both</a></td>
|
||||
<td>Returns the request as well as the response headers.</td>
|
||||
</tr>
|
||||
</table></center>
|
||||
|
||||
<p>If you need any more help with running this server, or have suggestions
|
||||
to improve it let <a href="mailto:gagan@netscape.com?subject=HTTP TestServer/1.1">me</a>
|
||||
know.
|
||||
</body>
|
||||
</html>
|
97
netwerk/testserver/docs/urlmap
Normal file
97
netwerk/testserver/docs/urlmap
Normal file
@ -0,0 +1,97 @@
|
||||
#
|
||||
# Foo- Copy/Paste these 9 lines to generate new cases.
|
||||
#
|
||||
START /foo
|
||||
INCLUDE generic.res
|
||||
Content-Type: text/html
|
||||
CRLF
|
||||
INCLUDE foo.html
|
||||
END
|
||||
|
||||
#
|
||||
# This is a simple multipart message example
|
||||
# for more complicated stuff try "complex"
|
||||
#
|
||||
START /multi
|
||||
INCLUDE generic.res
|
||||
Content-Type: multipart/mixed; boundary=ComfortablyNumb
|
||||
CRLF
|
||||
--ComfortablyNumb
|
||||
INCLUDE foo.html
|
||||
CRLF
|
||||
--ComfortablyNumb
|
||||
INCLUDE bar.html
|
||||
--ComfortablyNumb--
|
||||
END
|
||||
|
||||
#
|
||||
# Send the help file to see how this server is used.
|
||||
#
|
||||
START /help
|
||||
INCLUDE generic.res
|
||||
CRLF
|
||||
INCLUDE help.html
|
||||
END
|
||||
|
||||
#
|
||||
# A more complex variation of multipart messaging.
|
||||
# If this works, every code contributor to Necko
|
||||
# gets a treat from me personally :) -Gagan
|
||||
#
|
||||
START /complex
|
||||
INCLUDE generic.res
|
||||
Content-Type: multipart/mixed; boundary=TheWallFromPinkFloyd
|
||||
CRLF
|
||||
Preamble to multipart messages. Only clients that dont handle
|
||||
multipart would see this!
|
||||
CRLF
|
||||
--TheWallFromPinkFloyd
|
||||
Content-Type: text/plain
|
||||
CRLF
|
||||
The Thin Ice
|
||||
Another Brick In The Wall-I
|
||||
CRLF
|
||||
--TheWallFromPinkFloyd
|
||||
Content-Type: multipart/parallel; boundary=SideTwoOfTheWall
|
||||
CRLF
|
||||
--SideTwoOfTheWall
|
||||
Content-Type: text/plain
|
||||
CRLF
|
||||
Young Lust
|
||||
Goodbye Cruel World
|
||||
CRLF
|
||||
--SideTwoOfTheWall
|
||||
Content-Type: text/plain
|
||||
CRLF
|
||||
Another Brick In The Wall-II
|
||||
--SideTwoOfTheWall--
|
||||
CRLF
|
||||
--TheWallFromPinkFloyd
|
||||
Content-Type: text/plain
|
||||
CRLF
|
||||
Another Brick In The Wall-III
|
||||
--TheWallFromPinkFloyd--
|
||||
CRLF
|
||||
END
|
||||
|
||||
#
|
||||
# Pragma: no-cache test
|
||||
#
|
||||
START /pragma
|
||||
INCLUDE generic.res
|
||||
Pragma: no-cache
|
||||
Content-Type: text/html
|
||||
CRLF
|
||||
INCLUDE foo.html
|
||||
END
|
||||
|
||||
#
|
||||
# close: return a connection: close header
|
||||
#
|
||||
START /close
|
||||
INCLUDE generic.res
|
||||
Connection: Close
|
||||
Content-Type: text/html
|
||||
CRLF
|
||||
INCLUDE foo.html
|
||||
END
|
98
tools/testserver/Connection.java
Normal file
98
tools/testserver/Connection.java
Normal file
@ -0,0 +1,98 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class Connection extends Thread {
|
||||
|
||||
public static final boolean DEBUG = true;
|
||||
public Socket client;
|
||||
protected BufferedReader in;
|
||||
protected PrintWriter out;
|
||||
|
||||
final static String SERVER = "HTTP Test Server/1.1";
|
||||
|
||||
public Connection(Socket client_socket) {
|
||||
client = client_socket;
|
||||
|
||||
if (DEBUG)
|
||||
{
|
||||
System.out.println(
|
||||
"---------------------------------------------------------");
|
||||
System.out.println(client);
|
||||
}
|
||||
try {
|
||||
in = new BufferedReader(new InputStreamReader(
|
||||
client.getInputStream()));
|
||||
out = new PrintWriter(client.getOutputStream());
|
||||
this.start();
|
||||
}
|
||||
catch (IOException e) {
|
||||
try {
|
||||
client.close();
|
||||
}
|
||||
catch (IOException another) {
|
||||
}
|
||||
System.out.println("Exception while getting socket streams."
|
||||
+ e);
|
||||
}
|
||||
}
|
||||
|
||||
final public void run() {
|
||||
|
||||
String line = null;
|
||||
String firstline = null;
|
||||
request = new StringBuffer();
|
||||
|
||||
int len = 0;
|
||||
try {
|
||||
while(true) {
|
||||
line = in.readLine();
|
||||
if (line == null)
|
||||
break;
|
||||
if (firstline == null)
|
||||
firstline = new String(line);
|
||||
len = line.length();
|
||||
if (len == 0)
|
||||
break;
|
||||
request.append(line);
|
||||
request.append("\n");
|
||||
if (DEBUG)
|
||||
System.out.println(line);
|
||||
}
|
||||
// This will appropriately write all the stuff based
|
||||
// on the first line and the script file
|
||||
ScriptFile sf = new ScriptFile(this, firstline, out);
|
||||
out.flush();
|
||||
// mark for garbage collect
|
||||
sf = null;
|
||||
} // try
|
||||
catch (IOException e) {
|
||||
System.out.println("Some problem while communicating. " + e);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
client.close();
|
||||
}
|
||||
catch (IOException e2) {
|
||||
}
|
||||
}
|
||||
}
|
||||
StringBuffer request;
|
||||
}
|
190
tools/testserver/ScriptFile.java
Normal file
190
tools/testserver/ScriptFile.java
Normal file
@ -0,0 +1,190 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Date;
|
||||
/*
|
||||
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 {
|
||||
|
||||
if ((request == null) || (request.length() == 0) || request.startsWith("GET / "))
|
||||
{
|
||||
WriteDefaultResponse();
|
||||
return;
|
||||
}
|
||||
|
||||
boolean outDirty = false;
|
||||
if (file != null)
|
||||
{
|
||||
DataInputStream in =
|
||||
new DataInputStream(
|
||||
new BufferedInputStream(
|
||||
new FileInputStream(file)));
|
||||
|
||||
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));
|
||||
if (request.startsWith(filename))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else// Else skipto past the END
|
||||
{
|
||||
while (!s.startsWith("END"))
|
||||
{
|
||||
s = in.readLine();
|
||||
if (s != null)
|
||||
{
|
||||
s.trim();
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
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 {
|
||||
DataInputStream incl =
|
||||
new DataInputStream(
|
||||
new BufferedInputStream(
|
||||
new FileInputStream(filename)));
|
||||
// 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 {
|
||||
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("<H3>Rec'd. the following request-</H3>");
|
||||
out.println("<PRE>");
|
||||
out.println(con.request);
|
||||
out.println("</PRE>");
|
||||
out.println("From- " + con.client);
|
||||
}
|
||||
}
|
||||
|
||||
String file = null;
|
||||
// The string associated with this script occurence
|
||||
|
||||
String request;
|
||||
PrintWriter out;
|
||||
Connection con;
|
||||
}
|
76
tools/testserver/TestServer.java
Normal file
76
tools/testserver/TestServer.java
Normal file
@ -0,0 +1,76 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class TestServer extends Thread {
|
||||
|
||||
final static int DEFAULT_PORT = 4321;
|
||||
|
||||
protected int port;
|
||||
protected ServerSocket listen_socket;
|
||||
|
||||
public static void fail(Exception e, String msg) {
|
||||
System.err.println(msg + ":" + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
public TestServer() {
|
||||
this(DEFAULT_PORT);
|
||||
}
|
||||
|
||||
public TestServer(int port) {
|
||||
this.port = port;
|
||||
try {
|
||||
listen_socket = new ServerSocket(port);
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail(e, "Exception creating server socket");
|
||||
}
|
||||
System.out.println("Server: listening on port " + port);
|
||||
this.start();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
Socket client_socket = listen_socket.accept();
|
||||
Connection c = new Connection(client_socket);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail(e, "Exception while listening");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
int port = DEFAULT_PORT;
|
||||
if (args.length == 1) {
|
||||
try {
|
||||
port = Integer.parseInt(args[0]);
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
port = DEFAULT_PORT;
|
||||
}
|
||||
}
|
||||
new TestServer(port);
|
||||
}
|
||||
|
||||
}
|
||||
|
6
tools/testserver/docs/bar.html
Normal file
6
tools/testserver/docs/bar.html
Normal file
@ -0,0 +1,6 @@
|
||||
<HTML>
|
||||
<BODY>
|
||||
<BR>--------<BR>
|
||||
This is bar.html
|
||||
</BODY>
|
||||
</HTML>
|
5
tools/testserver/docs/foo.html
Normal file
5
tools/testserver/docs/foo.html
Normal file
@ -0,0 +1,5 @@
|
||||
<HTML>
|
||||
<BODY>
|
||||
This is foo.html
|
||||
</BODY>
|
||||
</HTML>
|
2
tools/testserver/docs/generic.res
Normal file
2
tools/testserver/docs/generic.res
Normal file
@ -0,0 +1,2 @@
|
||||
HTTP/1.1 200 OK
|
||||
Server: HTTP Test Server/1.1
|
58
tools/testserver/docs/help.html
Normal file
58
tools/testserver/docs/help.html
Normal file
@ -0,0 +1,58 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Help file for HTTP TestServer/1.1</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h3> Help file for HTTP TestServer/1.1</h3>
|
||||
The HTTP TestServer is a simple server written to test some special HTTP
|
||||
behaviour. It is based on a script file (docs/urlmap) and can be easily
|
||||
extended to add special response situations. <P>
|
||||
|
||||
To compile this you will need any version of JDK >1.1.2. Just compile them
|
||||
with command- <PRE>javac *.java</PRE>
|
||||
And then run the server with command- <PRE>java TestServer [port]</PRE>
|
||||
The port is optional and if not specified defaults to 4321.
|
||||
Assuming that the server is running on http://foo:4321/
|
||||
Use the following URLs to retrieve special responses. In order to create
|
||||
your own special case- telnet to foo, and edit the docs/urlmap file to
|
||||
handle special cases and then add info here about it.
|
||||
<P>
|
||||
<center><table WIDTH="80%" >
|
||||
<tr>
|
||||
<td><a href="/help">http://foo:4321/help</a></td>
|
||||
<td>This help file.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><a href="/">http://foo:4321/</a></td>
|
||||
<td>Default echo of the server. Prints the request out.</td>
|
||||
</tr>
|
||||
</table></center>
|
||||
The subsequent ones may be combined to generate multiple results. NOTE: This
|
||||
hasn't been fixed as yet. So it wont work for now. But if it did...
|
||||
You can try something fancy like- http://foo:4321/multi&close&both
|
||||
|
||||
<center><table WIDTH="80%" >
|
||||
|
||||
<tr>
|
||||
<td><a href="/multi">http://foo:4321/multi</a></td>
|
||||
<td>Returns a multipart message. </td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><a href="/close">http://foo:4321/close</a></td>
|
||||
<td>Returns "Connection: close" in a response header.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><a href="/both">http://foo:4321/both</a></td>
|
||||
<td>Returns the request as well as the response headers.</td>
|
||||
</tr>
|
||||
</table></center>
|
||||
|
||||
<p>If you need any more help with running this server, or have suggestions
|
||||
to improve it let <a href="mailto:gagan@netscape.com?subject=HTTP TestServer/1.1">me</a>
|
||||
know.
|
||||
</body>
|
||||
</html>
|
97
tools/testserver/docs/urlmap
Normal file
97
tools/testserver/docs/urlmap
Normal file
@ -0,0 +1,97 @@
|
||||
#
|
||||
# Foo- Copy/Paste these 9 lines to generate new cases.
|
||||
#
|
||||
START /foo
|
||||
INCLUDE generic.res
|
||||
Content-Type: text/html
|
||||
CRLF
|
||||
INCLUDE foo.html
|
||||
END
|
||||
|
||||
#
|
||||
# This is a simple multipart message example
|
||||
# for more complicated stuff try "complex"
|
||||
#
|
||||
START /multi
|
||||
INCLUDE generic.res
|
||||
Content-Type: multipart/mixed; boundary=ComfortablyNumb
|
||||
CRLF
|
||||
--ComfortablyNumb
|
||||
INCLUDE foo.html
|
||||
CRLF
|
||||
--ComfortablyNumb
|
||||
INCLUDE bar.html
|
||||
--ComfortablyNumb--
|
||||
END
|
||||
|
||||
#
|
||||
# Send the help file to see how this server is used.
|
||||
#
|
||||
START /help
|
||||
INCLUDE generic.res
|
||||
CRLF
|
||||
INCLUDE help.html
|
||||
END
|
||||
|
||||
#
|
||||
# A more complex variation of multipart messaging.
|
||||
# If this works, every code contributor to Necko
|
||||
# gets a treat from me personally :) -Gagan
|
||||
#
|
||||
START /complex
|
||||
INCLUDE generic.res
|
||||
Content-Type: multipart/mixed; boundary=TheWallFromPinkFloyd
|
||||
CRLF
|
||||
Preamble to multipart messages. Only clients that dont handle
|
||||
multipart would see this!
|
||||
CRLF
|
||||
--TheWallFromPinkFloyd
|
||||
Content-Type: text/plain
|
||||
CRLF
|
||||
The Thin Ice
|
||||
Another Brick In The Wall-I
|
||||
CRLF
|
||||
--TheWallFromPinkFloyd
|
||||
Content-Type: multipart/parallel; boundary=SideTwoOfTheWall
|
||||
CRLF
|
||||
--SideTwoOfTheWall
|
||||
Content-Type: text/plain
|
||||
CRLF
|
||||
Young Lust
|
||||
Goodbye Cruel World
|
||||
CRLF
|
||||
--SideTwoOfTheWall
|
||||
Content-Type: text/plain
|
||||
CRLF
|
||||
Another Brick In The Wall-II
|
||||
--SideTwoOfTheWall--
|
||||
CRLF
|
||||
--TheWallFromPinkFloyd
|
||||
Content-Type: text/plain
|
||||
CRLF
|
||||
Another Brick In The Wall-III
|
||||
--TheWallFromPinkFloyd--
|
||||
CRLF
|
||||
END
|
||||
|
||||
#
|
||||
# Pragma: no-cache test
|
||||
#
|
||||
START /pragma
|
||||
INCLUDE generic.res
|
||||
Pragma: no-cache
|
||||
Content-Type: text/html
|
||||
CRLF
|
||||
INCLUDE foo.html
|
||||
END
|
||||
|
||||
#
|
||||
# close: return a connection: close header
|
||||
#
|
||||
START /close
|
||||
INCLUDE generic.res
|
||||
Connection: Close
|
||||
Content-Type: text/html
|
||||
CRLF
|
||||
INCLUDE foo.html
|
||||
END
|
Loading…
Reference in New Issue
Block a user