001: import java.net.*;
002: import java.io.*;
003: import java.util.*;
004:
005: public class SingleFileHTTPServer extends Thread {
006:
007: private byte[] content;
008: private byte[] header;
009: private int port = 80;
010:
011: public SingleFileHTTPServer(String data, String encoding,
012: String MIMEType, int port) throws UnsupportedEncodingException {
013: this(data.getBytes(encoding), encoding, MIMEType, port);
014: }
015:
016: public SingleFileHTTPServer(byte[] data, String encoding,
017: String MIMEType, int port) throws UnsupportedEncodingException {
018:
019: this.content = data;
020: this.port = port;
021: String header = "HTTP/1.0 200 OK\r\n"
022: + "Server: OneFile 1.0\r\n"
023: + "Content-Length: " + this.content.length + "\r\n"
024: + "Content-Type: " + MIMEType + "\r\n\r\n";
025: this.header = header.getBytes("ASCII");
026:
027: }
028:
029:
030: public void run() {
031:
032: try {
033: ServerSocket server = new ServerSocket(this.port);
034: System.out.println("Accepting connections on port "
035: + server.getLocalPort());
036: System.out.println("Data to be sent:");
037: System.out.write(this.content);
038: while (true) {
039:
040: Socket connection = null;
041: try {
042: connection = server.accept();
043: OutputStream out = new BufferedOutputStream(
044: connection.getOutputStream()
045: );
046: InputStream in = new BufferedInputStream(
047: connection.getInputStream()
048: );
049: // read the first line only; that's all we need
050: StringBuffer request = new StringBuffer(80);
051: while (true) {
052: int c = in.read();
053: if (c == '\r' || c == '\n' || c == -1) break;
054: request.append((char) c);
055: // If this is HTTP/1.0 or later send a MIME header
056:
057: }
058: if (request.toString().indexOf("HTTP/") != -1) {
059: out.write(this.header);
060: }
061: out.write(this.content);
062: out.flush();
063: } // end try
064: catch (IOException e) {
065: }
066: finally {
067: if (connection != null) connection.close();
068: }
069:
070: } // end while
071: } // end try
072: catch (IOException e) {
073: System.err.println("Could not start server. Port Occupied");
074: }
075:
076: } // end run
077:
078:
079: public static void main(String[] args) {
080:
081: try {
082:
083: String contentType = "text/plain";
084: if (args[0].endsWith(".html") || args[0].endsWith(".htm")) {
085: contentType = "text/html";
086: }
087:
088: InputStream in = new FileInputStream(args[0]);
089: ByteArrayOutputStream out = new ByteArrayOutputStream();
090: int b;
091: while ((b = in.read()) != -1) out.write(b);
092: byte[] data = out.toByteArray();
093:
094: // set the port to listen on
095: int port;
096: try {
097: port = Integer.parseInt(args[1]);
098: if (port < 1 || port > 65535) port = 80;
099: }
100: catch (Exception e) {
101: port = 80;
102: }
103:
104: String encoding = "ASCII";
105: if (args.length >= 2) encoding = args[2];
106:
107: Thread t = new SingleFileHTTPServer(data, encoding,
108: contentType, port);
109: t.start();
110:
111: }
112: catch (ArrayIndexOutOfBoundsException e) {
113: System.out.println(
114: "Usage: java SingleFileHTTPServer filename port encoding");
115: }
116: catch (Exception e) {
117: System.err.println(e);
118: }
119:
120: }
121:
122: }