-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaHTTPServer.java
More file actions
369 lines (328 loc) · 9.29 KB
/
JavaHTTPServer.java
File metadata and controls
369 lines (328 loc) · 9.29 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
361
362
363
364
365
366
367
368
369
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.StringTokenizer;
public class JavaHTTPServer implements Runnable
{
static final File WEB_ROOT = new File(".");
static final String DEFAULT_FILE = "index.html";
static final String FILE_NOT_FOUND = "404.html";
static final String METHOD_NOT_SUPPORTED = "not_supported.html";
// port to listen connection
static final int PORT = 8080;
// verbose mode
static final boolean verbose = true;
// Client Connection via Socket Class
private Socket connect;
public JavaHTTPServer(Socket c)
{
connect = c;
}
public static void main(String[] args)
{
try
{
ServerSocket serverConnect = new ServerSocket(PORT);
System.out.println("Server started.\nListening for connections on port : " + PORT + " ...\n");
// we listen until user halts server execution
while (true)
{
JavaHTTPServer myServer = new JavaHTTPServer(serverConnect.accept());
if (verbose)
{
System.out.println("Connection opened. (" + new Date() + ")");
}
// create dedicated thread to manage the client connection
Thread thread = new Thread(myServer);
thread.start();
}
}
catch (IOException e)
{
System.err.println("Server Connection error : " + e.getMessage());
}
}
public void run()
{
// we manage our particular client connection
BufferedReader in = null;
PrintWriter out = null;
BufferedOutputStream dataOut = null;
String fileRequested = null;
FileWriter fw= null;
FileWriter data=null;
FileReader re =null;
BufferedReader br=null;
try
{
// we read characters from the client via input stream on the socket
in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
// we get character output stream to client (for headers)
out = new PrintWriter(connect.getOutputStream());
// get binary output stream to client (for requested data)
dataOut = new BufferedOutputStream(connect.getOutputStream());
int t=0;
int regi=0;
int cofirmp=0;
//Connecting output.txt file
fw=new FileWriter("output.txt",true);
//Connecting Database.txt file
data= new FileWriter("Database.txt",true);
//Connecting Database.txt for read
re= new FileReader("Database.txt");
br=new BufferedReader(re);
// get first line of the request from the client
String input = in.readLine();
// we parse the request with a string tokenizer
StringTokenizer parse = new StringTokenizer(input);
String requclie[]=input.split(" ",3);
String me1=requclie[0];
String queryRequested = requclie[1];
fw.write(input);
fw.write(System.getProperty("line.separator"));
fw.flush();
fw.close();
String query[] = new String [2];
String information="";
String fileop="";
String username="";
String password="";
String allinfo[]= new String[4];
String method = parse.nextToken().toUpperCase(); // we get the HTTP method of the client
// we get file requested
fileRequested = parse.nextToken().toLowerCase();
// we support only GET and HEAD methods, we check
if (!method.equals("GET") && !method.equals("HEAD")&& !method.equals("POST"))
{
if (verbose)
{
System.out.println("501 Not Implemented : " + method + " method.");
}
// we return the not supported file to the client
File file = new File(WEB_ROOT, METHOD_NOT_SUPPORTED);
int fileLength = (int) file.length();
String contentMimeType = "text/html";
//read content to return to client
byte[] fileData = readFileData(file, fileLength);
// we send HTTP Headers with data to client
out.println("HTTP/1.1 501 Not Implemented");
out.println("Server: Java HTTP Server from Sumit: 1.0");
out.println("Date: " + new Date());
out.println("Content-type: " + contentMimeType);
out.println("Content-length: " + fileLength);
out.println(); // blank line between headers and content, very important !
out.flush(); // flush character output stream buffer
// file
dataOut.write(fileData, 0, fileLength);
dataOut.flush();
}
else if(method.equals("GET")||method.equals("HEAD"))
{
// GET or HEAD method
if(queryRequested.startsWith("/login.html?"))
{
System.out.println("////////////////////////");
query=queryRequested.split("\\?",2);
fileop=query[0];
information=query[1];
allinfo=information.split("&",3);
String n;
int p=0;
while((n=br.readLine())!=null)
{
if(t<3)
{
if(n.equals(allinfo[p]))
{
System.out.println("-----Correct details----"+(p+1));
t++;
n=br.readLine();
if(n.equals(allinfo[p+1]))
{
System.out.println("-----Correct details----"+(p+1));
t++;
n=br.readLine();
if(n.equals(allinfo[p+2]))
{
System.out.println("-----Correct details----"+(p+1));
t++;
}
}
}
}
else
{
break;
}
}
}
if(queryRequested.startsWith("/register.html?"))
{
System.out.println("--------yes----------open----");
query=queryRequested.split("\\?",2);
fileop=query[0];
information=query[1];
allinfo=information.split("&",4);
int j=0 ;
int flag=0;
data.write(System.getProperty("line.separator"));
for(String i:allinfo)
{
if(allinfo[2].equals("Confirm"+allinfo[1]))
{
//data.write(System.getProperty("line.separator"));
data.write(i);
System.out.println("wrinting-----");
data.write(System.getProperty("line.separator"));
j++;
if(j==2)
{
data.write("submit=Login");
break;
}
}
else
{
cofirmp=1;
}
}
regi=1;
data.close();
}
if(t==3)
{
fileRequested="welcome.html";
t=0;
}
else if(cofirmp==1)
{
fileRequested="Wrong.html";
regi=0;
cofirmp=0;
}
else if(regi==1 && cofirmp==0)
{
fileRequested="index.html";
regi=0;
}
else if(t>0&&t<3)
{
fileRequested="Wrong.html";
t=0;
}
else if (fileRequested.endsWith("/"))
{
fileRequested += DEFAULT_FILE;
}
File file = new File(WEB_ROOT, fileRequested);
int fileLength = (int) file.length();
String content = getContentType(fileRequested);
if (method.equals("GET"))
{ // GET method so we return content
byte[] fileData = readFileData(file, fileLength);
// send HTTP Headers
out.println("HTTP/1.1 200 OK");
out.println("Server: Java HTTP Server from Sumit : 1.0");
out.println("Date: " + new Date());
out.println("Content-type: " + content);
out.println("Content-length: " + fileLength);
out.println(); // blank line between headers and content, very important !
out.flush(); // flush character output stream buffer
dataOut.write(fileData, 0, fileLength);
dataOut.flush();
}
if (verbose)
{
System.out.println("File " + fileRequested + " of type " + content + " returned");
}
}
}
catch (FileNotFoundException fnfe)
{
try
{
fileNotFound(out, dataOut, fileRequested);
}
catch (IOException ioe)
{
System.err.println("Error with file not found exception : " + ioe.getMessage());
}
}
catch (IOException ioe)
{
System.err.println("Server error : " + ioe);
}
finally
{
try
{
in.close();
out.close();
dataOut.close();
connect.close(); // we close socket connection
}
catch (Exception e)
{
System.err.println("Error closing stream : " + e.getMessage());
}
if (verbose)
{
System.out.println("Connection closed.\n");
}
}
}
private byte[] readFileData(File file, int fileLength) throws IOException
{
FileInputStream fileIn = null;
byte[] fileData = new byte[fileLength];
try
{
fileIn = new FileInputStream(file);
fileIn.read(fileData);
}
finally
{
if (fileIn != null)
fileIn.close();
}
return fileData;
}
// return supported MIME Types
private String getContentType(String fileRequested)
{
if (fileRequested.endsWith(".htm") || fileRequested.endsWith(".html"))
return "text/html";
else
return "text/plain";
}
private void fileNotFound(PrintWriter out, OutputStream dataOut, String fileRequested) throws IOException
{
File file = new File(WEB_ROOT, FILE_NOT_FOUND);
int fileLength = (int) file.length();
String content = "text/html";
byte[] fileData = readFileData(file, fileLength);
out.println("HTTP/1.1 404 File Not Found");
out.println("Server: Java HTTP Server from Sumit : 1.0");
out.println("Date: " + new Date());
out.println("Content-type: " + content);
out.println("Content-length: " + fileLength);
out.println(); // blank line between headers and content, very important !
out.flush(); // flush character output stream buffer
dataOut.write(fileData, 0, fileLength);
dataOut.flush();
if (verbose)
{
System.out.println("File " + fileRequested + " not found");
}
}
}