Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
360 lines (326 loc) · 11.1 KB
/
Copy pathMain.java
File metadata and controls
360 lines (326 loc) · 11.1 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import java.net.*;
import java.io.*;
import java.util.Date;
import java.awt.Desktop;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.Iterator;
import java.io.File;
class Main {
public static void main(String[] args) throws Exception {
// Listen for a connection from a client
ServerSocket serverSocket = new ServerSocket(1234);
if(Desktop.isDesktopSupported())
Desktop.getDesktop().browse(new URI("http://localhost:1234"));
else
System.out.println("Please direct your browser to http://localhost:1234.");
while(true)
{
Socket clientSocket = serverSocket.accept();
System.out.println("Got a connection!");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Receive the request from the client
String inputLine;
String url = "";
String post = "";
boolean isGet = false;
int postContentLength = 0;
StringBuilder postRequest = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
System.out.println("The client said: " + inputLine);
if(inputLine.contains("GET ")) {
int end = inputLine.indexOf(" HTTP");
url = inputLine.substring(4, end);
isGet = true;
}
if(inputLine.contains("POST ")) {
int end = inputLine.indexOf(" HTTP");
}
if(inputLine.contains("Content-Length: ")) {
String sub = inputLine.substring(16, inputLine.length());
postContentLength = Integer.parseInt(sub);
}
if(inputLine.length() < 2){
while (postRequest.length() < postContentLength) {
postRequest.append((char) in.read());
}
break;
}
}
String dateString = (new Date()).toGMTString();
// Determine if POST or GET
String payload = "";
if (url.length() > 0) {
payload = generatePage();
}
else {
payload = generatePostReturn(postRequest.toString());
}
// Send HTTP headers
System.out.println("Sending a response...");
out.print("HTTP/1.1 200 OK\r\n");
out.print("Content-Type: text/html\r\n");
out.print("Content-Length: " + Integer.toString(payload.length()) + "\r\n");
out.print("Date: " + dateString + "\r\n");
out.print("Last-Modified: " + dateString + "\r\n");
out.print("Connection: close\r\n");
out.print("\r\n");
// Send the payload
out.println(payload);
clientSocket.close();
}
}
// build response to POST request
static String generatePostReturn(String postRequest) {
System.out.println("received from post request: " + postRequest);
// parse request
String currentDirectory = "";
if (postRequest.contains("curDirectory:")) {
int start = postRequest.indexOf("curDirectory:")+13;
int end = getEndOfElement(postRequest, start);
currentDirectory = postRequest.substring(start, end);
}
String requestDirectory = "";
if (postRequest.contains("requestedFilepath:")) {
int start = postRequest.indexOf("requestedFilepath:")+18;
int end = getEndOfElement(postRequest, start);
requestDirectory = postRequest.substring(start, end);
}
// determine request
String sendBack = "";
String current = "";
// back one
if (requestDirectory.startsWith("..")) {
File parent = new File(currentDirectory);
parent = parent.getParentFile();
current = parent.getPath();
sendBack = sendBackFiles(parent);
}
// to folder
if (requestDirectory.startsWith("index.html?cd=")) {
File goTo = new File(requestDirectory.substring(14, requestDirectory.length()));
current = goTo.getPath();
sendBack = sendBackFiles(goTo);
}
// display file contents
if (requestDirectory.startsWith("openFile=")) {
String toOpen = requestDirectory.substring(9, requestDirectory.length());
return readFile(toOpen);
}
sendBack = (sendBack + "{currentDirectory:" + current + "}");
System.out.println("sendback is " + sendBack);
return sendBack;
}
static String readFile(String f) {
StringBuilder payload = new StringBuilder();
try {
String currentLine;
BufferedReader br = new BufferedReader(new FileReader(f));
while ((currentLine = br.readLine()) != null)
payload.append(currentLine + "<br>");
} catch (IOException e) {
e.printStackTrace();
}
return payload.toString();
}
static String sendBackFiles(File filePath) {
System.out.println(filePath.getPath());
LinkedList<File> listOfFiles = getFiles(filePath);
LinkedList<File> documents = getDocuments(listOfFiles);
LinkedList<File> directories = getDirectories(listOfFiles);
StringBuilder sendBack = new StringBuilder();
sendBack.append("{documents:[");
Iterator<File> itDoc = documents.iterator();
while (itDoc.hasNext()){
File f = itDoc.next();
sendBack.append(f.getName() + ":" + f);
if (itDoc.hasNext())
sendBack.append(",");
}
sendBack.append("]}{directories:[");
Iterator<File> itDir = directories.iterator();
while (itDir.hasNext()){
File f = itDir.next();
sendBack.append(f.getName() + ":" + f);
if (itDir.hasNext())
sendBack.append(",");
}
sendBack.append("]}");
return sendBack.toString();
}
static int getEndOfElement(String bigString, int start) {
int end = start;
while (bigString.charAt(end) != ',' && bigString.charAt(end) != '}') {
end++;
}
return end;
}
// build page for GETs
static String generatePage() {
String url = System.getProperty("user.dir");
File currentLocation = new File(url);
LinkedList<File> listOfFiles = getFiles(currentLocation);
LinkedList<File> documents = getDocuments(listOfFiles);
LinkedList<File> directories = getDirectories(listOfFiles);
StringBuilder payload = new StringBuilder();
payload.append ("<html><head>\n" +
"<script type=\"text/javascript\">\n" +
"\n" +
"function httpPost(url, payload, callback) {\n" +
"var request = new XMLHttpRequest();\n" +
"request.onreadystatechange = function() {\n" +
"if(request.readyState == 4) {\n" +
"if(request.status == 200)\n" +
" callback(request.responseText);\n" +
"else\n" +
"{\n" +
" if(request.status == 0 && request.statusText.length == 0)\n" +
" alert(\"Request blocked by same-origin policy\");\n" +
" else\n" +
" alert(\"Server returned status \" + request.status +\n" +
" \", \" + request.statusText);\n" +
"}\n" +
"}\n" +
"};\n" +
"request.open('post', url, true);\n" +
"request.setRequestHeader('Content-Type',\n" +
"'application/x-www-form-urlencoded');\n" +
"request.send(payload);\n" +
"}\n" +
"\n" +
"function cb(response) {\n" +
"if (response.startsWith(\"{\")) {\n" +
" updateFolders(response);\n" +
" updateFiles(response);\n" +
" updateCurDirectory(response);\n" +
" updateText(\" \")\n" +
"}\n" +
"else\n" +
" updateText(response);\n" +
"}\n" +
"\n" +
"function updateText(response) {\n" +
"document.getElementById(\"text\").innerHTML = response;\n" +
"}\n" +
"\n" +
"function updateFiles(response) {\n" +
"var files = parseFiles(response);\n" +
"fileList.options.length=0;\n" +
"for (var i = 0; i < files.length; i++) {\n" +
" var semi = files[i].indexOf(':');\n" +
" var fileName = files[i].substring(0,semi);\n" +
" var filePath = (\"openFile=\" + files[i].substring(semi+1));\n" +
" fileList.options[i]=new Option(fileName, filePath, false, false);\n" +
"}\n" +
"}\n" +
"\n" +
"function updateFolders(response) {\n" +
"var folders = parseFolders(response);\n" +
"folderList.options.length=1;\n" +
"for (var i = 0; i < folders.length; i++) {\n" +
" var semi = folders[i].indexOf(':');\n" +
" var folderName = folders[i].substring(0,semi);\n" +
" var folderPath = (\"index.html?cd=\" + folders[i].substring(semi+1));\n" +
" folderList.options[i+1]=new Option(folderName, folderPath, false, false);\n" +
"}\n" +
"}\n" +
"\n" +
"function updateCurDirectory(response) {\n" +
"var curDirectory = parseCurrentDirectory(response)\n" +
"document.getElementById(\"currentDirectory\").innerHTML = curDirectory;\n" +
"}\n" +
"\n" +
"function parseCurrentDirectory(response) {\n" +
" var start = response.lastIndexOf(':');\n" +
" var end = response.lastIndexOf('}');\n" +
" var curDirectory = response.substring(start+1, end);\n" +
" return curDirectory;\n" +
"}\n" +
"\n" +
"function parseFiles(response) {\n" +
" var start = response.indexOf('[');\n" +
" var end = response.indexOf(']');\n" +
" var files = response.substring(start+1, end);\n" +
" files = files.split(',');\n" +
" return files;\n" +
"}\n" +
"\n" +
"function parseFolders(response) {\n" +
" var start = response.lastIndexOf('[');\n" +
" var end = response.lastIndexOf(']');\n" +
" var folders = response.substring(start+1, end);\n" +
" folders = folders.split(',');\n" +
" return folders;\n" +
"}\n" +
"\n" +
"function makeSendPayload(msg) {\n" +
" var curDirectory = document.getElementById(\"currentDirectory\").innerHTML;\n" +
" payload = \"{curDirectory:\" + curDirectory + \",requestedFilepath:\" + msg + \"}\";\n" +
" httpPost(\"http://localhost:1234\", payload, cb);\n" +
"}\n" +
"\n" +
"</script>\n" +
"</head><body>\n");
payload.append("<table>\n" +
"<tr><td align=right>Current directory:</td><td id=\"currentDirectory\">\n");
payload.append(url);
payload.append("<tr><td>\n" +
"<b>Folders:</b><br>\n" +
"<select id=\"folderList\" size=\"15\" style=\"width: 280px\" ondblclick=\"makeSendPayload(this.value)\">\n" +
"<option value=\"..\">..</option>\n");
Iterator<File> itDir = directories.iterator();
while (itDir.hasNext()){
File f = itDir.next();
payload.append("<option value=\"index.html?cd=" + f + "\">" + f.getName() + "</option> \n");
}
payload.append("</select>\n" +
"</td><td>\n" +
"<b>Files:</b><br>\n" +
"<select id=\"fileList\" size=\"15\" style=\"width: 280px\" ondblclick=\"makeSendPayload(this.value)\">\n");
Iterator<File> itDoc = documents.iterator();
while (itDoc.hasNext()){
File f = itDoc.next();
payload.append("<option value=\"openFile=" + url + "/" + f.getName() + "\">" + f.getName() + "</option> \n");
}
payload.append("</select>\n" +
"</td></tr></table>\n" +
"<p id=text></p>\n" +
"</body></html>\n");
return payload.toString();
}
static LinkedList<File> getFiles(File fileName) {
File listOfFiles[] = fileName.listFiles();
if (listOfFiles == null)
System.out.println("empty set of files");
LinkedList<File> fileLL = new LinkedList<File>();
for (File file : listOfFiles) {
fileLL.add(file);
}
return fileLL;
}
static LinkedList<File> getDocuments(LinkedList<File> listOfFiles) {
LinkedList<File> documents = new LinkedList<File>();
Iterator<File> itDoc = listOfFiles.iterator();
while (itDoc.hasNext()){
File f = itDoc.next();
if (f.isFile()) {
documents.add(f);
}
}
return documents;
}
static LinkedList<File> getDirectories(LinkedList<File> listOfFiles) {
LinkedList<File> directories = new LinkedList<File>();
Iterator<File> itDir = listOfFiles.iterator();
while (itDir.hasNext()){
File f = itDir.next();
if (f.isDirectory()) {
directories.add(f);
}
}
return directories;
}
}