--- /work/DG834_PN/apps/mini_httpd-1.17beta1/mini_httpd.c	2005-08-30 17:09:24.000000000 +0800
+++ /work/DG834_AR7_2PLUS_CA/apps/mini_httpd-1.17beta1/mini_httpd.c	2005-09-08 16:40:25.000000000 +0800
@@ -53,6 +53,7 @@
 #include <netdb.h>
 #include <dirent.h>
 #include <sys/sysinfo.h>
+#include <grp.h>
 #include "port.h"
 #include "match.h"
 #include "tdate_parse.h"
@@ -188,6 +189,7 @@
 /* Ron */
 #define LOGOUT kill(getppid(),SIGHUP)
 #define SAVETIME kill(getppid(),SIGUSR2)
+static char *default_page=NULL; /* Default page */
 static char *http_realm=NULL; /* Auth title */
 static int  web_timeout=5*60; /* timeout */
 static time_t last_access_time=0; /* last access time*/
@@ -208,16 +210,16 @@
 
 static void handle_logout( int sig )
 {
-	usleep(1);
 	last_access_time=0;
 	still_timeout=0;
+	/* 2005.08.31 add by Joel for TMSS */
+	strcpy(last_remote_ip,"");
 }
   
 static void handle_web_time( int sig )
 {
 	struct sysinfo info;
 	sysinfo(&info);
-	usleep(1);
 	last_access_time=info.uptime;
 }
 static int check_timeout(void)
@@ -226,24 +228,20 @@
 	sysinfo(&info);
 	
 	if(strcmp(last_remote_ip,remote_ip)==0 && still_timeout==1){
-		usleep(1);
 		return 1;
 	}
 	
-	if(last_access_time==0){
-		usleep(1);
+	if(last_access_time==0)
 		return 0;
-	}
 	else if(((info.uptime-last_access_time)>web_timeout) && web_timeout!=0){ 
-		usleep(1);
 		return 1;
 	}
+
 	return 0;
 			
 }
 static void handle_timeout_stat(int sig)
 {
-	usleep(1);
 	/* enter timeout stat */
 	still_timeout=1;
 }
@@ -261,6 +259,7 @@
 static int method;
 static char* path;
 static char fakepath[128]="";
+static char firstdir[128]="";  //david
 static char* file;
 static char* pathinfo;
 struct stat sb;
@@ -362,7 +361,6 @@
     usockaddr usa;
     int sz, r;
     char* cp;
-    struct sysinfo info;
 
     /* Parse args. */
     argv0 = argv[0];
@@ -481,6 +479,12 @@
 	    ++argn;
 	    max_age = atoi( argv[argn] );
 	    }
+	/* Ron add for CA */
+	else if (strcmp ( argv[argn], "-f" ) == 0 && argn + 1 < argc )
+	    {
+	    ++argn;
+	    default_page = argv[argn];
+	    }
 	else
 	    exit(1);//usage();
 	++argn;
@@ -1266,14 +1270,90 @@
     if ( method_str == (char*) 0 )
 	send_error( 400, "Bad Request", "", "Can't parse request." );
     path = strpbrk( method_str, " \t\n\r" );
+	
     if ( path == (char*) 0 )
 	send_error( 400, "Bad Request", "", "Can't parse request." );
     
     *path++ = '\0';
+    
+    /* Parse the rest of the request headers. */
+    while ( ( line = get_request_line() ) != (char*) 0 )
+        {
+        if ( line[0] == '\0' )
+            break;
+        else if ( strncasecmp( line, "Authorization:", 14 ) == 0 )
+            {
+            cp = &line[14];
+            cp += strspn( cp, " \t" );
+            authorization = cp;
+            }
+        else if ( strncasecmp( line, "Content-Length:", 15 ) == 0 )
+            {
+            cp = &line[15];
+            cp += strspn( cp, " \t" );
+            content_length = atol( cp );
+            }
+        else if ( strncasecmp( line, "Content-Type:", 13 ) == 0 )
+            {
+            cp = &line[13];
+            cp += strspn( cp, " \t" );
+            content_type = cp;
+            }
+        else if ( strncasecmp( line, "Cookie:", 7 ) == 0 )
+            {
+            cp = &line[7];
+            cp += strspn( cp, " \t" );
+            cookie = cp;
+            }
+        else if ( strncasecmp( line, "Host:", 5 ) == 0 )
+            {
+            cp = &line[5];
+            cp += strspn( cp, " \t" );
+            host = cp;
+            }
+        else if ( strncasecmp( line, "If-Modified-Since:", 18 ) == 0 )
+            {
+            cp = &line[18];
+            cp += strspn( cp, " \t" );
+            if_modified_since = tdate_parse( cp );
+            }
+        else if ( strncasecmp( line, "Referer:", 8 ) == 0 )
+            {
+            cp = &line[8];
+            cp += strspn( cp, " \t" );
+            referer = cp;
+            }
+        else if ( strncasecmp( line, "User-Agent:", 11 ) == 0 )
+            {
+            cp = &line[11];
+            cp += strspn( cp, " \t" );
+            useragent = cp;
+            }
+        }
+
+    /* Ron hack for CA */
+    if(access("/tmp/blank_state.out",F_OK)==0
+	    && (!strstr(host,"routerlogin") 
+		    || (strstr(host,"routerlogin") && strncmp(path,"/ ",2)==0))){
+	    
+	    if(default_page)
+		sprintf(fakepath, "/ca/setup.cgi?next_file=%s HTTP/1.1\r\n",default_page);
+	    else
+		sprintf(fakepath, "/ca/setup.cgi?next_file=%s HTTP/1.1\r\n","indexca.htm");
+	path = fakepath;
+    }
+
     /* Ron add for auto add setup.cgi?next_file*/
     if(strstr(path,".cgi")==NULL && strstr(path,".htm")){
-	if(*path=='/') path++;
-	sprintf(fakepath,"/setup.cgi?next_file=%s",path);
+	char *pt;
+        strncpy(fakepath, path, strlen(path)-8);
+        pt=strrchr(fakepath,'/' );
+        strncpy(firstdir,fakepath,pt-fakepath);
+
+        if(*path=='/') path += strlen(firstdir)+1;
+//path++;
+        //if(*firstdir=='/') firstdir++;
+	sprintf(fakepath,"%s/setup.cgi?next_file=%s",firstdir,path);
 	path=fakepath;
     }
     
