import java.net.*;
import java.io.*;
class WriterThread extends Thread {
public static final int ERR_NORM = 0;
public static final int ERR_URIS = 1;
private Socket client;
public WriterThread(Socket client) {
this.client = client;
}
private String getContent(String fname) {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader( new FileReader(fname) );
while ( br.ready() ) {
sb.append( br.readLine() + "\r\n" );
}
} catch(IOException ioe) {
System.err.println( "Exception was risen: " + ioe.getMessage() );
ioe.printStackTrace();
sb.append( "EXCEPTION:" + ioe.getMessage() );
return displayErrorPage();
} finally {
try { br.close(); } catch(Exception e) { System.err.println("FATAL ERROR: cannot close requested filestream!"); };
}
return sb.toString();
}
public String displayErrorPage() {
return displayErrorPage(ERR_NORM);
}
public String displayErrorPage(int type) {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
String errorFileName = (ERR_NORM == type) ? "error.html" :
(ERR_URIS == type) ? "error_uris.html" : "error.html";
try {
br = new BufferedReader( new FileReader("content" + File.separator + errorFileName ) );
while ( br.ready() ) {
sb.append( br.readLine() + "\r\n" );
}
} catch(IOException ioe) {
System.err.println( "Exception was risen: " + ioe.getMessage() );
ioe.printStackTrace();
sb.append( "EXCEPTION:" + ioe.getMessage() );
} finally {
try { br.close(); } catch(Exception e) { System.err.println("FATAL ERROR: cannot close requested filestream!"); };
}
return sb.toString();
}
public void run() {
Socket s = client;
try {
BufferedReader br = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
PrintWriter pw = new PrintWriter( s.getOutputStream() );
String req = br.readLine();
System.out.println("[req:" + req + "]");
String firstWord = req.split("\\s")[0];
String secondWord = req.split("\\s")[1];
String rep = "";
if ( ! firstWord.toLowerCase().equals("get") ) {
rep = displayErrorPage();
} else {
req = "content" + URLDecoder.decode( req.split("\\s")[1].replaceAll("/", File.separator) );
if ( ! req.startsWith("/") ) {
rep = displayErrorPage();
}
System.out.println("[mod_req:" + req + "]");
rep = getContent(req);
}
System.out.println("[ <<<<<<<<<<<<<<<<<<<< Sent back content:");
System.out.println(rep);
System.out.println(">>>>>>>>>>>>>>>>>>>>>> ]");
pw.write(rep);
pw.flush();
} catch(Exception e) {
System.err.println( "Exception was risen: " + e.getMessage() );
e.printStackTrace();
} finally {
try { s.close(); } catch(Exception e) { System.err.println("FATAL ERROR: cannot close ServerSocket!"); };
}
}// run
}// class.WriterThread
public class WebServer {
public static final boolean DEBUG = true;
public static final int DEFAULT_PORT = 8080;
private static int port;
public WebServer(int port) {
this.port = port;
}
public void startServer() {
ServerSocket ss = null;
try {
ss = new ServerSocket(port);
System.out.println( "Service started, listening at " + ss.getInetAddress() + '/' + ss.getLocalPort() );
Socket s = null;
main_cycle:
while(true) {
s = ss.accept();
System.out.println( "Connection from " + s.getInetAddress() + '/' + s.getLocalPort() + '/' + s.getPort() );
Thread servant = new WriterThread(s);
servant.start();
}
} catch(Exception e) {
System.err.println( "Exception was risen: " + e.getMessage() );
e.printStackTrace();
} finally {
try { ss.close(); } catch(Exception e) { System.err.println("FATAL ERROR: cannot close ServerSocket!"); };
}
}// startServer
public static void main(String args[]) {
int port = -1;
if (args.length != 0) {
port = Integer.parseInt(args[0]);
} else {
port = DEFAULT_PORT;
}
WebServer ws = new WebServer(port);
ws.startServer();
}
}// class.WebServer
import java.io.*;
class WriterThread extends Thread {
public static final int ERR_NORM = 0;
public static final int ERR_URIS = 1;
private Socket client;
public WriterThread(Socket client) {
this.client = client;
}
private String getContent(String fname) {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader( new FileReader(fname) );
while ( br.ready() ) {
sb.append( br.readLine() + "\r\n" );
}
} catch(IOException ioe) {
System.err.println( "Exception was risen: " + ioe.getMessage() );
ioe.printStackTrace();
sb.append( "EXCEPTION:" + ioe.getMessage() );
return displayErrorPage();
} finally {
try { br.close(); } catch(Exception e) { System.err.println("FATAL ERROR: cannot close requested filestream!"); };
}
return sb.toString();
}
public String displayErrorPage() {
return displayErrorPage(ERR_NORM);
}
public String displayErrorPage(int type) {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
String errorFileName = (ERR_NORM == type) ? "error.html" :
(ERR_URIS == type) ? "error_uris.html" : "error.html";
try {
br = new BufferedReader( new FileReader("content" + File.separator + errorFileName ) );
while ( br.ready() ) {
sb.append( br.readLine() + "\r\n" );
}
} catch(IOException ioe) {
System.err.println( "Exception was risen: " + ioe.getMessage() );
ioe.printStackTrace();
sb.append( "EXCEPTION:" + ioe.getMessage() );
} finally {
try { br.close(); } catch(Exception e) { System.err.println("FATAL ERROR: cannot close requested filestream!"); };
}
return sb.toString();
}
public void run() {
Socket s = client;
try {
BufferedReader br = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
PrintWriter pw = new PrintWriter( s.getOutputStream() );
String req = br.readLine();
System.out.println("[req:" + req + "]");
String firstWord = req.split("\\s")[0];
String secondWord = req.split("\\s")[1];
String rep = "";
if ( ! firstWord.toLowerCase().equals("get") ) {
rep = displayErrorPage();
} else {
req = "content" + URLDecoder.decode( req.split("\\s")[1].replaceAll("/", File.separator) );
if ( ! req.startsWith("/") ) {
rep = displayErrorPage();
}
System.out.println("[mod_req:" + req + "]");
rep = getContent(req);
}
System.out.println("[ <<<<<<<<<<<<<<<<<<<< Sent back content:");
System.out.println(rep);
System.out.println(">>>>>>>>>>>>>>>>>>>>>> ]");
pw.write(rep);
pw.flush();
} catch(Exception e) {
System.err.println( "Exception was risen: " + e.getMessage() );
e.printStackTrace();
} finally {
try { s.close(); } catch(Exception e) { System.err.println("FATAL ERROR: cannot close ServerSocket!"); };
}
}// run
}// class.WriterThread
public class WebServer {
public static final boolean DEBUG = true;
public static final int DEFAULT_PORT = 8080;
private static int port;
public WebServer(int port) {
this.port = port;
}
public void startServer() {
ServerSocket ss = null;
try {
ss = new ServerSocket(port);
System.out.println( "Service started, listening at " + ss.getInetAddress() + '/' + ss.getLocalPort() );
Socket s = null;
main_cycle:
while(true) {
s = ss.accept();
System.out.println( "Connection from " + s.getInetAddress() + '/' + s.getLocalPort() + '/' + s.getPort() );
Thread servant = new WriterThread(s);
servant.start();
}
} catch(Exception e) {
System.err.println( "Exception was risen: " + e.getMessage() );
e.printStackTrace();
} finally {
try { ss.close(); } catch(Exception e) { System.err.println("FATAL ERROR: cannot close ServerSocket!"); };
}
}// startServer
public static void main(String args[]) {
int port = -1;
if (args.length != 0) {
port = Integer.parseInt(args[0]);
} else {
port = DEFAULT_PORT;
}
WebServer ws = new WebServer(port);
ws.startServer();
}
}// class.WebServer
Nincsenek megjegyzések:
Megjegyzés küldése