mirror of
https://github.com/lunarmodules/luasocket.git
synced 2025-07-18 23:09:56 +02:00
Added gethostname.
Cleaned up TODO. Moved luasocket specific stuff from auxiliar.c to luasocket.c
This commit is contained in:
@ -17,24 +17,7 @@
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void aux_open(lua_State *L)
|
||||
{
|
||||
/* create namespace table */
|
||||
lua_pushstring(L, LUASOCKET_LIBNAME);
|
||||
lua_newtable(L);
|
||||
#ifdef LUASOCKET_DEBUG
|
||||
lua_pushstring(L, "debug");
|
||||
lua_pushnumber(L, 1);
|
||||
lua_rawset(L, -3);
|
||||
#endif
|
||||
/* make version string available so scripts */
|
||||
lua_pushstring(L, "version");
|
||||
lua_pushstring(L, LUASOCKET_VERSION);
|
||||
lua_rawset(L, -3);
|
||||
/* store namespace as global */
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
/* make sure modules know what is our namespace */
|
||||
lua_pushstring(L, "LUASOCKET_LIBNAME");
|
||||
lua_pushstring(L, LUASOCKET_LIBNAME);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
|
55
src/http.lua
55
src/http.lua
@ -25,7 +25,7 @@ TIMEOUT = 60
|
||||
-- default port for document retrieval
|
||||
PORT = 80
|
||||
-- user agent field sent in request
|
||||
USERAGENT = "LuaSocket 2.0"
|
||||
USERAGENT = socket.version
|
||||
-- block size used in transfers
|
||||
BLOCKSIZE = 8192
|
||||
|
||||
@ -429,8 +429,7 @@ local function authorize(reqt, parsed, respt)
|
||||
body_cb = reqt.body_cb,
|
||||
headers = reqt.headers,
|
||||
timeout = reqt.timeout,
|
||||
proxyhost = reqt.proxyhost,
|
||||
proxyport = reqt.proxyport
|
||||
proxy = reqt.proxy,
|
||||
}
|
||||
return request_cb(autht, respt)
|
||||
end
|
||||
@ -471,8 +470,7 @@ local function redirect(reqt, respt)
|
||||
body_cb = reqt.body_cb,
|
||||
headers = reqt.headers,
|
||||
timeout = reqt.timeout,
|
||||
proxyhost = reqt.proxyhost,
|
||||
proxyport = reqt.proxyport
|
||||
proxy = reqt.proxy
|
||||
}
|
||||
respt = request_cb(redirt, respt)
|
||||
-- we pass the location header as a clue we tried to redirect
|
||||
@ -491,7 +489,7 @@ end
|
||||
-----------------------------------------------------------------------------
|
||||
local function request_uri(reqt, parsed)
|
||||
local url
|
||||
if not reqt.proxyhost and not reqt.proxyport then
|
||||
if not reqt.proxy then
|
||||
url = {
|
||||
path = parsed.path,
|
||||
params = parsed.params,
|
||||
@ -522,6 +520,39 @@ local function build_request(data)
|
||||
return reqt
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Connects to a server, be it a proxy or not
|
||||
-- Input
|
||||
-- reqt: the request table
|
||||
-- parsed: the parsed request url
|
||||
-- Returns
|
||||
-- sock: connection socket, or nil in case of error
|
||||
-- err: error message
|
||||
-----------------------------------------------------------------------------
|
||||
local function try_connect(reqt, parsed)
|
||||
reqt.proxy = reqt.proxy or PROXY
|
||||
local host, port
|
||||
if reqt.proxy then
|
||||
local pproxy = socket.url.parse(reqt.proxy)
|
||||
if not pproxy.port or not pproxy.host then
|
||||
return nil, "invalid proxy"
|
||||
end
|
||||
host, port = pproxy.host, pproxy.port
|
||||
else
|
||||
host, port = parsed.host, parsed.port
|
||||
end
|
||||
local sock, ret, err
|
||||
sock, err = socket.tcp()
|
||||
if not sock then return nil, err end
|
||||
sock:settimeout(reqt.timeout or TIMEOUT)
|
||||
ret, err = sock:connect(host, port)
|
||||
if not ret then
|
||||
sock:close()
|
||||
return nil, err
|
||||
end
|
||||
return sock
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Sends a HTTP request and retrieves the server reply using callbacks to
|
||||
-- send the request body and receive the response body
|
||||
@ -562,18 +593,8 @@ function request_cb(reqt, respt)
|
||||
-- fill default headers
|
||||
reqt.headers = fill_headers(reqt.headers, parsed)
|
||||
-- try to connect to server
|
||||
sock, respt.error = socket.tcp()
|
||||
sock, respt.error = try_connect(reqt, parsed)
|
||||
if not sock then return respt end
|
||||
-- set connection timeout so that we do not hang forever
|
||||
sock:settimeout(reqt.timeout or TIMEOUT)
|
||||
ret, respt.error = sock:connect(
|
||||
reqt.proxyhost or PROXYHOST or parsed.host,
|
||||
reqt.proxyport or PROXYPORT or parsed.port
|
||||
)
|
||||
if not ret then
|
||||
sock:close()
|
||||
return respt
|
||||
end
|
||||
-- send request message
|
||||
respt.error = send_request(sock, reqt.method,
|
||||
request_uri(reqt, parsed), reqt.headers, reqt.body_cb)
|
||||
|
@ -36,8 +36,63 @@
|
||||
#include "mime.h"
|
||||
|
||||
/*=========================================================================*\
|
||||
* Exported functions
|
||||
* Declarations
|
||||
\*=========================================================================*/
|
||||
static int global_gethostname(lua_State *L);
|
||||
static int base_open(lua_State *L);
|
||||
|
||||
/* functions in library namespace */
|
||||
static luaL_reg func[] = {
|
||||
{"gethostname", global_gethostname},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Setup basic stuff.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int base_open(lua_State *L)
|
||||
{
|
||||
/* create namespace table */
|
||||
lua_pushstring(L, LUASOCKET_LIBNAME);
|
||||
lua_newtable(L);
|
||||
#ifdef LUASOCKET_DEBUG
|
||||
lua_pushstring(L, "debug");
|
||||
lua_pushnumber(L, 1);
|
||||
lua_rawset(L, -3);
|
||||
#endif
|
||||
/* make version string available so scripts */
|
||||
lua_pushstring(L, "version");
|
||||
lua_pushstring(L, LUASOCKET_VERSION);
|
||||
lua_rawset(L, -3);
|
||||
/* store namespace as global */
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
/* make sure modules know what is our namespace */
|
||||
lua_pushstring(L, "LUASOCKET_LIBNAME");
|
||||
lua_pushstring(L, LUASOCKET_LIBNAME);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
/* define library functions */
|
||||
luaL_openlib(L, LUASOCKET_LIBNAME, func, 0);
|
||||
lua_pop(L, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Gets the host name
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int global_gethostname(lua_State *L)
|
||||
{
|
||||
char name[257];
|
||||
name[256] = '\0';
|
||||
if (gethostname(name, 256) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "gethostname failed");
|
||||
return 2;
|
||||
} else {
|
||||
lua_pushstring(L, name);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes all library modules.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
@ -45,6 +100,7 @@ LUASOCKET_API int luaopen_socket(lua_State *L)
|
||||
{
|
||||
if (!sock_open()) return 0;
|
||||
/* initialize all modules */
|
||||
base_open(L);
|
||||
aux_open(L);
|
||||
tm_open(L);
|
||||
buf_open(L);
|
||||
|
@ -13,7 +13,7 @@
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Current luasocket version
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#define LUASOCKET_VERSION "LuaSocket 2.0 (alpha)"
|
||||
#define LUASOCKET_VERSION "LuaSocket 2.0 (beta)"
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Library's namespace
|
||||
|
@ -153,7 +153,7 @@ static int opt_keepalive(lua_State *L)
|
||||
return opt_boolean(L, SOL_SOCKET, SO_KEEPALIVE);
|
||||
}
|
||||
|
||||
int opt_linger(lua_State *L)
|
||||
static int opt_linger(lua_State *L)
|
||||
{
|
||||
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1);
|
||||
struct linger li;
|
||||
@ -334,7 +334,7 @@ static int meth_settimeout(lua_State *L)
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Creates a master tcp object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int global_create(lua_State *L)
|
||||
static int global_create(lua_State *L)
|
||||
{
|
||||
t_sock sock;
|
||||
const char *err = inet_trycreate(&sock, SOCK_STREAM);
|
||||
|
@ -417,7 +417,7 @@ static int meth_setsockname(lua_State *L)
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Creates a master udp object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int global_create(lua_State *L)
|
||||
static int global_create(lua_State *L)
|
||||
{
|
||||
t_sock sock;
|
||||
const char *err = inet_trycreate(&sock, SOCK_DGRAM);
|
||||
|
Reference in New Issue
Block a user