diff --git a/examples/embedded_c/embedded_c.c b/examples/embedded_c/embedded_c.c
index f88440fc..531aa777 100644
--- a/examples/embedded_c/embedded_c.c
+++ b/examples/embedded_c/embedded_c.c
@@ -70,7 +70,7 @@ int ABHandler(struct mg_connection *conn, void *cbdata)
int FooHandler(struct mg_connection *conn, void *cbdata)
{
/* Handler may access the request info using mg_get_request_info */
- struct mg_request_info * req_info = mg_get_request_info(conn);
+ const struct mg_request_info * req_info = mg_get_request_info(conn);
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
mg_printf(conn, "
");
diff --git a/examples/embedded_cpp/embedded_cpp.cpp b/examples/embedded_cpp/embedded_cpp.cpp
index 07c5df96..ac1620e8 100644
--- a/examples/embedded_cpp/embedded_cpp.cpp
+++ b/examples/embedded_cpp/embedded_cpp.cpp
@@ -89,7 +89,7 @@ class FooHandler: public CivetHandler
public:
bool handleGet(CivetServer *server, struct mg_connection *conn) {
/* Handler may access the request info using mg_get_request_info */
- struct mg_request_info * req_info = mg_get_request_info(conn);
+ const struct mg_request_info * req_info = mg_get_request_info(conn);
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
mg_printf(conn, "");
diff --git a/examples/websocket/WebSockCallbacks.c b/examples/websocket/WebSockCallbacks.c
index 2ba3aee2..ee52fbfb 100644
--- a/examples/websocket/WebSockCallbacks.c
+++ b/examples/websocket/WebSockCallbacks.c
@@ -37,13 +37,13 @@ static void send_to_all_websockets(struct mg_context *ctx, const char * data, in
void websocket_ready_handler(struct mg_connection *conn) {
int i;
- struct mg_request_info * rq = mg_get_request_info(conn);
+ const struct mg_request_info * rq = mg_get_request_info(conn);
struct mg_context * ctx = mg_get_context(conn);
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
tWebSockInfo * wsock = malloc(sizeof(tWebSockInfo));
assert(wsock);
wsock->webSockState = 0;
- rq->conn_data = wsock;
+ mg_set_user_connection_data(conn, wsock);
mg_lock_context(ctx);
for (i=0;iconn_data;
struct mg_context * ctx = mg_get_context(conn);
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
@@ -89,7 +89,7 @@ int websocket_data_handler(struct mg_connection *conn, int flags, char *data, si
if (flags==136) {
// close websock
websocket_done(ws_ctx, wsock);
- rq->conn_data = 0;
+ mg_set_user_connection_data(conn, NULL);
mg_unlock_context(ctx);
return 1;
}
@@ -129,16 +129,16 @@ int websocket_data_handler(struct mg_connection *conn, int flags, char *data, si
}
-void connection_close_handler(struct mg_connection *conn) {
+void connection_close_handler(const struct mg_connection *conn) {
- struct mg_request_info * rq = mg_get_request_info(conn);
+ const struct mg_request_info * rq = mg_get_request_info(conn);
tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
struct mg_context * ctx = mg_get_context(conn);
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
mg_lock_context(ctx);
websocket_done(ws_ctx, wsock);
- rq->conn_data = 0;
+ mg_set_user_connection_data(conn, NULL);
mg_unlock_context(ctx);
}
@@ -174,15 +174,15 @@ void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_
}
}
-void websock_init_lib(struct mg_context *ctx) {
+void websock_init_lib(const struct mg_context *ctx) {
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
- memset(ws_ctx,0,sizeof(*ws_ctx));
+ memset(ws_ctx, 0, sizeof(*ws_ctx));
/* todo: use mg_start_thread_id instead of mg_start_thread */
- mg_start_thread(eventMain, ctx);
+ mg_start_thread(eventMain, (void*)ctx);
}
-void websock_exit_lib(struct mg_context *ctx) {
+void websock_exit_lib(const struct mg_context *ctx) {
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
ws_ctx->runLoop = 0;
diff --git a/examples/websocket/WebSockCallbacks.h b/examples/websocket/WebSockCallbacks.h
index 02aa64d6..fb959a1a 100644
--- a/examples/websocket/WebSockCallbacks.h
+++ b/examples/websocket/WebSockCallbacks.h
@@ -22,14 +22,14 @@ typedef struct tWebSockContext {
} tWebSockContext;
-void websock_init_lib(struct mg_context *ctx);
-void websock_exit_lib(struct mg_context *ctx);
+void websock_init_lib(const struct mg_context *ctx);
+void websock_exit_lib(const struct mg_context *ctx);
void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len);
void websocket_ready_handler(struct mg_connection *conn);
int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len);
-void connection_close_handler(struct mg_connection *conn);
+void connection_close_handler(const struct mg_connection *conn);
#ifdef __cplusplus
diff --git a/examples/websocket_client/websocket_client.c b/examples/websocket_client/websocket_client.c
index cc6e4b5c..a3367981 100644
--- a/examples/websocket_client/websocket_client.c
+++ b/examples/websocket_client/websocket_client.c
@@ -67,7 +67,7 @@ int websocket_server_data(struct mg_connection * conn, int bits, char *data, siz
return 1; /* return 1 to keep the connetion open */
}
-void websocket_server_connection_close(struct mg_connection * conn)
+void websocket_server_connection_close(const struct mg_connection * conn)
{
printf("Server: Close connection\n");
diff --git a/include/CivetServer.h b/include/CivetServer.h
index d261a0bb..f11341ed 100644
--- a/include/CivetServer.h
+++ b/include/CivetServer.h
@@ -344,12 +344,12 @@ private:
*
* @param conn - the connection information
*/
- static void closeHandler(struct mg_connection *conn);
+ static void closeHandler(const struct mg_connection *conn);
/**
* Stores the user provided close handler
*/
- void (*userCloseHandler)(struct mg_connection *conn);
+ void (*userCloseHandler)(const struct mg_connection *conn);
};
diff --git a/include/civetweb.h b/include/civetweb.h
index 27792d68..2a4aff64 100644
--- a/include/civetweb.h
+++ b/include/civetweb.h
@@ -145,7 +145,7 @@ struct mg_callbacks {
list of clients.
Using this callback for websocket connections is deprecated, use
mg_set_websocket_handler instead. */
- void (*connection_close)(struct mg_connection *);
+ void (*connection_close)(const struct mg_connection *);
/* Called when civetweb tries to open a file. Used to intercept file open
calls, and serve file data from memory instead.
@@ -164,7 +164,7 @@ struct mg_callbacks {
Lua support is enabled.
Parameters:
lua_context: "lua_State *" pointer. */
- void (*init_lua)(struct mg_connection *, void *lua_context);
+ void (*init_lua)(const struct mg_connection *, void *lua_context);
/* Called when civetweb has uploaded a file to a temporary directory as a
result of mg_upload() call.
@@ -185,12 +185,12 @@ struct mg_callbacks {
are processed.
Parameters:
ctx: context handle */
- void (*init_context)(struct mg_context * ctx);
+ void (*init_context)(const struct mg_context * ctx);
/* Called when civetweb context is deleted.
Parameters:
ctx: context handle */
- void (*exit_context)(struct mg_context * ctx);
+ void (*exit_context)(const struct mg_context * ctx);
};
@@ -324,7 +324,15 @@ CIVETWEB_API struct mg_context *mg_get_context(const struct mg_connection *conn)
/* Get user data passed to mg_start from context. */
-CIVETWEB_API void *mg_get_user_data(struct mg_context *ctx);
+CIVETWEB_API void *mg_get_user_data(const struct mg_context *ctx);
+
+
+/* Set user data for the current connection. */
+CIVETWEB_API void mg_set_user_connection_data(const struct mg_connection *conn, void *data);
+
+
+/* Get user data set for the current connection. */
+CIVETWEB_API void *mg_get_user_connection_data(const struct mg_connection *conn);
#if defined(MG_LEGACY_INTERFACE)
@@ -389,7 +397,7 @@ CIVETWEB_API int mg_modify_passwords_file(const char *passwords_file_name,
/* Return information associated with the request. */
-CIVETWEB_API struct mg_request_info *mg_get_request_info(struct mg_connection *);
+CIVETWEB_API const struct mg_request_info *mg_get_request_info(const struct mg_connection *);
/* Send data to the client.
@@ -636,7 +644,7 @@ CIVETWEB_API char *mg_md5(char buf[33], ...);
...: variable argument list
Example:
mg_cry(conn,"i like %s", "logging"); */
-CIVETWEB_API void mg_cry(struct mg_connection *conn,
+CIVETWEB_API void mg_cry(const struct mg_connection *conn,
PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
diff --git a/src/CivetServer.cpp b/src/CivetServer.cpp
index c4ce7c39..39d762aa 100644
--- a/src/CivetServer.cpp
+++ b/src/CivetServer.cpp
@@ -52,7 +52,7 @@ bool CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn
int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
{
- struct mg_request_info *request_info = mg_get_request_info(conn);
+ const struct mg_request_info *request_info = mg_get_request_info(conn);
assert(request_info != NULL);
CivetServer *me = (CivetServer*) (request_info->user_data);
assert(me != NULL);
@@ -106,9 +106,9 @@ CivetServer::~CivetServer()
close();
}
-void CivetServer::closeHandler(struct mg_connection *conn)
+void CivetServer::closeHandler(const struct mg_connection *conn)
{
- struct mg_request_info *request_info = mg_get_request_info(conn);
+ const struct mg_request_info *request_info = mg_get_request_info(conn);
assert(request_info != NULL);
CivetServer *me = (CivetServer*) (request_info->user_data);
assert(me != NULL);
@@ -118,7 +118,7 @@ void CivetServer::closeHandler(struct mg_connection *conn)
if (me->userCloseHandler) me->userCloseHandler(conn);
mg_lock_context(me->context);
- me->connections.erase(conn);
+ me->connections.erase(const_cast(conn));
mg_unlock_context(me->context);
}
@@ -190,7 +190,7 @@ CivetServer::getParam(struct mg_connection *conn, const char *name,
std::string &dst, size_t occurrence)
{
const char *formParams = NULL;
- struct mg_request_info *ri = mg_get_request_info(conn);
+ const struct mg_request_info *ri = mg_get_request_info(conn);
assert(ri != NULL);
CivetServer *me = (CivetServer*) (ri->user_data);
assert(me != NULL);
diff --git a/src/civetweb.c b/src/civetweb.c
index 6da97141..f32d0254 100755
--- a/src/civetweb.c
+++ b/src/civetweb.c
@@ -1218,7 +1218,7 @@ static int get_option_index(const char *name)
return -1;
}
-const char *mg_get_option(const struct mg_context *ctx, const char *name)
+const char * mg_get_option(const struct mg_context *ctx, const char *name)
{
int i;
if ((i = get_option_index(name)) == -1) {
@@ -1230,16 +1230,36 @@ const char *mg_get_option(const struct mg_context *ctx, const char *name)
}
}
-struct mg_context *mg_get_context(const struct mg_connection * conn)
+
+struct mg_context * mg_get_context(const struct mg_connection * conn)
{
return (conn == NULL) ? (struct mg_context *)NULL : (conn->ctx);
}
-void *mg_get_user_data(struct mg_context *ctx)
+
+void * mg_get_user_data(const struct mg_context *ctx)
{
return (ctx == NULL) ? NULL : ctx->user_data;
}
+
+void mg_set_user_connection_data(const struct mg_connection *conn, void *data)
+{
+ if (conn != NULL) {
+ ((struct mg_connection *)conn)->request_info.conn_data = data;
+ }
+}
+
+
+void * mg_get_user_connection_data(const struct mg_connection *conn)
+{
+ if (conn != NULL) {
+ return conn->request_info.conn_data;
+ }
+ return NULL;
+}
+
+
size_t mg_get_ports(const struct mg_context *ctx, size_t size, int* ports, int* ssl)
{
size_t i;
@@ -1288,7 +1308,7 @@ static double mg_difftimespec(const struct timespec *ts_now, const struct timesp
}
/* Print error message to the opened error log stream. */
-void mg_cry(struct mg_connection *conn, const char *fmt, ...)
+void mg_cry(const struct mg_connection *conn, const char *fmt, ...)
{
char buf[MG_BUF_LEN], src_addr[IP_ADDR_STR_LEN];
va_list ap;
@@ -1342,7 +1362,7 @@ const char *mg_version(void)
return CIVETWEB_VERSION;
}
-struct mg_request_info *mg_get_request_info(struct mg_connection *conn)
+const struct mg_request_info *mg_get_request_info(const struct mg_connection *conn)
{
return &conn->request_info;
}
@@ -6468,7 +6488,7 @@ static int get_request_handler(struct mg_connection *conn,
void **cbdata
)
{
- struct mg_request_info *request_info = mg_get_request_info(conn);
+ const struct mg_request_info *request_info = mg_get_request_info(conn);
const char *uri = request_info->uri;
size_t urilen = strlen(uri);
struct mg_request_handler_info *tmp_rh;
@@ -8343,7 +8363,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
const char *name, *value, *default_value;
int i, ok;
int workerthreadcount;
- void (*exit_callback)(struct mg_context * ctx) = 0;
+ void (*exit_callback)(const struct mg_context * ctx) = 0;
#if defined(_WIN32) && !defined(__SYMBIAN32__)
WSADATA data;
diff --git a/test/unit_test.c b/test/unit_test.c
index 63c30635..8363137d 100644
--- a/test/unit_test.c
+++ b/test/unit_test.c
@@ -407,7 +407,7 @@ static void test_mg_download(int use_ssl) {
int i, len1, len2, port;
struct mg_connection *conn;
struct mg_context *ctx;
- struct mg_request_info *ri;
+ const struct mg_request_info *ri;
if (use_ssl) port = atoi(HTTPS_PORT); else port = atoi(HTTP_PORT);
@@ -946,7 +946,7 @@ static void test_request_handlers(void) {
}
static int api_callback(struct mg_connection *conn) {
- struct mg_request_info *ri = mg_get_request_info(conn);
+ const struct mg_request_info *ri = mg_get_request_info(conn);
char post_data[100] = "";
ASSERT(ri->user_data == (void *) 123);