瀏覽代碼

Improve neko Http error handling

Pascal Peridont 19 年之前
父節點
當前提交
6f0df336a0
共有 1 個文件被更改,包括 14 次插入1 次删除
  1. 14 1
      std/haxe/Http.hx

+ 14 - 1
std/haxe/Http.hx

@@ -30,6 +30,7 @@ class Http {
 #if neko
 	var responseHeaders : Hash<String>;
 	var postData : String;
+	var status : Int;
 #else js
 	private var async : Bool;
 #end
@@ -206,10 +207,19 @@ class Http {
 			s.write(b.toString());
 			body = readHttpResponse(s);
 			s.close();
-		} catch( e : Int ) {
+		} catch( e : Dynamic ) {
 			onError(Std.string(e));
 			return;
 		}
+		if( status == 0 || status == null ){
+			onError("Response status error");
+			return;
+		}
+		onStatus(status);
+		if( status < 200 || status >= 400 ){
+			onError("Http Error #"+status);
+			return;
+		}
 		if( responseHeaders.get("Transfer-Encoding") == "chunked" )
 			body = unchunk(body);
 		onData(body);
@@ -293,6 +303,9 @@ class Http {
 		}
 		var headers = b.toString().split("\r\n");
 		var response = headers.shift();
+		var rp = response.split(" ");
+		status = Std.parseInt(rp[1]);
+		
 		// remove the two lasts \r\n\r\n
 		headers.pop();
 		headers.pop();