Skip to content

Commit 1401d18

Browse files
committed
Updating third-party C++ library dependencies
This resolves compiler warnings when compiling Civetweb and JsonCpp under Visual Studio.
1 parent fe013b5 commit 1401d18

File tree

6 files changed

+245
-52
lines changed

6 files changed

+245
-52
lines changed

third_party/civetweb/ReadMe.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ found at http://github.com/bel2125/civetweb.
44
The Civetweb project is a fork of the previously used Mongoose project, which
55
changed its licensing. The Civetweb project is licensed using the MIT
66
License, as not built with Lua or SQLite support. The full license terms
7-
can be found at https://github.com/bel2125/civetweb/blob/master/LICENSE.md
7+
can be found at https://github.com/bel2125/civetweb/blob/master/LICENSE.md.
8+
9+
To update the code in this project, pull the latest from the above GitHub
10+
repository, and copy:
11+
12+
* the contents of the include directory
13+
* the contents of the src directory, excluding main.c and the third_party
14+
directory

third_party/civetweb/civetweb.c

+134-9
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ struct mg_context {
779779
char *config[NUM_OPTIONS]; /* Civetweb configuration parameters */
780780
struct mg_callbacks callbacks; /* User-defined callback function */
781781
void *user_data; /* User-defined data */
782+
int context_type; /* 1 = server context, 2 = client context */
782783

783784
struct socket *listening_sockets;
784785
in_port_t *listening_ports;
@@ -1187,6 +1188,9 @@ static char *skip_quoted(char **buf, const char *delimiters,
11871188
if (end_word > begin_word) {
11881189
p = end_word - 1;
11891190
while (*p == quotechar) {
1191+
/* TODO (bel): it seems this code is never reached, so quotechar is actually
1192+
not needed - check if this code may be droped */
1193+
11901194
/* If there is anything beyond end_word, copy it */
11911195
if (*end_word == '\0') {
11921196
*p = '\0';
@@ -2267,6 +2271,7 @@ static int pull_all(FILE *fp, struct mg_connection *conn, char *buf, int len)
22672271
int mg_read(struct mg_connection *conn, void *buf, size_t len)
22682272
{
22692273
int64_t n, buffered_len, nread;
2274+
int64_t len64 = (int64_t)(len > INT_MAX ? INT_MAX : len); /* since the return value is int, we may not read more bytes */
22702275
const char *body;
22712276

22722277
/* If Content-Length is not set for a PUT or POST request, read until socket is closed */
@@ -2279,33 +2284,33 @@ int mg_read(struct mg_connection *conn, void *buf, size_t len)
22792284
if (conn->consumed_content < conn->content_len) {
22802285
/* Adjust number of bytes to read. */
22812286
int64_t to_read = conn->content_len - conn->consumed_content;
2282-
if (to_read < (int64_t) len) {
2287+
if (to_read < len64) {
22832288
len = (size_t) to_read;
22842289
}
22852290

22862291
/* Return buffered data */
22872292
body = conn->buf + conn->request_len + conn->consumed_content;
22882293
buffered_len = (int64_t)(&conn->buf[conn->data_len] - body);
22892294
if (buffered_len > 0) {
2290-
if (len < (size_t) buffered_len) {
2291-
buffered_len = (int64_t) len;
2295+
if (len64 < (size_t) buffered_len) {
2296+
buffered_len = len64;
22922297
}
22932298
memcpy(buf, body, (size_t) buffered_len);
2294-
len -= buffered_len;
2299+
len64 -= buffered_len;
22952300
conn->consumed_content += buffered_len;
22962301
nread += buffered_len;
22972302
buf = (char *) buf + buffered_len;
22982303
}
22992304

23002305
/* We have returned all buffered data. Read new data from the remote
23012306
socket. */
2302-
if ((n = pull_all(NULL, conn, (char *) buf, (int64_t) len)) >= 0) {
2307+
if ((n = pull_all(NULL, conn, (char *) buf, (int)len64)) >= 0) {
23032308
nread += n;
23042309
} else {
23052310
nread = (nread > 0 ? nread : n);
23062311
}
23072312
}
2308-
return nread;
2313+
return (int)nread;
23092314
}
23102315

23112316
int mg_write(struct mg_connection *conn, const void *buf, size_t len)
@@ -5140,8 +5145,9 @@ static void read_websocket(struct mg_connection *conn)
51405145

51415146
/* Loop continuously, reading messages from the socket, invoking the
51425147
callback, and waiting repeatedly until an error occurs. */
5143-
assert(conn->content_len == 0);
5144-
for (;;) {
5148+
/* TODO: Investigate if this next line is needed
5149+
assert(conn->content_len == 0); */
5150+
while (!conn->ctx->stop_flag) {
51455151
header_len = 0;
51465152
assert(conn->data_len >= conn->request_len);
51475153
if ((body_len = conn->data_len - conn->request_len) >= 2) {
@@ -6286,8 +6292,9 @@ static void close_connection(struct mg_connection *conn)
62866292
#endif
62876293

62886294
/* call the connection_close callback if assigned */
6289-
if (conn->ctx->callbacks.connection_close != NULL)
6295+
if ((conn->ctx->callbacks.connection_close != NULL) && (conn->ctx->context_type == 1)) {
62906296
conn->ctx->callbacks.connection_close(conn);
6297+
}
62916298

62926299
mg_lock_connection(conn);
62936300

@@ -6311,12 +6318,29 @@ static void close_connection(struct mg_connection *conn)
63116318

63126319
void mg_close_connection(struct mg_connection *conn)
63136320
{
6321+
struct mg_context * client_ctx = NULL;
6322+
int i;
6323+
6324+
if (conn->ctx->context_type == 2) {
6325+
client_ctx = conn->ctx;
6326+
/* client context: loops must end */
6327+
conn->ctx->stop_flag = 1;
6328+
}
6329+
63146330
#ifndef NO_SSL
63156331
if (conn->client_ssl_ctx != NULL) {
63166332
SSL_CTX_free((SSL_CTX *) conn->client_ssl_ctx);
63176333
}
63186334
#endif
63196335
close_connection(conn);
6336+
if (client_ctx != NULL) {
6337+
/* join worker thread and free context */
6338+
for (i = 0; i < client_ctx->workerthreadcount; i++) {
6339+
mg_join_thread(client_ctx->workerthreadids[i]);
6340+
}
6341+
mg_free(client_ctx->workerthreadids);
6342+
mg_free(client_ctx);
6343+
}
63206344
(void) pthread_mutex_destroy(&conn->mutex);
63216345
mg_free(conn);
63226346
}
@@ -6440,6 +6464,106 @@ struct mg_connection *mg_download(const char *host, int port, int use_ssl,
64406464
return conn;
64416465
}
64426466

6467+
#if defined(USE_WEBSOCKET)
6468+
#ifdef _WIN32
6469+
static unsigned __stdcall websocket_client_thread(void *data)
6470+
#else
6471+
static void* websocket_client_thread(void *data)
6472+
#endif
6473+
{
6474+
struct mg_connection* conn = (struct mg_connection*)data;
6475+
read_websocket(conn);
6476+
6477+
DEBUG_TRACE("Websocket client thread exited\n");
6478+
6479+
if (conn->ctx->callbacks.connection_close != NULL) {
6480+
conn->ctx->callbacks.connection_close(conn);
6481+
}
6482+
6483+
#ifdef _WIN32
6484+
return 0;
6485+
#else
6486+
return NULL;
6487+
#endif
6488+
}
6489+
#endif
6490+
6491+
struct mg_connection *mg_connect_websocket_client(const char *host, int port, int use_ssl,
6492+
char *error_buffer, size_t error_buffer_size,
6493+
const char *path, const char *origin,
6494+
websocket_data_func data_func, websocket_close_func close_func,
6495+
void * user_data)
6496+
{
6497+
struct mg_connection* conn = NULL;
6498+
struct mg_context * newctx = NULL;
6499+
6500+
#if defined(USE_WEBSOCKET)
6501+
static const char *magic = "x3JJHMbDL1EzLkh9GBhXDw==";
6502+
static const char *handshake_req;
6503+
6504+
if(origin != NULL)
6505+
{
6506+
handshake_req = "GET %s HTTP/1.1\r\n"
6507+
"Host: %s\r\n"
6508+
"Upgrade: websocket\r\n"
6509+
"Connection: Upgrade\r\n"
6510+
"Sec-WebSocket-Key: %s\r\n"
6511+
"Sec-WebSocket-Version: 13\r\n"
6512+
"Origin: %s\r\n"
6513+
"\r\n";
6514+
}
6515+
else
6516+
{
6517+
handshake_req = "GET %s HTTP/1.1\r\n"
6518+
"Host: %s\r\n"
6519+
"Upgrade: websocket\r\n"
6520+
"Connection: Upgrade\r\n"
6521+
"Sec-WebSocket-Key: %s\r\n"
6522+
"Sec-WebSocket-Version: 13\r\n"
6523+
"\r\n";
6524+
}
6525+
6526+
/* Establish the client connection and request upgrade */
6527+
conn = mg_download(host, port, use_ssl,
6528+
error_buffer, error_buffer_size,
6529+
handshake_req, path, host, magic, origin);
6530+
6531+
/* Connection object will be null if something goes wrong */
6532+
if(conn == NULL || (strcmp(conn->request_info.uri, "101") != 0))
6533+
{
6534+
DEBUG_TRACE("Websocket client connect error: %s\r\n", error_buffer);
6535+
if(conn != NULL) { mg_free(conn); conn = NULL; }
6536+
return conn;
6537+
}
6538+
6539+
/* For client connections, mg_context is fake. Since we need to set a callback
6540+
function, we need to create a copy and modify it. */
6541+
newctx = (struct mg_context *) mg_malloc(sizeof(struct mg_context));
6542+
memcpy(newctx, conn->ctx, sizeof(struct mg_context));
6543+
newctx->callbacks.websocket_data = data_func; /* read_websocket will automatically call it */
6544+
newctx->callbacks.connection_close = close_func;
6545+
newctx->user_data = user_data;
6546+
newctx->context_type = 2; /* client context type */
6547+
newctx->workerthreadcount = 1; /* one worker thread will be created */
6548+
newctx->workerthreadids = (pthread_t*) mg_calloc(newctx->workerthreadcount, sizeof(pthread_t));
6549+
conn->ctx = newctx;
6550+
6551+
/* Start a thread to read the websocket client connection
6552+
This thread will automatically stop when mg_disconnect is
6553+
called on the client connection */
6554+
if (mg_start_thread_with_id(websocket_client_thread, (void*)conn, newctx->workerthreadids) != 0)
6555+
{
6556+
mg_free((void*)newctx->workerthreadids);
6557+
mg_free((void*)newctx);
6558+
mg_free((void*)conn);
6559+
conn = NULL;
6560+
DEBUG_TRACE("Websocket client connect thread could not be started\r\n");
6561+
}
6562+
#endif
6563+
6564+
return conn;
6565+
}
6566+
64436567
static void process_new_connection(struct mg_connection *conn)
64446568
{
64456569
struct mg_request_info *ri = &conn->request_info;
@@ -7054,6 +7178,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
70547178
ctx->callbacks.init_context(ctx);
70557179
}
70567180
ctx->callbacks.exit_context = exit_callback;
7181+
ctx->context_type = 1; /* server context */
70577182

70587183
/* Start master (listening) thread */
70597184
mg_start_thread_with_id(master_thread, ctx, &ctx->masterthreadid);

third_party/civetweb/civetweb.h

+24
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,31 @@ CIVETWEB_API void mg_cry(struct mg_connection *conn,
573573
/* utility method to compare two buffers, case incensitive. */
574574
CIVETWEB_API int mg_strncasecmp(const char *s1, const char *s2, size_t len);
575575

576+
/* Connect to a websocket as a client
577+
Parameters:
578+
host: host to connect to, i.e. "echo.websocket.org" or "192.168.1.1" or "localhost"
579+
port: server port
580+
use_ssl: make a secure connection to server
581+
error_buffer, error_buffer_size: error message placeholder.
582+
path: server path you are trying to connect to, i.e. if connection to localhost/app, path should be "/app"
583+
origin: value of the Origin HTTP header
584+
data_func: callback that should be used when data is received from the server
585+
user_data: user supplied argument
586+
587+
Return:
588+
On success, valid mg_connection object.
589+
On error, NULL. */
576590

591+
typedef int (*websocket_data_func)(struct mg_connection *, int bits,
592+
char *data, size_t data_len);
593+
594+
typedef void (*websocket_close_func)(struct mg_connection *);
595+
596+
CIVETWEB_API struct mg_connection *mg_connect_websocket_client(const char *host, int port, int use_ssl,
597+
char *error_buffer, size_t error_buffer_size,
598+
const char *path, const char *origin,
599+
websocket_data_func data_func, websocket_close_func close_func,
600+
void * user_data);
577601
#ifdef __cplusplus
578602
}
579603
#endif /* __cplusplus */

third_party/civetweb/mod_lua.inl

+17-3
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,8 @@ static int lwebsock_write(lua_State *L)
754754
} else {
755755
return luaL_error(L, "invalid websocket write() call");
756756
}
757-
757+
#else
758+
(void)(L); /* unused */
758759
#endif
759760
return 0;
760761
}
@@ -818,7 +819,7 @@ static int lua_action_free(struct laction_arg *arg)
818819

819820
static int lwebsocket_set_timer(lua_State *L, int is_periodic)
820821
{
821-
#ifdef USE_TIMERS
822+
#if defined(USE_TIMERS) && defined(USE_WEBSOCKET)
822823
int num_args = lua_gettop(L);
823824
struct lua_websock_data *ws;
824825
int type1,type2, ok = 0;
@@ -867,6 +868,8 @@ static int lwebsocket_set_timer(lua_State *L, int is_periodic)
867868
return 1;
868869

869870
#else
871+
(void)(L); /* unused */
872+
(void)(is_periodic); /* unused */
870873
return 0;
871874
#endif
872875
}
@@ -892,6 +895,7 @@ enum {
892895
static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
893896
{
894897
char src_addr[IP_ADDR_STR_LEN] = "";
898+
const char *s;
895899
int i;
896900

897901
sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
@@ -910,6 +914,16 @@ static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
910914
reg_int(L, "num_headers", conn->request_info.num_headers);
911915
reg_int(L, "server_port", ntohs(conn->client.lsa.sin.sin_port));
912916

917+
if (conn->request_info.content_length >= 0) {
918+
/* reg_int64: content_length */
919+
lua_pushstring(L, "content_length");
920+
lua_pushnumber(L, (lua_Number)conn->request_info.content_length); /* lua_Number may be used as 52 bit integer */
921+
lua_rawset(L, -3);
922+
}
923+
if ((s = mg_get_header(conn, "Content-Type")) != NULL) {
924+
reg_string(L, "content_type", s);
925+
}
926+
913927
if (conn->request_info.remote_user != NULL) {
914928
reg_string(L, "remote_user", conn->request_info.remote_user);
915929
reg_string(L, "auth_type", "Digest");
@@ -945,7 +959,7 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
945959
luaopen_lsqlite3(L);
946960
}
947961
#endif
948-
#ifdef USE_LUA_SQLITE3
962+
#ifdef USE_LUA_LUAXML
949963
{
950964
extern int luaopen_LuaXML(lua_State *);
951965
luaopen_LuaXML(L);

0 commit comments

Comments
 (0)