[Zerofile-svn] SF.net SVN: zerofile: [76] trunk/src/HttpServer.java
Status: Pre-Alpha
Brought to you by:
karl-bengtsson
|
From: <kar...@us...> - 2007-12-05 07:25:06
|
Revision: 76
http://zerofile.svn.sourceforge.net/zerofile/?rev=76&view=rev
Author: karl-bengtsson
Date: 2007-12-04 23:25:07 -0800 (Tue, 04 Dec 2007)
Log Message:
-----------
The embedded HTTP server now returns a working 404 error when any file is requested. The server actually works and respects HTTP 1.0 (sort of). We now need to make it serve the files we've elected to share.
Modified Paths:
--------------
trunk/src/HttpServer.java
Modified: trunk/src/HttpServer.java
===================================================================
--- trunk/src/HttpServer.java 2007-12-04 16:26:31 UTC (rev 75)
+++ trunk/src/HttpServer.java 2007-12-05 07:25:07 UTC (rev 76)
@@ -9,46 +9,47 @@
private XMPPLinkLocalChatSession _session;
private Boolean _running = false;
private ServerSocket _serverSocket;
- private Thread _httpListenerThread;
- public void start()
+ private Thread _httpListenerThread = new Thread()
{
- try
+ public void run()
{
- _serverSocket = new ServerSocket();
- _portNr = _serverSocket.getLocalPort();
- System.out.println("httpServer starting running on port " + _portNr);
- _httpListenerThread = new Thread()
- {
- public void run()
+ try
{
- try
+ while(true)
{
- while(true)
+ Socket connection = _serverSocket.accept();
+ System.out.println("New connection accepted " + connection.getInetAddress() + ":" + connection.getPort());
+ try
{
- 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);
+ 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);
- }
+ // 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 start()
+ {
+ try
+ {
+ _serverSocket = new ServerSocket(0);
+ _portNr = _serverSocket.getLocalPort();
+ System.out.println("httpServer starting running on port " + _portNr);
+ _httpListenerThread.start();
+ }
catch (Exception e)
{
System.out.println(e);
@@ -115,7 +116,7 @@
private void processRequest() throws Exception
{
- while(true)
+ while (true)
{
String headerLine = br.readLine();
System.out.println(headerLine);
@@ -142,13 +143,14 @@
}
// Construct the response message.
- String serverLine = "Server: ZeroFile embedded http server";
+ String serverLine = "Server: ZeroFile embedded http server/1.0" + CRLF;
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
String contentLengthLine = "error";
if ( fileExists )
{
+ System.out.println("hittade fil");
statusLine = "HTTP/1.0 200 OK" + CRLF ;
contentTypeLine = "Content-type: " + contentType( fileName ) + CRLF ;
contentLengthLine = "Content-Length: "
@@ -157,13 +159,13 @@
}
else
{
+ System.out.println("hittade inte fil");
statusLine = "HTTP/1.0 404 Not Found" + CRLF ;
- contentTypeLine = "text/html" ;
+ contentTypeLine = "Content-Type: text/html" + CRLF ;
entityBody = "<HTML>"
+ "<HEAD><TITLE>404 Not Found</TITLE></HEAD>"
- + "<BODY>404 Not Found"
- + "<br>usage:http://www.snaip.com:4444/"
- + "fileName.html</BODY></HTML>" ;
+ + "<BODY>404 Not Found</BODY></HTML>" ;
+ contentLengthLine = "Content-Length: " + entityBody.length() + CRLF;
}
// Send the status line.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|