mirror of
https://github.com/lammertb/libhttp.git
synced 2025-09-07 11:07:23 +03:00
Set autoformat options and autoformat all source files
This commit is contained in:
@@ -1,3 +1,14 @@
|
|||||||
|
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
|
||||||
|
|
||||||
BasedOnStyle: LLVM
|
BasedOnStyle: LLVM
|
||||||
|
|
||||||
IndentWidth: 4
|
IndentWidth: 4
|
||||||
|
TabWidth: 4
|
||||||
|
UseTab: ForIndentation
|
||||||
|
ColumnLimit: 80
|
||||||
|
|
||||||
Language: Cpp
|
Language: Cpp
|
||||||
|
|
||||||
|
AccessModifierOffset: 2
|
||||||
|
AlignAfterOpenBracket: true
|
||||||
|
AllowShortIfStatementsOnASingleLine: false
|
||||||
|
@@ -59,8 +59,7 @@ struct mg_request_info {
|
|||||||
const char *remote_user; /* Authenticated user, or NULL if no auth
|
const char *remote_user; /* Authenticated user, or NULL if no auth
|
||||||
used */
|
used */
|
||||||
char remote_addr[48]; /* Client's IP address as a string. */
|
char remote_addr[48]; /* Client's IP address as a string. */
|
||||||
long
|
long remote_ip; /* Client's IP address. Deprecated: use remote_addr instead
|
||||||
remote_ip; /* Client's IP address. Deprecated: use remote_addr instead
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
long long content_length; /* Length (in bytes) of the request body,
|
long long content_length; /* Length (in bytes) of the request body,
|
||||||
|
@@ -588,7 +588,8 @@ static __inline void mg_free(void *a) { free(a); }
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* This following lines are just meant as a reminder to use the mg-functions for
|
/* This following lines are just meant as a reminder to use the mg-functions
|
||||||
|
* for
|
||||||
* memory management */
|
* memory management */
|
||||||
#ifdef malloc
|
#ifdef malloc
|
||||||
#undef malloc
|
#undef malloc
|
||||||
@@ -937,8 +938,8 @@ struct mg_connection {
|
|||||||
SSL *ssl; /* SSL descriptor */
|
SSL *ssl; /* SSL descriptor */
|
||||||
SSL_CTX *client_ssl_ctx; /* SSL context for client connections */
|
SSL_CTX *client_ssl_ctx; /* SSL context for client connections */
|
||||||
struct socket client; /* Connected client */
|
struct socket client; /* Connected client */
|
||||||
time_t
|
time_t conn_birth_time; /* Time (wall clock) when connection was
|
||||||
conn_birth_time; /* Time (wall clock) when connection was established */
|
established */
|
||||||
struct timespec
|
struct timespec
|
||||||
req_time; /* Time (since system start) when the request was received */
|
req_time; /* Time (since system start) when the request was received */
|
||||||
int64_t num_bytes_sent; /* Total bytes sent to client */
|
int64_t num_bytes_sent; /* Total bytes sent to client */
|
||||||
@@ -1702,7 +1703,8 @@ static const char *mg_get_response_code_text(int response_code,
|
|||||||
case 417:
|
case 417:
|
||||||
return "Expectation Failed"; /* RFC2616 Section 10.4.18 */
|
return "Expectation Failed"; /* RFC2616 Section 10.4.18 */
|
||||||
case 422:
|
case 422:
|
||||||
return "Unproccessable entity"; /* RFC2518 Section 10.3, RFC4918 Section
|
return "Unproccessable entity"; /* RFC2518 Section 10.3, RFC4918
|
||||||
|
Section
|
||||||
11.2 */
|
11.2 */
|
||||||
case 423:
|
case 423:
|
||||||
return "Locked"; /* RFC2518 Section 10.4, RFC4918 Section 11.3 */
|
return "Locked"; /* RFC2518 Section 10.4, RFC4918 Section 11.3 */
|
||||||
@@ -2312,7 +2314,8 @@ static void set_close_on_exec(SOCKET sock,
|
|||||||
|
|
||||||
int mg_start_thread(mg_thread_func_t f, void *p) {
|
int mg_start_thread(mg_thread_func_t f, void *p) {
|
||||||
#if defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1)
|
#if defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1)
|
||||||
/* Compile-time option to control stack size, e.g. -DUSE_STACK_SIZE=16384 */
|
/* Compile-time option to control stack size, e.g. -DUSE_STACK_SIZE=16384
|
||||||
|
*/
|
||||||
return ((_beginthread((void(__cdecl *)(void *))f, USE_STACK_SIZE, p) ==
|
return ((_beginthread((void(__cdecl *)(void *))f, USE_STACK_SIZE, p) ==
|
||||||
((uintptr_t)(-1L)))
|
((uintptr_t)(-1L)))
|
||||||
? -1
|
? -1
|
||||||
@@ -2777,7 +2780,8 @@ int mg_read_inner(struct mg_connection *conn, void *buf, size_t len) {
|
|||||||
if (conn == NULL)
|
if (conn == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* If Content-Length is not set for a PUT or POST request, read until socket
|
/* If Content-Length is not set for a PUT or POST request, read until
|
||||||
|
* socket
|
||||||
* is closed */
|
* is closed */
|
||||||
if (conn->consumed_content == 0 && conn->content_len == -1) {
|
if (conn->consumed_content == 0 && conn->content_len == -1) {
|
||||||
conn->content_len = INT64_MAX;
|
conn->content_len = INT64_MAX;
|
||||||
@@ -3916,7 +3920,8 @@ static int read_auth_file(struct file *filep,
|
|||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/* everything is invalid for the moment (might change in the future)
|
/* everything is invalid for the moment (might change in the
|
||||||
|
* future)
|
||||||
*/
|
*/
|
||||||
mg_cry(workdata->conn, "%s: syntax error in authorization file: %s",
|
mg_cry(workdata->conn, "%s: syntax error in authorization file: %s",
|
||||||
__func__, workdata->buf);
|
__func__, workdata->buf);
|
||||||
@@ -4071,7 +4076,8 @@ int mg_modify_passwords_file(const char *fname, const char *domain,
|
|||||||
if (fname == NULL || domain == NULL || user == NULL)
|
if (fname == NULL || domain == NULL || user == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* Using the given file format, user name and domain must not contain ':' */
|
/* Using the given file format, user name and domain must not contain ':'
|
||||||
|
*/
|
||||||
if (strchr(user, ':') != NULL)
|
if (strchr(user, ':') != NULL)
|
||||||
return 0;
|
return 0;
|
||||||
if (strchr(domain, ':') != NULL)
|
if (strchr(domain, ':') != NULL)
|
||||||
@@ -6676,7 +6682,8 @@ static void mg_set_request_handler_type(
|
|||||||
if (is_websocket_handler) {
|
if (is_websocket_handler) {
|
||||||
/*
|
/*
|
||||||
assert(handler == NULL);
|
assert(handler == NULL);
|
||||||
assert(is_delete_request || connect_handler!=NULL || ready_handler!=NULL
|
assert(is_delete_request || connect_handler!=NULL ||
|
||||||
|
ready_handler!=NULL
|
||||||
||
|
||
|
||||||
data_handler!=NULL || close_handler!=NULL);
|
data_handler!=NULL || close_handler!=NULL);
|
||||||
*/
|
*/
|
||||||
@@ -7080,7 +7087,8 @@ static void handle_request(struct mg_connection *conn) {
|
|||||||
&is_put_or_delete_request);
|
&is_put_or_delete_request);
|
||||||
callback_handler = NULL;
|
callback_handler = NULL;
|
||||||
|
|
||||||
/* TODO: for the moment, a goto is simpler than some curious
|
/* TODO: for the moment, a goto is simpler than some
|
||||||
|
* curious
|
||||||
* loop. */
|
* loop. */
|
||||||
/* The situation "callback does not handle the request"
|
/* The situation "callback does not handle the request"
|
||||||
* needs to be
|
* needs to be
|
||||||
@@ -7354,7 +7362,8 @@ static int parse_port_string(const struct vec *vec, struct socket *so) {
|
|||||||
splitting
|
splitting
|
||||||
code
|
code
|
||||||
ensure this
|
ensure this
|
||||||
condition */
|
condition
|
||||||
|
*/
|
||||||
if ((len < 0) && ((unsigned)len > (unsigned)vec->len))
|
if ((len < 0) && ((unsigned)len > (unsigned)vec->len))
|
||||||
return 0;
|
return 0;
|
||||||
ch = vec->ptr[len]; /* Next character after the port number */
|
ch = vec->ptr[len]; /* Next character after the port number */
|
||||||
@@ -8238,7 +8247,8 @@ struct mg_connection *mg_connect_websocket_client(
|
|||||||
/* Connection object will be null if something goes wrong */
|
/* Connection object will be null if something goes wrong */
|
||||||
if (conn == NULL || (strcmp(conn->request_info.uri, "101") != 0)) {
|
if (conn == NULL || (strcmp(conn->request_info.uri, "101") != 0)) {
|
||||||
if (!*error_buffer) {
|
if (!*error_buffer) {
|
||||||
/* if there is a connection, but it did not return 101, error_buffer
|
/* if there is a connection, but it did not return 101,
|
||||||
|
* error_buffer
|
||||||
* is
|
* is
|
||||||
* not yet set */
|
* not yet set */
|
||||||
mg_snprintf(conn, error_buffer, error_buffer_size,
|
mg_snprintf(conn, error_buffer, error_buffer_size,
|
||||||
@@ -8310,12 +8320,14 @@ static void process_new_connection(struct mg_connection *conn) {
|
|||||||
keep_alive_enabled =
|
keep_alive_enabled =
|
||||||
!strcmp(conn->ctx->config[ENABLE_KEEP_ALIVE], "yes");
|
!strcmp(conn->ctx->config[ENABLE_KEEP_ALIVE], "yes");
|
||||||
|
|
||||||
/* Important: on new connection, reset the receiving buffer. Credit goes
|
/* Important: on new connection, reset the receiving buffer. Credit
|
||||||
|
goes
|
||||||
to crule42. */
|
to crule42. */
|
||||||
conn->data_len = 0;
|
conn->data_len = 0;
|
||||||
do {
|
do {
|
||||||
if (!getreq(conn, ebuf, sizeof(ebuf), &reqerr)) {
|
if (!getreq(conn, ebuf, sizeof(ebuf), &reqerr)) {
|
||||||
/* The request sent by the client could not be understood by the
|
/* The request sent by the client could not be understood by
|
||||||
|
the
|
||||||
server,
|
server,
|
||||||
or it was incomplete or a timeout. Send an error message and
|
or it was incomplete or a timeout. Send an error message and
|
||||||
close
|
close
|
||||||
@@ -8879,7 +8891,8 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
|
|||||||
ok &= 0 == pthread_cond_init(&ctx->sq_full, NULL);
|
ok &= 0 == pthread_cond_init(&ctx->sq_full, NULL);
|
||||||
ok &= 0 == pthread_mutex_init(&ctx->nonce_mutex, NULL);
|
ok &= 0 == pthread_mutex_init(&ctx->nonce_mutex, NULL);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
/* Fatal error - abort start. However, this situation should never occur
|
/* Fatal error - abort start. However, this situation should never
|
||||||
|
* occur
|
||||||
* in
|
* in
|
||||||
* practice. */
|
* practice. */
|
||||||
mg_cry(fc(ctx), "Cannot initialize thread synchronization objects");
|
mg_cry(fc(ctx), "Cannot initialize thread synchronization objects");
|
||||||
|
41
src/md5.inl
41
src/md5.inl
@@ -58,15 +58,15 @@ typedef struct md5_state_s {
|
|||||||
} md5_state_t;
|
} md5_state_t;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C" {
|
||||||
{
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Initialize the algorithm. */
|
/* Initialize the algorithm. */
|
||||||
MD5_STATIC void md5_init(md5_state_t *pms);
|
MD5_STATIC void md5_init(md5_state_t *pms);
|
||||||
|
|
||||||
/* Append a string to the message. */
|
/* Append a string to the message. */
|
||||||
MD5_STATIC void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes);
|
MD5_STATIC void md5_append(md5_state_t *pms, const md5_byte_t *data,
|
||||||
|
int nbytes);
|
||||||
|
|
||||||
/* Finish the message and return the digest. */
|
/* Finish the message and return the digest. */
|
||||||
MD5_STATIC void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
|
MD5_STATIC void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
|
||||||
@@ -207,13 +207,9 @@ MD5_STATIC void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
|
|||||||
#define T63 0x2ad7d2bb
|
#define T63 0x2ad7d2bb
|
||||||
#define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e)
|
#define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e)
|
||||||
|
|
||||||
|
static void md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/) {
|
||||||
static void
|
md5_word_t a = pms->abcd[0], b = pms->abcd[1], c = pms->abcd[2],
|
||||||
md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/)
|
d = pms->abcd[3];
|
||||||
{
|
|
||||||
md5_word_t
|
|
||||||
a = pms->abcd[0], b = pms->abcd[1],
|
|
||||||
c = pms->abcd[2], d = pms->abcd[3];
|
|
||||||
md5_word_t t;
|
md5_word_t t;
|
||||||
#if BYTE_ORDER > 0
|
#if BYTE_ORDER > 0
|
||||||
/* Define storage only for big-endian CPUs. */
|
/* Define storage only for big-endian CPUs. */
|
||||||
@@ -244,7 +240,9 @@ md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/)
|
|||||||
if (!((data - (const md5_byte_t *)0) & 3)) {
|
if (!((data - (const md5_byte_t *)0) & 3)) {
|
||||||
/* data are properly aligned, a direct assignment is possible */
|
/* data are properly aligned, a direct assignment is possible */
|
||||||
/* cast through a (void *) should avoid a compiler warning,
|
/* cast through a (void *) should avoid a compiler warning,
|
||||||
see https://github.com/bel2125/civetweb/issues/94#issuecomment-98112861 */
|
see
|
||||||
|
https://github.com/bel2125/civetweb/issues/94#issuecomment-98112861
|
||||||
|
*/
|
||||||
X = (const md5_word_t *)(void *)data;
|
X = (const md5_word_t *)(void *)data;
|
||||||
} else {
|
} else {
|
||||||
/* not aligned */
|
/* not aligned */
|
||||||
@@ -391,9 +389,7 @@ md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/)
|
|||||||
pms->abcd[3] += d;
|
pms->abcd[3] += d;
|
||||||
}
|
}
|
||||||
|
|
||||||
MD5_STATIC void
|
MD5_STATIC void md5_init(md5_state_t *pms) {
|
||||||
md5_init(md5_state_t *pms)
|
|
||||||
{
|
|
||||||
pms->count[0] = pms->count[1] = 0;
|
pms->count[0] = pms->count[1] = 0;
|
||||||
pms->abcd[0] = 0x67452301;
|
pms->abcd[0] = 0x67452301;
|
||||||
pms->abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
|
pms->abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
|
||||||
@@ -401,9 +397,8 @@ md5_init(md5_state_t *pms)
|
|||||||
pms->abcd[3] = 0x10325476;
|
pms->abcd[3] = 0x10325476;
|
||||||
}
|
}
|
||||||
|
|
||||||
MD5_STATIC void
|
MD5_STATIC void md5_append(md5_state_t *pms, const md5_byte_t *data,
|
||||||
md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes)
|
int nbytes) {
|
||||||
{
|
|
||||||
const md5_byte_t *p = data;
|
const md5_byte_t *p = data;
|
||||||
int left = nbytes;
|
int left = nbytes;
|
||||||
int offset = (pms->count[0] >> 3) & 63;
|
int offset = (pms->count[0] >> 3) & 63;
|
||||||
@@ -439,15 +434,11 @@ md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes)
|
|||||||
memcpy(pms->buf, p, left);
|
memcpy(pms->buf, p, left);
|
||||||
}
|
}
|
||||||
|
|
||||||
MD5_STATIC void
|
MD5_STATIC void md5_finish(md5_state_t *pms, md5_byte_t digest[16]) {
|
||||||
md5_finish(md5_state_t *pms, md5_byte_t digest[16])
|
|
||||||
{
|
|
||||||
static const md5_byte_t pad[64] = {
|
static const md5_byte_t pad[64] = {
|
||||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
||||||
};
|
|
||||||
md5_byte_t data[8];
|
md5_byte_t data[8];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
389
src/mod_lua.inl
389
src/mod_lua.inl
@@ -3,8 +3,7 @@
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static void *mmap(void *addr, int64_t len, int prot, int flags, int fd,
|
static void *mmap(void *addr, int64_t len, int prot, int flags, int fd,
|
||||||
int offset)
|
int offset) {
|
||||||
{
|
|
||||||
HANDLE fh = (HANDLE)_get_osfhandle(fd);
|
HANDLE fh = (HANDLE)_get_osfhandle(fd);
|
||||||
HANDLE mh = CreateFileMapping(fh, 0, PAGE_READONLY, 0, 0, 0);
|
HANDLE mh = CreateFileMapping(fh, 0, PAGE_READONLY, 0, 0, 0);
|
||||||
void *p = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, (size_t)len);
|
void *p = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, (size_t)len);
|
||||||
@@ -12,10 +11,7 @@ static void *mmap(void *addr, int64_t len, int prot, int flags, int fd,
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void munmap(void *addr, int64_t length)
|
static void munmap(void *addr, int64_t length) { UnmapViewOfFile(addr); }
|
||||||
{
|
|
||||||
UnmapViewOfFile(addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define MAP_FAILED NULL
|
#define MAP_FAILED NULL
|
||||||
#define MAP_PRIVATE 0
|
#define MAP_PRIVATE 0
|
||||||
@@ -33,8 +29,7 @@ static void handle_request(struct mg_connection *);
|
|||||||
static int handle_lsp_request(struct mg_connection *, const char *,
|
static int handle_lsp_request(struct mg_connection *, const char *,
|
||||||
struct file *, struct lua_State *);
|
struct file *, struct lua_State *);
|
||||||
|
|
||||||
static void reg_string(struct lua_State *L, const char *name, const char *val)
|
static void reg_string(struct lua_State *L, const char *name, const char *val) {
|
||||||
{
|
|
||||||
if (name != NULL && val != NULL) {
|
if (name != NULL && val != NULL) {
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, name);
|
||||||
lua_pushstring(L, val);
|
lua_pushstring(L, val);
|
||||||
@@ -42,8 +37,7 @@ static void reg_string(struct lua_State *L, const char *name, const char *val)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reg_int(struct lua_State *L, const char *name, int val)
|
static void reg_int(struct lua_State *L, const char *name, int val) {
|
||||||
{
|
|
||||||
if (name != NULL) {
|
if (name != NULL) {
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, name);
|
||||||
lua_pushinteger(L, val);
|
lua_pushinteger(L, val);
|
||||||
@@ -51,8 +45,7 @@ static void reg_int(struct lua_State *L, const char *name, int val)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reg_boolean(struct lua_State *L, const char *name, int val)
|
static void reg_boolean(struct lua_State *L, const char *name, int val) {
|
||||||
{
|
|
||||||
if (name != NULL) {
|
if (name != NULL) {
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, name);
|
||||||
lua_pushboolean(L, val != 0);
|
lua_pushboolean(L, val != 0);
|
||||||
@@ -61,8 +54,7 @@ static void reg_boolean(struct lua_State *L, const char *name, int val)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void reg_conn_function(struct lua_State *L, const char *name,
|
static void reg_conn_function(struct lua_State *L, const char *name,
|
||||||
lua_CFunction func, struct mg_connection *conn)
|
lua_CFunction func, struct mg_connection *conn) {
|
||||||
{
|
|
||||||
if (name != NULL && func != NULL && conn != NULL) {
|
if (name != NULL && func != NULL && conn != NULL) {
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, name);
|
||||||
lua_pushlightuserdata(L, conn);
|
lua_pushlightuserdata(L, conn);
|
||||||
@@ -71,8 +63,8 @@ static void reg_conn_function(struct lua_State *L, const char *name,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reg_function(struct lua_State *L, const char *name, lua_CFunction func)
|
static void reg_function(struct lua_State *L, const char *name,
|
||||||
{
|
lua_CFunction func) {
|
||||||
if (name != NULL && func != NULL) {
|
if (name != NULL && func != NULL) {
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, name);
|
||||||
lua_pushcclosure(L, func, 0);
|
lua_pushcclosure(L, func, 0);
|
||||||
@@ -80,26 +72,30 @@ static void reg_function(struct lua_State *L, const char *name, lua_CFunction fu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void lua_cry(struct mg_connection *conn, int err, lua_State * L, const char * lua_title, const char * lua_operation)
|
static void lua_cry(struct mg_connection *conn, int err, lua_State *L,
|
||||||
{
|
const char *lua_title, const char *lua_operation) {
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case LUA_OK:
|
case LUA_OK:
|
||||||
case LUA_YIELD:
|
case LUA_YIELD:
|
||||||
break;
|
break;
|
||||||
case LUA_ERRRUN:
|
case LUA_ERRRUN:
|
||||||
mg_cry(conn, "%s: %s failed: runtime error: %s", lua_title, lua_operation, lua_tostring(L, -1));
|
mg_cry(conn, "%s: %s failed: runtime error: %s", lua_title,
|
||||||
|
lua_operation, lua_tostring(L, -1));
|
||||||
break;
|
break;
|
||||||
case LUA_ERRSYNTAX:
|
case LUA_ERRSYNTAX:
|
||||||
mg_cry(conn, "%s: %s failed: syntax error: %s", lua_title, lua_operation, lua_tostring(L, -1));
|
mg_cry(conn, "%s: %s failed: syntax error: %s", lua_title,
|
||||||
|
lua_operation, lua_tostring(L, -1));
|
||||||
break;
|
break;
|
||||||
case LUA_ERRMEM:
|
case LUA_ERRMEM:
|
||||||
mg_cry(conn, "%s: %s failed: out of memory", lua_title, lua_operation);
|
mg_cry(conn, "%s: %s failed: out of memory", lua_title, lua_operation);
|
||||||
break;
|
break;
|
||||||
case LUA_ERRGCMM:
|
case LUA_ERRGCMM:
|
||||||
mg_cry(conn, "%s: %s failed: error during garbage collection", lua_title, lua_operation);
|
mg_cry(conn, "%s: %s failed: error during garbage collection",
|
||||||
|
lua_title, lua_operation);
|
||||||
break;
|
break;
|
||||||
case LUA_ERRERR:
|
case LUA_ERRERR:
|
||||||
mg_cry(conn, "%s: %s failed: error in error handling: %s", lua_title, lua_operation, lua_tostring(L, -1));
|
mg_cry(conn, "%s: %s failed: error in error handling: %s", lua_title,
|
||||||
|
lua_operation, lua_tostring(L, -1));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
mg_cry(conn, "%s: %s failed: error %i", lua_title, lua_operation, err);
|
mg_cry(conn, "%s: %s failed: error %i", lua_title, lua_operation, err);
|
||||||
@@ -107,8 +103,7 @@ static void lua_cry(struct mg_connection *conn, int err, lua_State * L, const ch
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lsp_sock_close(lua_State *L)
|
static int lsp_sock_close(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
if ((num_args == 1) && lua_istable(L, -1)) {
|
if ((num_args == 1) && lua_istable(L, -1)) {
|
||||||
lua_getfield(L, -1, "sock");
|
lua_getfield(L, -1, "sock");
|
||||||
@@ -119,8 +114,7 @@ static int lsp_sock_close(lua_State *L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lsp_sock_recv(lua_State *L)
|
static int lsp_sock_recv(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
char buf[2000];
|
char buf[2000];
|
||||||
int n;
|
int n;
|
||||||
@@ -139,8 +133,7 @@ static int lsp_sock_recv(lua_State *L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lsp_sock_send(lua_State *L)
|
static int lsp_sock_send(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *buf;
|
const char *buf;
|
||||||
size_t len, sent = 0;
|
size_t len, sent = 0;
|
||||||
@@ -163,20 +156,18 @@ static int lsp_sock_send(lua_State *L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct luaL_Reg luasocket_methods[] = {
|
static const struct luaL_Reg luasocket_methods[] = {{"close", lsp_sock_close},
|
||||||
{"close", lsp_sock_close},
|
|
||||||
{"send", lsp_sock_send},
|
{"send", lsp_sock_send},
|
||||||
{"recv", lsp_sock_recv},
|
{"recv", lsp_sock_recv},
|
||||||
{NULL, NULL}
|
{NULL, NULL}};
|
||||||
};
|
|
||||||
|
|
||||||
static int lsp_connect(lua_State *L)
|
static int lsp_connect(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
char ebuf[100];
|
char ebuf[100];
|
||||||
SOCKET sock;
|
SOCKET sock;
|
||||||
|
|
||||||
if ((num_args == 3) && lua_isstring(L, -3) && lua_isnumber(L, -2) && lua_isnumber(L, -1)) {
|
if ((num_args == 3) && lua_isstring(L, -3) && lua_isnumber(L, -2) &&
|
||||||
|
lua_isnumber(L, -1)) {
|
||||||
sock = conn2(NULL, lua_tostring(L, -3), (int)lua_tonumber(L, -2),
|
sock = conn2(NULL, lua_tostring(L, -3), (int)lua_tonumber(L, -2),
|
||||||
(int)lua_tonumber(L, -1), ebuf, sizeof(ebuf));
|
(int)lua_tonumber(L, -1), ebuf, sizeof(ebuf));
|
||||||
if (sock == INVALID_SOCKET) {
|
if (sock == INVALID_SOCKET) {
|
||||||
@@ -189,13 +180,13 @@ static int lsp_connect(lua_State *L)
|
|||||||
lua_setmetatable(L, -2);
|
lua_setmetatable(L, -2);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return luaL_error(L, "connect(host,port,is_ssl): invalid parameter given.");
|
return luaL_error(
|
||||||
|
L, "connect(host,port,is_ssl): invalid parameter given.");
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lsp_error(lua_State *L)
|
static int lsp_error(lua_State *L) {
|
||||||
{
|
|
||||||
lua_getglobal(L, "mg");
|
lua_getglobal(L, "mg");
|
||||||
lua_getfield(L, -1, "onerror");
|
lua_getfield(L, -1, "onerror");
|
||||||
lua_pushvalue(L, -3);
|
lua_pushvalue(L, -3);
|
||||||
@@ -204,8 +195,7 @@ static int lsp_error(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Silently stop processing chunks. */
|
/* Silently stop processing chunks. */
|
||||||
static void lsp_abort(lua_State *L)
|
static void lsp_abort(lua_State *L) {
|
||||||
{
|
|
||||||
int top = lua_gettop(L);
|
int top = lua_gettop(L);
|
||||||
lua_getglobal(L, "mg");
|
lua_getglobal(L, "mg");
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
@@ -215,15 +205,13 @@ static void lsp_abort(lua_State *L)
|
|||||||
lua_error(L);
|
lua_error(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct lsp_var_reader_data
|
struct lsp_var_reader_data {
|
||||||
{
|
|
||||||
const char *begin;
|
const char *begin;
|
||||||
unsigned len;
|
unsigned len;
|
||||||
unsigned state;
|
unsigned state;
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char * lsp_var_reader(lua_State *L, void *ud, size_t *sz)
|
static const char *lsp_var_reader(lua_State *L, void *ud, size_t *sz) {
|
||||||
{
|
|
||||||
struct lsp_var_reader_data *reader = (struct lsp_var_reader_data *)ud;
|
struct lsp_var_reader_data *reader = (struct lsp_var_reader_data *)ud;
|
||||||
const char *ret;
|
const char *ret;
|
||||||
(void)(L); /* unused */
|
(void)(L); /* unused */
|
||||||
@@ -250,29 +238,34 @@ static const char * lsp_var_reader(lua_State *L, void *ud, size_t *sz)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lsp(struct mg_connection *conn, const char *path,
|
static int lsp(struct mg_connection *conn, const char *path, const char *p,
|
||||||
const char *p, int64_t len, lua_State *L)
|
int64_t len, lua_State *L) {
|
||||||
{
|
|
||||||
int i, j, pos = 0, lines = 1, lualines = 0, is_var, lua_ok;
|
int i, j, pos = 0, lines = 1, lualines = 0, is_var, lua_ok;
|
||||||
char chunkname[MG_BUF_LEN];
|
char chunkname[MG_BUF_LEN];
|
||||||
struct lsp_var_reader_data data;
|
struct lsp_var_reader_data data;
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
if (p[i] == '\n') lines++;
|
if (p[i] == '\n')
|
||||||
|
lines++;
|
||||||
if ((i + 1) < len && p[i] == '<' && p[i + 1] == '?') {
|
if ((i + 1) < len && p[i] == '<' && p[i + 1] == '?') {
|
||||||
|
|
||||||
/* <?= ?> means a variable is enclosed and its value should be printed */
|
/* <?= ?> means a variable is enclosed and its value should be
|
||||||
|
* printed */
|
||||||
is_var = ((i + 2) < len && p[i + 2] == '=');
|
is_var = ((i + 2) < len && p[i + 2] == '=');
|
||||||
|
|
||||||
if (is_var) j = i + 2;
|
if (is_var)
|
||||||
else j = i + 1;
|
j = i + 2;
|
||||||
|
else
|
||||||
|
j = i + 1;
|
||||||
|
|
||||||
while (j < len) {
|
while (j < len) {
|
||||||
if (p[j] == '\n') lualines++;
|
if (p[j] == '\n')
|
||||||
|
lualines++;
|
||||||
if ((j + 1) < len && p[j] == '?' && p[j + 1] == '>') {
|
if ((j + 1) < len && p[j] == '?' && p[j + 1] == '>') {
|
||||||
mg_write(conn, p + pos, i - pos);
|
mg_write(conn, p + pos, i - pos);
|
||||||
|
|
||||||
snprintf(chunkname, sizeof(chunkname), "@%s+%i", path, lines);
|
snprintf(chunkname, sizeof(chunkname), "@%s+%i", path,
|
||||||
|
lines);
|
||||||
lua_pushlightuserdata(L, conn);
|
lua_pushlightuserdata(L, conn);
|
||||||
lua_pushcclosure(L, lsp_error, 1);
|
lua_pushcclosure(L, lsp_error, 1);
|
||||||
|
|
||||||
@@ -280,13 +273,16 @@ static int lsp(struct mg_connection *conn, const char *path,
|
|||||||
data.begin = p + (i + 3);
|
data.begin = p + (i + 3);
|
||||||
data.len = j - (i + 3);
|
data.len = j - (i + 3);
|
||||||
data.state = 0;
|
data.state = 0;
|
||||||
lua_ok = lua_load(L, lsp_var_reader, &data, chunkname, NULL);
|
lua_ok =
|
||||||
|
lua_load(L, lsp_var_reader, &data, chunkname, NULL);
|
||||||
} else {
|
} else {
|
||||||
lua_ok = luaL_loadbuffer(L, p + (i + 2), j - (i + 2), chunkname);
|
lua_ok = luaL_loadbuffer(L, p + (i + 2), j - (i + 2),
|
||||||
|
chunkname);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lua_ok) {
|
if (lua_ok) {
|
||||||
/* Syntax error or OOM. Error message is pushed on stack. */
|
/* Syntax error or OOM. Error message is pushed on
|
||||||
|
* stack. */
|
||||||
lua_pcall(L, 1, 0, 0);
|
lua_pcall(L, 1, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
/* Success loading chunk. Call it. */
|
/* Success loading chunk. Call it. */
|
||||||
@@ -315,9 +311,9 @@ static int lsp(struct mg_connection *conn, const char *path,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.write: Send data to the client */
|
/* mg.write: Send data to the client */
|
||||||
static int lsp_write(lua_State *L)
|
static int lsp_write(lua_State *L) {
|
||||||
{
|
struct mg_connection *conn =
|
||||||
struct mg_connection *conn = (struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
(struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *str;
|
const char *str;
|
||||||
size_t size;
|
size_t size;
|
||||||
@@ -334,25 +330,27 @@ static int lsp_write(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.read: Read data from the client (e.g., from a POST request) */
|
/* mg.read: Read data from the client (e.g., from a POST request) */
|
||||||
static int lsp_read(lua_State *L)
|
static int lsp_read(lua_State *L) {
|
||||||
{
|
struct mg_connection *conn =
|
||||||
struct mg_connection *conn = (struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
(struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
int len = mg_read(conn, buf, sizeof(buf));
|
int len = mg_read(conn, buf, sizeof(buf));
|
||||||
|
|
||||||
if (len <= 0) return 0;
|
if (len <= 0)
|
||||||
|
return 0;
|
||||||
lua_pushlstring(L, buf, len);
|
lua_pushlstring(L, buf, len);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* mg.keep_alive: Allow Lua pages to use the http keep-alive mechanism */
|
/* mg.keep_alive: Allow Lua pages to use the http keep-alive mechanism */
|
||||||
static int lsp_keep_alive(lua_State *L)
|
static int lsp_keep_alive(lua_State *L) {
|
||||||
{
|
struct mg_connection *conn =
|
||||||
struct mg_connection *conn = (struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
(struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
|
|
||||||
/* This function may be called with one parameter (boolean) to set the keep_alive state.
|
/* This function may be called with one parameter (boolean) to set the
|
||||||
|
keep_alive state.
|
||||||
Or without a parameter to just query the current keep_alive state. */
|
Or without a parameter to just query the current keep_alive state. */
|
||||||
if ((num_args == 1) && lua_isboolean(L, 1)) {
|
if ((num_args == 1) && lua_isboolean(L, 1)) {
|
||||||
conn->must_close = !lua_toboolean(L, 1);
|
conn->must_close = !lua_toboolean(L, 1);
|
||||||
@@ -361,23 +359,26 @@ static int lsp_keep_alive(lua_State *L)
|
|||||||
return luaL_error(L, "invalid keep_alive() call");
|
return luaL_error(L, "invalid keep_alive() call");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return the current "keep_alive" state. This may be false, even it keep_alive(true) has been called. */
|
/* Return the current "keep_alive" state. This may be false, even it
|
||||||
|
* keep_alive(true) has been called. */
|
||||||
lua_pushboolean(L, should_keep_alive(conn));
|
lua_pushboolean(L, should_keep_alive(conn));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* mg.include: Include another .lp file */
|
/* mg.include: Include another .lp file */
|
||||||
static int lsp_include(lua_State *L)
|
static int lsp_include(lua_State *L) {
|
||||||
{
|
struct mg_connection *conn =
|
||||||
struct mg_connection *conn = (struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
(struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
struct file file = STRUCT_FILE_INITIALIZER;
|
struct file file = STRUCT_FILE_INITIALIZER;
|
||||||
const char *filename = (num_args == 1) ? lua_tostring(L, 1) : NULL;
|
const char *filename = (num_args == 1) ? lua_tostring(L, 1) : NULL;
|
||||||
|
|
||||||
if (filename) {
|
if (filename) {
|
||||||
if (handle_lsp_request(conn, filename, &file, L)) {
|
if (handle_lsp_request(conn, filename, &file, L)) {
|
||||||
/* handle_lsp_request returned an error code, meaning an error occured in
|
/* handle_lsp_request returned an error code, meaning an error
|
||||||
the included page and mg.onerror returned non-zero. Stop processing. */
|
occured in
|
||||||
|
the included page and mg.onerror returned non-zero. Stop processing.
|
||||||
|
*/
|
||||||
lsp_abort(L);
|
lsp_abort(L);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -388,9 +389,9 @@ static int lsp_include(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.cry: Log an error. Default value for mg.onerror. */
|
/* mg.cry: Log an error. Default value for mg.onerror. */
|
||||||
static int lsp_cry(lua_State *L)
|
static int lsp_cry(lua_State *L) {
|
||||||
{
|
struct mg_connection *conn =
|
||||||
struct mg_connection *conn = (struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
(struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *text = (num_args == 1) ? lua_tostring(L, 1) : NULL;
|
const char *text = (num_args == 1) ? lua_tostring(L, 1) : NULL;
|
||||||
|
|
||||||
@@ -404,9 +405,9 @@ static int lsp_cry(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.redirect: Redirect the request (internally). */
|
/* mg.redirect: Redirect the request (internally). */
|
||||||
static int lsp_redirect(lua_State *L)
|
static int lsp_redirect(lua_State *L) {
|
||||||
{
|
struct mg_connection *conn =
|
||||||
struct mg_connection *conn = (struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
(struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *target = (num_args == 1) ? lua_tostring(L, 1) : NULL;
|
const char *target = (num_args == 1) ? lua_tostring(L, 1) : NULL;
|
||||||
|
|
||||||
@@ -422,9 +423,9 @@ static int lsp_redirect(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.send_file */
|
/* mg.send_file */
|
||||||
static int lsp_send_file(lua_State *L)
|
static int lsp_send_file(lua_State *L) {
|
||||||
{
|
struct mg_connection *conn =
|
||||||
struct mg_connection *conn = (struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
(struct mg_connection *)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *filename = (num_args == 1) ? lua_tostring(L, 1) : NULL;
|
const char *filename = (num_args == 1) ? lua_tostring(L, 1) : NULL;
|
||||||
|
|
||||||
@@ -438,8 +439,7 @@ static int lsp_send_file(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.get_time */
|
/* mg.get_time */
|
||||||
static int lsp_get_time(lua_State *L)
|
static int lsp_get_time(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
int monotonic = (num_args > 0) ? lua_toboolean(L, 1) : 0;
|
int monotonic = (num_args > 0) ? lua_toboolean(L, 1) : 0;
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
@@ -452,8 +452,7 @@ static int lsp_get_time(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.get_var */
|
/* mg.get_var */
|
||||||
static int lsp_get_var(lua_State *L)
|
static int lsp_get_var(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *data, *var_name;
|
const char *data, *var_name;
|
||||||
size_t data_len, occurrence;
|
size_t data_len, occurrence;
|
||||||
@@ -465,7 +464,8 @@ static int lsp_get_var(lua_State *L)
|
|||||||
var_name = lua_tostring(L, 2);
|
var_name = lua_tostring(L, 2);
|
||||||
occurrence = (num_args > 2) ? (long)lua_tonumber(L, 3) : 0;
|
occurrence = (num_args > 2) ? (long)lua_tonumber(L, 3) : 0;
|
||||||
|
|
||||||
ret = mg_get_var2(data, data_len, var_name, dst, sizeof(dst), occurrence);
|
ret =
|
||||||
|
mg_get_var2(data, data_len, var_name, dst, sizeof(dst), occurrence);
|
||||||
if (ret >= 0) {
|
if (ret >= 0) {
|
||||||
/* Variable found: return value to Lua */
|
/* Variable found: return value to Lua */
|
||||||
lua_pushstring(L, dst);
|
lua_pushstring(L, dst);
|
||||||
@@ -481,8 +481,7 @@ static int lsp_get_var(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.get_mime_type */
|
/* mg.get_mime_type */
|
||||||
static int lsp_get_mime_type(lua_State *L)
|
static int lsp_get_mime_type(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
struct vec mime_type = {0, 0};
|
struct vec mime_type = {0, 0};
|
||||||
struct mg_context *ctx;
|
struct mg_context *ctx;
|
||||||
@@ -514,8 +513,7 @@ static int lsp_get_mime_type(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.get_cookie */
|
/* mg.get_cookie */
|
||||||
static int lsp_get_cookie(lua_State *L)
|
static int lsp_get_cookie(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *cookie;
|
const char *cookie;
|
||||||
const char *var_name;
|
const char *var_name;
|
||||||
@@ -544,8 +542,7 @@ static int lsp_get_cookie(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.md5 */
|
/* mg.md5 */
|
||||||
static int lsp_md5(lua_State *L)
|
static int lsp_md5(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *text;
|
const char *text;
|
||||||
md5_byte_t hash[16];
|
md5_byte_t hash[16];
|
||||||
@@ -572,8 +569,7 @@ static int lsp_md5(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.url_encode */
|
/* mg.url_encode */
|
||||||
static int lsp_url_encode(lua_State *L)
|
static int lsp_url_encode(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *text;
|
const char *text;
|
||||||
size_t text_len;
|
size_t text_len;
|
||||||
@@ -595,8 +591,7 @@ static int lsp_url_encode(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.url_decode */
|
/* mg.url_decode */
|
||||||
static int lsp_url_decode(lua_State *L)
|
static int lsp_url_decode(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *text;
|
const char *text;
|
||||||
size_t text_len;
|
size_t text_len;
|
||||||
@@ -620,8 +615,7 @@ static int lsp_url_decode(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.base64_encode */
|
/* mg.base64_encode */
|
||||||
static int lsp_base64_encode(lua_State *L)
|
static int lsp_base64_encode(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *text;
|
const char *text;
|
||||||
size_t text_len;
|
size_t text_len;
|
||||||
@@ -649,8 +643,7 @@ static int lsp_base64_encode(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.base64_encode */
|
/* mg.base64_encode */
|
||||||
static int lsp_base64_decode(lua_State *L)
|
static int lsp_base64_decode(lua_State *L) {
|
||||||
{
|
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
const char *text;
|
const char *text;
|
||||||
size_t text_len, dst_len;
|
size_t text_len, dst_len;
|
||||||
@@ -662,16 +655,19 @@ static int lsp_base64_decode(lua_State *L)
|
|||||||
if (text) {
|
if (text) {
|
||||||
dst = (char *)mg_malloc(text_len);
|
dst = (char *)mg_malloc(text_len);
|
||||||
if (dst) {
|
if (dst) {
|
||||||
ret = base64_decode((const unsigned char *)text, text_len, dst, &dst_len);
|
ret = base64_decode((const unsigned char *)text, text_len, dst,
|
||||||
|
&dst_len);
|
||||||
if (ret != -1) {
|
if (ret != -1) {
|
||||||
mg_free(dst);
|
mg_free(dst);
|
||||||
return luaL_error(L, "illegal character in lsp_base64_decode() call");
|
return luaL_error(
|
||||||
|
L, "illegal character in lsp_base64_decode() call");
|
||||||
} else {
|
} else {
|
||||||
lua_pushlstring(L, dst, dst_len);
|
lua_pushlstring(L, dst, dst_len);
|
||||||
mg_free(dst);
|
mg_free(dst);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return luaL_error(L, "out of memory in lsp_base64_decode() call");
|
return luaL_error(L,
|
||||||
|
"out of memory in lsp_base64_decode() call");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
@@ -697,7 +693,8 @@ static int lsp_get_response_code_text(lua_State *L) {
|
|||||||
convert it to the corresponding text. */
|
convert it to the corresponding text. */
|
||||||
code = lua_tonumber(L, 1);
|
code = lua_tonumber(L, 1);
|
||||||
text = mg_get_response_code_text((int)code, NULL);
|
text = mg_get_response_code_text((int)code, NULL);
|
||||||
if (text) lua_pushstring(L, text);
|
if (text)
|
||||||
|
lua_pushstring(L, text);
|
||||||
return text ? 1 : 0;
|
return text ? 1 : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -717,8 +714,7 @@ struct lua_websock_data {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* mg.write for websockets */
|
/* mg.write for websockets */
|
||||||
static int lwebsock_write(lua_State *L)
|
static int lwebsock_write(lua_State *L) {
|
||||||
{
|
|
||||||
#ifdef USE_WEBSOCKET
|
#ifdef USE_WEBSOCKET
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
struct lua_websock_data *ws;
|
struct lua_websock_data *ws;
|
||||||
@@ -744,12 +740,18 @@ static int lwebsock_write(lua_State *L)
|
|||||||
} else if (lua_isstring(L, 1)) {
|
} else if (lua_isstring(L, 1)) {
|
||||||
/* opcode string and message text */
|
/* opcode string and message text */
|
||||||
str = lua_tostring(L, 1);
|
str = lua_tostring(L, 1);
|
||||||
if (!mg_strncasecmp(str, "text", 4)) opcode = WEBSOCKET_OPCODE_TEXT;
|
if (!mg_strncasecmp(str, "text", 4))
|
||||||
else if (!mg_strncasecmp(str, "bin", 3)) opcode = WEBSOCKET_OPCODE_BINARY;
|
opcode = WEBSOCKET_OPCODE_TEXT;
|
||||||
else if (!mg_strncasecmp(str, "close", 5)) opcode = WEBSOCKET_OPCODE_CONNECTION_CLOSE;
|
else if (!mg_strncasecmp(str, "bin", 3))
|
||||||
else if (!mg_strncasecmp(str, "ping", 4)) opcode = WEBSOCKET_OPCODE_PING;
|
opcode = WEBSOCKET_OPCODE_BINARY;
|
||||||
else if (!mg_strncasecmp(str, "pong", 4)) opcode = WEBSOCKET_OPCODE_PONG;
|
else if (!mg_strncasecmp(str, "close", 5))
|
||||||
else if (!mg_strncasecmp(str, "cont", 4)) opcode = WEBSOCKET_OPCODE_CONTINUATION;
|
opcode = WEBSOCKET_OPCODE_CONNECTION_CLOSE;
|
||||||
|
else if (!mg_strncasecmp(str, "ping", 4))
|
||||||
|
opcode = WEBSOCKET_OPCODE_PING;
|
||||||
|
else if (!mg_strncasecmp(str, "pong", 4))
|
||||||
|
opcode = WEBSOCKET_OPCODE_PONG;
|
||||||
|
else if (!mg_strncasecmp(str, "cont", 4))
|
||||||
|
opcode = WEBSOCKET_OPCODE_CONTINUATION;
|
||||||
} else if (lua_isuserdata(L, 1)) {
|
} else if (lua_isuserdata(L, 1)) {
|
||||||
/* client id and message text */
|
/* client id and message text */
|
||||||
client = (struct mg_connection *)lua_touserdata(L, 1);
|
client = (struct mg_connection *)lua_touserdata(L, 1);
|
||||||
@@ -764,12 +766,18 @@ static int lwebsock_write(lua_State *L)
|
|||||||
} else if (lua_isstring(L, 2)) {
|
} else if (lua_isstring(L, 2)) {
|
||||||
/* client id, opcode string and message text */
|
/* client id, opcode string and message text */
|
||||||
str = lua_tostring(L, 2);
|
str = lua_tostring(L, 2);
|
||||||
if (!mg_strncasecmp(str, "text", 4)) opcode = WEBSOCKET_OPCODE_TEXT;
|
if (!mg_strncasecmp(str, "text", 4))
|
||||||
else if (!mg_strncasecmp(str, "bin", 3)) opcode = WEBSOCKET_OPCODE_BINARY;
|
opcode = WEBSOCKET_OPCODE_TEXT;
|
||||||
else if (!mg_strncasecmp(str, "close", 5)) opcode = WEBSOCKET_OPCODE_CONNECTION_CLOSE;
|
else if (!mg_strncasecmp(str, "bin", 3))
|
||||||
else if (!mg_strncasecmp(str, "ping", 4)) opcode = WEBSOCKET_OPCODE_PING;
|
opcode = WEBSOCKET_OPCODE_BINARY;
|
||||||
else if (!mg_strncasecmp(str, "pong", 4)) opcode = WEBSOCKET_OPCODE_PONG;
|
else if (!mg_strncasecmp(str, "close", 5))
|
||||||
else if (!mg_strncasecmp(str, "cont", 4)) opcode = WEBSOCKET_OPCODE_CONTINUATION;
|
opcode = WEBSOCKET_OPCODE_CONNECTION_CLOSE;
|
||||||
|
else if (!mg_strncasecmp(str, "ping", 4))
|
||||||
|
opcode = WEBSOCKET_OPCODE_PING;
|
||||||
|
else if (!mg_strncasecmp(str, "pong", 4))
|
||||||
|
opcode = WEBSOCKET_OPCODE_PONG;
|
||||||
|
else if (!mg_strncasecmp(str, "cont", 4))
|
||||||
|
opcode = WEBSOCKET_OPCODE_CONTINUATION;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -803,8 +811,7 @@ struct laction_arg {
|
|||||||
char txt[1];
|
char txt[1];
|
||||||
};
|
};
|
||||||
|
|
||||||
static int lua_action(struct laction_arg *arg)
|
static int lua_action(struct laction_arg *arg) {
|
||||||
{
|
|
||||||
int err, ok;
|
int err, ok;
|
||||||
struct mg_context *ctx;
|
struct mg_context *ctx;
|
||||||
|
|
||||||
@@ -845,16 +852,14 @@ static int lua_action(struct laction_arg *arg)
|
|||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lua_action_free(struct laction_arg *arg)
|
static int lua_action_free(struct laction_arg *arg) {
|
||||||
{
|
|
||||||
if (lua_action(arg)) {
|
if (lua_action(arg)) {
|
||||||
mg_free(arg);
|
mg_free(arg);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lwebsocket_set_timer(lua_State *L, int is_periodic)
|
static int lwebsocket_set_timer(lua_State *L, int is_periodic) {
|
||||||
{
|
|
||||||
#if defined(USE_TIMERS) && defined(USE_WEBSOCKET)
|
#if defined(USE_TIMERS) && defined(USE_WEBSOCKET)
|
||||||
int num_args = lua_gettop(L);
|
int num_args = lua_gettop(L);
|
||||||
struct lua_websock_data *ws;
|
struct lua_websock_data *ws;
|
||||||
@@ -874,7 +879,8 @@ static int lwebsocket_set_timer(lua_State *L, int is_periodic)
|
|||||||
ws = (struct lua_websock_data *)lua_touserdata(L, -1);
|
ws = (struct lua_websock_data *)lua_touserdata(L, -1);
|
||||||
|
|
||||||
if (num_args < 2) {
|
if (num_args < 2) {
|
||||||
return luaL_error(L, "not enough arguments for set_timer/interval() call");
|
return luaL_error(L,
|
||||||
|
"not enough arguments for set_timer/interval() call");
|
||||||
}
|
}
|
||||||
|
|
||||||
type1 = lua_type(L, 1);
|
type1 = lua_type(L, 1);
|
||||||
@@ -884,7 +890,8 @@ static int lwebsocket_set_timer(lua_State *L, int is_periodic)
|
|||||||
timediff = (double)lua_tonumber(L, 2);
|
timediff = (double)lua_tonumber(L, 2);
|
||||||
txt = lua_tostring(L, 1);
|
txt = lua_tostring(L, 1);
|
||||||
txt_len = strlen(txt);
|
txt_len = strlen(txt);
|
||||||
arg = (struct laction_arg *) mg_malloc(sizeof(struct laction_arg) + txt_len + 10);
|
arg = (struct laction_arg *)mg_malloc(sizeof(struct laction_arg) +
|
||||||
|
txt_len + 10);
|
||||||
arg->state = L;
|
arg->state = L;
|
||||||
arg->script = ws->script;
|
arg->script = ws->script;
|
||||||
arg->pmutex = &(ws->ws_mutex);
|
arg->pmutex = &(ws->ws_mutex);
|
||||||
@@ -892,7 +899,10 @@ static int lwebsocket_set_timer(lua_State *L, int is_periodic)
|
|||||||
memcpy(arg->txt + 7, txt, txt_len);
|
memcpy(arg->txt + 7, txt, txt_len);
|
||||||
arg->txt[txt_len + 7] = ')';
|
arg->txt[txt_len + 7] = ')';
|
||||||
arg->txt[txt_len + 8] = 0;
|
arg->txt[txt_len + 8] = 0;
|
||||||
ok = (0==timer_add(ctx, timediff, is_periodic, 1, (taction)(is_periodic ? lua_action : lua_action_free), (void*)arg));
|
ok = (0 ==
|
||||||
|
timer_add(ctx, timediff, is_periodic, 1,
|
||||||
|
(taction)(is_periodic ? lua_action : lua_action_free),
|
||||||
|
(void *)arg));
|
||||||
} else if (type1 == LUA_TFUNCTION && type2 == LUA_TNUMBER) {
|
} else if (type1 == LUA_TFUNCTION && type2 == LUA_TNUMBER) {
|
||||||
/* TODO: not implemented yet */
|
/* TODO: not implemented yet */
|
||||||
return luaL_error(L, "invalid arguments for set_timer/interval() call");
|
return luaL_error(L, "invalid arguments for set_timer/interval() call");
|
||||||
@@ -911,14 +921,12 @@ static int lwebsocket_set_timer(lua_State *L, int is_periodic)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* mg.set_timeout for websockets */
|
/* mg.set_timeout for websockets */
|
||||||
static int lwebsocket_set_timeout(lua_State *L)
|
static int lwebsocket_set_timeout(lua_State *L) {
|
||||||
{
|
|
||||||
return lwebsocket_set_timer(L, 0);
|
return lwebsocket_set_timer(L, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* mg.set_interval for websockets */
|
/* mg.set_interval for websockets */
|
||||||
static int lwebsocket_set_interval(lua_State *L)
|
static int lwebsocket_set_interval(lua_State *L) {
|
||||||
{
|
|
||||||
return lwebsocket_set_timer(L, 1);
|
return lwebsocket_set_timer(L, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -928,8 +936,7 @@ enum {
|
|||||||
LUA_ENV_TYPE_LUA_WEBSOCKET = 2,
|
LUA_ENV_TYPE_LUA_WEBSOCKET = 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
|
static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L) {
|
||||||
{
|
|
||||||
const char *s;
|
const char *s;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@@ -941,7 +948,10 @@ static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
|
|||||||
reg_string(L, "http_version", conn->request_info.http_version);
|
reg_string(L, "http_version", conn->request_info.http_version);
|
||||||
reg_string(L, "query_string", conn->request_info.query_string);
|
reg_string(L, "query_string", conn->request_info.query_string);
|
||||||
#if defined(MG_LEGACY_INTERFACE)
|
#if defined(MG_LEGACY_INTERFACE)
|
||||||
reg_int(L, "remote_ip", conn->request_info.remote_ip); /* remote_ip is deprecated, use remote_addr instead */
|
reg_int(
|
||||||
|
L, "remote_ip",
|
||||||
|
conn->request_info
|
||||||
|
.remote_ip); /* remote_ip is deprecated, use remote_addr instead */
|
||||||
#endif
|
#endif
|
||||||
reg_string(L, "remote_addr", conn->request_info.remote_addr);
|
reg_string(L, "remote_addr", conn->request_info.remote_addr);
|
||||||
/* TODO: ip version */
|
/* TODO: ip version */
|
||||||
@@ -952,7 +962,10 @@ static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
|
|||||||
if (conn->request_info.content_length >= 0) {
|
if (conn->request_info.content_length >= 0) {
|
||||||
/* reg_int64: content_length */
|
/* reg_int64: content_length */
|
||||||
lua_pushstring(L, "content_length");
|
lua_pushstring(L, "content_length");
|
||||||
lua_pushnumber(L, (lua_Number)conn->request_info.content_length); /* lua_Number may be used as 52 bit integer */
|
lua_pushnumber(
|
||||||
|
L,
|
||||||
|
(lua_Number)conn->request_info
|
||||||
|
.content_length); /* lua_Number may be used as 52 bit integer */
|
||||||
lua_rawset(L, -3);
|
lua_rawset(L, -3);
|
||||||
}
|
}
|
||||||
if ((s = mg_get_header(conn, "Content-Type")) != NULL) {
|
if ((s = mg_get_header(conn, "Content-Type")) != NULL) {
|
||||||
@@ -974,15 +987,15 @@ static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
|
|||||||
lua_pushstring(L, "http_headers");
|
lua_pushstring(L, "http_headers");
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
for (i = 0; i < conn->request_info.num_headers; i++) {
|
for (i = 0; i < conn->request_info.num_headers; i++) {
|
||||||
reg_string(L, conn->request_info.http_headers[i].name, conn->request_info.http_headers[i].value);
|
reg_string(L, conn->request_info.http_headers[i].name,
|
||||||
|
conn->request_info.http_headers[i].value);
|
||||||
}
|
}
|
||||||
lua_rawset(L, -3);
|
lua_rawset(L, -3);
|
||||||
|
|
||||||
lua_rawset(L, -3);
|
lua_rawset(L, -3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua_civet_open_all_libs(lua_State *L)
|
void lua_civet_open_all_libs(lua_State *L) {
|
||||||
{
|
|
||||||
{
|
{
|
||||||
extern void luaL_openlibs(lua_State *);
|
extern void luaL_openlibs(lua_State *);
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
@@ -1008,8 +1021,11 @@ void lua_civet_open_all_libs(lua_State *L)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void prepare_lua_environment(struct mg_context * ctx, struct mg_connection *conn, struct lua_websock_data *conn_list, lua_State *L, const char *script_name, int lua_env_type)
|
static void prepare_lua_environment(struct mg_context *ctx,
|
||||||
{
|
struct mg_connection *conn,
|
||||||
|
struct lua_websock_data *conn_list,
|
||||||
|
lua_State *L, const char *script_name,
|
||||||
|
int lua_env_type) {
|
||||||
lua_civet_open_all_libs(L);
|
lua_civet_open_all_libs(L);
|
||||||
|
|
||||||
luaL_newmetatable(L, LUASOCKET);
|
luaL_newmetatable(L, LUASOCKET);
|
||||||
@@ -1046,7 +1062,8 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lua_env_type==LUA_ENV_TYPE_LUA_SERVER_PAGE || lua_env_type==LUA_ENV_TYPE_PLAIN_LUA_PAGE) {
|
if (lua_env_type == LUA_ENV_TYPE_LUA_SERVER_PAGE ||
|
||||||
|
lua_env_type == LUA_ENV_TYPE_PLAIN_LUA_PAGE) {
|
||||||
reg_conn_function(L, "cry", lsp_cry, conn);
|
reg_conn_function(L, "cry", lsp_cry, conn);
|
||||||
reg_conn_function(L, "read", lsp_read, conn);
|
reg_conn_function(L, "read", lsp_read, conn);
|
||||||
reg_conn_function(L, "write", lsp_write, conn);
|
reg_conn_function(L, "write", lsp_write, conn);
|
||||||
@@ -1099,7 +1116,8 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
|
|||||||
lua_setglobal(L, "mg");
|
lua_setglobal(L, "mg");
|
||||||
|
|
||||||
/* Register default mg.onerror function */
|
/* Register default mg.onerror function */
|
||||||
IGNORE_UNUSED_RESULT(luaL_dostring(L, "mg.onerror = function(e) mg.write('\\nLua error:\\n', "
|
IGNORE_UNUSED_RESULT(luaL_dostring(
|
||||||
|
L, "mg.onerror = function(e) mg.write('\\nLua error:\\n', "
|
||||||
"debug.traceback(e, 1)) end"));
|
"debug.traceback(e, 1)) end"));
|
||||||
|
|
||||||
/* Preload */
|
/* Preload */
|
||||||
@@ -1112,8 +1130,7 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lua_error_handler(lua_State *L)
|
static int lua_error_handler(lua_State *L) {
|
||||||
{
|
|
||||||
const char *error_msg = lua_isstring(L, -1) ? lua_tostring(L, -1) : "?\n";
|
const char *error_msg = lua_isstring(L, -1) ? lua_tostring(L, -1) : "?\n";
|
||||||
|
|
||||||
lua_getglobal(L, "mg");
|
lua_getglobal(L, "mg");
|
||||||
@@ -1122,10 +1139,12 @@ static int lua_error_handler(lua_State *L)
|
|||||||
lua_pushstring(L, error_msg);
|
lua_pushstring(L, error_msg);
|
||||||
lua_pushliteral(L, "\n");
|
lua_pushliteral(L, "\n");
|
||||||
lua_call(L, 2, 0);
|
lua_call(L, 2, 0);
|
||||||
IGNORE_UNUSED_RESULT(luaL_dostring(L, "mg.write(debug.traceback(), '\\n')"));
|
IGNORE_UNUSED_RESULT(
|
||||||
|
luaL_dostring(L, "mg.write(debug.traceback(), '\\n')"));
|
||||||
} else {
|
} else {
|
||||||
printf("Lua error: [%s]\n", error_msg);
|
printf("Lua error: [%s]\n", error_msg);
|
||||||
IGNORE_UNUSED_RESULT(luaL_dostring(L, "print(debug.traceback(), '\\n')"));
|
IGNORE_UNUSED_RESULT(
|
||||||
|
luaL_dostring(L, "print(debug.traceback(), '\\n')"));
|
||||||
}
|
}
|
||||||
/* TODO(lsm): leave the stack balanced */
|
/* TODO(lsm): leave the stack balanced */
|
||||||
|
|
||||||
@@ -1134,7 +1153,8 @@ static int lua_error_handler(lua_State *L)
|
|||||||
|
|
||||||
static void *lua_allocator(void *ud, void *ptr, size_t osize, size_t nsize) {
|
static void *lua_allocator(void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||||
|
|
||||||
(void)ud; (void)osize; /* not used */
|
(void)ud;
|
||||||
|
(void)osize; /* not used */
|
||||||
|
|
||||||
if (nsize == 0) {
|
if (nsize == 0) {
|
||||||
mg_free(ptr);
|
mg_free(ptr);
|
||||||
@@ -1144,17 +1164,18 @@ static void * lua_allocator(void *ud, void *ptr, size_t osize, size_t nsize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void mg_exec_lua_script(struct mg_connection *conn, const char *path,
|
void mg_exec_lua_script(struct mg_connection *conn, const char *path,
|
||||||
const void **exports)
|
const void **exports) {
|
||||||
{
|
|
||||||
int i;
|
int i;
|
||||||
lua_State *L;
|
lua_State *L;
|
||||||
|
|
||||||
/* Assume the script does not support keep_alive. The script may change this by calling mg.keep_alive(true). */
|
/* Assume the script does not support keep_alive. The script may change this
|
||||||
|
* by calling mg.keep_alive(true). */
|
||||||
conn->must_close = 1;
|
conn->must_close = 1;
|
||||||
|
|
||||||
/* Execute a plain Lua script. */
|
/* Execute a plain Lua script. */
|
||||||
if (path != NULL && (L = lua_newstate(lua_allocator, NULL)) != NULL) {
|
if (path != NULL && (L = lua_newstate(lua_allocator, NULL)) != NULL) {
|
||||||
prepare_lua_environment(conn->ctx, conn, NULL, L, path, LUA_ENV_TYPE_PLAIN_LUA_PAGE);
|
prepare_lua_environment(conn->ctx, conn, NULL, L, path,
|
||||||
|
LUA_ENV_TYPE_PLAIN_LUA_PAGE);
|
||||||
lua_pushcclosure(L, &lua_error_handler, 0);
|
lua_pushcclosure(L, &lua_error_handler, 0);
|
||||||
|
|
||||||
if (exports != NULL) {
|
if (exports != NULL) {
|
||||||
@@ -1176,21 +1197,23 @@ void mg_exec_lua_script(struct mg_connection *conn, const char *path,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_lsp_request(struct mg_connection *conn, const char *path, struct file *filep, struct lua_State *ls)
|
static int handle_lsp_request(struct mg_connection *conn, const char *path,
|
||||||
{
|
struct file *filep, struct lua_State *ls) {
|
||||||
void *p = NULL;
|
void *p = NULL;
|
||||||
lua_State *L = NULL;
|
lua_State *L = NULL;
|
||||||
int error = 1;
|
int error = 1;
|
||||||
|
|
||||||
/* Assume the script does not support keep_alive. The script may change this by calling mg.keep_alive(true). */
|
/* Assume the script does not support keep_alive. The script may change this
|
||||||
|
* by calling mg.keep_alive(true). */
|
||||||
conn->must_close = 1;
|
conn->must_close = 1;
|
||||||
|
|
||||||
/* We need both mg_stat to get file size, and mg_fopen to get fd */
|
/* We need both mg_stat to get file size, and mg_fopen to get fd */
|
||||||
if (!mg_stat(conn, path, filep) || !mg_fopen(conn, path, "r", filep)) {
|
if (!mg_stat(conn, path, filep) || !mg_fopen(conn, path, "r", filep)) {
|
||||||
/* File not found or not accessible */
|
/* File not found or not accessible */
|
||||||
if (ls == NULL) {
|
if (ls == NULL) {
|
||||||
send_http_error(conn, 500,
|
send_http_error(
|
||||||
"Error: Cannot open script\nFile %s can not be read", path);
|
conn, 500, "Error: Cannot open script\nFile %s can not be read",
|
||||||
|
path);
|
||||||
} else {
|
} else {
|
||||||
luaL_error(ls, "File [%s] not found", path);
|
luaL_error(ls, "File [%s] not found", path);
|
||||||
}
|
}
|
||||||
@@ -1199,26 +1222,33 @@ static int handle_lsp_request(struct mg_connection *conn, const char *path, stru
|
|||||||
fileno(filep->fp), 0)) == MAP_FAILED) {
|
fileno(filep->fp), 0)) == MAP_FAILED) {
|
||||||
/* mmap failed */
|
/* mmap failed */
|
||||||
if (ls == NULL) {
|
if (ls == NULL) {
|
||||||
send_http_error(conn, 500,
|
send_http_error(
|
||||||
|
conn, 500,
|
||||||
"Error: Cannot open script\nFile %s can not be mapped", path);
|
"Error: Cannot open script\nFile %s can not be mapped", path);
|
||||||
} else {
|
} else {
|
||||||
luaL_error(ls, "mmap(%s, %zu, %d): %s", path, (size_t)filep->size,
|
luaL_error(ls, "mmap(%s, %zu, %d): %s", path, (size_t)filep->size,
|
||||||
fileno(filep->fp), strerror(errno));
|
fileno(filep->fp), strerror(errno));
|
||||||
}
|
}
|
||||||
} else if ((L = (ls != NULL ? ls : lua_newstate(lua_allocator, NULL))) == NULL) {
|
} else if ((L = (ls != NULL ? ls : lua_newstate(lua_allocator, NULL))) ==
|
||||||
|
NULL) {
|
||||||
send_http_error(conn, 500, "%s",
|
send_http_error(conn, 500, "%s",
|
||||||
"Error: Cannot execute script\nlua_newstate failed");
|
"Error: Cannot execute script\nlua_newstate failed");
|
||||||
} else {
|
} else {
|
||||||
/* We're not sending HTTP headers here, Lua page must do it. */
|
/* We're not sending HTTP headers here, Lua page must do it. */
|
||||||
if (ls == NULL) {
|
if (ls == NULL) {
|
||||||
prepare_lua_environment(conn->ctx, conn, NULL, L, path, LUA_ENV_TYPE_LUA_SERVER_PAGE);
|
prepare_lua_environment(conn->ctx, conn, NULL, L, path,
|
||||||
|
LUA_ENV_TYPE_LUA_SERVER_PAGE);
|
||||||
}
|
}
|
||||||
error = lsp(conn, path, (filep->membuf == NULL) ? (const char *)p : (const char *)filep->membuf,
|
error = lsp(conn, path,
|
||||||
|
(filep->membuf == NULL) ? (const char *)p
|
||||||
|
: (const char *)filep->membuf,
|
||||||
filep->size, L);
|
filep->size, L);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (L != NULL && ls == NULL) lua_close(L);
|
if (L != NULL && ls == NULL)
|
||||||
if (p != NULL) munmap(p, filep->size);
|
lua_close(L);
|
||||||
|
if (p != NULL)
|
||||||
|
munmap(p, filep->size);
|
||||||
mg_fclose(filep);
|
mg_fclose(filep);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
@@ -1229,9 +1259,9 @@ struct mg_shared_lua_websocket_list {
|
|||||||
struct mg_shared_lua_websocket_list *next;
|
struct mg_shared_lua_websocket_list *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void * lua_websocket_new(const char * script, struct mg_connection *conn)
|
static void *lua_websocket_new(const char *script, struct mg_connection *conn) {
|
||||||
{
|
struct mg_shared_lua_websocket_list **shared_websock_list =
|
||||||
struct mg_shared_lua_websocket_list **shared_websock_list = &(conn->ctx->shared_lua_websockets);
|
&(conn->ctx->shared_lua_websockets);
|
||||||
struct lua_websock_data *ws;
|
struct lua_websock_data *ws;
|
||||||
int err, ok = 0;
|
int err, ok = 0;
|
||||||
|
|
||||||
@@ -1248,7 +1278,8 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn)
|
|||||||
}
|
}
|
||||||
if (*shared_websock_list == NULL) {
|
if (*shared_websock_list == NULL) {
|
||||||
/* add ws to list */
|
/* add ws to list */
|
||||||
*shared_websock_list = (struct mg_shared_lua_websocket_list *) mg_calloc(sizeof(struct mg_shared_lua_websocket_list), 1);
|
*shared_websock_list = (struct mg_shared_lua_websocket_list *)mg_calloc(
|
||||||
|
sizeof(struct mg_shared_lua_websocket_list), 1);
|
||||||
if (*shared_websock_list == NULL) {
|
if (*shared_websock_list == NULL) {
|
||||||
mg_unlock_context(conn->ctx);
|
mg_unlock_context(conn->ctx);
|
||||||
mg_cry(conn, "Cannot create shared websocket struct, OOM");
|
mg_cry(conn, "Cannot create shared websocket struct, OOM");
|
||||||
@@ -1262,7 +1293,8 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn)
|
|||||||
ws->conn[0] = conn;
|
ws->conn[0] = conn;
|
||||||
ws->references = 1;
|
ws->references = 1;
|
||||||
(void)pthread_mutex_lock(&(ws->ws_mutex));
|
(void)pthread_mutex_lock(&(ws->ws_mutex));
|
||||||
prepare_lua_environment(conn->ctx, NULL, ws, ws->state, script, LUA_ENV_TYPE_LUA_WEBSOCKET);
|
prepare_lua_environment(conn->ctx, NULL, ws, ws->state, script,
|
||||||
|
LUA_ENV_TYPE_LUA_WEBSOCKET);
|
||||||
err = luaL_loadfile(ws->state, script);
|
err = luaL_loadfile(ws->state, script);
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
lua_cry(conn, err, ws->state, script, "load");
|
lua_cry(conn, err, ws->state, script, "load");
|
||||||
@@ -1298,7 +1330,8 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn)
|
|||||||
}
|
}
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
/* Remove from ws connection list. */
|
/* Remove from ws connection list. */
|
||||||
/* TODO: Check if list entry and Lua state needs to be deleted (see websocket_close). */
|
/* TODO: Check if list entry and Lua state needs to be deleted (see
|
||||||
|
* websocket_close). */
|
||||||
(*shared_websock_list)->ws.conn[--(ws->references)] = 0;
|
(*shared_websock_list)->ws.conn[--(ws->references)] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1307,8 +1340,8 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn)
|
|||||||
return ok ? (void *)ws : NULL;
|
return ok ? (void *)ws : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lua_websocket_data(struct mg_connection * conn, int bits, char *data, size_t data_len, void *ws_arg)
|
static int lua_websocket_data(struct mg_connection *conn, int bits, char *data,
|
||||||
{
|
size_t data_len, void *ws_arg) {
|
||||||
struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
|
struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
|
||||||
int err, ok = 0;
|
int err, ok = 0;
|
||||||
|
|
||||||
@@ -1322,7 +1355,10 @@ static int lua_websocket_data(struct mg_connection * conn, int bits, char *data,
|
|||||||
lua_pushstring(ws->state, "client");
|
lua_pushstring(ws->state, "client");
|
||||||
lua_pushlightuserdata(ws->state, (void *)conn);
|
lua_pushlightuserdata(ws->state, (void *)conn);
|
||||||
lua_rawset(ws->state, -3);
|
lua_rawset(ws->state, -3);
|
||||||
lua_pushstring(ws->state, "bits"); /* TODO: dont use "bits" but fields with a meaning according to http://tools.ietf.org/html/rfc6455, section 5.2 */
|
lua_pushstring(ws->state, "bits"); /* TODO: dont use "bits" but fields with
|
||||||
|
a meaning according to
|
||||||
|
http://tools.ietf.org/html/rfc6455,
|
||||||
|
section 5.2 */
|
||||||
lua_pushnumber(ws->state, bits);
|
lua_pushnumber(ws->state, bits);
|
||||||
lua_rawset(ws->state, -3);
|
lua_rawset(ws->state, -3);
|
||||||
lua_pushstring(ws->state, "data");
|
lua_pushstring(ws->state, "data");
|
||||||
@@ -1343,8 +1379,7 @@ static int lua_websocket_data(struct mg_connection * conn, int bits, char *data,
|
|||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lua_websocket_ready(struct mg_connection * conn, void * ws_arg)
|
static int lua_websocket_ready(struct mg_connection *conn, void *ws_arg) {
|
||||||
{
|
|
||||||
struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
|
struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
|
||||||
int err, ok = 0;
|
int err, ok = 0;
|
||||||
|
|
||||||
@@ -1374,10 +1409,10 @@ static int lua_websocket_ready(struct mg_connection * conn, void * ws_arg)
|
|||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void lua_websocket_close(struct mg_connection * conn, void * ws_arg)
|
static void lua_websocket_close(struct mg_connection *conn, void *ws_arg) {
|
||||||
{
|
|
||||||
struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
|
struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
|
||||||
struct mg_shared_lua_websocket_list **shared_websock_list = &(conn->ctx->shared_lua_websockets);
|
struct mg_shared_lua_websocket_list **shared_websock_list =
|
||||||
|
&(conn->ctx->shared_lua_websockets);
|
||||||
int err = 0;
|
int err = 0;
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
||||||
|
@@ -19,8 +19,8 @@ struct ttimers {
|
|||||||
unsigned timer_count; /* Current size of timer list */
|
unsigned timer_count; /* Current size of timer list */
|
||||||
};
|
};
|
||||||
|
|
||||||
static int timer_add(struct mg_context * ctx, double next_time, double period, int is_relative, taction action, void * arg)
|
static int timer_add(struct mg_context *ctx, double next_time, double period,
|
||||||
{
|
int is_relative, taction action, void *arg) {
|
||||||
unsigned u, v;
|
unsigned u, v;
|
||||||
int error = 0;
|
int error = 0;
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
@@ -57,8 +57,7 @@ static int timer_add(struct mg_context * ctx, double next_time, double period, i
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void timer_thread_run(void *thread_func_param)
|
static void timer_thread_run(void *thread_func_param) {
|
||||||
{
|
|
||||||
struct mg_context *ctx = (struct mg_context *)thread_func_param;
|
struct mg_context *ctx = (struct mg_context *)thread_func_param;
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
double d;
|
double d;
|
||||||
@@ -68,7 +67,10 @@ static void timer_thread_run(void *thread_func_param)
|
|||||||
|
|
||||||
#if defined(HAVE_CLOCK_NANOSLEEP) /* Linux with librt */
|
#if defined(HAVE_CLOCK_NANOSLEEP) /* Linux with librt */
|
||||||
/* TODO */
|
/* TODO */
|
||||||
while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &request, &request)==EINTR) {/*nop*/;}
|
while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &request,
|
||||||
|
&request) == EINTR) { /*nop*/
|
||||||
|
;
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
|
d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
|
||||||
@@ -94,25 +96,21 @@ static void timer_thread_run(void *thread_func_param)
|
|||||||
d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
|
d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static unsigned __stdcall timer_thread(void *thread_func_param)
|
static unsigned __stdcall timer_thread(void *thread_func_param) {
|
||||||
{
|
|
||||||
timer_thread_run(thread_func_param);
|
timer_thread_run(thread_func_param);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
static void *timer_thread(void *thread_func_param)
|
static void *timer_thread(void *thread_func_param) {
|
||||||
{
|
|
||||||
timer_thread_run(thread_func_param);
|
timer_thread_run(thread_func_param);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
static int timers_init(struct mg_context * ctx)
|
static int timers_init(struct mg_context *ctx) {
|
||||||
{
|
|
||||||
ctx->timers = (struct ttimers *)mg_calloc(sizeof(struct ttimers), 1);
|
ctx->timers = (struct ttimers *)mg_calloc(sizeof(struct ttimers), 1);
|
||||||
(void)pthread_mutex_init(&ctx->timers->mutex, NULL);
|
(void)pthread_mutex_init(&ctx->timers->mutex, NULL);
|
||||||
|
|
||||||
@@ -122,8 +120,7 @@ static int timers_init(struct mg_context * ctx)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void timers_exit(struct mg_context * ctx)
|
static void timers_exit(struct mg_context *ctx) {
|
||||||
{
|
|
||||||
if (ctx->timers) {
|
if (ctx->timers) {
|
||||||
(void)pthread_mutex_destroy(&ctx->timers->mutex);
|
(void)pthread_mutex_destroy(&ctx->timers->mutex);
|
||||||
mg_free(ctx->timers);
|
mg_free(ctx->timers);
|
||||||
|
Reference in New Issue
Block a user