@@ -1290,7 +1370,7 @@
 	*query++ = '\0';
 
     /* Parse the rest of the request headers. */
-    while ( ( line = get_request_line() ) != (char*) 0 )
+/*    while ( ( line = get_request_line() ) != (char*) 0 )
 	{
 	if ( line[0] == '\0' )
 	    break;
@@ -1343,7 +1423,7 @@
 	    useragent = cp;
 	    }
 	}
-
+*/
     if ( strcasecmp( method_str, get_method_str( METHOD_GET ) ) == 0 )
 	method = METHOD_GET;
     else if ( strcasecmp( method_str, get_method_str( METHOD_HEAD ) ) == 0 )
@@ -1357,8 +1437,16 @@
     if ( path[0] != '/' )
 	send_error( 400, "Bad Request", "", "Bad filename." );
  /*Ron*/
-    if ( someone_in_use == 1 )
+    if ( someone_in_use == 1 ) {
+	/* 2005.07.27 add by Joel for TMSS */
+	if(strncmp(path, "/trend/", 7) \
+	&& strncmp(path, "/html/", 6)	\
+	&& strncmp(path, "/images/", 8)	\
+	&& strncmp(path, "/javascripts/", 13) \
+	&& strncmp(path, "/css/", 5))
 	send_error( 401, "Unauthorized", "", "Another Administrator online." );
+    } else if(!strncmp(path, "/trend/", 7) || !strncmp(path, "/html/", 6))
+		strcpy(last_remote_ip,"");
  /*Ron*/
     
     file = &(path[1]);
@@ -1367,8 +1455,10 @@
 	file = "./";
     if ( file[0] == '/' ||
 	 ( file[0] == '.' && file[1] == '.' &&
-	   ( file[2] == '\0' || file[2] == '/' ) ) )
+	   ( file[2] == '\0' || file[2] == '/' ) ) ){
 	send_error( 400, "Bad Request", "", "Illegal filename." );
+    }
+
     if ( vhost )
 	file = virtual_file( file );
 
@@ -1376,8 +1466,11 @@
     r = stat( file, &sb );
     if ( r < 0 )
 	r = get_pathinfo();
-    if ( r < 0 )
+    if ( r < 0 ){
 	send_error( 404, "Not Found", "", "File not found." );
+    }
+    
+    
     file_len = strlen( file );
     if ( ! S_ISDIR( sb.st_mode ) )
 	{
@@ -1427,7 +1520,6 @@
 //	do_dir();
 
 	got_one:
-		;
 	}
 
 #ifdef USE_SSL
@@ -2436,7 +2528,7 @@
 
     send_error_body( s, title, text );
 
-    send_error_tail();
+//    send_error_tail();
 
     send_response();
 
@@ -2530,16 +2622,13 @@
 	buflen = snprintf( buf, sizeof(buf), "-->\n" );
 	add_to_response( buf, buflen );
 	}
-#if 0
+
     buflen = snprintf( buf, sizeof(buf), "\
 <HR>\n\
 <ADDRESS><A HREF=\"%s\">%s</A></ADDRESS>\n\
 </BODY>\n\
 </HTML>\n",
 	SERVER_URL, SERVER_SOFTWARE );
-#else
-    buflen = snprintf( buf, sizeof(buf), "</BODY>\n</HTML>\n");
-#endif
     add_to_response( buf, buflen );
     }
 
