2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.transport.http;
25
import java.io.BufferedInputStream;
26
import java.io.BufferedOutputStream;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.OutputStream;
30
import java.net.ServerSocket;
31
import java.net.Socket;
32
import java.util.ArrayList;
33
import java.util.Collections;
34
import java.util.Iterator;
35
import java.util.List;
38
* @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
40
public class RawHTTPServer
42
private ServerSocket socket;
44
private boolean started = false;
45
private List clients = Collections.synchronizedList(new ArrayList());
47
private static int clientCount = 0;
49
private static final int DEFAULT_PORT = 80;
50
private Listener listener;
52
public RawHTTPServer()
57
public RawHTTPServer(int port)
66
System.out.println("Starting raw http server.");
69
this.socket = new ServerSocket(this.port);
70
this.port = socket.getLocalPort();
72
this.listener = new Listener();
73
this.listener.start();
74
System.out.println("Server started.");
78
System.out.println("Server start failed.");
84
public synchronized void stop()
89
this.listener.interrupt();
92
this.listener.join(500);
97
Iterator iter = clients.iterator();
100
Client client = (Client) iter.next();
112
this.listener = null;
117
private final class Client extends Thread
122
String MIMEType = "text/html";
123
String content = "<HTML><BODY>This is test results page from RawHTTPServer.</BODY></HTML>";
124
String header = "HTTP 1.0 200 OK\r\n" +
125
"Server: RawHTTPServer\r\n" +
126
"Content-length: " + this.content.getBytes().length + "\r\n" +
127
"Content-type: " + MIMEType + "\r\n\rn\n";
130
Client(Socket socket)
133
super("RawHTTPServer-Client [" + (++clientCount) + "]");
135
this.socket = socket;
136
this.input = new BufferedInputStream(socket.getInputStream());
137
this.output = new BufferedOutputStream(socket.getOutputStream());
147
StringBuffer request = new StringBuffer(80);
150
int c = input.read();
151
if(c == '\r' || c == '\n' || c == -1)
155
request.append((char) c);
157
if(request.toString().indexOf("HTTP/") != -1)
159
output.write(this.header.getBytes());
161
output.write(this.content.getBytes());
178
e.printStackTrace(); //TODO: -TME Implement
187
byte[] buffer = new byte[1024];
188
while (n != -1 || input.available() > 0)
190
n = input.read(buffer);
191
System.out.println(new String(buffer));
194
catch (IOException e)
201
// output.write(new StringBuffer().append(200).toString().getBytes());
204
// catch (IOException e)
206
// e.printStackTrace(); //TODO: -TME Implement
210
clients.remove(this);
214
private final class Listener extends Thread
218
super("RawHTTPServer-Listener");
229
// blocks until a new client arrives
230
Socket client = socket.accept();
233
// make this a thread pool task
234
new Client(client).start();
241
ex.printStackTrace();
249
public static void main(String[] args)
251
int port = RawHTTPServer.DEFAULT_PORT;
252
if(args != null && args.length > 0)
254
port = Integer.parseInt(args[0]);
256
RawHTTPServer server = new RawHTTPServer(port);
b'\\ No newline at end of file'