소스 검색

完成:状态码判断;置顶后缀不缓存

oott123 12 년 전
부모
커밋
169263c1d9
3개의 변경된 파일41개의 추가작업 그리고 30개의 파일을 삭제
  1. 29 23
      include/controller.php
  2. 10 5
      include/lib.php
  3. 2 2
      index.php

+ 29 - 23
include/controller.php

@@ -40,26 +40,19 @@ class controller{
 				$this->succeed = FALSE;
 			}else{
 				//匹配文件后缀
-//				$temp = array();
-//				if(preg_match('/\.(jpg|jpeg|png|pdf|gif|css|js|zip)$/i', $request,$temp)===1){//暂时先就这几种
-//					//http://en.wikipedia.org/wiki/Internet_media_type#List_of_common_media_types
-//					switch($temp[1]){
-//						case 'jpg':{$this->content_type="image/jpeg";}break;
-//						case 'gif':{$this->content_type="image/gif";}break;
-//						case 'png':{$this->content_type="image/png";}break;
-//						case 'css':{$this->content_type="text/css";}break;
-//						case 'js':{$this->content_type="text/javascript";}break;
-//					}
-//				}
 				$mime_types = array(
 					'jpg' => 'image/jpeg',
+					'jpeg' => 'image/jpeg',
 					'gif' => 'image/gif',
 					'png' => 'image/png',
+					'ico' => 'image/jpeg',
 					'css' => 'text/css',
 					'txt' => 'text/plain',
 					'js' => 'text/javascript',
 					'html' => 'text/html',
-					'htm' => 'text/htm',
+					'htm' => 'text/html',
+					'php' => 'text/html',
+					'asp' => 'text/html',
 					'rss' => 'application/atom+xml',
 					'json' => 'application/json',
 					'ogg' => 'audio/ogg',
@@ -67,6 +60,8 @@ class controller{
 					'xml' => 'text/xml',
 					'zip' => 'application/zip',
 					'rar' => 'application/octet-stream',
+					'exe' => 'application/octet-stream',
+					'chm' => 'application/octet-stream',
 					'gz' => 'application/gzip',
 					'gzip' => 'application/gzip',
 					'wav' => 'audio/vnd.wave',
@@ -77,7 +72,11 @@ class controller{
 				$basename = basename($request);
 				$ext = strtolower(substr($basename,strrpos($basename,'.')+1));
 				if(isset($mime_types[$ext])){
-					$this->content_type=$mime_types[$ext];
+					//$this->content_type=$mime_types[$ext];
+				}
+				$direct = false;
+				if(in_array($ext,explode('|',strtolower(DIRECT_EXT)))){
+					$direct = true;
 				}
 			}
 		}
@@ -90,14 +89,14 @@ class controller{
 		}
 		$key = md5($request).'_'.strlen($request).'.cache';
 		$this->hit = $key;
-		$this->handle($request,$key,$delete);
+		$this->handle($request,$key,$delete,$direct);
 		
 	}
 	/**
 	 * 获取内容并输出
 	 * 如果stroage里面不存在,则从URL里面获取
 	 * */
-	private function handle($filename,$key,$delete = false){
+	private function handle($filename,$key,$delete = false,$direct = false){
 		$content = '';
 		if($this->succeed){
 			$storage = storage::gethandle();
@@ -108,21 +107,28 @@ class controller{
 				$return = $storage->delete($key);
 				die(json_encode(array('purge'=>$filename,'key'=>$key,'success'=>$return)));
 			}
-			if($storage->exists($key)){
+			if($storage->exists($key) && !$direct){
 				if($url = $storage->url($key)){
 					$this->locate($url);
 				}
 				$content = $storage->read($key);
+				if(empty($content)){
+					$this->succeed = false;
+					$this->error_type = 'empty_conent';
+				}
 			}else{
 				//$content = @file_get_contents(BASE_URL.$filename);
 				$content = lib::fetch_url(BASE_URL.$filename);
-				$storage->write($key, $content);
-			}
-			if(empty($content)){
-				$this->error_type = 'empty_content';
-				$this->succeed = FALSE;
-			}else{
-				//这里应该有更多的检查
+				if(!is_array($content) || count($content)<2){
+					$this->succeed = false;
+					$this->error_type = 'fetch_error';
+				}elseif($content[0]==200){
+					//返回200,才写入
+					if(!$direct) $storage->write($key, $content[1]);
+				}else{
+					header('HTTP/1.1 '.$content[0]);
+				}
+				$content = $content[1];
 			}
 		}
 		//显示内容

+ 10 - 5
include/lib.php

@@ -26,7 +26,7 @@ class lib{
 					$f->setHeader('Host',STATIC_HOST);
 				}
 				$content = $f->fetch($url);
-				return $content;
+				return array($f->HttpCode(),$content);
 			break;
 			case 'BAE':
 			case 'LOCAL':
@@ -42,18 +42,23 @@ class lib{
 						curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 					}
 					if(STATIC_HOST){
-						curl_setopt($ch, CURLOPT_HTTPHEADER, 'Host: '.STATIC_HOST);
+						curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: '.STATIC_HOST));
 					}
-					return curl_exec($ch);
+					$con = curl_exec($ch);
+					$cod = curl_getinfo($ch,CURLINFO_HTTP_CODE);
+					return array($cod,$con);
 				}else{
 					//否则使用file_get_contents
+					$content = '';
 					if(STATIC_HOST){
 						$opt=array('http'=>array('header'=>'Host: '.STATIC_HOST));
 						$context=stream_context_create($opt);
-						return file_get_contents($url,false,$context);
+						$content = file_get_contents($url,false,$context);
 					}else{
-						return file_get_contents($url);
+						$content = file_get_contents($url);
 					}
+					list($version,$status_code,$msg) = explode(' ',$http_response_header[0], 3);
+					return array($status_code,$content);
 				}
 		}
 	}

+ 2 - 2
index.php

@@ -4,8 +4,8 @@
 /**
  * 设置源静态文件的根目录的URL地址
  * */
-define('STATIC_URL','http://bbs.its.csu.edu.cn/');
-define('STATIC_HOST','');	//特殊应用下可以填写源站域名,会作为http头的hosts传递,正常情况请留空
+define('STATIC_URL','http://www.baidu.com/');
+define('STATIC_HOST','');	//特殊应用下可以填写源站域名,会作为http头的hosts传递,正常情况请留空
 
 //define('RUN_ENV', 'BAE');	//自定义环境(如不去掉前面的//则自动判断)可选:BAE/SAE/LOCAL 请大写