Commit 98214ce1 authored by Sergey Lyubka's avatar Sergey Lyubka

Removed init_lua() and open_file() callbacks

parent 7eed7eab
...@@ -361,32 +361,29 @@ static int handle_lsp_request(struct mg_connection *conn, const char *path, ...@@ -361,32 +361,29 @@ static int handle_lsp_request(struct mg_connection *conn, const char *path,
struct file *filep, struct lua_State *ls) { struct file *filep, struct lua_State *ls) {
void *p = NULL; void *p = NULL;
lua_State *L = NULL; lua_State *L = NULL;
FILE *fp = NULL;
int error = 1; int error = 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(path, filep) || (fp = mg_fopen(path, "r")) == NULL) {
lsp_send_err(conn, ls, "File [%s] not found", path); lsp_send_err(conn, ls, "File [%s] not found", path);
} else if (filep->membuf == NULL && } else if ((p = mmap(NULL, (size_t) filep->size, PROT_READ, MAP_PRIVATE,
(p = mmap(NULL, (size_t) filep->size, PROT_READ, MAP_PRIVATE, fileno(fp), 0)) == MAP_FAILED) {
fileno(filep->fp), 0)) == MAP_FAILED) {
lsp_send_err(conn, ls, "mmap(%s, %zu, %d): %s", path, (size_t) filep->size, lsp_send_err(conn, ls, "mmap(%s, %zu, %d): %s", path, (size_t) filep->size,
fileno(filep->fp), strerror(errno)); fileno(fp), strerror(errno));
} else if ((L = ls != NULL ? ls : luaL_newstate()) == NULL) { } else if ((L = ls != NULL ? ls : luaL_newstate()) == NULL) {
send_http_error(conn, 500, http_500_error, "%s", "luaL_newstate failed"); send_http_error(conn, 500, http_500_error, "%s", "luaL_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, L); prepare_lua_environment(conn, L);
if (conn->ctx->callbacks.init_lua != NULL) {
conn->ctx->callbacks.init_lua(conn, L);
}
} }
error = lsp(conn, path, filep->membuf == NULL ? p : filep->membuf, error = lsp(conn, path, p, filep->size, L);
filep->size, L);
} }
if (L != NULL && ls == NULL) lua_close(L); if (L != NULL && ls == NULL) lua_close(L);
if (p != NULL) munmap(p, filep->size); if (p != NULL) munmap(p, filep->size);
mg_fclose(filep); fclose(fp);
return error; return error;
} }
This diff is collapsed.
...@@ -59,9 +59,6 @@ struct mg_callbacks { ...@@ -59,9 +59,6 @@ struct mg_callbacks {
void (*websocket_ready)(struct mg_connection *); void (*websocket_ready)(struct mg_connection *);
int (*websocket_data)(struct mg_connection *, int bits, int (*websocket_data)(struct mg_connection *, int bits,
char *data, size_t data_len); char *data, size_t data_len);
const char * (*open_file)(const struct mg_connection *,
const char *path, size_t *data_len);
void (*init_lua)(struct mg_connection *, void *lua_context);
void (*upload)(struct mg_connection *, const char *file_name); void (*upload)(struct mg_connection *, const char *file_name);
void (*thread_start)(void *user_data, void **conn_data); void (*thread_start)(void *user_data, void **conn_data);
void (*thread_stop)(void *user_data, void **conn_data); void (*thread_stop)(void *user_data, void **conn_data);
......
...@@ -201,21 +201,10 @@ static char *read_file(const char *path, int *size) { ...@@ -201,21 +201,10 @@ static char *read_file(const char *path, int *size) {
} }
static const char *fetch_data = "hello world!\n"; static const char *fetch_data = "hello world!\n";
static const char *inmemory_file_data = "hi there";
static const char *upload_filename = "upload_test.txt"; static const char *upload_filename = "upload_test.txt";
static const char *upload_filename2 = "upload_test2.txt"; static const char *upload_filename2 = "upload_test2.txt";
static const char *upload_ok_message = "upload successful"; static const char *upload_ok_message = "upload successful";
static const char *open_file_cb(const struct mg_connection *conn,
const char *path, size_t *size) {
(void) conn;
if (!strcmp(path, "./blah")) {
*size = strlen(inmemory_file_data);
return inmemory_file_data;
}
return NULL;
}
static void upload_cb(struct mg_connection *conn, const char *path) { static void upload_cb(struct mg_connection *conn, const char *path) {
const struct mg_request_info *ri = mg_get_request_info(conn); const struct mg_request_info *ri = mg_get_request_info(conn);
char *p1, *p2; char *p1, *p2;
...@@ -282,7 +271,7 @@ static int log_message_cb(const struct mg_connection *conn, const char *msg) { ...@@ -282,7 +271,7 @@ static int log_message_cb(const struct mg_connection *conn, const char *msg) {
static const struct mg_callbacks CALLBACKS = { static const struct mg_callbacks CALLBACKS = {
&begin_request_handler_cb, NULL, &log_message_cb, NULL, NULL, NULL, NULL, &begin_request_handler_cb, NULL, &log_message_cb, NULL, NULL, NULL, NULL,
&open_file_cb, NULL, &upload_cb, NULL, NULL &upload_cb, NULL, NULL
}; };
static const char *OPTIONS[] = { static const char *OPTIONS[] = {
...@@ -339,25 +328,6 @@ static void test_mg_download(void) { ...@@ -339,25 +328,6 @@ static void test_mg_download(void) {
free(p1), free(p2); free(p1), free(p2);
mg_close_connection(conn); mg_close_connection(conn);
// Fetch in-memory file, should succeed.
ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
"GET /blah HTTP/1.1\r\n\r\n")) != NULL);
ASSERT((p1 = read_conn(conn, &len1)) != NULL);
ASSERT(len1 == (int) strlen(inmemory_file_data));
ASSERT(memcmp(p1, inmemory_file_data, len1) == 0);
free(p1);
mg_close_connection(conn);
// Fetch in-memory data with no Content-Length, should succeed.
ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
"GET /data HTTP/1.1\r\n\r\n")) != NULL);
ASSERT((p1 = read_conn(conn, &len1)) != NULL);
ASSERT(len1 == (int) strlen(fetch_data));
ASSERT(memcmp(p1, fetch_data, len1) == 0);
free(p1);
mg_close_connection(conn);
// Test SSL redirect, IP address // Test SSL redirect, IP address
ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0, ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
ebuf, sizeof(ebuf), "%s", ebuf, sizeof(ebuf), "%s",
...@@ -576,9 +546,8 @@ static void test_lua(void) { ...@@ -576,9 +546,8 @@ static void test_lua(void) {
#endif #endif
static void test_mg_stat(void) { static void test_mg_stat(void) {
static struct mg_context ctx;
struct file file = STRUCT_FILE_INITIALIZER; struct file file = STRUCT_FILE_INITIALIZER;
ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file)); ASSERT(!mg_stat(" does not exist ", &file));
} }
static void test_skip_quoted(void) { static void test_skip_quoted(void) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment