commit 57093ddd8b1099b5c667a6966d89d513f93dd90c Author: xiaoifei Date: Sat Oct 12 00:50:40 2024 +0800 Upload diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca3f2cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,30 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ +.idea +/*.iml +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/WebContent/index.html b/WebContent/index.html new file mode 100644 index 0000000..8289c0c --- /dev/null +++ b/WebContent/index.html @@ -0,0 +1,10 @@ + + + + + Title + + +

This is Index Page.

+ + \ No newline at end of file diff --git a/src/MyHttpRequest.java b/src/MyHttpRequest.java new file mode 100644 index 0000000..df36193 --- /dev/null +++ b/src/MyHttpRequest.java @@ -0,0 +1,40 @@ +import java.io.IOException; +import java.io.InputStream; + +public class MyHttpRequest { + private InputStream inputStream; + private String uri; + + public MyHttpRequest(InputStream inputStream) { + this.inputStream = inputStream; + } + + /** + * 解析流 + */ + public void parse(){ + try { + byte[] bytes = new byte[1024]; + inputStream.read(bytes); + String request = new String(bytes); + parseUri(request); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * 请求判断 + */ + public void parseUri(String request) { + int index1,index2; + index1 = request.indexOf(" ");//找空格下标 + index2 = request.indexOf(" ",index1+1);//找空格下标2 + uri = request.substring(index1+1,index2);//输出资源位置 + System.out.println(uri); + } + + public String getUri() { + return this.uri; + } +} diff --git a/src/MyHttpResponse.java b/src/MyHttpResponse.java new file mode 100644 index 0000000..c555a6a --- /dev/null +++ b/src/MyHttpResponse.java @@ -0,0 +1,50 @@ +import java.io.*; + +public class MyHttpResponse { + private int statusCode; + private String statusMessage; + + private OutputStream outputStream; + + public MyHttpResponse(OutputStream outputStream){ + this.outputStream = outputStream; + } + + public void sendRedirect(String uri){ + //判断资源是否存在 + // 不存在返回404 + // 存在返回目标资源 + File file = new File(System.getProperty("user.dir")+"/WebContent" + uri); + if (file.exists()){ + try { + FileInputStream fileInputStream = new FileInputStream(file); + byte[] bytes = new byte[(int)file.length()]; + fileInputStream.read(bytes); + String result = new String(bytes); +// System.out.println(result); + String response = getResponseMessage("200,",result); + this.outputStream.write(response.getBytes()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + }else { + try { + String error = getResponseMessage("404","404 File Not Found!"); + this.outputStream.write(error.getBytes()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + public String getResponseMessage(String code, String message){ + return "HTTP/1.1 " + code + "\r\n" + + "Content-Type: text/html\r\n" + + "Content-Length: " + message.length() + + "\r\n" + + "\r\n" + + message; + + } +} diff --git a/src/MyHttpServer.java b/src/MyHttpServer.java new file mode 100644 index 0000000..3391224 --- /dev/null +++ b/src/MyHttpServer.java @@ -0,0 +1,36 @@ +import java.io.InputStream; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; + +public class MyHttpServer { + private int port = 8080; + + public void receiving() + { + try{ + //create socket service + ServerSocket serverSocket = new ServerSocket(port); + while(true){ + //获取连接对象 + Socket socket = serverSocket.accept(); + //获取链接对象输入流 + InputStream inputStream = socket.getInputStream(); + //创建request + MyHttpRequest request = new MyHttpRequest(inputStream); + //解析请求 + request.parse(); + //创建响应 + OutputStream outputStream = socket.getOutputStream(); + MyHttpResponse response = new MyHttpResponse(outputStream); + //进行响应 + response.sendRedirect(request.getUri()); + + } + //loop accept requests + }catch (Exception e){ + + } + + } +} diff --git a/src/Test.java b/src/Test.java new file mode 100644 index 0000000..e133076 --- /dev/null +++ b/src/Test.java @@ -0,0 +1,7 @@ +public class Test { + public static void main(String[] args) { + System.out.println("server startup successfully"); + MyHttpServer server = new MyHttpServer(); + server.receiving(); + } +}