@@ -34,6 +34,10 @@ public class SimpleServer {
3434 public static boolean placeholderTest = false ;
3535 public static String WEBROOT = "./public" ;
3636 public static ArrayList <String > allowList = new ArrayList <String >();
37+ public static BufferedReader in ;
38+ public static PrintWriter out ;
39+ public static DataOutputStream dataOut ;
40+ public static boolean isFileAvailable = false ;
3741
3842
3943 public static void main (String args []) {
@@ -50,9 +54,10 @@ public static void main(String args[]) {
5054 for (;;) {
5155
5256 Socket client = ss .accept ();
53- BufferedReader in = new BufferedReader (new InputStreamReader (client .getInputStream ()));
54- PrintWriter out = new PrintWriter (client .getOutputStream ());
55- DataOutputStream dataOut = new DataOutputStream (client .getOutputStream ());
57+ in = new BufferedReader (new InputStreamReader (client .getInputStream ()));
58+ out = new PrintWriter (client .getOutputStream ());
59+ dataOut = new DataOutputStream (client .getOutputStream ());
60+
5661 String line ;
5762 int count =0 ;
5863 boolean malformedHeader = false ;
@@ -124,7 +129,8 @@ public static void main(String args[]) {
124129 if (method .equals ("TRACE" )){
125130
126131 }
127- else if (fileRequested .endsWith ("/" ) && (fileRequested .indexOf ('/' ) >= 0 ) && (fileRequested .lastIndexOf ('/' ) > 0 )){
132+
133+ if (fileRequested .endsWith ("/" ) && (fileRequested .indexOf ('/' ) >= 0 ) && (fileRequested .lastIndexOf ('/' ) > 0 )){
128134 fileRequested = fileRequested +"index.html" ;
129135 }
130136 else if (fileRequested .equals ("/" )){
@@ -153,14 +159,7 @@ else if (fileRequested.endsWith("/")) {
153159 if (method .equals ("GET" )) {
154160 boolean isFileAvailable = checkAvailability (fileRequested );
155161 System .out .println ("Is Available : " +isFileAvailable );
156- // if(placeholderTest){
157- //
158- // out.print("999 \r\n");
159- // out.print("Connection: Alive123\r\n");
160- // out.print("\r\n\r\n");
161- //
162- // }
163- //else
162+
164163 if (malformedHeader || missingHost ){
165164 out .print ("HTTP/1.1 400 Bad Request Error \r \n " );
166165 out .println ("Date: " +formatted +"\r \n " );
@@ -187,40 +186,42 @@ else if(missingHost){
187186 if (isFileAvailable ){
188187 File fileNow = null ;
189188 String fileNewName = "" ;
190-
191- // if(fileRequested.indexOf('/') >= 0 && fileRequested.lastIndexOf('/') > 0 && (fileRequested.indexOf('/') != fileRequested.lastIndexOf('/'))){
192- // fileNewName = fileRequested;
193- // fileNow = new File(fileNewName);
194- // newfileLength = fileNow.length();
195- //
196- // }else{
197- // fileNewName = fileRequested;
198- // fileNow = new File(fileNewName);
199- // fileLength = String.valueOf(fileNow.length());
200- // }
189+
201190 System .out .println ("File Req :" +fileRequested );
202191 if (!fileRequested .equals ("./public/" )){
203- verifiedCaseFile = verifyCaseSensitiveFiles (fileNewName );
192+ verifiedCaseFile = verifyCaseSensitiveFiles (fileRequested );
204193 }else {
194+ fileRequested = "/public/" +"index.html" ;
205195 verifiedCaseFile = true ;
206196 }
207197 if (verifiedCaseFile ){
208- System .out .println ("HERE" );
209- String str = "HTTP/1.1 200 OK\r \n " +"Content-Type: " +content +"\r \n " +"Content-Length:" +newfileLength +"\r \n " +"Date: " +formatted +"\r \n " + "Connection: close\r \n " ;
210- dataOut .write (str .getBytes ());
211- dataOut .write ("\r \n " .getBytes ());
212198
213- if (fileRequested .contains (":.html" ) || fileRequested .contains ("%this.html" ) ||
214- fileRequested .contains (".txt" ) || fileRequested .contains ("directory3isempty" )){
199+ String filePath = new File (fileRequested ).getAbsolutePath ().replace ("./" ,"" );
200+ System .out .println ("filePath : " +filePath );
201+ File fileForLength = new File (filePath );
202+ newfileLength = fileForLength .length ();
215203
216- FileReader fr = new FileReader ("." +fileRequested );
204+ String str = "HTTP/1.1 200 OK\r \n " +
205+ "Date: " +formatted +"\r \n " +
206+ "Server :localhost:8090" +"\r \n " +
207+ "Content-Type: " +content +"\r \n " +
208+ "Last-Modified :" +getLastModified (filePath )+"\r \n " +
209+ "Accept-Ranges : bytes" +"\r \n " +
210+ "Content-Length:" +newfileLength +"\r \n " +
211+ "Connection: close" +"\r \n \r \n " ;
212+
213+ dataOut .write (str .getBytes ());
214+
215+ if (newfileLength != 0 ){
216+ FileReader fr = new FileReader (fileRequested );
217217 BufferedReader br = new BufferedReader (fr );
218218 String fileLine ;
219219 while ((fileLine = br .readLine ()) != null ) {
220220 dataOut .write ((fileLine ).getBytes ());
221221 }
222222 dataOut .flush ();
223223 }
224+
224225 else if (fileRequested .contains (".gif" )){
225226
226227 FileReader fr = new FileReader ("." +fileRequested );
@@ -234,19 +235,7 @@ else if(fileRequested.contains(".gif")){
234235 dataOut .flush ();
235236 placeholderTest =true ;
236237 }
237- else if (fileRequested .contains (".jpeg" ) || fileRequested .contains (".jpg" )){
238- System .out .println ("Here :" +fileRequested );
239- FileReader fr = new FileReader (fileRequested );
240- BufferedReader br = new BufferedReader (fr );
241- String fileLine ;
242-
243- while ((fileLine = br .readLine ()) != null ) {
244- dataOut .writeBytes (fileLine );
245- }
246-
247- dataOut .flush ();
248238
249- }
250239
251240
252241 }else {
@@ -286,7 +275,7 @@ else if(fileRequested.contains(".jpeg") || fileRequested.contains(".jpg")){
286275 if (method .equals ("TRACE" )) {
287276 String fileNewName ;
288277 File fileNow ;
289- System .out .println ("Inside TRACE :" +host );
278+ System .out .println ("Inside TRACE :" +content );
290279 if (fileRequested .indexOf ('/' ) >= 0 && fileRequested .lastIndexOf ('/' ) > 0 && (fileRequested .indexOf ('/' ) != fileRequested .lastIndexOf ('/' ))){
291280 fileNewName = "." +fileRequested ;
292281 fileNow = new File (fileNewName );
@@ -297,15 +286,19 @@ else if(fileRequested.contains(".jpeg") || fileRequested.contains(".jpg")){
297286 fileLength = String .valueOf (fileNow .length ());
298287 }
299288 fileRequested = fileRequested .replace ("./public" ,"" );
289+ fileRequested = fileRequested .replace ("index.html" ,"" );
290+
300291 System .out .println ("Here :" +fileRequested );
301- out .print ("HTTP/1.1 200 OK\r \n " );
302- out .print ("Content-Type: " +content +"\r \n " );
303- out .print ("Content-Length: " +fileLength +"\r \n " );
304- out .print ("\r \n " );
305- out .print ("TRACE " +fileRequested +" HTTP/1.1\r \n " );
306- out .print ("Host: " +host +"\r \n " );
307- out .println ("Connection: close \r \n " );
292+ out .print ("HTTP/1.1 200 OK" +"\r \n " );
293+ out .print ("Date: " +formatted +"\r \n " );
294+ out .print ("Server :localhost:8090" +"\r \n " );
295+ out .print ("Connection: close" +"\r \n " );
296+ out .print ("Content-Type: " +content );
308297 out .print ("\r \n \r \n " );
298+ out .print ("TRACE " +fileRequested +" HTTP/1.1" +"\r \n " );
299+ out .print ("Host: " +host +"\r \n " );
300+ out .print ("Connection: close" +"\r \n " );
301+ out .print ("\r \n " );
309302
310303
311304 }
@@ -343,29 +336,44 @@ else if(fileRequested.contains(".jpeg") || fileRequested.contains(".jpg")){
343336
344337 public static boolean checkAvailability (String fileName ){
345338
346-
347339 System .out .println ("file at Check :" +fileName );
348- if (fileName .equals ("./public/" )){
349- fileName = "./public/a1-test/2/index.html" ;
350- return true ;
351- }
352- else if (fileName .indexOf ('/' ) == fileName .lastIndexOf ('/' )){
353- System .out .println ("Here 11" );
354- File f = new File (fileName );
355- return Files .exists (f .toPath ());
356- }
357- else if (fileName .indexOf ('/' ) >= 0 && fileName .lastIndexOf ('/' ) > 0 ){
358- System .out .println ("Here 12" );
359- //fileName = "."+fileName;
360- File f = new File (fileName );
361- return Files .exists (f .toPath ());
362- }
363-
364- else {
365- System .out .println ("Here 13" );
366- File f = new File (fileName );
367- return Files .exists (f .toPath ());
340+ String directory = fileName .substring (0 ,fileName .lastIndexOf ('/' ));
341+ String filename = fileName .substring (fileName .lastIndexOf ('/' )+1 );
342+
343+ System .out .println ("Dir :" +directory );
344+ System .out .println ("File :" +filename );
345+ File currentDir = new File (directory );
346+ if (directory .equals ("./public" )){
347+ filename = "index.html" ;
368348 }
349+ return displayDirectoryContents (currentDir ,filename );
350+
351+ }
352+
353+
354+ public static boolean displayDirectoryContents (File dir ,String filename ) {
355+ System .out .println ("File2 :" +filename );
356+ try {
357+ File [] files = dir .listFiles ();
358+ System .out .println ("LEN : " +files .length );
359+ for (File file : files ) {
360+ System .out .println ("FILE NAME :" +file .getName ());
361+ if (file .isDirectory ()) {
362+ displayDirectoryContents (file ,filename );
363+ } else {
364+ if (file .getName ().equals (filename )){
365+
366+ isFileAvailable = true ;
367+ System .out .println ("Right here" +isFileAvailable );
368+ break ;
369+ }
370+ }
371+ }
372+ }catch (NullPointerException ne ){
373+ isFileAvailable = false ;
374+ }
375+ System .out .println ("Right here11" +isFileAvailable );
376+ return isFileAvailable ;
369377 }
370378
371379 private static String getContentType (String fileRequested ,String method ) {
@@ -396,6 +404,16 @@ public static String getServerTime() {
396404
397405
398406
407+ public static String getLastModified (String filePath ) {
408+ File file = new File (filePath );
409+ Date lastModified = new Date (file .lastModified ());
410+ Calendar calendar = Calendar .getInstance ();
411+ SimpleDateFormat dateFormat = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss z" , Locale .US );
412+ dateFormat .setTimeZone (TimeZone .getTimeZone ("GMT" ));
413+ return dateFormat .format (lastModified );
414+ }
415+
416+
399417 public static boolean verifyCaseSensitiveFiles (String fileDir ){
400418 System .out .println ("URL Encoded ---" +fileDir );
401419 String fileName = fileDir .substring (fileDir .lastIndexOf ('/' )+1 );
@@ -410,4 +428,18 @@ public static boolean verifyCaseSensitiveFiles(String fileDir){
410428 return false ;
411429
412430 }
431+
432+ public static void sendResponse (String methodType ) throws IOException {
433+
434+ if (methodType == "GET" ){
435+ dataOut .writeBytes ("HTTP/1.1 200 OK" );
436+ dataOut .writeBytes ("Date: " +getServerTime ());
437+ dataOut .writeBytes ("Server: localhost" );
438+ dataOut .writeBytes ("Last-Modified: " );
439+ dataOut .writeBytes ("Accept-Ranges: bytes" );
440+ dataOut .writeBytes ("Content-Length: " );
441+ dataOut .writeBytes ("Connection: close" );
442+ dataOut .writeBytes ("Content-Type: text/html" );
443+ }
444+ }
413445}
0 commit comments