Ver código fonte

Add timeout to default socket in Http.hx (#8646)

* Updated Http.hx

Updated the default socket to have a timeout of 10 seconds

* Use the cnxTimeout variable instead of magic number

* fix settimeout on lua

* oops

* maybe sys.net.Socket will work on PHP?
qwertykg 6 anos atrás
pai
commit
c34e46ff76
3 arquivos alterados com 10 adições e 6 exclusões
  1. 8 2
      std/lua/_std/sys/net/Socket.hx
  2. 1 0
      std/lua/lib/luasocket/Socket.hx
  3. 1 4
      std/sys/Http.hx

+ 8 - 2
std/lua/_std/sys/net/Socket.hx

@@ -55,6 +55,7 @@ class Socket {
 	var _socket:LuaSocket;
 
 	var blocking = false;
+	var timeout = null;
 
 	/**
 		Creates a new unconnected socket.
@@ -92,6 +93,7 @@ class Socket {
 		input = new SocketInput(res.result);
 		output = new SocketOutput(res.result);
 		_socket = res.result;
+		_socket.settimeout(timeout);
 	}
 
 	/**
@@ -103,6 +105,7 @@ class Socket {
 			throw 'Socket Listen Error : ${res.message}';
 		res.result.listen(connections);
 		_socket = res.result;
+		_socket.settimeout(timeout);
 	}
 
 	/**
@@ -171,8 +174,11 @@ class Socket {
 		Gives a timeout after which blocking socket operations (such as reading and writing) will abort and throw an exception.
 	**/
 	public inline function setTimeout(timeout:Float):Void {
-		var client:TcpClient = cast _socket;
-		client.settimeout(timeout);
+		this.timeout = timeout;
+		if (_socket != null) {
+			var client:TcpClient = cast _socket;
+			client.settimeout(timeout);
+		}
 	}
 
 	/**

+ 1 - 0
std/lua/lib/luasocket/Socket.hx

@@ -35,4 +35,5 @@ extern class Socket {
 	public static function select(recvt:Table<Int, Socket>, sendt:Table<Int, Socket>, ?timeout:Float):SelectResult;
 	public function close():Void;
 	public function getsockname():AddrInfo;
+	public function settimeout(value:Float, ?mode:TimeoutMode):Void;
 }

+ 1 - 4
std/sys/Http.hx

@@ -110,12 +110,9 @@ class Http extends haxe.http.HttpBase {
 				throw "Https is only supported with -lib hxssl";
 				#end
 			} else {
-				#if php
-				sock = new php.net.Socket();
-				#else
 				sock = new Socket();
-				#end
 			}
+			sock.setTimeout(cnxTimeout);
 		}
 		var host = url_regexp.matched(2);
 		var portString = url_regexp.matched(3);