Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 0880651

Browse files
committed
QA file handler
1 parent 9485ccb commit 0880651

File tree

4 files changed

+44
-33
lines changed

4 files changed

+44
-33
lines changed

src/ktt/lib/httpserver/SimpleHttpExchangeImpl.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ static SimpleHttpExchange createSimpleHttpExchange(final HttpExchange exchange){
138138

139139
hasPost = (rawPost = OUT) != null;
140140

141-
// region todo: optimize this
142141
if(hasPost){
143142
final String content_type = requestHeaders.getFirst("Content-type");
144143
if(content_type != null && content_type.startsWith("multipart/form-data")){
@@ -189,7 +188,6 @@ static SimpleHttpExchange createSimpleHttpExchange(final HttpExchange exchange){
189188
}else{
190189
postMap = new HashMap();
191190
}
192-
// endregion
193191

194192
final String rawCookie = requestHeaders.getFirst("Cookie");
195193
cookies = new HashMap<>();

src/ktt/lib/httpserver/handler/DirectoryEntry.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ class DirectoryEntry {
5252
if(isFilesPreloaded){
5353
if(!isWalkthrough){
5454
final File[] listFiles = directory.listFiles();
55-
for(final File file : (listFiles == null) ? new File[0] : listFiles){
56-
files.put(
57-
getContext(adapter.getName(file)),
58-
new FileEntry(file, true, adapter)
59-
);
60-
}
55+
for(final File file : (listFiles == null) ? new File[0] : listFiles)
56+
if(!file.isDirectory())
57+
files.put(
58+
getContext(adapter.getName(file)),
59+
new FileEntry(file, true, adapter)
60+
);
6161
}else{
6262
final Path dirPath = directory.toPath();
6363

@@ -68,8 +68,8 @@ class DirectoryEntry {
6868
final File[] listFiles = pathFile.listFiles();
6969
for(final File file : (listFiles == null) ? new File[0] : listFiles){
7070
try{
71-
DirectoryEntry.this.files.put(
72-
getContext(rel + "/" + adapter.getName(file)),
71+
files.put(
72+
(rel.isEmpty() || rel.equals("/") || rel.equals("\\") ? "" : getContext(rel)) + getContext(adapter.getName(file)),
7373
new FileEntry(file, true, adapter)
7474
);
7575
}catch(final FileNotFoundException ignored){
@@ -178,7 +178,7 @@ public final File getFile(final String path){
178178
}
179179

180180
/**
181-
* Returns the file's bytes after the {@link FileHandlerAdapter} was used or null if it was not found or failed to read.
181+
* Returns the file's bytes after the {@link FileHandlerAdapter} was used or null if it was not found or failed to read. <b>Preload only.</b>
182182
*
183183
* @param path context to check
184184
* @return processed file bytes
@@ -226,11 +226,11 @@ private static String getContext(final String path){
226226
public String toString(){
227227
final StringBuilder OUT = new StringBuilder();
228228
OUT.append("DirectoryEntry") .append("{");
229-
OUT.append("isWalkthrough") .append("= ") .append(isWalkthrough) .append(", ");
230-
OUT.append("isFilePreloaded") .append("= ") .append(isFilesPreloaded) .append(", ");
231-
OUT.append("directory") .append("= ") .append(directory.toString()) .append(", ");
232-
OUT.append("(preloaded) files") .append("= ") .append(files.toString()) .append(", ");
233-
OUT.append("adapter") .append("= ") .append(adapter.toString());
229+
OUT.append("isWalkthrough") .append("= ") .append(isWalkthrough) .append(", ");
230+
OUT.append("isFilePreloaded") .append("= ") .append(isFilesPreloaded) .append(", ");
231+
OUT.append("directory") .append("= ") .append(directory) .append(", ");
232+
OUT.append("(preloaded) files") .append("= ") .append(files) .append(", ");
233+
OUT.append("adapter") .append("= ") .append(adapter);
234234
OUT.append("}");
235235
return OUT.toString();
236236
}

src/ktt/lib/httpserver/handler/FileEntry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ public final File getFile(){
8585
*/
8686
public final byte[] getBytes(){
8787
if(isPreloaded)
88-
return preloadedBytes;
88+
return preloadedBytes; // adapter determined preloaded bytes
8989
else
9090
try{
91-
return Files.readAllBytes(file.toPath());
92-
}catch(final IOException ignored){
91+
return Files.readAllBytes(file.toPath()); // return literal bytes (no preload); adapt bytes in next part
92+
}catch(final IOException e){
9393
return null;
9494
}
9595
}

src/ktt/lib/httpserver/handler/FileHandler.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import ktt.lib.httpserver.SimpleHttpHandler;
66

77
import java.io.*;
8+
import java.nio.file.Files;
89
import java.util.*;
910

1011
/**
@@ -14,7 +15,7 @@
1415
* The <code>directoryName</code> parameter determines the directory's name. Add the files at the top level by keeping this field empty. <br>
1516
* The <code>preload</code> parameter determines if the handler should read the bytes when they are added or read the file at the exchange. <br>
1617
* The <code>walk</code> parameter determines if all the inner directories should be used.
17-
* The handler will not add any null files.
18+
* The handler will not add any null files and will always use the latest file added for a particular context.
1819
*
1920
* @see FileHandlerAdapter
2021
* @see SimpleHttpHandler
@@ -235,7 +236,10 @@ public final void addFile(final String context, final File file, final String fi
235236
*/
236237
public final void addFile(final String context, final File file, final String fileName, final boolean preload){
237238
try{
238-
files.put(getContext(context) + getContext(fileName),new FileEntry(file,preload,adapter));
239+
files.put(
240+
(context.isEmpty() || context.equals("/") || context.equals("\\") ? "" : getContext(context)) + getContext(fileName),
241+
new FileEntry(file,preload,adapter)
242+
);
239243
}catch(final FileNotFoundException ignored){ }
240244
}
241245

@@ -625,8 +629,12 @@ public final void addDirectory(final String context, final File directory, final
625629
*/
626630
public final void addDirectory(final String context, final File directory, final String directoryName, final boolean preload, final boolean walk){
627631
try{
628-
directories.put(getContext(context) + (directoryName.isEmpty() ? "" : getContext(directoryName)), new DirectoryEntry(directory, preload, adapter, walk));
629-
}catch(final IOException ignored){ }
632+
final String target = (context.isEmpty() || context.equals("/") || context.equals("\\") ? "" : getContext(context)) + (directoryName.isEmpty() ? "" : getContext(directoryName));
633+
directories.put(
634+
target.isEmpty() ? "/" : target,
635+
new DirectoryEntry(directory, preload, adapter, walk)
636+
);
637+
}catch(final Exception ignored){ }
630638
}
631639

632640
//
@@ -640,10 +648,10 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException{
640648
if(rel.startsWith(key) && key.startsWith(match))
641649
match = key;
642650

643-
if(!match.isEmpty() && files.containsKey(match)){
644-
final FileEntry entry = files.get(match);
645-
handle(exchange,entry.getFile(),entry.getBytes());
646-
}else{
651+
if(!match.isEmpty() && files.containsKey(match)){ // exact match
652+
final FileEntry entry = files.get(match); // preloaded ? use preloaded bytes : adapt bytes now
653+
handle(exchange,entry.getFile(),entry.isPreloaded() ? entry.getBytes() : adapter.getBytes(entry.getFile(),entry.getBytes()));
654+
}else{ // beginning match
647655
match = "";
648656
for(final String key : directories.keySet())
649657
if(rel.startsWith(key) && key.startsWith(match))
@@ -653,15 +661,20 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException{
653661
final DirectoryEntry entry = directories.get(match);
654662
final String rel2;
655663
try{
656-
rel2 = rel.substring(match.length()+1);
657-
658-
final File file;
659-
if((file = entry.getFile(rel2)) != null){
660-
handle(exchange, file, entry.getBytes(rel2)); return;
664+
rel2 = rel.substring(match.length());
665+
666+
if(entry.isFilesPreloaded()){
667+
final File file;
668+
if((file = entry.getFile(rel2)) != null){
669+
handle(exchange, file, entry.getBytes(rel2)); // use adapted preload
670+
}
671+
}else{
672+
final File file = new File(entry.getDirectory().getAbsolutePath() + "\\" + rel2);
673+
handle(exchange,file,adapter.getBytes(file, Files.readAllBytes(file.toPath()))); // use adapted now
661674
}
662675
}catch(final IndexOutOfBoundsException ignored){ }
663676
}
664-
handle(exchange,null,null);
677+
handle(exchange,null,null); // not found
665678
}
666679
}
667680

0 commit comments

Comments
 (0)