Thread: [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.
|
|
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.
|
|
From: <kar...@us...> - 2007-12-13 15:06:32
|
Revision: 86
http://zerofile.svn.sourceforge.net/zerofile/?rev=86&view=rev
Author: karl-bengtsson
Date: 2007-12-13 07:06:29 -0800 (Thu, 13 Dec 2007)
Log Message:
-----------
Updated status messages for file transfer
Modified Paths:
--------------
trunk/src/HttpServer.java
Modified: trunk/src/HttpServer.java
===================================================================
--- trunk/src/HttpServer.java 2007-12-13 14:51:29 UTC (rev 85)
+++ trunk/src/HttpServer.java 2007-12-13 15:06:29 UTC (rev 86)
@@ -109,9 +109,9 @@
}
return null;
}
- public void printDoneMsg(String fileName)
+ public void printStatusMsg(String msg)
{
- _session.printTextToChatWindow("File \"" + fileName + "\" transferred successfully!");
+ _session.printTextToChatWindow(msg);
}
}
@@ -214,10 +214,11 @@
// Send the entity body.
if (fileExists)
{
+ _httpServer.printStatusMsg("Sending \"" + requestedFile.getName() + "\"...");
sendBytes(fis, output) ;
fis.close();
_httpServer.removeFile(requestedFile);
- _httpServer.printDoneMsg(requestedFile.getName());
+ _httpServer.printStatusMsg("File \"" + requestedFile.getName() + "\" transferred successfully!");
}
else
{
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|