File name: myhtpservertest.java
Import java.io. *; import java.Net. *; import java.util. *;
/ ** * main * / public class myhttpsertest {
String m_szServerName; // server name String m_szVersion; // server version int m_nServerPort; // port public MyHttpServerTest (String szServerName, String szVersion, int nServerPort) {this.m_szServerName = szServerName; this.m_szVersion = szVersion; this.m_nServerPort = nServerPort } / ** * Show version information * / public void showserverinformation () {system.out.println ("Welcome to newman HTTP Server!"); System.out.Println ("Server Name: this.m_szservername) System.out.println ("Server Version:" this.m_szversion);} public void start () {this.showserverinformation ();
try {ServerSocket server = new ServerSocket (this.m_nServerPort); while (true) {Socket client = server.accept (); (new HttpClientThread (client)) run ();.}} catch (Exception e) {e.printStackTrace (); System.exit (1);}} public static void main (String args []) {MyHttpservertest Server = New MyHttpservertest ("MyHttpservertest", "1.0", 80); server.start ();}}
class HttpClientThread implements Runnable {Socket m_sClient; HTTPRequest m_Request; HttpClientThread (Socket sClient) throws IOException {this.m_sClient = sClient; this.m_Request = new HTTPRequest (this.m_sClient);} public void run () {try {System.out. Println (this.getconnectinfo ()); bufferedoutputstream out = new bufferedoutputstream (this.m_sclient .getoutputstream ()); system.Out.println (this.m_request.getlog ());
if (this.m_Request.isGetRequest ()) this.processGetRequest (this.m_Request, out); System.out.println ( "Request completed Closing connection.."); this.m_sClient .close ();} catch (IOException e ) {System.out.println ( "IOException occurred."); e.printStackTrace ();}} / ** * get Connect Info * / public String getConnectInfo () {String szClientHostName = this.m_sClient .getInetAddress () getHostName. (); String szClientAddress = this.m_sClient .getInetAddress () .getHostAddress (); int ClientPort = this.m_sClient .getPort (); String szRet = ""; szRet = "Accepted connection from" szClientHostName "(" szClientAddress " ) " " on port " clientport ". "; returnis
void processGetRequest (HTTPRequest request, BufferedOutputStream out) throws IOException {String szFileName = request.getFileName (); File file = new File (szFileName); if (file.exists ()) this.sendFile (out, file); else {/ The file does not exist system.out.println ("file" file.getcanonicalPath () "does not exist."); String notfound = "
" " File Name: " File " Not Found " "
" " return index " " body> html> ";
File: //out.write (New String "). getBytes ()); out.write (New String (" HTTP / 1.0 200 OK / R / N "). GetBytes ()) Out.write ("Content_Length:" NOTFOUND.LENGTH () 2) .GetBytes ()); Out.write ("Content_Type: Text / HTML / R / N / R / N") .getbytes ()); out.write ("") .getBytes ()); out.write (notfound.getbytes ()); out.flush ();}} void sendfile (BufferedoutputStream out, file file) {Try {DataInputStream DIN = New DataInputStream (New FileInputStream (file)); int Len = (int) file.Length (); byte buffer [] = new byte [len]; din.readfully (buffer); DIN.CLOSE Out.write ("HTTP / 1.0 200 OK / R / N"). GetBytes ()); Out.write ("Content-Length:" Buffer.Length "/ R / N ") .getbytes ()); out.write (" Content-Type: Text / HTML / R / N / R / N "). getBytes ()); out.write (buffer); out.flush ); Out.close (); system.out.println ("File Sent:" file.getcanonicalPath ()); system.out.println ("Number of Bytes:" LEN);} Catch (Exception E) {Try {Out.write ("HTTP / 1.0 400" "No CAN DO" "/ R / N"). getBytes ()); Out.write ("Content-Type: Text / HTML / R / N / R / N ".GetBytes ());} catch (ioexception ooe) {system.out.println (" ERROR RETRIEVING " File;}}}} vector m_vlines}} m_sClient; HttpInputStream m_In; HTTPRequest (Socket client) throws IOException {this.m_sClient = client; this.m_In = new HttpInputStream (this.m_sClient.getInputStream ()); this.getRequest ();}
public String getLog () {StringBuffer szRet = new StringBuffer (); szRet.append ( "Received the following request:" "/ n"); Enumeration enum = m_vLines.elements (); String szstr1 = ""; while (enum .hasMoreElements ()) {szstr1 = (string) enum.nexTelement (); szret.append (szstr1 "/ n");} return szstr1.toString ();} boolean isgetRequest () {if (this.m_vlines .size) )> 0) {String firstline = (string) this.m_vlines.elementat (0); if (firstline.Length ()> 0) IF (firstline.substring (0,3) .Equalsignorecase ("get") Return True Return False;} String getFileName () {if (this.m_vlines .size ()> 0) {string firstline = (string) this.m_vlines.elementat (0); string filename = firstline.substring (firstline.indexof) "" ") 1); int N = filename.indexof (" "); if (n! = - 1) filename = filename.substring (0, n); try {if (filename.Charat (0) == ' / ') Filename = filename.substring (1);} catch (StringIndexoutofboundsexception ex) {} if (filename.equals (")) filename =" index.htm "; if (filename.charAt (filename.length () -1) == '/') filename = "index.htm"; // not understand return filename;} else return "";} public HttpInputStream getInputStream () {return this .m_in;} public httprequest getRequest () throw ;; while {line = this.m_in.readline (); if (line.length ()> 0) THIS.M_IN.Addline (line); Else Break;} Return this;} / ** * Only Use to recognize methed "get"
* / Class HttpInputStream extends FilterInputStream {HttpInputStream (InputStream in) {super (in);} public void addLine (String line) {m_vLines .addElement (line);} public String readLine () throws IOException {StringBuffer result = new StringBuffer () Boolean finished = false; boolean isenter = false; while (! Finished) {INT CH = -1; ch = read (); if (isenter) && (ch == 10)) {// if it is "/ N / R "Result.setlength (Result.Length () - 1); // Had Added" / N ", Remove It Return Result.toString ();} if (ch == 13) // if enterpener iester = True; Else iSenter = false; if (ch == - 1) // end return fruit.toString (); else result.Append ((char) ch);} returnrate.tostring ();}}}
Please advise