-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleThread.java
186 lines (174 loc) · 6.74 KB
/
SingleThread.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
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
import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.Scanner;
public class SingleThread extends Thread
{
private Socket socket;
// Reads and prints to socket streams
private BufferedReader in;
private PrintWriter out;
final static String CRLF = "\r\n";
public SingleThread(Socket clientSocket)
{
this.socket = clientSocket;
}
public void run()
{
try
{
//create a buffer reader
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//create a PrintWriter
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
String line;
String FilePath = "D:/htdocs"; // File path to the required file. NOTE: Can be changed
String httpRequest = "";
while (((line = in.readLine()) != null) && !line.equals("") && !line.equals(CRLF))
{
httpRequest += line;
httpRequest += CRLF;
}
if (!httpRequest.equals("")) {
String[] request = httpRequest.split(" ");
System.out.println("Client HTTP Request Header: " + CRLF + httpRequest);
if (request[0].equals("GET")) // if line contains get
{
//extract the html filename
if (request[1].equals("/"))
request[1] = "/index.html";
FilePath += request[1];
processRequestGET(FilePath); // process the request
}
else if (request[0].equals("POST"))
{
String requestBody = "";
while(((line = in.readLine()) != null) && !line.equals("") && !line.equals(CRLF))
{
requestBody += line;
requestBody += CRLF;
}
FilePath += request[1];
System.out.println("Client HTTP Request Body: " + CRLF + requestBody);
processRequestPOST(FilePath, requestBody);
}
else if(request[0].equals("DELETE"))
{
FilePath += request[1];
System.out.println("INSIDE DELETE: " + FilePath);
File file = new File(FilePath);
if(file.delete())
{
System.out.println(FilePath + " has been deleted");
}
else
{
System.out.println("File cannot be found");
}
}
}
closeConnection(); // close the socket connection and input/output buffers
}
catch(Exception e) //print error stack trace
{
e.printStackTrace();
}
}
public void processRequestGET(String FilePath) throws Exception
{
File file = new File(FilePath); //create a file variable
if(file.exists() && FilePath.contains(".html")) // if file exists
{if(file.exists() && FilePath.contains(".html")) // if file exists
{
Scanner reader = new Scanner(new File (FilePath));
//sent the HTTP head (HTTP 200 OK)
out.print("HTTP/1.0 200 OK" + CRLF); // HTTP/1.1 results in a failure in showing the content of the file
Date date = new Date();
out.print("Date: " + date.toString() + CRLF);
out.print("Server: java tiny web server" + CRLF);
out.print("Content-Type: text/html" + CRLF);
out.print("Content-Length: " + file.length() + CRLF);
out.println("Content-Type: text/html; charset=iso-8859-1" + CRLF);
//end of http header
String line = "";
while(reader.hasNextLine()) //read a line from the html file
{
line = reader.nextLine();
out.println(line); //write the line to the socket connection
}
reader.close(); // must close reader to allow further access of file
}
}
else if (!file.exists()) //if file does not exists
{
//sent the HTTP head (404 Not Found)
out.print("HTTP/1.1 404 Not Found" + CRLF);
Date date = new Date();
out.print("Date: " + date.toString() + CRLF);
out.print("Server: java tiny web server" + CRLF);
out.print("Connection: close" + CRLF);
out.println("Content-Type: text/html; charset=iso-8859-1" + CRLF);
//end of http header
//send file not found message
file = new File("D:/htdocs/404.html");
Scanner reader = new Scanner(new FileReader(file));
String line;
while (reader.hasNextLine())
{
line = reader.nextLine();
out.println(line);
}
reader.close();
}
else
{
//sent the HTTP head (415 Unsupported Media Type)
out.print("HTTP/1.1 415 Unsupported Media Type" + CRLF);
Date date = new Date();
out.print("Date: " + date.toString() + CRLF);
out.print("Server: java tiny web server" + CRLF);
out.print("Connection: close" + CRLF);
out.println("Content-Type: text/html; charset=iso-8859-1" + CRLF);
//end of http header
//send file not found message
file = new File("D:/htdocs/415.html");
Scanner reader = new Scanner(new FileReader(file));
String line;
while (reader.hasNextLine())
{
line = reader.nextLine();
out.println(line);
}
reader.close();
}
}
public void processRequestPOST(String FilePath, String RequestBody) throws Exception {
File file = new File(FilePath);
FileWriter writer = new FileWriter(FilePath);
if (file.exists())
{
writer.flush();
}
else
{
file.createNewFile();
}
writer.write(RequestBody);
writer.flush();
processRequestGET(FilePath);
writer.close();
}
private void closeConnection()
{
try
{
out.close(); // close output stream
in.close(); // close input stream
socket.close(); //close socket
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}