-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWebServer.java
150 lines (133 loc) · 5.19 KB
/
WebServer.java
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
package webserver;
import in2011.http.RequestMessage;
import in2011.http.ResponseMessage;
import in2011.http.MessageFormatException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
public class WebServer {
private int port;
private String rootDir;
private boolean logging;
Date date = new Date();
public WebServer(int port, String rootDir, boolean logging) {
this.port = port;
this.rootDir = rootDir;
this.logging = logging;
}
public void start() throws IOException, MessageFormatException {
// create a server socket
ServerSocket serverSock = new ServerSocket(port);
while (true) {
// listen for a new connection on the server socket
Socket conn = serverSock.accept();
//examine byte stream sent by client
InputStream is = conn.getInputStream();
//extract HTTP message from stream
RequestMessage req = RequestMessage.parse(is);
OutputStream os = conn.getOutputStream();
String methName = req.getMethod();
String URI = req.getURI();
String pathname = rootDir + URI;
Path Fullpath = Paths.get(pathname);
Path AllPath = Fullpath.toAbsolutePath().normalize();
if ("GET".equals(methName)){
ResponseMessage msg = new ResponseMessage(200);
String uri = URLDecoder.decode(URI,"ASCII");
Path path = Paths.get(rootDir).resolve(uri).normalize();
if (!path.startsWith(Paths.get(rootDir))){
try{
ResponseMessage msgBroken = new ResponseMessage(400);
msgBroken.write(os);
byte[] b = Files.readAllBytes(path);
os.write(" Request is bad ".getBytes());
os.write(b);
}
catch
(IOException x) {
System.err.format("IOException: %s%n", x);
}
}
if (path.startsWith(rootDir)&& uri.equals(URI)){
InputStream thefile = Files.newInputStream(AllPath);
while (true) {
int a = thefile.read();
if (a == -1) {
break;
}
ResponseMessage FileObtained = new ResponseMessage(400);
FileObtained.write(os);
byte[] b = Files.readAllBytes(path);
os.write(" File Obtained:".getBytes());
os.write(b);
os.write(a);
}
}
}
if ("HEAD".equals(methName)){
ResponseMessage head = new ResponseMessage(200);
head.getHeaderFieldValue("Connection");
head.addHeaderField("Date",date.toString());
head.getHeaderFieldValue("Status");
head.getHeaderFieldValue("Server");
head.write(os);
conn.close();
}
if ("PUT".equals(methName)){
OutputStream putfile = Files.newOutputStream(AllPath);
int count = 0;
while (true) {
int c = is.read();
if (c == -1) {
break;
}
String uri = URLDecoder.decode(URI,"ASCII");
Path path = Paths.get(rootDir).resolve(uri).normalize();
byte[] d = Files.readAllBytes(path);
os.write(" File Created:".getBytes());
putfile.write(d);
++count;
}
putfile.close();
ResponseMessage putresp = new ResponseMessage(201);
putresp.write(os);
conn.close();
}
else{
ResponseMessage msg = new ResponseMessage(500);
}
ResponseMessage msg = new ResponseMessage(200);
msg.write(os);
os.write("Team 13: ".getBytes());
conn.close();
}
}
public static void main (String[] args) throws IOException, MessageFormatException {
String usage = "Usage: java webserver.WebServer <port-number> <root-dir> (\"0\" | \"1\")";
if (args.length != 3) {
throw new Error(usage);
}
int port;
try {
port = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
throw new Error(usage + "\n" + "<port-number> must be an integer");
}
String rootDir = args[1];
boolean logging;
if (args[2].equals("0")) {
logging = false;
} else if (args[2].equals("1")) {
logging = true;
} else {
throw new Error(usage);
}
WebServer server = new WebServer(port, rootDir, logging);
server.start();
}
}