//httpFile 下载文件
namespace inet

class httpFile {

     ctor( url,path,cfg ){
     	this.url = url;
		this.file = ..io.open(path,"a+b" )
		if(!this.file)
			error( '创建文件失败\n请指定正确的存储路径' ); 
		
		if(!cfg){ 
			cfg  = path + ".dowload"
		} 
     }
     
     download = function( headers,referer,accept,flags,postData){
		method = postData?"POST":"GET"
		this.file.seek("end")
		var fileSize = this.file.seek();
		var rangeInfoOld;
		
		var http = self.http();
		if(!http.beginRequest(this.url,method,referer,accept,flags) ){
			this.file.close();
			http.close();
			return null,"请求失败";
		}
		http.writeHeader(headers)
		
		if( fileSize > 0 ) {
    		if( ..io.exist(cfg,4) ) {
    			var content = ..string.load( cfg );
    			if( content ){
     				rangeInfoOld = eval( content ) ;
     				//if( rangeInfoOld.acceptRanges ? ..string.cmp( rangeInfoOld.acceptRanges,"bytes" ) == 0 ) {
     				http.writeHeaderRange(rangeInfoOld,fileSize) 
     				//}
     			}
    		}
		}  
		 
		var ok; 
		ok,err,errCode  = http.send(postData)  
		if(!ok){
			this.file.close();
			http.close();
			return null,err,errCode
		} 
		this.statusCode = err;
		this.contentLength = errCode;
			
		//保存一次断点续传配置
		var rangeInfoNew = http.readHeaderRange()   
		if( this.statusCode == 200 ){
			//如果需要重新下载,存储相关信息,清除原来的文件 
			if( fileSize>0 && rangeInfoOld ){ 
				if( ! this.contentLength || ( ( rangeInfoNew.eTag == rangeInfoOld.eTag) && ( this.contentLength == fileSize) ) ){ 
					http.close()
					this.file.close() 
     				return true,"已下载完成, 无须重新下载!",fileSize
     			}
			}
			if(fileSize!=0){
				this.file.close();
				this.file = ..io.open(path,"w+b" ) 
			}
			rangeInfoNew.contentLength = this.contentLength
			fileSize = 0;
		} 
		elseif( this.statusCode == 206 ) { 
			rangeInfoNew.contentLength = this.contentLength + fileSize
		}
		elseif( this.statusCode == 416 ){
			this.file.close();
     		http.close() 
			if( this.contentLength == 0 ){ 
     			return true,"已下载完成, 无须重新下载!",fileSize
     		}
     		return null,"下载遇到错误, HTTP状态码:" + this.statusCode;
		} 
		else {
			this.file.close();
			return null,"下载遇到错误, HTTP状态码:" + this.statusCode;
		}
		
		..string.save(cfg,..table.tostring(rangeInfoNew) ) 
		 
		//开始下载 
    	if(this.onRecive){	
    		if( this.onReciveBegin )
    			this.onReciveBegin( this.statusCode,this.contentLength,fileSize  )
    		
    		var total = this.contentLength	
			for(str,size in http.eachRead() ){ 
				this.file.write(str)
				if( this.onRecive( str,size,total ) === false ){
					this.file.flush();
    				this.file.close();
    				http.close();
    				return false;
    			}
			} 
		}
		else { 
			for(str,size in http.eachRead() ){ 
				this.file.write(str)
			} 
		} 
		
		this.file.flush();
		this.file.close();
		http.close();  
		return true,,this.contentLength;
     } 
}   

namespace httpFile{
	http = ..inet.http : ..inet.whttp;
	if(!http){
		error('请在导入inet.file以前\n导入 inet.http 或 inet.whttp ',2)
	} 
}

/**intellisense()
inet.httpFile.http = 获取或指定使用的http类\n可指定为inet.http或inet.whttp
inet.httpFile = 下载文件
inet.httpFile(.(URL,本地存储路径,配置文件路径) = 如果不指定配置文件路径\n则配置文件路径为 本地存储路径 + ".download" 
inet.httpFile() = !inet_file.
?inet.file() = !inet_file.
!inet_file.download(.(HTTP头,引用网址,accept,flags,postData) = 下载文件,所有参数都是可选参数.\n下载成功返回true,失败返回false 
!inet_file.statusCode = HTTP状态码
!inet_file.contentLength = 文件长度
!inet_file.onRecive = @.onRecive = function(str,size,contentLength,fileSize){
	
}
!inet_file.onReciveBegin = @.onReciveBegin = function(statusCode,contentLength){
	if( statusCode == 206/*断点续传*/ ){
		 __
	}
}
end intellisense**/

 

