1 7 package org.jboss.test.remoting.transport.http; 8 9 import java.io.BufferedInputStream ; 10 import java.io.BufferedOutputStream ; 11 import java.io.ByteArrayOutputStream ; 12 import java.io.FileInputStream ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.OutputStream ; 16 import java.io.UnsupportedEncodingException ; 17 import java.net.ServerSocket ; 18 import java.net.Socket ; 19 import java.util.Timer ; 20 import java.util.TimerTask ; 21 22 25 public class SimpleHTTPServer extends Thread 26 { 27 private byte[] content; 28 private byte[] header; 29 private int port = 80; 30 private static boolean timeout = false; 31 32 public SimpleHTTPServer(String data, String encoding, String MIMEType, int port) 33 throws UnsupportedEncodingException 34 { 35 this(data.getBytes(encoding), encoding, MIMEType, port); 36 } 37 38 public SimpleHTTPServer(byte[] data, String encoding, String MIMEType, int port) 39 throws UnsupportedEncodingException 40 { 41 this.content = data; 42 this.port = port; 43 String header = "HTTP/1.1 200 OK\r\n" + 44 "Server: SimpleHTTPServer 1.0\r\n" + 45 "Content-length: " + this.content.length + "\r\n" + 46 "Content-type: " + MIMEType + "\r\n\r\n"; 47 this.header = header.getBytes("ASCII"); 48 } 49 50 51 protected void startTimer(Thread currentThread) 52 { 53 Timer timer = new Timer (false); 54 timer.schedule(new TimeoutTimerTask(currentThread), 3000); 55 } 56 57 public void run() 58 { 59 try 60 { 61 ServerSocket server = new ServerSocket (this.port); 62 System.out.println("Accepting connections on port " + server.getLocalPort()); 63 System.out.println("Data to be sent: "); 64 System.out.write(this.content); 65 66 while(true) 67 { 68 Socket connection = null; 69 try 70 { 71 connection = server.accept(); 72 OutputStream out = new BufferedOutputStream (connection.getOutputStream()); 73 InputStream in = new BufferedInputStream (connection.getInputStream()); 74 75 77 StringBuffer request = new StringBuffer (80); 78 try 79 { 80 int c = in.read(); 81 while(c != -1 && !timeout) 82 { 83 int q = 0; 84 if(request.length() > 0) 85 { 86 q = request.charAt(request.length() - 1); 87 } 88 request.append((char) c); 89 int n = in.read(); 91 92 if(c == '\r' && n == '\n') 93 { 94 if(q == '\n') 95 { 96 break; 97 } 98 else 99 { 100 c = n; 101 } 102 } 103 else 104 { 105 c = n; 106 } 107 } 109 } 110 catch(Exception e) 111 { 112 e.printStackTrace(); 113 } 114 System.out.println("\n\nHTTP Request:\n\n" + request + "\n\n"); 115 116 StringBuffer requestHeader = new StringBuffer (80); 117 for(int x = 0; x < request.length(); x++) 118 { 119 char cr = request.charAt(x); 120 if(cr == '\r' || cr == '\n' || cr == -1) 121 { 122 break; 123 } 124 requestHeader.append(cr); 125 } 126 if(requestHeader.toString().indexOf("HTTP/") != -1) 127 { 128 out.write(this.header); 129 } 130 out.write(this.content); 131 out.flush(); 132 } 133 catch(IOException e) 134 { 135 } 136 finally 137 { 138 if(connection != null) 139 { 140 connection.close(); 141 } 142 } 143 } 144 } 145 catch(IOException e) 146 { 147 System.err.println("Could not start server. Port " + this.port + " occupied."); 148 } 149 150 } 151 152 public class TimeoutTimerTask extends TimerTask 153 { 154 private Thread curThread; 155 156 public TimeoutTimerTask(Thread current) 157 { 158 this.curThread = current; 159 } 160 161 public void run() 162 { 163 timeout = true; 164 curThread.interrupt(); 165 } 166 } 167 168 public static void main(String [] args) 169 { 170 try 171 { 172 String contentType = "text/plain"; 173 if(args[0].endsWith(".html") || args[0].endsWith(".htm")) 174 { 175 contentType = "text/html"; 176 } 177 178 InputStream in = new FileInputStream (args[0]); 179 ByteArrayOutputStream out = new ByteArrayOutputStream (); 180 int b; 181 while((b = in.read()) != -1) 182 { 183 out.write(b); 184 } 185 byte[] data = out.toByteArray(); 186 187 int port; 188 try 189 { 190 port = Integer.parseInt(args[1]); 191 if(port < 1 || port > 65535) 192 { 193 port = 80; 194 } 195 } 196 catch(Exception e) 197 { 198 port = 80; 199 } 200 201 String encoding = "ASCII"; 202 if(args.length >= 2) 203 { 204 encoding = args[2]; 205 } 206 Thread t = new SimpleHTTPServer(data, encoding, contentType, port); 207 t.start(); 208 } 209 catch(ArrayIndexOutOfBoundsException e) 210 { 211 System.out.println("Usage: java SimpleHTTPServer filename port encoding"); 212 } 213 catch(Exception e) 214 { 215 System.err.println(e); 216 } 217 } 218 } | Popular Tags |