main
xiaoifei 10 months ago
commit 57093ddd8b

30
.gitignore vendored

@ -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

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>This is Index Page.</h1>
</body>
</html>

@ -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;
}
}

@ -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;
}
}

@ -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){
}
}
}

@ -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();
}
}
Loading…
Cancel
Save