namespace web.url 


split  = function(url)begin 
    
	//把URL里的\ 替换成/
	if(!url or url=="")
		return null;
		
	url = ..string.replace(url,"@\","/");
	
	var rawurl = url;
	
	var param ;//url参数
	var block;
	var pos = ..string.indexAny(url,"?"); 
	if(pos)
	{
	param = ..string.sub(url,pos+1);
	url = ..string.left(url,pos-1); 
	}
	
	//URL规范二：删除分块
	pos = ..string.indexAny(url,"#");
	if(pos)
	{ 
	block = ..string.sub(url,pos+1);
	url = ..string.left(url,pos-1);
	}
	
	//URL规范二：最后面不能有反斜杠
	if(..string.right( url,1) =="/" )
	   url = ..string.left( url,#url -1 );

    var servicetype = "http"; 
 
    //去掉URL里的 "://" i 和j分别是"://" 在url里的开始位置和结束位置 find
    pos  = ..string.find( url,"://")
 
    //得到协议
    if(pos){
    servicetype = ..string.left(url, pos-1);
    url = ..string.sub(url,pos+3);
    }  
  
    var path = ..io.splitpath(url);
    path.drive = null; 
    path.block = block;
    
    
    path.param = param;
	path.servicetype=servicetype
	
	//找主机
	var turl = ..string.split(url,"/"); 
	path.host = turl[1];
	path.host = ..string.lower(path.host);
	  
    //查找端口   
    path.port =  ( path.servicetype == "https")?443 : 80;//设置端口默认值
    pos = ..string.find( path.host,":")//i2 j2分别是地址中 ":"的位置 
    if( pos ){ 
   	    path.port = ..string.sub(url,pos+1);  
		path.port = tonumber(path.port);
		path.host = ..string.left(url,pos-1);
		if( not path.port)
			return null;
    	
    }  
    
    
    if(#turl>1){//如果有目录 
       path.dir = ..string.sub( path.dir,..string.indexAny(path.dir,"/")   ) //从主机端口号后面 取文件路径 
 
       if( ..string.right( path.dir ,1)!="/")
           path.dir ++= "/"
    }
    else{
        path.dir = "/"
        path.ext = ""; 
        path.name = ""
    }
    path.path  = path.dir;
    if( path.ext != "") 
        path.path ++= path.file;
    else{
        if( path.name != ""){
        path.dir ++= path.name; 
        path.dir ++= "/" 
        }
        path.file = null;
        path.path = path.dir;
    }
     
    if( ..string.right( path.path,1) =="/" )
          path.path = ..string.left( path.path, #(path.path)-1);
   	 
   	//如果有目录且有文件
 	if(path.ext != "" ){ 
   		path.urldir = ..string.left( url, #url -  #path.file)
   	}else{
   	    path.urldir = url;
   	    if(  not ..string.endWith(url,"/") ){
       		path.urldir ++= "/"
   		}
   	}
   	path.urldir = servicetype + "://" + path.urldir
   	
   	if(path.param)
   	   path.path = path.path + "?" + path.param;
   	   
    return  path;       
end;
 
 

encode = function(s){  
var byte;
var bytes = {};
var a,b;

for(i=1;#s;1){ //循环
	byte = s[i];//取字节码
	
	select(byte) {
		case 48;57{//0->9
		..table.push(bytes,byte)
		}
		case 65;90{//a->z
		..table.push(bytes,byte)
		}
		case 97;122 {//A->Z
		..table.push(bytes,byte)
		}
		case 32//space
		{//A->Z
		..table.push(bytes,43)//+
		}
		else
		{ 
		..table.push(bytes,37)//  '%';
		a  = ( (byte& 0xff)>>4)  ;
		b =  byte%16
		..table.push(bytes, ((a)>9 ? (a)+55 : (a)+48) )
		..table.push(bytes, ((b)>9 ? (b)+55 : (b)+48) )
			
		}
		
	}
}
return ..string.pack( bytes ); 
}


function decode(s)
{ 
var byte;
var bytes = {}; 
var a,b;
for(i=1;#s;1){ //循环
	byte =  s[i] //取字节码
	select(byte) {
		case 37{//  '%';
		i++;
		a = s[ i];
		i++;
		b = s[ i];
		
		a = (a>96?(a-32):a) ;
		b = (b>96?(b-32):b) 
		a = ( ( (a)>57 ? (a)-55 : (a)-48) ) 
		b=  ( ( (b)>57 ? (b)-55 : (b)-48) )  
		..table.push(bytes, ( ( ( a << 4 ) &0xff)  + b)  );
		} 
		case 43//space
		{ 
		..table.push(bytes,32)//+
		}
		else
		{ 
		..table.push(bytes,byte)
			
		}
	}
	
	
}
return ..string.pack( bytes ); 
}

fullpath = function(location,url) { 
   if( ..string.find(url,"@://")  ){
       return url; 
   }
   //url = ..string.lower(url);
   if( ..string.startWith(url,"javascript:") )
       return url;
        
   if( not ..string.find(location,"@://")  ){
       location = "http://" + location;
       
   }
   if(   ..string.startWith(url,"./")  or   ..string.startWith(url,".\" ) ){
       url = ..string.sub(url,3); 
   }
  
  
   location = split(location);
   location = location.urldir; 
   
   //识别 '#'这样的链接
   if(!url or  (..string.trim(url,"#")=="") )
      return location;
 
   if( ( url[1] == '\\  '#  ) or (url[1] == '/'#) ) 
   { 
       //如果相对路径指向网站根目录
       var tab = ..string.split(location,"/\");
       var root = location;
       if(#tab>=3) 
       { 
          //取得域名部分
          root =  ..string.join( { ..table.left(tab,3) },"/" ); 
       }
       root = ..string.trimright( root,"\/")
       url = root ++ url;
   }
   else
   {
       //否则简单相加
       root = ..string.trimright( url,"\/")
       url += "/";
       url = location + url;
   }
   
 
   var tab = ..string.split(url,"/\");
   for(k,v in tab){
      if(v ==".."){
          //解析 ../../test.html 这样的路径
          ..table.remove(tab,k);
          ..table.remove(tab,k-1);
       }
   }
 
   
   return   ..string.trimright( ..string.join(tab,"/"),"\/" )
   
};

deunicode = function(html){
  return  decode(  ..string.replace( html,"\\u(\x\x)(\x\x)","%\1%\2") )
}

/*intellisense(web.url)
fullpath(__, ) = 此函数已废弃,请使用inet.url.fullpath
split(__) = 此函数已废弃,请使用inet.url.split
encode(__) = 此函数已废弃,请使用inet.url.encode
decode(__) = 此函数已废弃,请使用inet.url.unEscape
end intellisense*/