My First Web Server

My First Web Server

HttpServer.java

 1 package com.xiejiaohui.ch1;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.io.OutputStream;
 7 import java.net.InetAddress;
 8 import java.net.ServerSocket;
 9 import java.net.Socket;
10 
11 public class HttpServer {
12 
13     public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot";
14     private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
15 
16     private boolean shutdown = false;
17 
18     public static void main(String[] args) {
19         HttpServer server = new HttpServer();
20         server.await();
21     }
22 
23     public void await() {
24         ServerSocket serverSocket = null;
25         int port = 8080;
26         try {
27             serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
28         } catch (IOException e) {
29             e.printStackTrace();
30             System.exit(1);
31         }
32         while(!shutdown) {
33             Socket socket = null;
34             InputStream input = null;
35             OutputStream output = null;
36             try {
37                 socket = serverSocket.accept();
38                 input = socket.getInputStream();
39                 output = socket.getOutputStream();
40                 Request request = new Request(input);
41                 request.parse();
42                 
43                 Response response = new Response(output);
44                 response.setRequest(request);
45                 response.sendStaticResource();
46                 
47                 socket.close();
48                 
49                 shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
50             } catch (Exception e) {
51                 e.printStackTrace();
52                 continue;
53             }
54         }
55         
56     }
57 
58 }

Request.java

 1 package com.xiejiaohui.ch1;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 
 6 public class Request {
 7     private InputStream input;
 8     private String uri;
 9     
10     public Request(InputStream input) {
11         this.input = input;
12     }
13     
14     public void parse() {
15         StringBuffer request = new StringBuffer(2048);
16         int i;
17         byte[] buffer = new byte[2048];
18         try {
19             i = input.read(buffer);
20         } catch (IOException e) {
21             e.printStackTrace();
22             i = -1;
23         }
24         for (int j = 0; j < i; j++) {
25             request.append((char)buffer[j]);
26         }
27         System.out.print(request.toString());
28         uri = parseUri(request.toString());
29     }
30     
31     private String parseUri(String requestString) {
32         int index1, index2;
33         index1 = requestString.indexOf(' ');
34         if (index1 != -1) {
35             index2 = requestString.indexOf(' ', index1 + 1);
36             if (index2 > index1) {
37                 return requestString.substring(index1 + 1, index2);
38             }
39         }
40         return null;
41     }
42     
43     public String getUri() {
44         return uri;
45     }
46 
47 }

Response.java

 1 package com.xiejiaohui.ch1;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 import java.io.OutputStream;
 7 
 8 public class Response {
 9     
10     private static final int Buffer_Size = 1024;
11     private Request request;
12     private OutputStream output;
13     
14     public Response(OutputStream output) {
15         this.output = output;
16     }
17     
18     public void setRequest(Request request) {
19         this.request = request;
20     }
21     
22     public void sendStaticResource() throws IOException {
23         byte[] bytes = new byte[Buffer_Size];
24         FileInputStream fis = null;
25         try {
26             File file = new File(HttpServer.WEB_ROOT, request.getUri());
27             if (file.exists()) {
28                 fis = new FileInputStream(file);
29                 int ch = fis.read(bytes, 0, Buffer_Size);
30                 while (ch != -1) {
31                     output.write(bytes, 0, ch);
32                     ch = fis.read(bytes, 0, Buffer_Size);
33                 }
34             } else {
35                 String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + 
36                         "Content-Type: text/html\r\n" +
37                         "Content-Length: 23\r\n" +
38                         "\r\n" + 
39                         "<h1>File Not Found</h1>";
40                 output.write(errorMessage.getBytes());
41             }
42         } catch (Exception e) {
43             e.printStackTrace();
44             System.out.println(e.toString());
45         } finally {
46             if (fis != null) {
47                 fis.close();
48             }
49         }
50     }
51     
52 
53 }

 

posted @ 2016-01-24 16:03  Josh_Xie  阅读(212)  评论(0编辑  收藏  举报