-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSrv5pm.java
More file actions
189 lines (163 loc) · 6.08 KB
/
FileSrv5pm.java
File metadata and controls
189 lines (163 loc) · 6.08 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.util.*;
/**
* PRACTICE FILE FOR THE NETWORKING ASSIGNMENT
* @author Daniel Weiner
*
*/
public class FileSrv5pm {
public static void main(String[] args) throws IOException {
String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System: " + currentDir);
System.out.println("I'm a web server that's a *file sever*...");
final String crlf = "\r\n";
int port = 11114;
ServerSocket ssock = new ServerSocket(port);
System.out.println("listening on port " + port + "...");
while (true) {
Socket sock = ssock.accept();
System.out.println("received a new client connection!");
OutputStream out = sock.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String password = null;
boolean loggedIn = false;
String req = "";
String line;
while ((line = in.readLine()) != null && !(line.equals("")) )
req += (line+crlf);
if (req.length()==0) {
System.out.println("!!!!!!!empty request (?)");
continue;
}
System.out.println("at " + new Date() + ", req received:\n" + req);
System.out.println("req.length() = " + req.length());
String firstLine = req.split(crlf)[0];
String res = firstLine.split(" ")[1];
res = res.split("\\?")[0];
System.out.println("res req'ed: " + res);
Path pathfile = Paths.get("." + res);
boolean exists = Files.exists(pathfile);
File dir = new File(pathfile.toString());
//File dir1 = dir.getParent();
System.out.println("\n\n\n!!!!!!!!!!!!" + dir.isDirectory() + "\n\n\n");
System.out.println("\n\n\n!!!!!!!!!!!!" + dir + "\n\n\n");
String fline = req.split("\r\n")[0];
System.out.println("firstline: "+fline);
String getParamL = fline.substring(fline.indexOf("?")+1,fline.indexOf("HTTP")-1);
//String path2 = firstline.split(" ")[1].substring(2);
System.out.println("getParmList: "+getParamL); //+",path2="+path2+".");
String[] GETKeyV = getParamL.split("&");
for (int i = 0; i < GETKeyV.length; i++) {
System.out.println(" get param: " + GETKeyV[i]);
String[] keyAndVal = GETKeyV[i].split("=");
if (keyAndVal[0].equals("password")) {
password = keyAndVal[1];
System.out.println("\n\n!!!!!password value from get param: "+ password + "\n\n");
}
}
if (password == null)
{
String cookLine = req.split("Cookie: ")[1];
cookLine = cookLine.split("\r\n")[0];
String[] cookKeyAndVal = cookLine.split("=");
if (cookKeyAndVal[0].equals("password"))
password = cookKeyAndVal[1];
System.out.println("!!!!password from cookie: " + password);
}
if ("1234".equals(password)) loggedIn = true;
String body = "";
String status;
String url = "http://localhost:11114/src/";
byte[] content;
if (!loggedIn)
{
/*
String fline = req.split("\r\n")[0];
System.out.println("firstline: "+fline);
String getParamL = fline.substring(fline.indexOf("?")+1,fline.indexOf("HTTP")-1);
//String path2 = firstline.split(" ")[1].substring(2);
System.out.println("getParmList: "+getParamL); //+",path2="+path2+".");
String[] GETKeyV = getParamL.split("&");
for (int i = 0; i < GETKeyV.length; i++) {
System.out.println(" get param: " + GETKeyV[i]);
String[] keyAndVal = GETKeyV[i].split("=");
if (keyAndVal[0].equals("password")) {
password = keyAndVal[1];
System.out.println("\n\n!!!!!password value from get param: "+ password + "\n\n");
}
}
if (password == null)
{
String cookLine = req.split("Cookie: ")[1];
cookLine = cookLine.split("\r\n")[0];
String[] cookKeyAndVal = cookLine.split("=");
if (cookKeyAndVal[0].equals("password"))
password = cookKeyAndVal[1];
System.out.println("!!!!password from cookie: " + password);
}
*/
status = "200 OK";
body += "<html><body><form name=\"loginForm\"><input type=\"password\" id=\"password\" name=\"password\"><br /><input type=\"submit\" value=\"Login\"></form></body></html>";
content = body.getBytes();
String head = "HTTP/1.1 " + status + crlf + "Content-Length: " +
content.length + crlf + "Set-Cookie: password="+password + "; Path=/" + crlf + crlf;
out.write(head.getBytes());
out.write(content);
}
else
{
if (dir.isDirectory()) {
status = "200 OK";
File[] listOfFiles = dir.listFiles();
body += "<html><body>";
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
//body += "this file is: " + listOfFiles[i] + "<br>";
//String link = listOfFiles[i].getName().split(".")[0];
String link = listOfFiles[i].getName();
body += "<a href =\"" + url + link +"\">" + link + "</a><br>";
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
body += "</body></html>";
content = body.getBytes();
String head = "HTTP/1.1 " + status + crlf +
"Content-Length: " +
//body.length() +
content.length + crlf + "Set-Cookie: password="+password + "; Path=/" + crlf + crlf;
// String resp = head + body;
out.write(head.getBytes());
out.write(content);
System.out.println("\n\n" + head + "\n\n");
//content = Files.readAllBytes(pathfile);
}
else {
if (!exists) {
body = "<html><body>this file does not exist: " + pathfile + "</body></html>";
content = body.getBytes();
status = "404 File Not Found";
} else {
body = "the file exists; here's where its content should go";
status = "200 OK";
content = Files.readAllBytes(pathfile);
}
String head = "HTTP/1.1 " + status + crlf +
"Content-Length: " +
//body.length() +
content.length + crlf + "Set-Cookie: password="+password + crlf + crlf;
// String resp = head + body;
out.write(head.getBytes());
out.write(content);
}
}
out.flush();
out.close();
in.close();
sock.close();
}
}
}