[Zerofile-svn] SF.net SVN: zerofile: [75] trunk/src/HttpServer.java
Status: Pre-Alpha
Brought to you by:
karl-bengtsson
|
From: <kar...@us...> - 2007-12-04 16:26:44
|
Revision: 75
http://zerofile.svn.sourceforge.net/zerofile/?rev=75&view=rev
Author: karl-bengtsson
Date: 2007-12-04 08:26:31 -0800 (Tue, 04 Dec 2007)
Log Message:
-----------
blandade tester
Modified Paths:
--------------
trunk/src/HttpServer.java
Modified: trunk/src/HttpServer.java
===================================================================
--- trunk/src/HttpServer.java 2007-12-04 15:46:53 UTC (rev 74)
+++ trunk/src/HttpServer.java 2007-12-04 16:26:31 UTC (rev 75)
@@ -5,30 +5,74 @@
public class HttpServer {
private int _portNr;
- private File[] _files;
+ private List<File> _files;
private XMPPLinkLocalChatSession _session;
- private Boolean _running;
+ private Boolean _running = false;
+ private ServerSocket _serverSocket;
+ private Thread _httpListenerThread;
public void start()
{
- _running = true;
+ try
+ {
+ _serverSocket = new ServerSocket();
+ _portNr = _serverSocket.getLocalPort();
+ System.out.println("httpServer starting running on port " + _portNr);
+ _httpListenerThread = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ while(true)
+ {
+ Socket connection = _serverSocket.accept();
+ System.out.println("New connection accepted " + connection.getInetAddress() + ":" + connection.getPort());
+ try
+ {
+ httpRequestHandler request = new httpRequestHandler(connection);
+ // Create a new thread to process the request.
+ Thread thread = new Thread(request);
+
+ // Start the thread.
+ thread.start();
+ }
+ catch(Exception e)
+ {
+ System.out.println(e);
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ System.out.println(e);
+ }
+ }
+ };
+ }
+ catch (Exception e)
+ {
+ System.out.println(e);
+ }
}
+
public void stop()
{
+ _httpListenerThread.interrupt();
_running = false;
}
HttpServer(XMPPLinkLocalChatSession session)
{
_session = session;
- if (!_running)
- start();
}
public void addFile(File filen)
{
-
+ //_files.add(filen); TODO N\x8Ctt buggar h\x8Ar
+ if (!_running)
+ start();
}
public void removeFile(File filen)
{
-
+ _files.remove(filen);
}
public int getPort()
{
@@ -39,3 +83,145 @@
return _running;
}
}
+
+class httpRequestHandler implements Runnable
+{
+ final static String CRLF = "\r\n";
+ Socket socket;
+ InputStream input;
+ OutputStream output;
+ BufferedReader br;
+
+ // Constructor
+ public httpRequestHandler(Socket socket) throws Exception
+ {
+ this.socket = socket;
+ this.input = socket.getInputStream();
+ this.output = socket.getOutputStream();
+ this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ }
+
+ public void run()
+ {
+ try
+ {
+ processRequest();
+ }
+ catch(Exception e)
+ {
+ System.out.println(e);
+ }
+ }
+
+ private void processRequest() throws Exception
+ {
+ while(true)
+ {
+ String headerLine = br.readLine();
+ System.out.println(headerLine);
+ if(headerLine.equals(CRLF) || headerLine.equals("")) break;
+
+ StringTokenizer s = new StringTokenizer(headerLine);
+ String temp = s.nextToken();
+
+ if(temp.equals("GET"))
+ {
+ String fileName = s.nextToken();
+ fileName = "." + fileName ;
+
+ // Open the requested file.
+ FileInputStream fis = null ;
+ boolean fileExists = true ;
+ try
+ {
+ fis = new FileInputStream( fileName ) ;
+ }
+ catch ( FileNotFoundException e )
+ {
+ fileExists = false ;
+ }
+
+ // Construct the response message.
+ String serverLine = "Server: ZeroFile embedded http server";
+ String statusLine = null;
+ String contentTypeLine = null;
+ String entityBody = null;
+ String contentLengthLine = "error";
+ if ( fileExists )
+ {
+ statusLine = "HTTP/1.0 200 OK" + CRLF ;
+ contentTypeLine = "Content-type: " + contentType( fileName ) + CRLF ;
+ contentLengthLine = "Content-Length: "
+ + (new Integer(fis.available())).toString()
+ + CRLF;
+ }
+ else
+ {
+ statusLine = "HTTP/1.0 404 Not Found" + CRLF ;
+ contentTypeLine = "text/html" ;
+ entityBody = "<HTML>"
+ + "<HEAD><TITLE>404 Not Found</TITLE></HEAD>"
+ + "<BODY>404 Not Found"
+ + "<br>usage:http://www.snaip.com:4444/"
+ + "fileName.html</BODY></HTML>" ;
+ }
+
+ // Send the status line.
+ output.write(statusLine.getBytes());
+
+ // Send the server line.
+ output.write(serverLine.getBytes());
+
+ // Send the content type line.
+ output.write(contentTypeLine.getBytes());
+
+ // Send the Content-Length
+ output.write(contentLengthLine.getBytes());
+
+ // Send a blank line to indicate the end of the header lines.
+ output.write(CRLF.getBytes());
+
+ // Send the entity body.
+ if (fileExists)
+ {
+ sendBytes(fis, output) ;
+ fis.close();
+ }
+ else
+ {
+ output.write(entityBody.getBytes());
+ }
+ }
+ }
+
+ try
+ {
+ output.close();
+ br.close();
+ socket.close();
+ }
+ catch(Exception e) {}
+ }
+
+ private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception
+ {
+ // Construct a 1K buffer to hold bytes on their way to the socket.
+ byte[] buffer = new byte[1024] ;
+ int bytes = 0 ;
+
+ // Copy requested file into the socket's output stream.
+ while ((bytes = fis.read(buffer)) != -1 )
+ {
+ os.write(buffer, 0, bytes);
+ }
+ }
+
+ private static String contentType(String fileName)
+ {
+ if (fileName.endsWith(".htm") || fileName.endsWith(".html"))
+ {
+ return "text/html";
+ }
+ return "";
+ }
+}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|