瀏覽代碼

Merge pull request #748 from civetweb/pr-lsp-connect

Fix Lua connect()
bel2125 6 年之前
父節點
當前提交
8f5cf80e90
共有 3 個文件被更改,包括 10 次插入70 次删除
  1. 5 4
      docs/UserManual.md
  2. 0 63
      src/civetweb.c
  3. 5 3
      src/mod_lua.inl

+ 5 - 4
docs/UserManual.md

@@ -826,17 +826,18 @@ connect (function):
     -- of simple socket interface. It returns a socket object with three
     -- methods: send, recv, close, which are synchronous (blocking).
     -- connect() throws an exception on connection error.
+    -- use_ssl is not implemented.
     connect(host, port, use_ssl)
 
     -- Example of using connect() interface:
-    local host = 'code.google.com'  -- IP address or domain name
-    local ok, sock = pcall(connect, host, 80, 1)
+    local host = 'www.example.com'  -- IP address or domain name
+    local ok, sock = pcall(connect, host, 80, 0)
     if ok then
-      sock:send('GET /p/civetweb/ HTTP/1.0\r\n' ..
+      sock:send('GET / HTTP/1.0\r\n' ..
                 'Host: ' .. host .. '\r\n\r\n')
       local reply = sock:recv()
       sock:close()
-      -- reply now contains the web page https://code.google.com/p/civetweb
+      -- reply now contains the web page http://www.example.com/
     end
 
 

+ 0 - 63
src/civetweb.c

@@ -16083,55 +16083,6 @@ reset_per_request_attributes(struct mg_connection *conn)
 }
 
 
-#if 0
-/* Note: set_sock_timeout is not required for non-blocking sockets.
- * Leave this function here (commented out) for reference until
- * CivetWeb 1.9 is tested, and the tests confirme this function is
- * no longer required.
-*/
-static int
-set_sock_timeout(SOCKET sock, int milliseconds)
-{
-        int r0 = 0, r1, r2;
-
-#if defined(_WIN32)
-        /* Windows specific */
-
-        DWORD tv = (DWORD)milliseconds;
-
-#else
-        /* Linux, ... (not Windows) */
-
-        struct timeval tv;
-
-/* TCP_USER_TIMEOUT/RFC5482 (http://tools.ietf.org/html/rfc5482):
- * max. time waiting for the acknowledged of TCP data before the connection
- * will be forcefully closed and ETIMEDOUT is returned to the application.
- * If this option is not set, the default timeout of 20-30 minutes is used.
-*/
-/* #define TCP_USER_TIMEOUT (18) */
-
-#if defined(TCP_USER_TIMEOUT)
-        unsigned int uto = (unsigned int)milliseconds;
-        r0 = setsockopt(sock, 6, TCP_USER_TIMEOUT, (const void *)&uto, sizeof(uto));
-#endif
-
-        memset(&tv, 0, sizeof(tv));
-        tv.tv_sec = milliseconds / 1000;
-        tv.tv_usec = (milliseconds * 1000) % 1000000;
-
-#endif /* _WIN32 */
-
-        r1 = setsockopt(
-            sock, SOL_SOCKET, SO_RCVTIMEO, (SOCK_OPT_TYPE)&tv, sizeof(tv));
-        r2 = setsockopt(
-            sock, SOL_SOCKET, SO_SNDTIMEO, (SOCK_OPT_TYPE)&tv, sizeof(tv));
-
-        return r0 || r1 || r2;
-}
-#endif
-
-
 static int
 set_tcp_nodelay(SOCKET sock, int nodelay_on)
 {
@@ -16593,13 +16544,6 @@ mg_connect_client_impl(const struct mg_client_options *client_options,
 	}
 #endif
 
-	if (0 != set_non_blocking_mode(sock)) {
-		mg_cry_internal(conn,
-		                "Cannot set non-blocking mode for client %s:%i",
-		                client_options->host,
-		                client_options->port);
-	}
-
 	return conn;
 }
 
@@ -17126,9 +17070,6 @@ mg_get_response(struct mg_connection *conn,
 	if (timeout >= 0) {
 		mg_snprintf(conn, NULL, txt, sizeof(txt), "%i", timeout);
 		new_timeout = txt;
-		/* Not required for non-blocking sockets.
-		set_sock_timeout(conn->client.sock, timeout);
-		*/
 	} else {
 		new_timeout = NULL;
 	}
@@ -18064,10 +18005,6 @@ accept_new_connection(const struct socket *listener, struct mg_context *ctx)
 			}
 		}
 
-		/* We are using non-blocking sockets. Thus, the
-		 * set_sock_timeout(so.sock, timeout);
-		 * call is no longer required. */
-
 		/* The "non blocking" property should already be
 		 * inherited from the parent socket. Set it for
 		 * non-compliant socket implementations. */

+ 5 - 3
src/mod_lua.inl

@@ -327,6 +327,7 @@ lsp_connect(lua_State *L)
 		if (!ok) {
 			return luaL_error(L, ebuf);
 		} else {
+			set_blocking_mode(sock);
 			lua_newtable(L);
 			reg_lstring(L, "sock", (const char *)&sock, sizeof(SOCKET));
 			reg_string(L, "host", lua_tostring(L, -4));
@@ -2134,9 +2135,10 @@ prepare_lua_environment(struct mg_context *ctx,
 	 * TODO: Redesign the interface.
 	 */
 	luaL_newmetatable(L, LUASOCKET);
-	lua_pushliteral(L, "__index");
-	luaL_newlib(L, luasocket_methods);
-	lua_rawset(L, -3);
+	/* self.__index = self */
+	lua_pushvalue(L, -1);
+	lua_setfield(L, -2, "__index");
+	luaL_setfuncs(L, luasocket_methods, 0);
 	lua_pop(L, 1);
 	lua_register(L, "connect", lsp_connect);
 #endif