Hey guys,
after the OneShotHttpServer was running, I wanted to try out the extended version. Somehow theres a problem again. This time when I want to compile the .java in the command shell it says:
Ok. So I found out how to compile with Xlint and it says:Note: SimpleHTTpd2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
So.. theres something "wrong" with this method here in line 109?SimpleHttpd2.java:109: warning: [unchecked] unchecked call to put(K,V) as a member of the raw type Hashtable
myHeaders.put(key, value);
where K,V are type-variables:
K extends Object declared in class Hashtable
V extends Object declared in class Hashtable
1 warning
protected void setHeader(String key, String value){ myHeaders.put(key, value); }
Here's the code for the whole programme:
package httpServer; import java.io.*; import java.net.*; import java.util.*; public class SimpleHttpd2 extends Thread { protected Socket s = null; protected static File docRoot; protected static String canonicalDocRoot; public final static int HTTP_PORT = 8080; public final static String CRLF = "\r\n"; public final static String PROTOCOL = "HTTP/1.0 "; public final static String SC_OK = "200 OK"; public final static String SC_BAD_REQUEST = "400 Bad Request"; public final static String SC_FORBIDDEN = "403 Forbidden"; public final static String SC_NOT_FOUND = "404 Not Found"; protected static Properties typeMap = new Properties(); protected String statusCode = SC_OK; protected Hashtable myHeaders = new Hashtable(); public static void main(String[] args) { try { typeMap.load(new FileInputStream("index.html")); docRoot = new File ("."); canonicalDocRoot = docRoot.getCanonicalPath(); ServerSocket listen = new ServerSocket(HTTP_PORT); while(true){ SimpleHttpd2 aRequest = new SimpleHttpd2(listen.accept()); } } catch(IOException e) { System.err.println("Fehler: " + e.toString()); } } public SimpleHttpd2(Socket s) { this.s = s; start(); } public void run() { try { setHeader("Server", "SimpleHttpd2"); BufferedReader is = new BufferedReader(new InputStreamReader(s.getInputStream())); DataOutputStream os = new DataOutputStream(s.getOutputStream()); String request = is.readLine(); System.out.println("Request: " + request); StringTokenizer st = new StringTokenizer(request); if ((st.countTokens()==3) && st.nextToken().equals("GET")) { String filename = docRoot.getPath() + st.nextToken(); if (filename.endsWith("/") || filename.equals("")) filename += "index.html"; File file = new File(filename); if (file.getCanonicalPath().startsWith(canonicalDocRoot)) sendDocument(os, file); else sendError(SC_FORBIDDEN, os); } else { sendError(SC_BAD_REQUEST, os); } is.close(); os.close(); s.close(); } catch ( IOException ioe) { System.err.println("Fehler: " + ioe.toString()); } } protected void sendDocument(DataOutputStream os, File file) throws IOException { try { BufferedInputStream in = new BufferedInputStream (new FileInputStream(file)); sendStatusLine(os); setHeader("Content-Length", (new Long(file.length())).toString()); setHeader("Content-Type", guessType(file.getPath())); sendHeader(os); os.writeBytes(CRLF); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf, 0, 1024)) != -1) { os.write(buf, 0, len); } in.close(); } catch (FileNotFoundException fnfe) { sendError(SC_NOT_FOUND, os); } } protected void setStatusCode(String statusCode) { this.statusCode = statusCode; } protected String getStatusCode() { return statusCode; } protected void sendStatusLine(DataOutputStream out) throws IOException { out.writeBytes(PROTOCOL + getStatusCode() + CRLF); } protected void setHeader(String key, String value) { myHeaders.put(key, value); } protected void sendHeader(DataOutputStream out) throws IOException { String line; String key; Enumeration e = myHeaders.keys(); while (e.hasMoreElements()) { key = (String)e.nextElement(); out.writeBytes(key + ": " + myHeaders.get(key) + CRLF); } } protected void sendError(String statusCode, DataOutputStream out) throws IOException { setStatusCode(statusCode); sendStatusLine(out); out.writeBytes( CRLF + "<html>" + "<head><title>" + getStatusCode() + "</title></head>" + "<body><hl>" + getStatusCode() + "</hl></body>" + "</html>" ); System.err.println(getStatusCode()); } public String guessType(String filename) { String type = null; int i = filename.lastIndexOf("."); if (i > 0) type = typeMap.getProperty(filename.substring(i)); if (type == null) type = "unknown/unknown"; return type; } }
Is there someone out there who knows how to fix that problem?
Zaphod_B.. do you have an idea?
Edit: ok I put the index.html in the project folder this time the programme works with my IDE. However the header data etc is not given out as it should. Only "Request: GET / HTTP/1.1" Maybe theres something missing in the programme.
Edit: I changed the out.writeBytes() stuff with System.out.println() and now he gives out a lot more. Dont know yet if everything works properly as it should, but at least it works better now. Someone knows why the out.writeBytes() dont work?
Now the output when I enter http://localhost:8080/ in my browser is:
And someone knows why there is 2x in the output:Request: GET / HTTP/1.1
HTTP/1.0 200 OK
Server: SimpleHttpd2
Content-Type: unknown/unknown
Content-Length: 44
Request: GET /favicon.ico HTTP/1.1
HTTP/1.0 404 Not Found
404 Not Found
Request: GET /favicon.ico HTTP/1.1
HTTP/1.0 404 Not Found
404 Not Found
?Request: GET /favicon.ico HTTP/1.1
HTTP/1.0 404 Not Found
404 Not Found