changed tabs to spaces + little cleanup
This commit is contained in:
parent
d5f79cb746
commit
7de5c549ca
1 changed files with 130 additions and 119 deletions
249
WebServer.java
249
WebServer.java
|
@ -11,34 +11,35 @@ import java.net.InetAddress.*;
|
||||||
|
|
||||||
public final class WebServer
|
public final class WebServer
|
||||||
{
|
{
|
||||||
public static void main(String argv[]) throws Exception
|
public static void main(String argv[]) throws Exception
|
||||||
{
|
{
|
||||||
// Set port number
|
// Set port number
|
||||||
int port = 0;
|
int port = 0;
|
||||||
|
|
||||||
// Establish the listening socket
|
|
||||||
ServerSocket serverSocket = new ServerSocket(port);
|
|
||||||
System.out.println("Port number is: "+serverSocket.getLocalPort());
|
|
||||||
|
|
||||||
|
// Establish the listening socket
|
||||||
// Wait for and process HTTP service requests
|
ServerSocket serverSocket = new ServerSocket(port);
|
||||||
while (true) {
|
System.out.println("Port number is: " + serverSocket.getLocalPort());
|
||||||
// Wait for TCP connection
|
|
||||||
Socket requestSocket = serverSocket.accept();
|
|
||||||
requestSocket.setSoLinger(true, 5);
|
|
||||||
|
|
||||||
// Create an object to handle the request
|
|
||||||
HttpRequest request = new HttpRequest(requestSocket);
|
|
||||||
|
|
||||||
//request.run()
|
|
||||||
|
|
||||||
// Create a new thread for the request
|
|
||||||
Thread thread = new Thread(request);
|
|
||||||
|
|
||||||
// Start the thread
|
// Wait for and process HTTP service requests
|
||||||
thread.start();
|
while (true) {
|
||||||
|
|
||||||
|
// Wait for TCP connection
|
||||||
|
Socket requestSocket = serverSocket.accept();
|
||||||
|
requestSocket.setSoLinger(true, 5);
|
||||||
|
|
||||||
|
// Create an object to handle the request
|
||||||
|
HttpRequest request = new HttpRequest(requestSocket);
|
||||||
|
|
||||||
|
//request.run()
|
||||||
|
|
||||||
|
// Create a new thread for the request
|
||||||
|
Thread thread = new Thread(request);
|
||||||
|
|
||||||
|
// Start the thread
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final class HttpRequest implements Runnable
|
final class HttpRequest implements Runnable
|
||||||
|
@ -47,133 +48,143 @@ final class HttpRequest implements Runnable
|
||||||
// Recognized HTTP methods
|
// Recognized HTTP methods
|
||||||
final static class HTTP_METHOD
|
final static class HTTP_METHOD
|
||||||
{
|
{
|
||||||
final static String GET = "GET";
|
final static String GET = "GET";
|
||||||
final static String HEAD = "HEAD";
|
final static String HEAD = "HEAD";
|
||||||
final static String POST = "POST";
|
final static String POST = "POST";
|
||||||
}
|
}
|
||||||
|
|
||||||
final static String HTTPVERSION = "HTTP/1.0";
|
final static String HTTPVERSION = "HTTP/1.0";
|
||||||
final static String CRLF = "\r\n";
|
final static String CRLF = "\r\n";
|
||||||
final static String Response200 = HTTPVERSION + " 200 OK "+CRLF;
|
final static String RESPONSE_200 = HTTPVERSION + " 200 OK " + CRLF;
|
||||||
final static String Response404 = HTTPVERSION + " 404 Not Found "+CRLF;
|
final static String RESPONSE_404 = HTTPVERSION + " 404 Not Found " + CRLF;
|
||||||
final static String Response400 = HTTPVERSION + " 400 Bad Request "+CRLF;
|
final static String RESPONSE_400 = HTTPVERSION + " 400 Bad Request " + CRLF;
|
||||||
final static String Response501 = HTTPVERSION + " 501 Not Implemented "+CRLF;
|
final static String RESPONSE_501 = HTTPVERSION + " 501 Not Implemented " + CRLF;
|
||||||
final static String Location = " datalabb "+CRLF;
|
final static String LOCATION = " datalabb " + CRLF;
|
||||||
|
|
||||||
Socket socket;
|
Socket socket;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
public HttpRequest(Socket socket) throws Exception
|
public HttpRequest(Socket socket) throws Exception
|
||||||
{
|
{
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements the run() method of the Runnable interface
|
// Implements the run() method of the Runnable interface
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
processRequest();
|
processRequest();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println(e);
|
System.out.println(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process a HTTP request
|
// Process a HTTP request
|
||||||
private void processRequest() throws Exception
|
private void processRequest() throws Exception
|
||||||
{
|
{
|
||||||
// Get the input and output streams of the socket.
|
// Get the input and output streams of the socket.
|
||||||
InputStream ins = socket.getInputStream();
|
InputStream ins = socket.getInputStream();
|
||||||
DataOutputStream outs = new DataOutputStream(socket.getOutputStream());
|
DataOutputStream outs = new DataOutputStream(socket.getOutputStream());
|
||||||
|
|
||||||
// Set up input stream filters
|
// Set up input stream filters
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
|
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
|
||||||
Date d = new Date();
|
Date d = new Date();
|
||||||
|
|
||||||
// Get the request line of the HTTP request
|
// Get the request line of the HTTP request
|
||||||
String requestLine = br.readLine();
|
String requestLine = br.readLine();
|
||||||
|
|
||||||
// Display the request line
|
// Display the request line
|
||||||
System.out.println();
|
System.out.println();
|
||||||
System.out.println("Request:");
|
System.out.println("Request:");
|
||||||
System.out.println(" " + requestLine);
|
System.out.println(" " + requestLine);
|
||||||
|
|
||||||
String[] tokens = requestLine.split("\\s+");
|
String[] tokens = requestLine.split("\\s+");
|
||||||
//System.out.println("-->"+tokens.length);
|
//System.out.println("-->"+tokens.length);
|
||||||
String Request = tokens[0];
|
|
||||||
if(tokens.length != 3){
|
String Request = tokens[0];
|
||||||
System.out.println("Wrong number of arguments in request!");
|
if(tokens.length != 3) {
|
||||||
outs.writeChars(Response400+Location+getDateString(d));
|
|
||||||
}
|
System.out.println("Wrong number of arguments in request!");
|
||||||
else if(tokens[1].charAt(0) != '/'){
|
outs.writeChars(RESPONSE_400 + LOCATION + getDateString(d));
|
||||||
System.out.println("illegal url");
|
|
||||||
outs.writeChars(Response501+Location+getDateString(d));
|
} else if(tokens[1].charAt(0) != '/') {
|
||||||
}
|
|
||||||
else if(Request.equals(HTTP_METHOD.GET) || Request.equals(HTTP_METHOD.HEAD)){
|
System.out.println("illegal url");
|
||||||
FileInputStream filein;
|
outs.writeChars(RESPONSE_501 + LOCATION + getDateString(d));
|
||||||
try{
|
|
||||||
File f = new File("." +tokens[1]);
|
} else if(Request.equals(HTTP_METHOD.GET) || Request.equals(HTTP_METHOD.HEAD)) {
|
||||||
filein = new FileInputStream(f);
|
|
||||||
String response = createHeader(d,f);
|
FileInputStream filein;
|
||||||
outs.writeChars(response);
|
try {
|
||||||
if(Request.equals(HTTP_METHOD.GET))
|
|
||||||
sendBytes(filein,outs);
|
File f = new File("." +tokens[1]);
|
||||||
}
|
filein = new FileInputStream(f);
|
||||||
catch (FileNotFoundException e){
|
String response = createHeader(d,f);
|
||||||
outs.writeChars(Response404+getDateString(d));
|
outs.writeChars(response);
|
||||||
}
|
|
||||||
} else if(Request.equals(HTTP_METHOD.POST)){
|
if(Request.equals(HTTP_METHOD.GET)) {
|
||||||
outs.writeChars(Response501+getDateString(d));
|
sendBytes(filein, outs);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
outs.writeChars(Response400+getDateString(d));
|
} catch (FileNotFoundException e) {
|
||||||
|
outs.writeChars(RESPONSE_404 + getDateString(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if(Request.equals(HTTP_METHOD.POST)){
|
||||||
|
|
||||||
|
outs.writeChars(RESPONSE_501 + getDateString(d));
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
outs.writeChars(RESPONSE_400 + getDateString(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Close streams and sockets
|
// Close streams and sockets
|
||||||
outs.close();
|
outs.close();
|
||||||
br.close();
|
br.close();
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getDateString(Date d){
|
private String getDateString(Date d){
|
||||||
return "Date: "+d.toString()+CRLF;
|
return "Date: " + d.toString() + CRLF;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String createHeader(Date d, File f){
|
private String createHeader(Date d, File f){
|
||||||
String response = Response200;
|
String response = RESPONSE_200;
|
||||||
response += getDateString(d);
|
response += getDateString(d);
|
||||||
response += "Location: " + f.getName() +CRLF;
|
response += "LOCATION: " + f.getName() +CRLF;
|
||||||
response += "Server: Labbserver" + CRLF;
|
response += "Server: Labbserver" + CRLF;
|
||||||
response += "Allow: " + HTTP_METHOD.GET + " " + HTTP_METHOD.HEAD+CRLF;
|
response += "Allow: " + HTTP_METHOD.GET + " " + HTTP_METHOD.HEAD+CRLF;
|
||||||
response += "Content-Length: " + f.length() +CRLF;
|
response += "Content-Length: " + f.length() +CRLF;
|
||||||
response += "Content-Type: " + contentType(f.getName()) +CRLF;
|
response += "Content-Type: " + contentType(f.getName()) +CRLF;
|
||||||
response += "Last-Modified: " + new Date(f.lastModified()).toString() +CRLF;
|
response += "Last-Modified: " + new Date(f.lastModified()).toString() +CRLF;
|
||||||
return response;
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
private static void sendBytes(FileInputStream fins, OutputStream outs) throws Exception
|
||||||
private static void sendBytes(FileInputStream fins,
|
{
|
||||||
OutputStream outs) throws Exception
|
// Coopy buffer
|
||||||
{
|
byte[] buffer = new byte[1024];
|
||||||
// Coopy buffer
|
int bytes = 0;
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int bytes = 0;
|
|
||||||
|
|
||||||
while ((bytes = fins.read(buffer)) != -1) {
|
while ((bytes = fins.read(buffer)) != -1) {
|
||||||
outs.write(buffer, 0, bytes);
|
outs.write(buffer, 0, bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String contentType(String fileName)
|
private static String contentType(String fileName)
|
||||||
{
|
{
|
||||||
if (fileName.toLowerCase().endsWith(".htm") ||
|
if (fileName.toLowerCase().endsWith(".htm") ||
|
||||||
fileName.toLowerCase().endsWith(".html")) {
|
fileName.toLowerCase().endsWith(".html")) {
|
||||||
return "text/html";
|
return "text/html";
|
||||||
} else if (fileName.toLowerCase().endsWith(".gif")) {
|
} else if (fileName.toLowerCase().endsWith(".gif")) {
|
||||||
return "image/gif";
|
return "image/gif";
|
||||||
} else if (fileName.toLowerCase().endsWith(".jpg")) {
|
} else if (fileName.toLowerCase().endsWith(".jpg")) {
|
||||||
return "image/jpeg";
|
return "image/jpeg";
|
||||||
} else {
|
} else {
|
||||||
return "application/octet-stream";
|
return "application/octet-stream";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue