Programozás | Java, JSP » Farkas István - Egyszerű webszerver kialakítása Java nyelven

Alapadatok

Év, oldalszám:2003, 29 oldal

Nyelv:magyar

Letöltések száma:1781

Feltöltve:2004. június 17.

Méret:224 KB

Intézmény:
-

Megjegyzés:

Csatolmány:-

Letöltés PDF-ben:Kérlek jelentkezz be!



Értékelések

Nincs még értékelés. Legyél Te az első!


Tartalmi kivonat

!"# $ # % !& & ! ())*! $ ! # * ! $ +,--! & " % & " " $. ! ! /+ 0 1 ( 0 ) ! " +! , 1 ! 3 4) # & / 0 ! "5 ! & / 5) + ! 6+ & 8 -9 "50 % & 7 + + / *# 1( " 5 # ( 8 $ & ( . +1 " ! & ( + ( % & 7 ( & $ $ # +! + 9 8 +! " (! - . (!+! / 7 5 & ( ! 7 + 1 +1 ( " : ! + 7 ( 0 & , " 5 ;<=>/ ! & = 7 & 0 1 " 1( ! $ $ 1( 9?@ "A ! 07 0$ 1 ! B + & $ 0 &7 + " 5 @?9 1 +1 & &7 & + & & " 1( 0 0 $ & 7 $ " &7 & $ $ 1 1( " C $ + " 2 0 / (!(! / 0&1 & 2 5 @?9 E ( 1 2 + " 5 @?9 0 + ( 8 -9 ( "51 "5 . & 0 5 " & (" ( ! 7 + + " " 1 0G 5 +0 1 $! 9 &2 * 8 . " " "+ 9 07 1 ! ( "5 0 + 0 "

