88import java .io .InputStream ;
99import java .io .OutputStream ;
1010import java .io .UnsupportedEncodingException ;
11+ import java .net .InetSocketAddress ;
1112import java .net .ServerSocket ;
1213import java .net .Socket ;
1314import java .net .URLDecoder ;
3233 * @author chrisws
3334 */
3435public abstract class WebServer {
35- private static final int BUFFER_SIZE = 32768 / 2 ;
36+ private static final int BUFFER_SIZE = 32768 ;
3637 private static final int SEND_SIZE = BUFFER_SIZE / 4 ;
3738 private static final int LINE_SIZE = 128 ;
3839 private static final String UTF_8 = "utf-8" ;
@@ -62,15 +63,15 @@ public void run() {
6263 socketThread .start ();
6364 }
6465
65- protected abstract void deleteFile (String fileName ) throws IOException ;
66+ protected abstract void deleteFile (String remoteHost , String fileName ) throws IOException ;
6667 protected abstract void execStream (InputStream inputStream ) throws IOException ;
67- protected abstract Response getFile (String path , boolean asset ) throws IOException ;
68- protected abstract Collection <FileData > getFileData () throws IOException ;
68+ protected abstract Response getFile (String remoteHost , String path , boolean asset ) throws IOException ;
69+ protected abstract Collection <FileData > getFileData (String remoteHost ) throws IOException ;
6970 protected abstract byte [] decodeBase64 (String data );
7071 protected abstract void log (String message );
7172 protected abstract void log (String message , Exception exception );
72- protected abstract void renameFile (String from , String to ) throws IOException ;
73- protected abstract void saveFile (String fileName , byte [] content ) throws IOException ;
73+ protected abstract void renameFile (String remoteHost , String from , String to ) throws IOException ;
74+ protected abstract void saveFile (String remoteHost , String fileName , byte [] content ) throws IOException ;
7475
7576 /**
7677 * WebServer main loop to be run in a separate thread
@@ -108,13 +109,15 @@ public abstract class AbstractRequest {
108109 final String tokenKey ;
109110 final List <String > headers ;
110111 final InputStream inputStream ;
112+ final String remoteHost ;
111113
112114 public AbstractRequest (Socket socket , String tokenKey ) throws IOException {
113115 this .socket = socket ;
114116 this .tokenKey = tokenKey ;
115117 this .inputStream = socket .getInputStream ();
116118 this .headers = getHeaders ();
117119 this .requestToken = getToken (headers );
120+ this .remoteHost = ((InetSocketAddress ) socket .getRemoteSocketAddress ()).getHostName ();
118121 String first = headers .size () > 0 ? headers .get (0 ) : null ;
119122 String [] fields ;
120123 if (first != null ) {
@@ -387,7 +390,7 @@ private void afterRun() {
387390 */
388391 private Collection <String > getAllFileNames () throws IOException {
389392 Collection <String > result = new ArrayList <>();
390- for (FileData fileData : getFileData ()) {
393+ for (FileData fileData : getFileData (remoteHost )) {
391394 result .add (fileData .fileName );
392395 }
393396 return result ;
@@ -416,13 +419,13 @@ private Response handleDownload(Map<String, Collection<String>> data) throws IOE
416419 result = handleStatus (false , "File list is empty" );
417420 } else if (fileNames .size () == 1 ) {
418421 // plain text download single file
419- result = getFile (fileNames .iterator ().next (), false );
422+ result = getFile (remoteHost , fileNames .iterator ().next (), false );
420423 } else {
421424 // download multiple as zip
422425 ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
423426 ZipOutputStream zipOutputStream = new ZipOutputStream (outputStream );
424427 for (String fileName : fileNames ) {
425- Response response = getFile (fileName , false );
428+ Response response = getFile (remoteHost , fileName , false );
426429 ZipEntry entry = new ZipEntry (fileName );
427430 zipOutputStream .putNextEntry (entry );
428431 response .toStream (zipOutputStream );
@@ -444,7 +447,7 @@ private Response handleFileList() throws IOException {
444447 builder .append ('[' );
445448 long id = 0 ;
446449 char comma = 0 ;
447- for (FileData nextFile : getFileData ()) {
450+ for (FileData nextFile : getFileData (remoteHost )) {
448451 builder .append (comma );
449452 builder .append ('{' );
450453 builder .append ("id" , id ++, true );
@@ -516,7 +519,7 @@ private Response handleDelete(Map<String, FormField> data) throws IOException {
516519 String fileName = getString (data , "fileName" );
517520 Response result ;
518521 try {
519- deleteFile (fileName );
522+ deleteFile (remoteHost , fileName );
520523 log ("Deleted " + fileName );
521524 result = handleFileList ();
522525 } catch (IOException e ) {
@@ -533,7 +536,7 @@ private Response handleRename(Map<String, FormField> data) throws IOException {
533536 String to = getString (data , "to" );
534537 Response result ;
535538 try {
536- renameFile (from , to );
539+ renameFile (remoteHost , from , to );
537540 result = handleStatus (true , "File renamed" );
538541 } catch (IOException e ) {
539542 result = handleStatus (false , e .getMessage ());
@@ -575,7 +578,7 @@ private Response handleUpload(Map<String, FormField> data) throws IOException {
575578 if (fileName == null || content == null ) {
576579 result = handleStatus (false , "Invalid input" );
577580 } else {
578- saveFile (fileName , content );
581+ saveFile (remoteHost , fileName , content );
579582 result = handleStatus (true , "File saved" );
580583 }
581584 } catch (Exception e ) {
@@ -598,7 +601,7 @@ private Response handleWebResponse(String asset) throws IOException {
598601 }
599602 Response result ;
600603 try {
601- result = getFile (path , true );
604+ result = getFile (remoteHost , path , true );
602605 } catch (Exception e ) {
603606 log ("Error: " + e );
604607 result = null ;
0 commit comments