hje
This commit is contained in:
parent
df197e09c2
commit
7b4a8d8bd0
1 changed files with 72 additions and 59 deletions
131
WebServer.java
131
WebServer.java
|
@ -13,32 +13,32 @@ 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
|
// Establish the listening socket
|
||||||
ServerSocket serverSocket = new ServerSocket(port);
|
ServerSocket serverSocket = new ServerSocket(port);
|
||||||
System.out.println("Port number is: "+serverSocket.getLocalPort());
|
System.out.println("Port number is: "+serverSocket.getLocalPort());
|
||||||
|
|
||||||
|
|
||||||
// Wait for and process HTTP service requests
|
// Wait for and process HTTP service requests
|
||||||
while (true) {
|
while (true) {
|
||||||
// Wait for TCP connection
|
// Wait for TCP connection
|
||||||
Socket requestSocket = serverSocket.accept();
|
Socket requestSocket = serverSocket.accept();
|
||||||
requestSocket.setSoLinger(true, 5);
|
requestSocket.setSoLinger(true, 5);
|
||||||
|
|
||||||
// Create an object to handle the request
|
// Create an object to handle the request
|
||||||
HttpRequest request = new HttpRequest(requestSocket);
|
HttpRequest request = new HttpRequest(requestSocket);
|
||||||
|
|
||||||
//request.run()
|
//request.run()
|
||||||
|
|
||||||
// Create a new thread for the request
|
// Create a new thread for the request
|
||||||
Thread thread = new Thread(request);
|
Thread thread = new Thread(request);
|
||||||
|
|
||||||
// Start the thread
|
// Start the thread
|
||||||
thread.start();
|
thread.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final class HttpRequest implements Runnable
|
final class HttpRequest implements Runnable
|
||||||
|
@ -47,9 +47,9 @@ 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";
|
||||||
|
@ -59,68 +59,81 @@ final class HttpRequest implements Runnable
|
||||||
// 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));
|
||||||
|
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
// Close streams and sockets
|
String[] tokens = requestLine.split(" ");
|
||||||
outs.close();
|
// for(String token : tokens)
|
||||||
br.close();
|
// System.out.println("---> " + token);
|
||||||
socket.close();
|
sendResponse(tokens[1],outs);
|
||||||
|
|
||||||
|
|
||||||
|
// Close streams and sockets
|
||||||
|
outs.close();
|
||||||
|
br.close();
|
||||||
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendResponse(String f,DataOutputStream outs)
|
||||||
|
throws Exception{
|
||||||
|
String response = HTTPVERSION+" 200 OK "+CRLF+"kottedala"+CRLF;
|
||||||
|
outs.writeChars(response);
|
||||||
|
sendBytes(new FileInputStream(f),outs);
|
||||||
|
|
||||||
|
}
|
||||||
private static void sendBytes(FileInputStream fins,
|
private static void sendBytes(FileInputStream fins,
|
||||||
OutputStream outs) throws Exception
|
OutputStream outs) throws Exception
|
||||||
{
|
{
|
||||||
// Coopy buffer
|
// Coopy buffer
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
int bytes = 0;
|
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