";" = 7 1 7& 1 ( 1 + 5! 0 ;" * 0 5 F8 =<;/ " 5 @?9 ! ( & *9 . 7 ( 2 1 7 ( " D (1 1 + " 1 " 5 9 0 70 + $ 1 7 7 0 ( 8 / 7 0 $ & 7 F8 =< / ( 0 $ 7 1 " 8 7" 0 " 8 ! 1 ! & "9 & " & "9 ! & 1 1 +" 7 & $ 5 0 1 +" & ! & 7 77 $ (1 1 "5 70 " 7 ( ! $ & 1 1 " # 7 + (" 5+ & 3$+ 1 1 & ( 4 *+ " 0 $+ +1 1 . " : / 5 >E /; &7 $+ ( 1 +1 0 + I " $ J 0 " 7, 5 K/ , ?LM K 5/ , 58 ? NOP ? NOP 5 K/ , 0 $ ? NOL 58 ? NOL + ( 0 H 0 " ( " & ( " ?LM 5 " $ 7 4 ! (" 5 8 & " & 8 5 8 K ? NO; 2 ?LM ? NO 58 O; ? NO; D ?LM 58 D 58 O 2 58 ! " & 0 & 0 "5 5 & 1 & " ! $ 1 9M & ! & 1 + ( 1 "5

+1 7 7 " + 7 8 / + C3 / ! " C3 / + 7 $ 9:5 . " $% & 5 9 / + 70 & +1 $ # (1 0 , (0 + ! $ & 0 ! " 8 $ 0 0 1 1 +1 ! + * & Q Q ! " & . & # *3M?. 9 1 / 0 " 8 + ! + *?: Q @ *# 5 @ ." (!*! / 3 5 $ ( + ! C G & ( ( 1 $ ! ! +1 $ /+ ! "5% & 1 0 " $ $ = 1( & + 1 ( *9 8. & ( & 1 & / "5 1 & 5 1 / + ( + E & B & !$ B $ ! " ( & . * .+ "5 " . ! & 1 ! 1 1 / $ 0 0 & ! $ 7 & & *+ 1 1 " $ * / 0 0$ ( 1 / "5 1 + ! ( ! &7 & & +1 & ! & & 1 (" 5 / 7 $ 0 " " 5 1 & / 1 + + 1 & 1 "# + 0 & $ 1( ! &7 1 ! (0 1 0 1 / 1 0 " ! 0 " $ 1( ! & $ ++ 7 & ! & & + $ & +1 7 (! " 1 1 & " 3 + *+

" C3 . 0 # ? 1 1 " $ / 0 0 $ 1 ( + 1 " + 1( 5 1 1 $ " 9 / ( 0 + & 1 7 0 +$ " $ +1 " 5 ! / " 5 + 7 G+ 1 ( ( 1 ( 0 1( ! 0 0 + ( & + (! ( ( ! (" ( 47 (+ 9 + 1 1 ( 0 & 7 ( & $ " ( R $ & + ++ ( + + " 8 / + & ! * ". (!4! / - . 5# 5 & + " 7& 7 & 0 & # & 3 (! # -;"> &7 # & ->"< ! 9 & F8 H;H/ -;"; & F8 " # H;=/ " 5 + 0 & ! 0 + " 5+ ( 8 ! 7 & /0 ( & " 0/ $+ G. 1 E 4 1( +1 + + 0 ! 7 5# 7 7 • 5# • # & & +" & "5 0 ( 4 & ( , & +1 ( +1 0 0 7 I? +J + ! ! , + 1 70 0 ! 1 • 0 ( & + " 5 +& 7 # & & " :& ( "5 $ * E& / 0 + " 5+ # 8 +1 " ! + , ! "# $ % & "( )$ $# )*( ,) . $$

*"/ 01 2 3 +3 1 2 4 *"3 # ,%+3 )"5 6//7//8( ) 9 9 $ $ 9 $ $ 9# $:9 $ 9 # $3;</"= 9 $ 3;</"> ? 3;</"6 @ @3;</"( ,A )# , 3;</"*( , ) $ ( ,B# ) CD,>>*=,6 ,>3;</"8 @3;</"8( E ,-$ ) 7//( B ) , $ ( % + - < " ( 5 S ! C G & + ($ A" 5 0 ! " 7 # 1 ! 8FTA & /0 ! 1 0 # 0 & " % 5 C /5 511 + 5 5 + 0 5 0 5 0 70 / + ! " W • W & G & 7. 0 +$ B ! " IVOJ +$ " & 0 7 + 0 $ $ ( +( ! & & $ 0 ! 1 + 10 ! &7 + G " $ " 0 -;";/ # /& ( +1 % & " 6// DE( F )C 6G D 6//7 *)6)6 C ) . 1 HC "/"6( B ,A #)>/( B , ) 9 # $( ( I# $JI# JI $ J( % $$ I $ JI # J( IH J% $$ I H JI # $J( $ & . ( ;> 7, I $ +J " "9 / ! & & & $ $ " ! 0 ! 1 + 2< " " 7, • "5 7 " 5 F8 H;H/ & • 0 (! & " *! 5 8

>> ! 5 1 ;"; & (! +/+ 511 + /8 +/5 & & +7 , 0 511 + /A $ 511 + / 1 ! U 1 1 " 1( + ! 1 # $ - $ 1+ & 7 0 ! 1& 5 ) ! " -;"; ! 5 5# Q / + 70 8 " " 5 (# ! ! " 5& ( # $ & ( & & (! & & & " +( ! & & ( " 5 & " 5 & *+ " +7 + & ( 0 ! 1 ( & ! " ! / + 1 70 / & ( @? 0 & $ 4 ) , $ $ $ "5 $ %& # ! % " 5 / 4 7 & 0 $ & ) *! / & +1 +1 & $ A & & "5 5% & 7 / 7 + & +1 & @? " 5 " 5 # 0 " +$ " 1 & & , % 5& 5& 5 + 1 8 ". 0 ! 1 ? & 8 /A 8 / + 7 0 ! 1 ! R 5# " & 5 0 ! 1 + 7 +& 7 & + $ ! !$ " " ( / / ! & " + ;; 1 / ! " +1 " % & / ! + 5% & $ & / ! " *!+!

6 3 7 5 % & / 0 5 $ & ( !$ $ 1( 1( & ( + 1 1 8 ( !$ / & ( "? 1 / ( # ) % & " ! 7 0 ! & ( # ) +1 + 1 1( ! " 8 -9 / + 1 (! 0 ) 0 7 & K?3 + 0 (/ 1 & & 1 0 1 / & 7 ) K?3 $ & ( 9-@ " ( $ & 4& ( $ & 1 " 5 / " ? & 1 $ ( # ) + ) # & ! 1 $ / & !$ " try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.outprintln("Could not listen on port: 4444"); System.exit(-1); } 5 +1 0 ( $ & + & ( $ $ & 0 ! " 1 " " + 0 7 , Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.outprintln("Accept failed: 4444"); System.exit(-1); } C3 ( # ) 1 )! & * 5 $ & ( # ) 1( + )! ( & , ) 5 ; $ & & " ! 7 7 + $ 1 0 !$ !$ " A & - + 1( % & " 7 $ ( $

1( ! " ! 1 5 + 0 1( & +$00 " C3 1 ! 5. # ) + ) G & # 7 & + 7 8 / " * 1 0 1 ! . -;"; 0 ? 4 & & 0 " ( & + 1 "5 0 $ +1 7 ( & (. !$ " + !$ 7+ & 7 & & $ 3 8 " 5 +& 7 + ? ! " + 7 & ( ( 1 ! & + . 0 ( +$00 & $ 0 G & 0 1 (! ( * ! + ) +1 7 $ + 4! / 5 # / " & ( 0 ( $ $ " D (" + 7 , • Q • ? & • 8 • Q 1 3 • 8 0 , ) & , 07+ 0 , $ ! : , 0 1( 0 , & 47 $/ 1 0 + 1 4!+! / 0 3 5 & 8 $+ +1 ++ & 0 & 0 $ 0 + 7 + +1 +1 ( $ & ( ! (+ 9 40 + 4" 8 ! $ + ; +1 " + ! ( + 7" +1 0 " 0 + + " # & ( $ " 5 + " +1 ! & ( $ " 4!*! / 0 58 0 + ) & 0 $ $ + $ 0 5 ) & " $ & & & & 0 ! , & ( # ) 0 7 & 0 1 /

& $ $ M B 0 ) & " 0 $ 0 " " X ! ! 0 & + 0 0 & 9B 9B & *+ " + 4 7 K " 5 ) $ /0 & " & & & + ++ / ( $ ". "5 5 0 4 ( & ( + " 0 $ 1 (! ) "? 7 7 & (+ ( + !$ *+ " 7 0 0 / ! +1 7 & ." G ! Y 5 , # 9 $ H <GL 5 + ( , if (tempLine.toUpperCase()startsWith("MAXCONNECTIONS")){ int index=tempLine.lastIndexOf("="); try { bufferSize=Integer.parseInt(tempLinesubstring(index+1)); } catch (Exception ex){ } System.outprintln("Config(): Set maxConnections to "+bufferSize); } 5 $00 ? & ( 7 IOJ ! $ 58 0 & $ 7& +! 0 $ & 7 ! ;2 + +" " , A 5 G8 1 6+ & 5 # : 1 @$ 0 + ! 7 +7 & +1 & 70 & " " & / " & +1 ( $00 ? & "5 G "# 7 !$ " +$00 70 5 5+ 0 $ ( $ + " " 4!*! / : ( $ ! $

$ & $ + ! ! . ( $ ( + ( ( $ 4 + + ! ! "5 * + ( "Z " public static void main(String args[]) { WebServer myServer=new WebServer(); myServer.serve(args); } 5 # + + 01 & $ ( $ & +! $ + <>>>/ ( $ + ) 1 1 1 0 1 ! 7, " + 0 $ . + +$ 0 $ " 5 + + &( 0 $ 1 (! " 5 + & 1 "5+ + + ! & * 1 0 + & + + $ 5 & $ " 1 $ 1 ! ! / # & 7 0 ++ " ServerSocket srvr = new ServerSocket(port); System.outprintln( "Serving requests on port "+port+"" ); Socket tempClientSocket; (# * + 5 $ !$ + $ " 5 ";" + ! &7 ;D +1 ( % & ( $ & + & 0$ $ $ 0 " 5 11 + & ( $ & 7 + ! &7 ) 1 0 0 + $ & 7 + $ $ + $ & & !(& & & + 0$ & ( 8 C "Z ( & A 0$ " ( + " & ! & ! 1 " 1 + & $ # 0 +1 2

+$ $ ! $ & ( " & ! $ ( 0 1 " ServerThread tempThread; while( true ) { if (ConnectionManager.getNrOfConnections()<ConfigmaxConnections){ tempClientSocket = srvr.accept(); tempThread=new ServerThread (tempClientSocket); System.outprintln("Accepting connection, this is nr "+ConnectionManager.getNrOfConnections()); ConnectionManager.addConnection(); tempThread.start(); } else{ System.outprintln("Error: too many connections, denying request."); } } 0 7 G 0 +1 1 ! & "5 7 " 5 & # +1 2 +! 7 0$ ! ! ! 5 !$ ( $ &!$ " 0$ 7! "5 1 + 7 "5% & 0 $ 1( + ! ! / ( $ & ! ( + 7 & $ + ( & 0$ ! $ # 0 " ( 0 70 1 ;H 1( " 2 4!4! 6 " 2 5 $ 3 +$ ! & 1 $ " / & + ( 0 1 ! 7 # & " 5 & ( ( $ 0$ 7 7! ( % & # :A 7! ! ! 4 ! *5 & " " 5 $ & ! , 0$ (" %

& 7 $ !$ . / 7 7 & " 7 " outToServer = new PrintStream(clientSocket.getOutputStream() ); inFromServer = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); 5 & $( & + , , 7 !$ 1 I $ $ 4 7/ J 5 )))) & ( $ # ( $ & $ $ & $ " & J & "5 7 0 & ( & B+" & & & $ E & ( ( + 1 $ & 1 ( B . # ), ) + ! F8 5 6, $ & & $ " 5 4 $ ! - ! "5 ! 7& $ + 0 ! 1 ! / / 0$ ( & & " 5 / " ( ( ( I ( $ ( $ & &! $ - / 7 " 47 " 0 & & / +$00 1 / # ) 1 ( + ( $ 4 , ( $ & ( ( 4 " 47 , 0 / $ & + & & I H;H ( $ ! J I2>> K " ;= !$ " 5 &!$ F V$ # * J $ ( $ B $ & 7 & 5 0 . ( $ & $ 0 ! 7 (( 1 ,- 5 ( & ( (" . , /0 ( $ 7 & + & & & 1 $

7 7 1 I511 + / *& $ (! ! + / & J " "M / I ! 70 + 0 +J 7 ! & I ! & +BVO>J " for (int i=0; i<requests.size();i++){ if ((requests.get(i)toString()toUpperCase()startsWith("ACCEPTENCODING")) && (requestsget(i)toString()toUpperCase() .indexOf("GZIP")!=-1)) { clientSupportsGZip = true; } if ((requests.get(i)toString()toUpperCase()indexOf("GZIP;Q=0") ==-1) && (requests.get(i)toString()toUpperCase()indexOf ("GZIP;Q=0")!=-1)){ clientSupportsGZip = false; } } 5 7 D" + & 7 $ 7& !$ $ & & 1 0 1(" +70 / ! & # :A 0 / ! & ! +$ $ +$ & I G- 0 ! 1 , "5 4 J +$ " inFile=new FileInputStream(requestedFile); outToServer.print("HTTP/11 200 OK "); currentDate=new Date(); outToServer.print("Date: "+currentDatetoGMTString()+" "); outToServer.print("Server: Mini WebServer

v."+Configversion+" "); outToServer.print("Content-Type: text/html "); 5 70 & +$00 ! 0 & $ & ( $ ! ( ;R " 4 & & *8FTA A. ! & ( 70 + +$ 7 ! 70 ( $ ( " 5 +$00 ( $ ( ( "5 & $ & $ $ ) ( $ & 1 & $ 0 / ! " byte [] buffer = new byte[Config.bufferSize]; try{ int numBytes = inFile.read(buffer, 0, ConfigbufferSize); while (numBytes != -1) { outToServer.write(buffer, 0, numBytes); numBytes = inFile.read(buffer, 0, ConfigbufferSize); } 5% & & 4 0 $ & " $ , ! + & (Q 1 3 I ( 8 +1 (1 + & $ " X 70 + ! Q 1 3 0 1 / $ & ! ;< 1 ! 0 $ 1 (! +1 ( + ": 0 & & J $ " 0 " 5 0 I +1 "Q 1 3 & $ G +1 $ ." 7 & G 5+ 7 1 ( +1 +1 & ( $ & " ! & $ *+ " +1 0 & # & +1 +1 +1 ( $ 70 & J " 1 3" 4

& 2 ! " & ( $ J "5 & 4!;! / : 3"% & "# 7 " +1 & 4 ( # & ( M & ( $ : 5 & & + 1 ! 7 I2>2 E 5? & 8 7 !$ " 5 Q 1 3 7 & ( 0 $ + 1 8 C/ 7 & ( & & & ( + 1 " 7" public void run(){ while (canRun){ try { sleep (3000); } catch (InterruptedException ex) { } counter+=3; if (counter >= Config.timeOut){ theServer.killConnection(); canRun=false; } } } # ! ( + 7 ! 0 $ 1( $ 5 Q 1 3 & ( $ ( $ $ & ("5 & ! +1 & & 0 $ 1 (! $ $/ 70 $ Q 1 3 0 ! 0$ +$00 $ +$00 Q 1 3 7 5, 7 & +1 + 7 ! " !$ 7 0 0 6+ & ! & +1 0 # ( & 1 ( ! & 8 ! +. & & " ;! 5 $ , & & " ( ( $ " , 0 & +1 0 ( ! " > CM9P 8 7 " + 7 0 ! 11 ! *+ " 8 ! + ( / 1 & , + 5 ! ) & ! "A ! &7 /$+ +1

;!+! 5 % & *8 $ 7! . & ! " 3 47 4 , , 47 & 0 1 & 1 " : ! " 5 *8 , , ! & "$ " + (" outToServer.flush(); myWatchDog.reset(); gzout=new GZIPOutputStream (outToServer); int numBytes = inFile.read(buffer, 0, ConfigbufferSize); while (numBytes != -1) { myWatchDog.reset(); gzout.write(buffer, 0, numBytes); numBytes = inFile.read(buffer, 0, ConfigbufferSize); } gzout.close(); 5 , # ) ,2 & 1 !$ +$00 ( & $ & 7 , # 1 ( $ + & 7 1 & ! "5 ;!(! & $ $ $ + ,) & !$ " 7 & + " ! " 5 ! $ 8 C 0 4+ 1 & ! ! / & & 7 ! , , 8 ! 0 & *8 7 69 0 ! 1 1 5 0 / ! ( $ !$ " & & + , " 5 % & B& " X & " ( + 1 +$ ! ; & ( 0 " 7 1 1 "9 5 & ( 0 & !$ & 0 7 1 0 / " H DR 7 * $( 1 G ". ! # :A 7 * K ; 2; P 0 / . [ 4! &

"5 . +$ * K 6*L ==78 $ 1 0 / P 7 + ( % & ! 1 &$ Q K 1( & ( 5 : 7 0 ! & " 7 ! $ & 4& & & " 5 & " # 0 "5 6 G* M G 7> M 69 ( ( 0 ( 7 " >[/ $ & 0 # :A : *=> G/G* & 1 & (, +$ : # :A P ( / ! " 69 7 : 0 / ! & " & " 5 8MM " : R"R H2"2 + 1 " ;R2"2 + + 1 +! 4 < = 9 : 6 8 28 /* Mini Web-Server *This is a mini web server for demonstration purposes. *It is written by Istvan Farkas with the very useful help of Gabor Paller. */ import import import import import java.io*; java.net*; java.util*; java.utilzipGZIPOutputStream; java.text*; class WatchDog extends Thread{ private ServerThread theServer; private int counter=0; private boolean canRun=true; WatchDog(ServerThread theServer){ this.theServer=theServer; this.setDaemon(true); } public void reset(){ counter=0; } public void disable (){

canRun=false; } public void run(){ while (canRun){ try { sleep (3000); } catch (InterruptedException ex) { } counter+=3; if (counter >= Config.timeOut){ theServer.killConnection(); canRun=false; } } } // run } //---------------------------------------------------------class ServerThread extends Thread{ private Socket clientSocket; private Vector requests; // vector of all lines of request private Vector currentRequest; // elements of request line being parsed private String beforeLastStringRead,lastStringRead; // the last string read while reading the req private PrintStream outToServer; private GZIPOutputStream gzout; private BufferedReader inFromServer; private Date currentDate; //will get value when comm’ing with client private StringTokenizer tokenizer=null; //used to tokenize the current request private WatchDog myWatchDog; ServerThread (Socket clientSocket){ this.clientSocket=clientSocket; requests=new Vector(); currentRequest=new Vector(); myWatchDog=new WatchDog(this);

myWatchDog.start(); } public void serveGet(){ String fileName=null; // name of requested file File requestedFile=null; //the requested file as an object FileInputStream inFile=null; boolean clientSupportsGZip=false; // Now serving a get. At the call of this function, currentRequest contains line 1 of the request. if (currentRequest.get(1)toString()equals("/")) { fileName = "index.html"; } else { fileName = currentRequest.get(1)toString(); } System.outprintln("serveGet(): Requested filename: "+fileName); //now detect whether client supports gzip for (int i=0; i<requests.size();i++){ if ((requests.get(i)toString()toUpperCase()startsWith("ACCEPTENCODING")) && (requests.get(i)toString()toUpperCase()indexOf("GZIP")!=-1)) { clientSupportsGZip = true; } if ((requests.get(i)toString()toUpperCase()indexOf ("GZIP;Q=0.")==-1) && (requestsget(i)toString()toUpperCase() indexOf ("GZIP;Q=0")!=-1)){

clientSupportsGZip = false; // false if q=0 but not q=0.x } } try { requestedFile=new File(Config.basePath,fileName); inFile=new FileInputStream(requestedFile); myWatchDog.reset(); outToServer.print("HTTP/11 200 OK "); currentDate=new Date(); outToServer.print("Date: "+currentDatetoGMTString()+" "); outToServer.print("Server: Mini WebServer v."+Configversion+" "); outToServer.print("Content-Type: text/html "); if (Config.enableGZipEncoding && clientSupportsGZip) { outToServer.print("Content-Encoding: gzip "); } else { outToServer.print("Content-Length: "+requestedFile.length()+" "); } outToServer.print(" "); 2 if (Config.enableGZipEncoding && clientSupportsGZip){ byte [] buffer = new byte[Config.bufferSize]; try{ outToServer.flush(); myWatchDog.reset(); gzout=new GZIPOutputStream (outToServer); //GZipped int numBytes = inFile.read(buffer, 0,

ConfigbufferSize); while (numBytes != -1) { myWatchDog.reset(); gzout.write(buffer, 0, numBytes); numBytes = inFile.read(buffer, 0, ConfigbufferSize); } gzout.close(); } catch (IOException ioex){ System.outprintln("DEBUG: Exception occured while GZiptransferring file."); ioex.printStackTrace(); } } else { //normal, uncompressed transfer byte [] buffer = new byte[Config.bufferSize]; try{ myWatchDog.reset(); int numBytes = inFile.read(buffer, 0, ConfigbufferSize); while (numBytes != -1) { myWatchDog.reset(); outToServer.write(buffer, 0, numBytes); numBytes = inFile.read(buffer, 0, ConfigbufferSize); } } catch (IOException ioex){ System.outprintln("DEBUG: Exception occured while transferring file."); ioex.printStackTrace(); } } // end of else try { inFile.close(); } catch (Exception ex){ System.outprintln("DEBUG: error occured while closing input file"); } } catch (FileNotFoundException ex){ // 404 String messageContent="<HTML> <H1>404 - File

Not Found</H1> "+ "Mini WebServer could not find the file you requested.<br> "+ "The filename you specified was: <strong>"+fileName+ "</strong><br> Please try again.<br><br>Mini WebServer v."+Configversion+ " - written by Istvan Farkas in 2003 "+ "<br>Many heartfelt thanks to Gabor Paller. </HTML>"; myWatchDog.reset(); outToServer.print("HTTP/11 404 File Not Found "); currentDate=new Date(); outToServer.print("Date: "+currentDatetoGMTString()+" "); outToServer.print("Server: Mini WebServer D v."+Configversion+" "); outToServer.print("Content-Type: text/html "); outToServer.print("Content-Length: "+messageContent.length()+" "); outToServer.print(messageContent); return; } } public void tellBadRequest (){ // Called on unsupported HTTP command try { String messageContent = "<HTML>

<H1>400 - Bad Request</H1> " + "Sorry, but Mini WebServer does not support the HTTP command you requested.<br> " + " Please try again.<br><br>Mini WebServer v"+Configversion + " - written by Istvan Farkas in 2003 " + "<br>Many heartfelt thanks to Gabor Paller. </HTML>"; myWatchDog.reset(); outToServer.print("HTTP/11 400 Bad Request "); currentDate = new Date(); outToServer.print("Date: " + currentDatetoGMTString()+" "); outToServer.print("Server: Mini WebServer v" + Config.version+" "); outToServer.print("Content-Type: text/html "); outToServer.print("Content-Length: " + messageContentlength() + " "); outToServer.print(messageContent); return; } catch (Exception ex) { System.outprintln("DEBUG: Exception occured while writing to outToServer"); } } public void killConnection(){ // to terminate connection

System.outprintln("ERROR: No active transfer for the determined timeout period - closing socket."); System.outprintln("Closing connection on port "+clientSocket.getPort()+" with address: "+clientSocketgetInetAddress()); ConnectionManager.removeConnection(); try{ clientSocket.close(); } catch (Exception ex){ System.outprintln("Debug: exception occured while closing the timed out transfer socket."); } } public void run(){ System.outprintln("Activated connection on port "+clientSocket.getPort()+" with address: "+clientSocketgetInetAddress()); try { outToServer = new PrintStream( clientSocket.getOutputStream() ); inFromServer = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); //now read all requests into a vector do { try { H lastStringRead=inFromServer.readLine(); requests.add(lastStringRead); } catch (IOException ex){ } } while ((lastStringRead.length()>0)); tokenizer=new

StringTokenizer((String)requests.get(0)); while (tokenizer.hasMoreTokens()) { currentRequest.add(tokenizernextToken()); } // Calls to serve different HTTP commands if (currentRequest.get(0)toString()toUpperCase()startsWith("GET")) { serveGet(); } // else if. else { // Unsupported command tellBadRequest(); } // The approriate function has finished serving request, now close connection. System.outprintln("Closing connection on port "+clientSocket.getPort()+" with address: "+clientSocketgetInetAddress()); ConnectionManager.removeConnection(); myWatchDog.disable(); clientSocket.close(); // The Thread will finish after this point. } catch (IOException ex){ ex.printStackTrace(); } }} //-------------------------------------------------------------------class Config{ //here we declare the config variables as static ones. We also give them default values. static final String version="0.2"; static String basePath="."; // the directory from which

we are serving files static int bufferSize=1024; static int maxConnections=64; static boolean enableGZipEncoding=true; static int timeOut=60; Config(){ //The constructor will read config from WebServer.ini BufferedReader iniFile=null; try { iniFile = new BufferedReader( new FileReader("WebServer.ini")); } catch (FileNotFoundException ex){ System.outprintln("Unable to read WebServerini, falling back to default configuration values."); return; } String tempLine=null; // the last line we read while parsing ini file do { try { = tempLine=iniFile.readLine(); if (tempLine.toUpperCase()startsWith("BASEPATH")){ int index=tempLine.lastIndexOf("="); basePath=tempLine.substring(index+1); System.outprintln("Config(): Set basepath to "+basePath); } if (tempLine.toUpperCase()startsWith("BUFFERSIZE")){ int index=tempLine.lastIndexOf("="); try { bufferSize=Integer.parseInt(tempLinesubstring(index+1)); } catch (Exception ex){ }

System.outprintln("Config(): Set bufferSize to "+bufferSize+ " bytes"); } if (tempLine.toUpperCase()startsWith("MAXCONNECTIONS")){ int index=tempLine.lastIndexOf("="); try { bufferSize=Integer.parseInt(tempLinesubstring(index+1)); } catch (Exception ex){ } System.outprintln("Config(): Set maxConnections to "+bufferSize); } if (tempLine.toUpperCase()startsWith("ENABLEGZIPENCODING")){ int index=tempLine.lastIndexOf("="); try { if (Integer.parseInt(tempLinesubstring(index+1))==0) enableGZipEncoding=false; } catch (Exception ex){ } System.outprintln("Config(): Set enableGZipEncoding to "+enableGZipEncoding); } if (tempLine.toUpperCase()startsWith("TIMEOUT")){ int index=tempLine.lastIndexOf("="); try { timeOut=Integer.parseInt(tempLinesubstring(index+1)); } catch (Exception ex){ } if (timeOut<10) timeOut=10; System.outprintln("Config(): Set timeOut to "+timeOut+"

seconds."); } } catch (Exception ex){ } } while (tempLine != null); } } //---------------------------------------------------------------------class ConnectionManager{ private static int nrOfConnections=0; public static void addConnection(){ nrOfConnections++; } R public static void removeConnection(){ if (nrOfConnections!=0) { nrOfConnections--; } } } public static int getNrOfConnections(){ return nrOfConnections; } } //---------------------------------------------------------------------public class WebServer { int port=9000; // Where we are listening, default value is 9000 public void serve(String args[]){ System.outprintln(" Simple WebServer version "+Configversion+" "); try { port=Integer.parseInt(args[0]); } catch (Exception ex){ System.outprintln("Error parsing input port from args[0], falling back to default (9000)."); } Config myConfig=new Config(); try { ServerSocket srvr = new ServerSocket(port); System.outprintln( "Serving

requests on port "+port+"" ); Socket tempClientSocket; ServerThread tempThread; while( true ) { if (ConnectionManager.getNrOfConnections()<ConfigmaxConnections){ tempClientSocket = srvr.accept(); tempThread=new ServerThread (tempClientSocket); System.outprintln("Accepting connection, this is nr "+ConnectionManager.getNrOfConnections()); ConnectionManager.addConnection(); tempThread.start(); } else { System.outprintln("Error: too many connections, denying request."); } } } catch( IOException ex ) { ex.printStackTrace(); System.outprintln("Could not create server socket, maybe already in use."); return; } } public static void main(String args[]) { WebServer myServer=new WebServer(); myServer.serve(args); } } //---------------------------------------------------------------------- < " 2 F8 =<; +,--)))"0 V " - 01 - 01=<;" F8 =< +,--)))"0 V " - 01 - 01=< " F8 H;H +,--)))"0 V

" 5 ) ?" * / - 01 - 01 H;H" $ ,? 1 /# ( +/ ;<<<. % 1K +,--)))" K$ $ " #" : 0 $ +,--! & " $ "1 "1 $ ,? 1 "1 , 8$ - 1 ,# -+ ! 1 - + 1 - +,--)))"! & ) ?$ % & , ) ,# )% & +,--)))" N$ ( & -+$ - - 1 + +- Q ! & - >> ->2- -! & U) + % & , -! & ) -!)/; /;<<H-!)/; / 1 & " $ " M ) 1- -$ - ) > - 1 -1 ? & "