Skip to content

Commit

Permalink
Auto detect index file from web directory
Browse files Browse the repository at this point in the history
added support for auto detect index file from web directory (support
any type of index file (index.*)
  • Loading branch information
Sonu Auti authored and Sonu Auti committed Aug 11, 2018
1 parent 676c993 commit a7b39b4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Binary file not shown.
Binary file modified AndroidWebServer/build/classes/androidhttpweb/TinyWebServer.class
Binary file not shown.
31 changes: 29 additions & 2 deletions AndroidWebServer/src/androidhttpweb/TinyWebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public class TinyWebServer extends Thread {
public static String SERVER_IP="localhost";
public static int SERVER_PORT=9000;
public static boolean isStart=true;
public static String INDEX_FILE_NAME="index.html";


public TinyWebServer(final String ip, final int port) throws IOException {
Expand Down Expand Up @@ -281,7 +282,7 @@ public void processLocation(DataOutputStream out, String location, String postDa
case "/":
//root location, server index file
CONTENT_TYPE = "text/html";
data=readFile(WEB_DIR_PATH+"/index.php");
data=readFile(WEB_DIR_PATH+"/"+INDEX_FILE_NAME);
constructHeader(out, data.length() + "", data);
break;
default:
Expand Down Expand Up @@ -528,6 +529,7 @@ public String readFile(String fileName){
}
}catch(Exception er){
pageNotFound();
return "";
}
return content;
}
Expand All @@ -538,6 +540,7 @@ public static void init(String ip,int port,String public_dir){
SERVER_IP=ip;
SERVER_PORT=port;
WEB_DIR_PATH=public_dir;
scanFileDirectory();

}

Expand Down Expand Up @@ -568,7 +571,31 @@ public static void stopServer(){
}
}

/*//use for testing

//scan for index file
public static void scanFileDirectory(){
boolean isIndexFound=false;
try{
File file=new File(WEB_DIR_PATH);
if(file.isDirectory()){
File[] allFiles=file.listFiles();
for (File allFile : allFiles) {
//System.out.println(allFile.getName().split("\\.")[0]);
if(allFile.getName().split("\\.")[0].equalsIgnoreCase("index")){
TinyWebServer.INDEX_FILE_NAME=allFile.getName();
isIndexFound=true;
}
}
}

}catch(Exception er){}

if(!isIndexFound){
System.out.println("Index file not found !");
}
}

/* //use for testing
public static void main(String[] args) {
try {
Expand Down

0 comments on commit a7b39b4

Please sign in to comment.