瀏覽代碼

use Eof type.

Nicolas Cannasse 19 年之前
父節點
當前提交
322f6317d5
共有 7 個文件被更改,包括 79 次插入33 次删除
  1. 28 20
      std/haxe/Http.hx
  2. 36 0
      std/neko/io/Eof.hx
  3. 0 2
      std/neko/io/Error.hx
  4. 2 2
      std/neko/io/FileInput.hx
  5. 1 3
      std/neko/io/Input.hx
  6. 10 4
      std/neko/io/SocketInput.hx
  7. 2 2
      std/neko/io/StringInput.hx

+ 28 - 20
std/haxe/Http.hx

@@ -373,29 +373,37 @@ class Http {
 		api.prepare(size);
 		if( size == null ) {
 			sock.shutdown(false,true);
-			while( true ) {
-				var len = sock.input.readBytes(buf,0,bufsize);
-				if( len == 0 )
-					break;
-				if( chunked ) {
-					if( !readChunk(chunk_re,api,buf,len) )
-						break;
-				} else
-					api.writeBytes(buf,0,len);
+			try {
+				while( true ) {
+					var len = sock.input.readBytes(buf,0,bufsize);
+					if( chunked ) {
+						if( !readChunk(chunk_re,api,buf,len) )
+							break;
+					} else
+						api.writeBytes(buf,0,len);
+				}
+			} catch( e : neko.io.Eof ) {
+			} catch( e : Dynamic ) {
+				onError(e);
+				return;
 			}
 		} else {
-			while( size > 0 ) {
-				var len = sock.input.readBytes(buf,0,if( size > bufsize ) bufsize else size);
-				if( len == 0 ) {
-					onError("Transfert aborted");
-					return;
+			try {
+				while( size > 0 ) {
+					var len = sock.input.readBytes(buf,0,if( size > bufsize ) bufsize else size);
+					if( chunked ) {
+						if( !readChunk(chunk_re,api,buf,len) )
+							break;
+					} else
+						api.writeBytes(buf,0,len);
+					size -= len;
 				}
-				if( chunked ) {
-					if( !readChunk(chunk_re,api,buf,len) )
-						break;
-				} else
-					api.writeBytes(buf,0,len);
-				size -= len;
+			} catch( e : neko.io.Eof ) {
+				onError("Transfert aborted");
+				return;
+			} catch( e : Dynamic ) {
+				onError(e);
+				return;
 			}
 		}
 		if( chunked && (chunk_size != null || chunk_buf != null) ) {

+ 36 - 0
std/neko/io/Eof.hx

@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package neko.io;
+
+/**
+	This exception is raised when reading while data is no longer available in the [Input].
+**/
+class Eof {
+	public function new() {
+	}
+	function toString() {
+		return "Eof";
+	}
+}

+ 0 - 2
std/neko/io/Error.hx

@@ -28,8 +28,6 @@ package neko.io;
 	The possible IO errors that can occur
 **/
 enum Error {
-	/** The end of the Input has been reached **/
-	Eof;
 	/** The IO is set into nonblocking mode and some data cannot be read or written **/
 	Blocked;
 	/** An operation has occured while the Input or Output has already been closed **/

+ 2 - 2
std/neko/io/FileInput.hx

@@ -41,7 +41,7 @@ class FileInput extends Input {
 			file_read_char(__f);
 		} catch( e : Dynamic ) {
 			if( untyped __dollar__typeof(e) == __dollar__tarray )
-				throw Error.Eof;
+				throw new Eof();
 			else
 				throw Error.Custom(e);
 		}
@@ -52,7 +52,7 @@ class FileInput extends Input {
 			file_read(__f,untyped s.__s,p,l);
 		} catch( e : Dynamic ) {
 			if( untyped __dollar__typeof(e) == __dollar__tarray )
-				throw Error.Eof;
+				throw new Eof();
 			else
 				throw Error.Custom(e);
 		}

+ 1 - 3
std/neko/io/Input.hx

@@ -67,9 +67,7 @@ class Input {
 					throw Error.Blocked;
 				total.addSub(buf,0,len);
 			}
-		} catch( e : Error ) {
-			if( e != Error.Eof )
-				neko.Lib.rethrow(e);
+		} catch( e : Eof ) {
 		}
 		return total.toString();
 	}

+ 10 - 4
std/neko/io/SocketInput.hx

@@ -39,20 +39,26 @@ class SocketInput extends Input {
 		} catch( e : Dynamic ) {
 			if( e == "Blocking" )
 				throw Error.Blocked;
+			else if( __s == null )
+				throw Error.Custom(e);
 			else
-				throw Error.Eof; // might also be closed socket
+				throw new Eof();
 		}
 	}
 
 	public override function readBytes( buf : String, pos : Int, len : Int ) : Int {
-		return try {
-			socket_recv(__s,untyped buf.__s,pos,len);
+		var r;
+		try {
+			r = socket_recv(__s,untyped buf.__s,pos,len);
 		} catch( e : Dynamic ) {
 			if( e == "Blocking" )
 				throw Error.Blocked;
 			else
-				throw Error.Eof; // might also be closed socket or invalid param...
+				throw Error.Custom(e);
 		}
+		if( r == 0 )
+			throw new Eof();
+		return r;
 	}
 
 	public override function close() {

+ 2 - 2
std/neko/io/StringInput.hx

@@ -40,7 +40,7 @@ class StringInput extends Input {
 
 	public override function readChar() {
 		if( this.len == 0 )
-			throw Error.Eof;
+			throw new Eof();
 		var c = untyped __dollar__sget(s.__s,pos);
 		pos += 1;
 		len -= 1;
@@ -49,7 +49,7 @@ class StringInput extends Input {
 
 	public override function readBytes( buf : String, bpos, blen ) : Int {
 		if( len == 0 && blen > 0 )
-			throw Error.Eof;
+			throw new Eof();
 		if( len < blen )
 			blen = len;
 		untyped __dollar__sblit(buf.__s,bpos,s.__s,pos,blen);