Java HTTP server is down

I host a webpage from home. I created my own HTTP server using Java. This is SSCCE:

if(command.startsWith("GET")) { //client is a socket on which I reply. PrintWriter pw = new PrintWriter(client.getOutputStream(), true); String commule = command.split(" "); if(commule[0].equals("GET")) { if(commule[1].contains(".")) { File file = new File(GEQO_SERVER_ROOT + commule[1].substring(1).replaceAll("%20", " ")); if(file.exists()) { OutputStream out = client.getOutputStream(); InputStream stream = new FileInputStream(file); String response = new String(); response += "HTTP/1.1 200 OK\r\n"; response += "Date: Thu, 08 Aug 2013 08:49:37 GMT\r\n"; response += "Content-Type: text/html\r\n"; response += "Content-Length: " + file.length() + "\r\n"; response += "Connection: keep-alive\r\n"; response += "\r\n"; pw.write(response); //Assume I already initialized pw as a PrintWriter pw.flush(); copy(stream, out); stream.close(); out.close(); } else { pw.write("<html><h1>The request 404ed.</h1>"); pw.write("<body>The requested URL <b>" + commule[1] + "</b> could not be found on this server.</body></html>"); pw.flush(); } } else { BufferedReader br = new BufferedReader(new FileReader(GEQO_SERVER_ROOT + commule[1].substring(1) + "main.html")); String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { pw.print(sCurrentLine); } br.close(); } } else { pw.println("Unrecognized HTTP command."); } } 

This is the source of main.html:

<html>
<title>Geqo Server</title>
<body>Geqo server online and functioning!</body>
</html>

The problem is that when I try to access this page using Chrome, it displays correctly (at least when using 127.0.0.1). But when I tried to access it in Firefox at 127.0.0.1, it works, but just gives me the html source. IE also gives me the source. Can someone tell me why Firefox and IE only show the source, instead of parsing it?

I think this contains some tips (Firebug screenshot):

Firebug screenshot

My source seems to be in the <pre> . I don’t know why, but is it not such a problem?

I port is redirected. Here are the guys pages: http://110.172.170.83:17416/ (Sorry, Stackoverflow does not allow numeric links.)

EDIT: I found a problem. But before I explain, thanks to Bart for SSCCE, which I used to compare with my code. This is the problem: the if on the eighth line of if(commule[1].contains(".")) code to skip most of the code here. In this corresponding else block, there is not even any command to send headers. Thanks to artbristol for pointing this out.

Thanks in advance.

+7
java
source share
4 answers

Your print editor is not cleared (as Ernest pointed out), so HTTP headers are not sent. Look at the connection result directly - it simply returns raw data without headers.

 nc 110.172.170.83 17416 GET / <html><title>Geqo Server</title><body>Geqo server online and functioning!</body></html> 

Writing an HTTP server is hard work. If this is not an exercise, you should use a lightweight existing one, such as Jetty, or Sun's built-in HTTP server in the JDK.

Edit - A PrintWriter really not suitable for doing HTTP. It is designed to process linear data, such as a file written to disk. It also depends on platform-specific settings for encoding text and line endings. Check the HTTP specification for more details on how a proper HTTP server should work.

+5
source

It would seem that there are some potential problems with buffering. You write part of your output to the PrintWriter wrapper around out , and the other output directly to out . I would definitely add a call to pw.flush() after a call to pw.write() .

+1
source

You turned on autoFlush with the second argument

 new PrintWriter(client.getOutputStream(), true) 

http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html

Unlike the PrintStream class, if automatic flushing is enabled, this will be done only when one of the println, printf or format methods is called, and not every time a newline character is displayed. These methods use the platform's own line break concept, rather than a newline.

So basically your pw.write() did not merge with the output stream. So all you have to do is replace

 pw.write(response); 

from

 pw.println(response); 
+1
source

You are not sending a response header.

I can not find the definition of pw in your source code?

0
source

All Articles