-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.java
More file actions
169 lines (141 loc) · 5.84 KB
/
HttpServer.java
File metadata and controls
169 lines (141 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.net.*;
import java.io.*;
import java.util.*;
public class HttpServer{
private ArrayList<HttpServerSession> sessions;
public static void main(String args[]){
//Sever starting message
System.out.println("Web server starting");
HttpServer server = new HttpServer();
server.start_server();
}
public void start_server(){
try{
//Listen on port 51333 for incoming connections
ServerSocket ss = new ServerSocket(51333);
sessions = new ArrayList<HttpServerSession>();
//Loop and accept new connections as they arrive
while(true){
Socket s = ss.accept();
HttpServerSession session = new HttpServerSession(s);
sessions.add(session);
//Start the session thread to handle the connection
session.start();
System.out.println("Connection received from: " + s.getInetAddress());
}
}catch (IOException e){
e.printStackTrace();
}
}
}
class HttpServerSession extends Thread{
private Socket s;
private BufferedOutputStream bos;
public HttpServerSession(Socket s){
this.s = s;
}
private boolean println(BufferedOutputStream bos, String s){
String news = s + "\r\n";
byte[] array = news.getBytes();
try{
bos.write(array, 0, array.length);
return true;
}catch(IOException e){
e.printStackTrace();
return false;
}
}
//Entry point into the server session
public void run(){
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
bos = new BufferedOutputStream(s.getOutputStream());
HttpServerRequest request = new HttpServerRequest();
String requestLine;
//While isDone() is false loop through the header
while(!request.isDone()){
requestLine = reader.readLine();
request.process(requestLine);
System.out.println("Header: " + requestLine);
}
//Use the parsed information from HttpServerRequest
String file = request.getFile();
String host = request.getHost();
System.out.println("File: " + file + " " + "Host: " + host);
//Checks if the host or file is null when creating the file path
String filePath;
if (host != null && file != null) {
//host = host.replace(":", "_"); //For Windows OS
filePath = host + "/" + file;
} else if (file != null) {
filePath = "localhost:51333/" + file;
} else{
System.out.println("Error: file and host are null");
filePath = null;
}
System.out.println("FilePath: " + filePath);
//Create a file object to see if the file exsists
File fileObject = new File(filePath);
//Try/Catch loop for running the requested file
try{
if(!fileObject.exists()){
//404 message for file not found
println(bos, "HTTP/1.1 404 Not Found");
println(bos, "Content-Type: text/html");
println(bos, "");
//Give the user a basic 404 file not found HTML page
FileInputStream fileInputStream = new FileInputStream("404.html");
byte[] buffer = new byte[1000];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
//Close the fileInputStream
fileInputStream.close();
//Flush the output stream
bos.flush();
//Close socket
s.close();
}else{
FileInputStream fileInputStream = new FileInputStream(filePath);
//Checks for the requested file type
//Then sends the file content type in the response
String contentType;
if(file.endsWith(".html")){
contentType = "text/html";
}else if(file.endsWith(".jpeg")){
contentType = "image/jpg";
}else{
contentType = "text/plain";
}
//Print HTTP header response
println(bos, "HTTP/1.1 200 OK");
//Print the content type
println(bos, "Content-Type: " + contentType);
//Send empty line to show end of header
println(bos, "");
//Read and send file content
byte[] buffer = new byte[1000];
int bytesRead;
while((bytesRead = fileInputStream.read(buffer)) != -1){
bos.write(buffer, 0, bytesRead);
//Thread.sleep(1000);
}
//Close the fileInputStream
fileInputStream.close();
//Flush the output stream
bos.flush();
//Close socket
s.close();
}
}catch(FileNotFoundException e){
//404 message for file not found
println(bos, "HTTP/1.1 404 Not Found");
println(bos, "");
e.printStackTrace();
}
}catch (IOException e){
e.printStackTrace();
}
}
}