mirror of
https://github.com/lammertb/libhttp.git
synced 2025-08-09 03:22:45 +03:00
Renamed mg_ to httplib_
This commit is contained in:
@@ -54,13 +54,13 @@ static long last_message_id;
|
||||
static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
|
||||
|
||||
// Get session object for the connection. Caller must hold the lock.
|
||||
static struct session *get_session(const struct mg_connection *conn)
|
||||
static struct session *get_session(const struct httplib_connection *conn)
|
||||
{
|
||||
int i;
|
||||
const char *cookie = mg_get_header(conn, "Cookie");
|
||||
const char *cookie = httplib_get_header(conn, "Cookie");
|
||||
char session_id[33];
|
||||
time_t now = time(NULL);
|
||||
mg_get_cookie(cookie, "session", session_id, sizeof(session_id));
|
||||
httplib_get_cookie(cookie, "session", session_id, sizeof(session_id));
|
||||
for (i = 0; i < MAX_SESSIONS; i++) {
|
||||
if (sessions[i].expire != 0 &&
|
||||
sessions[i].expire > now &&
|
||||
@@ -71,11 +71,10 @@ static struct session *get_session(const struct mg_connection *conn)
|
||||
return i == MAX_SESSIONS ? NULL : &sessions[i];
|
||||
}
|
||||
|
||||
static void get_qsvar(const struct mg_request_info *request_info,
|
||||
const char *name, char *dst, size_t dst_len)
|
||||
static void get_qsvar(const struct httplib_request_info *request_info, const char *name, char *dst, size_t dst_len)
|
||||
{
|
||||
const char *qs = request_info->query_string;
|
||||
mg_get_var(qs, strlen(qs == NULL ? "" : qs), name, dst, dst_len);
|
||||
httplib_get_var(qs, strlen(qs == NULL ? "" : qs), name, dst, dst_len);
|
||||
}
|
||||
|
||||
// Get a get of messages with IDs greater than last_id and transform them
|
||||
@@ -118,14 +117,14 @@ static char *messages_to_json(long last_id)
|
||||
// If "callback" param is present in query string, this is JSONP call.
|
||||
// Return 1 in this case, or 0 if "callback" is not specified.
|
||||
// Wrap an output in Javascript function call.
|
||||
static int handle_jsonp(struct mg_connection *conn,
|
||||
const struct mg_request_info *request_info)
|
||||
static int handle_jsonp(struct httplib_connection *conn,
|
||||
const struct httplib_request_info *request_info)
|
||||
{
|
||||
char cb[64];
|
||||
|
||||
get_qsvar(request_info, "callback", cb, sizeof(cb));
|
||||
if (cb[0] != '\0') {
|
||||
mg_printf(conn, "%s(", cb);
|
||||
httplib_printf(conn, "%s(", cb);
|
||||
}
|
||||
|
||||
return cb[0] == '\0' ? 0 : 1;
|
||||
@@ -133,23 +132,23 @@ static int handle_jsonp(struct mg_connection *conn,
|
||||
|
||||
// A handler for the /ajax/get_messages endpoint.
|
||||
// Return a list of messages with ID greater than requested.
|
||||
static void ajax_get_messages(struct mg_connection *conn,
|
||||
const struct mg_request_info *request_info)
|
||||
static void ajax_get_messages(struct httplib_connection *conn,
|
||||
const struct httplib_request_info *request_info)
|
||||
{
|
||||
char last_id[32], *json;
|
||||
int is_jsonp;
|
||||
|
||||
mg_printf(conn, "%s", ajax_reply_start);
|
||||
httplib_printf(conn, "%s", ajax_reply_start);
|
||||
is_jsonp = handle_jsonp(conn, request_info);
|
||||
|
||||
get_qsvar(request_info, "last_id", last_id, sizeof(last_id));
|
||||
if ((json = messages_to_json(strtoul(last_id, NULL, 10))) != NULL) {
|
||||
mg_printf(conn, "[%s]", json);
|
||||
httplib_printf(conn, "[%s]", json);
|
||||
free(json);
|
||||
}
|
||||
|
||||
if (is_jsonp) {
|
||||
mg_printf(conn, "%s", ")");
|
||||
httplib_printf(conn, "%s", ")");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,15 +169,15 @@ static void my_strlcpy(char *dst, const char *src, size_t len)
|
||||
}
|
||||
|
||||
// A handler for the /ajax/send_message endpoint.
|
||||
static void ajax_send_message(struct mg_connection *conn,
|
||||
const struct mg_request_info *request_info)
|
||||
static void ajax_send_message(struct httplib_connection *conn,
|
||||
const struct httplib_request_info *request_info)
|
||||
{
|
||||
struct message *message;
|
||||
struct session *session;
|
||||
char text[sizeof(message->text) - 1];
|
||||
int is_jsonp;
|
||||
|
||||
mg_printf(conn, "%s", ajax_reply_start);
|
||||
httplib_printf(conn, "%s", ajax_reply_start);
|
||||
is_jsonp = handle_jsonp(conn, request_info);
|
||||
|
||||
get_qsvar(request_info, "text", text, sizeof(text));
|
||||
@@ -195,19 +194,19 @@ static void ajax_send_message(struct mg_connection *conn,
|
||||
pthread_rwlock_unlock(&rwlock);
|
||||
}
|
||||
|
||||
mg_printf(conn, "%s", text[0] == '\0' ? "false" : "true");
|
||||
httplib_printf(conn, "%s", text[0] == '\0' ? "false" : "true");
|
||||
|
||||
if (is_jsonp) {
|
||||
mg_printf(conn, "%s", ")");
|
||||
httplib_printf(conn, "%s", ")");
|
||||
}
|
||||
}
|
||||
|
||||
// Redirect user to the login form. In the cookie, store the original URL
|
||||
// we came from, so that after the authorization we could redirect back.
|
||||
static void redirect_to_login(struct mg_connection *conn,
|
||||
const struct mg_request_info *request_info)
|
||||
static void redirect_to_login(struct httplib_connection *conn,
|
||||
const struct httplib_request_info *request_info)
|
||||
{
|
||||
mg_printf(conn, "HTTP/1.1 302 Found\r\n"
|
||||
httplib_printf(conn, "HTTP/1.1 302 Found\r\n"
|
||||
"Set-Cookie: original_url=%s\r\n"
|
||||
"Location: %s\r\n\r\n",
|
||||
request_info->uri, login_url);
|
||||
@@ -244,7 +243,7 @@ static struct session *new_session(void)
|
||||
static void generate_session_id(char *buf, const char *random,
|
||||
const char *user)
|
||||
{
|
||||
mg_md5(buf, random, user, NULL);
|
||||
httplib_md5(buf, random, user, NULL);
|
||||
}
|
||||
|
||||
static void send_server_message(const char *fmt, ...)
|
||||
@@ -264,8 +263,8 @@ static void send_server_message(const char *fmt, ...)
|
||||
|
||||
// A handler for the /authorize endpoint.
|
||||
// Login page form sends user name and password to this endpoint.
|
||||
static void authorize(struct mg_connection *conn,
|
||||
const struct mg_request_info *request_info)
|
||||
static void authorize(struct httplib_connection *conn,
|
||||
const struct httplib_request_info *request_info)
|
||||
{
|
||||
char user[MAX_USER_LEN], password[MAX_USER_LEN];
|
||||
struct session *session;
|
||||
@@ -290,7 +289,7 @@ static void authorize(struct mg_connection *conn,
|
||||
snprintf(session->random, sizeof(session->random), "%d", rand());
|
||||
generate_session_id(session->session_id, session->random, session->user);
|
||||
send_server_message("<%s> joined", session->user);
|
||||
mg_printf(conn, "HTTP/1.1 302 Found\r\n"
|
||||
httplib_printf(conn, "HTTP/1.1 302 Found\r\n"
|
||||
"Set-Cookie: session=%s; max-age=3600; http-only\r\n" // Session ID
|
||||
"Set-Cookie: user=%s\r\n" // Set user, needed by Javascript code
|
||||
"Set-Cookie: original_url=/; max-age=0\r\n" // Delete original_url
|
||||
@@ -303,8 +302,8 @@ static void authorize(struct mg_connection *conn,
|
||||
}
|
||||
|
||||
// Return 1 if request is authorized, 0 otherwise.
|
||||
static int is_authorized(const struct mg_connection *conn,
|
||||
const struct mg_request_info *request_info)
|
||||
static int is_authorized(const struct httplib_connection *conn,
|
||||
const struct httplib_request_info *request_info)
|
||||
{
|
||||
struct session *session;
|
||||
char valid_id[33];
|
||||
@@ -329,22 +328,22 @@ static int is_authorized(const struct mg_connection *conn,
|
||||
return authorized;
|
||||
}
|
||||
|
||||
static void redirect_to_ssl(struct mg_connection *conn,
|
||||
const struct mg_request_info *request_info)
|
||||
static void redirect_to_ssl(struct httplib_connection *conn,
|
||||
const struct httplib_request_info *request_info)
|
||||
{
|
||||
const char *p, *host = mg_get_header(conn, "Host");
|
||||
const char *p, *host = httplib_get_header(conn, "Host");
|
||||
if (host != NULL && (p = strchr(host, ':')) != NULL) {
|
||||
mg_printf(conn, "HTTP/1.1 302 Found\r\n"
|
||||
httplib_printf(conn, "HTTP/1.1 302 Found\r\n"
|
||||
"Location: https://%.*s:8082/%s:8082\r\n\r\n",
|
||||
(int) (p - host), host, request_info->uri);
|
||||
} else {
|
||||
mg_printf(conn, "%s", "HTTP/1.1 500 Error\r\n\r\nHost: header is not set");
|
||||
httplib_printf(conn, "%s", "HTTP/1.1 500 Error\r\n\r\nHost: header is not set");
|
||||
}
|
||||
}
|
||||
|
||||
static int begin_request_handler(struct mg_connection *conn)
|
||||
static int begin_request_handler(struct httplib_connection *conn)
|
||||
{
|
||||
const struct mg_request_info *request_info = mg_get_request_info(conn);
|
||||
const struct httplib_request_info *request_info = httplib_get_request_info(conn);
|
||||
int processed = 1;
|
||||
|
||||
if (!request_info->is_ssl) {
|
||||
@@ -375,8 +374,8 @@ static const char *options[] = {
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct mg_callbacks callbacks;
|
||||
struct mg_context *ctx;
|
||||
struct httplib_callbacks callbacks;
|
||||
struct httplib_context *ctx;
|
||||
|
||||
// Initialize random number generator. It will be used later on for
|
||||
// the session identifier creation.
|
||||
@@ -385,16 +384,16 @@ int main(void)
|
||||
// Setup and start Civetweb
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.begin_request = begin_request_handler;
|
||||
if ((ctx = mg_start(&callbacks, NULL, options)) == NULL) {
|
||||
if ((ctx = httplib_start(&callbacks, NULL, options)) == NULL) {
|
||||
printf("%s\n", "Cannot start chat server, fatal exit");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Wait until enter is pressed, then exit
|
||||
printf("Chat server started on ports %s, press enter to quit.\n",
|
||||
mg_get_option(ctx, "listening_ports"));
|
||||
httplib_get_option(ctx, "listening_ports"));
|
||||
getchar();
|
||||
mg_stop(ctx);
|
||||
httplib_stop(ctx);
|
||||
printf("%s\n", "Chat server stopped.");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
@@ -38,157 +38,157 @@ int exitNow = 0;
|
||||
|
||||
|
||||
int
|
||||
ExampleHandler(struct mg_connection *conn, void *cbdata)
|
||||
ExampleHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
|
||||
"close\r\n\r\n");
|
||||
mg_printf(conn, "<html><body>");
|
||||
mg_printf(conn, "<h2>This is an example text from a C handler</h2>");
|
||||
mg_printf(
|
||||
httplib_printf(conn, "<html><body>");
|
||||
httplib_printf(conn, "<h2>This is an example text from a C handler</h2>");
|
||||
httplib_printf(
|
||||
conn,
|
||||
"<p>To see a page from the A handler <a href=\"A\">click A</a></p>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the A handler <a href=\"A/A\">click "
|
||||
"A/A</a></p>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the A/B handler <a "
|
||||
"href=\"A/B\">click A/B</a></p>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the B handler (0) <a "
|
||||
"href=\"B\">click B</a></p>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the B handler (1) <a "
|
||||
"href=\"B/A\">click B/A</a></p>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the B handler (2) <a "
|
||||
"href=\"B/B\">click B/B</a></p>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the *.foo handler <a "
|
||||
"href=\"xy.foo\">click xy.foo</a></p>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the close handler <a "
|
||||
"href=\"close\">click close</a></p>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the FileHandler handler <a "
|
||||
"href=\"form\">click form</a> (the starting point of the "
|
||||
"<b>form</b> test)</p>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the CookieHandler handler <a "
|
||||
"href=\"cookie\">click cookie</a></p>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see an example for parsing files on the fly <a "
|
||||
"href=\"on_the_fly_form\">click form</a> (form for "
|
||||
"uploading files)</p>");
|
||||
|
||||
#ifdef USE_WEBSOCKET
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To test websocket handler <a href=\"/websocket\">click "
|
||||
"websocket</a></p>");
|
||||
#endif
|
||||
mg_printf(conn, "<p>To exit <a href=\"%s\">click exit</a></p>", EXIT_URI);
|
||||
mg_printf(conn, "</body></html>\n");
|
||||
httplib_printf(conn, "<p>To exit <a href=\"%s\">click exit</a></p>", EXIT_URI);
|
||||
httplib_printf(conn, "</body></html>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
ExitHandler(struct mg_connection *conn, void *cbdata)
|
||||
ExitHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: "
|
||||
"text/plain\r\nConnection: close\r\n\r\n");
|
||||
mg_printf(conn, "Server will shut down.\n");
|
||||
mg_printf(conn, "Bye!\n");
|
||||
httplib_printf(conn, "Server will shut down.\n");
|
||||
httplib_printf(conn, "Bye!\n");
|
||||
exitNow = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
AHandler(struct mg_connection *conn, void *cbdata)
|
||||
AHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
|
||||
"close\r\n\r\n");
|
||||
mg_printf(conn, "<html><body>");
|
||||
mg_printf(conn, "<h2>This is the A handler!!!</h2>");
|
||||
mg_printf(conn, "</body></html>\n");
|
||||
httplib_printf(conn, "<html><body>");
|
||||
httplib_printf(conn, "<h2>This is the A handler!!!</h2>");
|
||||
httplib_printf(conn, "</body></html>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
ABHandler(struct mg_connection *conn, void *cbdata)
|
||||
ABHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
|
||||
"close\r\n\r\n");
|
||||
mg_printf(conn, "<html><body>");
|
||||
mg_printf(conn, "<h2>This is the AB handler!!!</h2>");
|
||||
mg_printf(conn, "</body></html>\n");
|
||||
httplib_printf(conn, "<html><body>");
|
||||
httplib_printf(conn, "<h2>This is the AB handler!!!</h2>");
|
||||
httplib_printf(conn, "</body></html>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
BXHandler(struct mg_connection *conn, void *cbdata)
|
||||
BXHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
/* Handler may access the request info using mg_get_request_info */
|
||||
const struct mg_request_info *req_info = mg_get_request_info(conn);
|
||||
/* Handler may access the request info using httplib_get_request_info */
|
||||
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
|
||||
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
|
||||
"close\r\n\r\n");
|
||||
mg_printf(conn, "<html><body>");
|
||||
mg_printf(conn, "<h2>This is the BX handler %p!!!</h2>", cbdata);
|
||||
mg_printf(conn, "<p>The actual uri is %s</p>", req_info->uri);
|
||||
mg_printf(conn, "</body></html>\n");
|
||||
httplib_printf(conn, "<html><body>");
|
||||
httplib_printf(conn, "<h2>This is the BX handler %p!!!</h2>", cbdata);
|
||||
httplib_printf(conn, "<p>The actual uri is %s</p>", req_info->uri);
|
||||
httplib_printf(conn, "</body></html>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
FooHandler(struct mg_connection *conn, void *cbdata)
|
||||
FooHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
/* Handler may access the request info using mg_get_request_info */
|
||||
const struct mg_request_info *req_info = mg_get_request_info(conn);
|
||||
/* Handler may access the request info using httplib_get_request_info */
|
||||
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
|
||||
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
|
||||
"close\r\n\r\n");
|
||||
mg_printf(conn, "<html><body>");
|
||||
mg_printf(conn, "<h2>This is the Foo handler!!!</h2>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn, "<html><body>");
|
||||
httplib_printf(conn, "<h2>This is the Foo handler!!!</h2>");
|
||||
httplib_printf(conn,
|
||||
"<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>",
|
||||
req_info->request_method,
|
||||
req_info->uri,
|
||||
req_info->http_version);
|
||||
mg_printf(conn, "</body></html>\n");
|
||||
httplib_printf(conn, "</body></html>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
CloseHandler(struct mg_connection *conn, void *cbdata)
|
||||
CloseHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
/* Handler may access the request info using mg_get_request_info */
|
||||
const struct mg_request_info *req_info = mg_get_request_info(conn);
|
||||
/* Handler may access the request info using httplib_get_request_info */
|
||||
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
|
||||
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
|
||||
"close\r\n\r\n");
|
||||
mg_printf(conn, "<html><body>");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn, "<html><body>");
|
||||
httplib_printf(conn,
|
||||
"<h2>This handler will close the connection in a second</h2>");
|
||||
#ifdef _WIN32
|
||||
Sleep(1000);
|
||||
#else
|
||||
sleep(1);
|
||||
#endif
|
||||
mg_printf(conn, "bye");
|
||||
httplib_printf(conn, "bye");
|
||||
printf("CloseHandler: close connection\n");
|
||||
mg_close_connection(conn);
|
||||
httplib_close_connection(conn);
|
||||
printf("CloseHandler: wait 10 sec\n");
|
||||
#ifdef _WIN32
|
||||
Sleep(10000);
|
||||
@@ -201,12 +201,12 @@ CloseHandler(struct mg_connection *conn, void *cbdata)
|
||||
|
||||
|
||||
int
|
||||
FileHandler(struct mg_connection *conn, void *cbdata)
|
||||
FileHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
/* In this handler, we ignore the req_info and send the file "fileName". */
|
||||
const char *fileName = (const char *)cbdata;
|
||||
|
||||
mg_send_file(conn, fileName);
|
||||
httplib_send_file(conn, fileName);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -218,9 +218,9 @@ field_found(const char *key,
|
||||
size_t pathlen,
|
||||
void *user_data)
|
||||
{
|
||||
struct mg_connection *conn = (struct mg_connection *)user_data;
|
||||
struct httplib_connection *conn = (struct httplib_connection *)user_data;
|
||||
|
||||
mg_printf(conn, "\r\n\r\n%s:\r\n", key);
|
||||
httplib_printf(conn, "\r\n\r\n%s:\r\n", key);
|
||||
|
||||
if (filename && *filename) {
|
||||
#ifdef _WIN32
|
||||
@@ -237,12 +237,12 @@ field_found(const char *key,
|
||||
int
|
||||
field_get(const char *key, const char *value, size_t valuelen, void *user_data)
|
||||
{
|
||||
struct mg_connection *conn = (struct mg_connection *)user_data;
|
||||
struct httplib_connection *conn = (struct httplib_connection *)user_data;
|
||||
|
||||
if (key[0]) {
|
||||
mg_printf(conn, "%s = ", key);
|
||||
httplib_printf(conn, "%s = ", key);
|
||||
}
|
||||
mg_write(conn, value, valuelen);
|
||||
httplib_write(conn, value, valuelen);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -251,62 +251,59 @@ field_get(const char *key, const char *value, size_t valuelen, void *user_data)
|
||||
int
|
||||
field_stored(const char *path, long long file_size, void *user_data)
|
||||
{
|
||||
struct mg_connection *conn = (struct mg_connection *)user_data;
|
||||
struct httplib_connection *conn = (struct httplib_connection *)user_data;
|
||||
|
||||
mg_printf(conn,
|
||||
"stored as %s (%lu bytes)\r\n\r\n",
|
||||
path,
|
||||
(unsigned long)file_size);
|
||||
httplib_printf(conn, "stored as %s (%lu bytes)\r\n\r\n", path, (unsigned long)file_size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
FormHandler(struct mg_connection *conn, void *cbdata)
|
||||
FormHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
/* Handler may access the request info using mg_get_request_info */
|
||||
const struct mg_request_info *req_info = mg_get_request_info(conn);
|
||||
/* Handler may access the request info using httplib_get_request_info */
|
||||
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
|
||||
int ret;
|
||||
struct mg_form_data_handler fdh = {field_found, field_get, field_stored, 0};
|
||||
struct httplib_form_data_handler fdh = {field_found, field_get, field_stored, 0};
|
||||
|
||||
/* It would be possible to check the request info here before calling
|
||||
* mg_handle_form_request. */
|
||||
* httplib_handle_form_request. */
|
||||
(void)req_info;
|
||||
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: "
|
||||
"text/plain\r\nConnection: close\r\n\r\n");
|
||||
fdh.user_data = (void *)conn;
|
||||
|
||||
/* Call the form handler */
|
||||
mg_printf(conn, "Form data:");
|
||||
ret = mg_handle_form_request(conn, &fdh);
|
||||
mg_printf(conn, "\r\n%i fields found", ret);
|
||||
httplib_printf(conn, "Form data:");
|
||||
ret = httplib_handle_form_request(conn, &fdh);
|
||||
httplib_printf(conn, "\r\n%i fields found", ret);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
FileUploadForm(struct mg_connection *conn, void *cbdata)
|
||||
FileUploadForm(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
|
||||
"close\r\n\r\n");
|
||||
|
||||
mg_printf(conn, "<!DOCTYPE html>\n");
|
||||
mg_printf(conn, "<html>\n<head>\n");
|
||||
mg_printf(conn, "<meta charset=\"UTF-8\">\n");
|
||||
mg_printf(conn, "<title>File upload</title>\n");
|
||||
mg_printf(conn, "</head>\n<body>\n");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn, "<!DOCTYPE html>\n");
|
||||
httplib_printf(conn, "<html>\n<head>\n");
|
||||
httplib_printf(conn, "<meta charset=\"UTF-8\">\n");
|
||||
httplib_printf(conn, "<title>File upload</title>\n");
|
||||
httplib_printf(conn, "</head>\n<body>\n");
|
||||
httplib_printf(conn,
|
||||
"<form action=\"%s\" method=\"POST\" "
|
||||
"enctype=\"multipart/form-data\">\n",
|
||||
(const char *)cbdata);
|
||||
mg_printf(conn, "<input type=\"file\" name=\"filesin\" multiple>\n");
|
||||
mg_printf(conn, "<input type=\"submit\" value=\"Submit\">\n");
|
||||
mg_printf(conn, "</form>\n</body>\n</html>\n");
|
||||
httplib_printf(conn, "<input type=\"file\" name=\"filesin\" multiple>\n");
|
||||
httplib_printf(conn, "<input type=\"submit\" value=\"Submit\">\n");
|
||||
httplib_printf(conn, "</form>\n</body>\n</html>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -371,66 +368,63 @@ field_get_checksum(const char *key,
|
||||
|
||||
|
||||
int
|
||||
CheckSumHandler(struct mg_connection *conn, void *cbdata)
|
||||
CheckSumHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
/* Handler may access the request info using mg_get_request_info */
|
||||
const struct mg_request_info *req_info = mg_get_request_info(conn);
|
||||
/* Handler may access the request info using httplib_get_request_info */
|
||||
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
|
||||
int i, j, ret;
|
||||
struct tfiles_checksums chksums;
|
||||
md5_byte_t digest[16];
|
||||
struct mg_form_data_handler fdh = {field_disp_read_on_the_fly,
|
||||
field_get_checksum,
|
||||
0,
|
||||
(void *)&chksums};
|
||||
struct httplib_form_data_handler fdh = {field_disp_read_on_the_fly, field_get_checksum, 0,(void *)&chksums};
|
||||
|
||||
/* It would be possible to check the request info here before calling
|
||||
* mg_handle_form_request. */
|
||||
* httplib_handle_form_request. */
|
||||
(void)req_info;
|
||||
|
||||
memset(&chksums, 0, sizeof(chksums));
|
||||
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"Connection: close\r\n\r\n");
|
||||
|
||||
/* Call the form handler */
|
||||
mg_printf(conn, "File checksums:");
|
||||
ret = mg_handle_form_request(conn, &fdh);
|
||||
httplib_printf(conn, "File checksums:");
|
||||
ret = httplib_handle_form_request(conn, &fdh);
|
||||
for (i = 0; i < chksums.index; i++) {
|
||||
md5_finish(&(chksums.file[i].chksum), digest);
|
||||
/* Visual Studio 2010+ support llu */
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"\r\n%s %llu ",
|
||||
chksums.file[i].name,
|
||||
chksums.file[i].length);
|
||||
for (j = 0; j < 16; j++) {
|
||||
mg_printf(conn, "%02x", (unsigned int)digest[j]);
|
||||
httplib_printf(conn, "%02x", (unsigned int)digest[j]);
|
||||
}
|
||||
}
|
||||
mg_printf(conn, "\r\n%i files\r\n", ret);
|
||||
httplib_printf(conn, "\r\n%i files\r\n", ret);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
CookieHandler(struct mg_connection *conn, void *cbdata)
|
||||
CookieHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
/* Handler may access the request info using mg_get_request_info */
|
||||
const struct mg_request_info *req_info = mg_get_request_info(conn);
|
||||
const char *cookie = mg_get_header(conn, "Cookie");
|
||||
/* Handler may access the request info using httplib_get_request_info */
|
||||
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
|
||||
const char *cookie = httplib_get_header(conn, "Cookie");
|
||||
char first_str[64], count_str[64];
|
||||
int count;
|
||||
|
||||
(void)mg_get_cookie(cookie, "first", first_str, sizeof(first_str));
|
||||
(void)mg_get_cookie(cookie, "count", count_str, sizeof(count_str));
|
||||
httplib_get_cookie(cookie, "first", first_str, sizeof(first_str));
|
||||
httplib_get_cookie(cookie, "count", count_str, sizeof(count_str));
|
||||
|
||||
mg_printf(conn, "HTTP/1.1 200 OK\r\nConnection: close\r\n");
|
||||
httplib_printf(conn, "HTTP/1.1 200 OK\r\nConnection: close\r\n");
|
||||
if (first_str[0] == 0) {
|
||||
time_t t = time(0);
|
||||
struct tm *ptm = localtime(&t);
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"Set-Cookie: first=%04i-%02i-%02iT%02i:%02i:%02i\r\n",
|
||||
ptm->tm_year + 1900,
|
||||
ptm->tm_mon + 1,
|
||||
@@ -440,42 +434,42 @@ CookieHandler(struct mg_connection *conn, void *cbdata)
|
||||
ptm->tm_sec);
|
||||
}
|
||||
count = (count_str[0] == 0) ? 0 : atoi(count_str);
|
||||
mg_printf(conn, "Set-Cookie: count=%i\r\n", count + 1);
|
||||
mg_printf(conn, "Content-Type: text/html\r\n\r\n");
|
||||
httplib_printf(conn, "Set-Cookie: count=%i\r\n", count + 1);
|
||||
httplib_printf(conn, "Content-Type: text/html\r\n\r\n");
|
||||
|
||||
mg_printf(conn, "<html><body>");
|
||||
mg_printf(conn, "<h2>This is the CookieHandler.</h2>");
|
||||
mg_printf(conn, "<p>The actual uri is %s</p>", req_info->uri);
|
||||
httplib_printf(conn, "<html><body>");
|
||||
httplib_printf(conn, "<h2>This is the CookieHandler.</h2>");
|
||||
httplib_printf(conn, "<p>The actual uri is %s</p>", req_info->uri);
|
||||
|
||||
if (first_str[0] == 0) {
|
||||
mg_printf(conn, "<p>This is the first time, you opened this page</p>");
|
||||
httplib_printf(conn, "<p>This is the first time, you opened this page</p>");
|
||||
} else {
|
||||
mg_printf(conn, "<p>You opened this page %i times before.</p>", count);
|
||||
mg_printf(conn, "<p>You first opened this page on %s.</p>", first_str);
|
||||
httplib_printf(conn, "<p>You opened this page %i times before.</p>", count);
|
||||
httplib_printf(conn, "<p>You first opened this page on %s.</p>", first_str);
|
||||
}
|
||||
|
||||
mg_printf(conn, "</body></html>\n");
|
||||
httplib_printf(conn, "</body></html>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
WebSocketStartHandler(struct mg_connection *conn, void *cbdata)
|
||||
WebSocketStartHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
|
||||
"close\r\n\r\n");
|
||||
|
||||
mg_printf(conn, "<!DOCTYPE html>\n");
|
||||
mg_printf(conn, "<html>\n<head>\n");
|
||||
mg_printf(conn, "<meta charset=\"UTF-8\">\n");
|
||||
mg_printf(conn, "<title>Embedded websocket example</title>\n");
|
||||
httplib_printf(conn, "<!DOCTYPE html>\n");
|
||||
httplib_printf(conn, "<html>\n<head>\n");
|
||||
httplib_printf(conn, "<meta charset=\"UTF-8\">\n");
|
||||
httplib_printf(conn, "<title>Embedded websocket example</title>\n");
|
||||
|
||||
#ifdef USE_WEBSOCKET
|
||||
/* mg_printf(conn, "<script type=\"text/javascript\"><![CDATA[\n"); ...
|
||||
/* httplib_printf(conn, "<script type=\"text/javascript\"><![CDATA[\n"); ...
|
||||
* xhtml style */
|
||||
mg_printf(conn, "<script>\n");
|
||||
mg_printf(
|
||||
httplib_printf(conn, "<script>\n");
|
||||
httplib_printf(
|
||||
conn,
|
||||
"function load() {\n"
|
||||
" var wsproto = (location.protocol === 'https:') ? 'wss:' : 'ws:';\n"
|
||||
@@ -491,17 +485,17 @@ WebSocketStartHandler(struct mg_connection *conn, void *cbdata)
|
||||
" connection.close();\n"
|
||||
" }\n"
|
||||
"}\n");
|
||||
/* mg_printf(conn, "]]></script>\n"); ... xhtml style */
|
||||
mg_printf(conn, "</script>\n");
|
||||
mg_printf(conn, "</head>\n<body onload=\"load()\">\n");
|
||||
mg_printf(
|
||||
/* httplib_printf(conn, "]]></script>\n"); ... xhtml style */
|
||||
httplib_printf(conn, "</script>\n");
|
||||
httplib_printf(conn, "</head>\n<body onload=\"load()\">\n");
|
||||
httplib_printf(
|
||||
conn,
|
||||
"<div id='websock_text_field'>No websocket connection yet</div>\n");
|
||||
#else
|
||||
mg_printf(conn, "</head>\n<body>\n");
|
||||
mg_printf(conn, "Example not compiled with USE_WEBSOCKET\n");
|
||||
httplib_printf(conn, "</head>\n<body>\n");
|
||||
httplib_printf(conn, "Example not compiled with USE_WEBSOCKET\n");
|
||||
#endif
|
||||
mg_printf(conn, "</body>\n</html>\n");
|
||||
httplib_printf(conn, "</body>\n</html>\n");
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -517,7 +511,7 @@ WebSocketStartHandler(struct mg_connection *conn, void *cbdata)
|
||||
#define MAX_WS_CLIENTS (5)
|
||||
|
||||
struct t_ws_client {
|
||||
struct mg_connection *conn;
|
||||
struct httplib_connection *conn;
|
||||
int state;
|
||||
} static ws_clients[MAX_WS_CLIENTS];
|
||||
|
||||
@@ -533,39 +527,36 @@ struct t_ws_client {
|
||||
|
||||
|
||||
int
|
||||
WebSocketConnectHandler(const struct mg_connection *conn, void *cbdata)
|
||||
WebSocketConnectHandler(const struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
struct mg_context *ctx = mg_get_context(conn);
|
||||
struct httplib_context *ctx = httplib_get_context(conn);
|
||||
int reject = 1;
|
||||
int i;
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_lock_context(ctx);
|
||||
for (i = 0; i < MAX_WS_CLIENTS; i++) {
|
||||
if (ws_clients[i].conn == NULL) {
|
||||
ws_clients[i].conn = (struct mg_connection *)conn;
|
||||
ws_clients[i].conn = (struct httplib_connection *)conn;
|
||||
ws_clients[i].state = 1;
|
||||
mg_set_user_connection_data(ws_clients[i].conn,
|
||||
(void *)(ws_clients + i));
|
||||
httplib_set_user_connection_data(ws_clients[i].conn, (void *)(ws_clients + i));
|
||||
reject = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
|
||||
fprintf(stdout,
|
||||
"Websocket client %s\r\n\r\n",
|
||||
(reject ? "rejected" : "accepted"));
|
||||
fprintf(stdout, "Websocket client %s\r\n\r\n", (reject ? "rejected" : "accepted"));
|
||||
return reject;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
WebSocketReadyHandler(struct mg_connection *conn, void *cbdata)
|
||||
WebSocketReadyHandler(struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
const char *text = "Hello from the websocket ready handler";
|
||||
struct t_ws_client *client = mg_get_user_connection_data(conn);
|
||||
struct t_ws_client *client = httplib_get_user_connection_data(conn);
|
||||
|
||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
|
||||
httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
|
||||
fprintf(stdout, "Greeting message sent to websocket client\r\n\r\n");
|
||||
ASSERT(client->conn == conn);
|
||||
ASSERT(client->state == 1);
|
||||
@@ -575,13 +566,9 @@ WebSocketReadyHandler(struct mg_connection *conn, void *cbdata)
|
||||
|
||||
|
||||
int
|
||||
WebsocketDataHandler(struct mg_connection *conn,
|
||||
int bits,
|
||||
char *data,
|
||||
size_t len,
|
||||
void *cbdata)
|
||||
WebsocketDataHandler(struct httplib_connection *conn, int bits, char *data, size_t len, void *cbdata)
|
||||
{
|
||||
struct t_ws_client *client = mg_get_user_connection_data(conn);
|
||||
struct t_ws_client *client = httplib_get_user_connection_data(conn);
|
||||
ASSERT(client->conn == conn);
|
||||
ASSERT(client->state >= 1);
|
||||
|
||||
@@ -594,25 +581,24 @@ WebsocketDataHandler(struct mg_connection *conn,
|
||||
|
||||
|
||||
void
|
||||
WebSocketCloseHandler(const struct mg_connection *conn, void *cbdata)
|
||||
WebSocketCloseHandler(const struct httplib_connection *conn, void *cbdata)
|
||||
{
|
||||
struct mg_context *ctx = mg_get_context(conn);
|
||||
struct t_ws_client *client = mg_get_user_connection_data(conn);
|
||||
struct httplib_context *ctx = httplib_get_context(conn);
|
||||
struct t_ws_client *client = httplib_get_user_connection_data(conn);
|
||||
ASSERT(client->conn == conn);
|
||||
ASSERT(client->state >= 1);
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_lock_context(ctx);
|
||||
client->state = 0;
|
||||
client->conn = NULL;
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
|
||||
fprintf(stdout,
|
||||
"Client droped from the set of webserver connections\r\n\r\n");
|
||||
fprintf(stdout, "Client droped from the set of webserver connections\r\n\r\n");
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
InformWebsockets(struct mg_context *ctx)
|
||||
InformWebsockets(struct httplib_context *ctx)
|
||||
{
|
||||
static unsigned long cnt = 0;
|
||||
char text[32];
|
||||
@@ -620,16 +606,13 @@ InformWebsockets(struct mg_context *ctx)
|
||||
|
||||
sprintf(text, "%lu", ++cnt);
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_lock_context(ctx);
|
||||
for (i = 0; i < MAX_WS_CLIENTS; i++) {
|
||||
if (ws_clients[i].state == 2) {
|
||||
mg_websocket_write(ws_clients[i].conn,
|
||||
WEBSOCKET_OPCODE_TEXT,
|
||||
text,
|
||||
strlen(text));
|
||||
httplib_websocket_write(ws_clients[i].conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
|
||||
}
|
||||
}
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -747,15 +730,15 @@ main(int argc, char *argv[])
|
||||
#endif
|
||||
#endif
|
||||
0};
|
||||
struct mg_callbacks callbacks;
|
||||
struct mg_context *ctx;
|
||||
struct mg_server_ports ports[32];
|
||||
struct httplib_callbacks callbacks;
|
||||
struct httplib_context *ctx;
|
||||
struct httplib_server_ports ports[32];
|
||||
int port_cnt, n;
|
||||
int err = 0;
|
||||
|
||||
/* Check if libcivetweb has been built with all required features. */
|
||||
#ifdef USE_IPV6
|
||||
if (!mg_check_feature(8)) {
|
||||
if (!httplib_check_feature(8)) {
|
||||
fprintf(stderr,
|
||||
"Error: Embedded example built with IPv6 support, "
|
||||
"but civetweb library build without.\n");
|
||||
@@ -763,7 +746,7 @@ main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_WEBSOCKET
|
||||
if (!mg_check_feature(16)) {
|
||||
if (!httplib_check_feature(16)) {
|
||||
fprintf(stderr,
|
||||
"Error: Embedded example built with websocket support, "
|
||||
"but civetweb library build without.\n");
|
||||
@@ -771,7 +754,7 @@ main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
#ifndef NO_SSL
|
||||
if (!mg_check_feature(2)) {
|
||||
if (!httplib_check_feature(2)) {
|
||||
fprintf(stderr,
|
||||
"Error: Embedded example built with SSL support, "
|
||||
"but civetweb library build without.\n");
|
||||
@@ -788,58 +771,49 @@ main(int argc, char *argv[])
|
||||
#ifndef NO_SSL
|
||||
callbacks.init_ssl = init_ssl;
|
||||
#endif
|
||||
ctx = mg_start(&callbacks, 0, options);
|
||||
ctx = httplib_start(&callbacks, 0, options);
|
||||
|
||||
/* Add handler EXAMPLE_URI, to explain the example */
|
||||
mg_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0);
|
||||
mg_set_request_handler(ctx, EXIT_URI, ExitHandler, 0);
|
||||
httplib_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0);
|
||||
httplib_set_request_handler(ctx, EXIT_URI, ExitHandler, 0);
|
||||
|
||||
/* Add handler for /A* and special handler for /A/B */
|
||||
mg_set_request_handler(ctx, "/A", AHandler, 0);
|
||||
mg_set_request_handler(ctx, "/A/B", ABHandler, 0);
|
||||
httplib_set_request_handler(ctx, "/A", AHandler, 0);
|
||||
httplib_set_request_handler(ctx, "/A/B", ABHandler, 0);
|
||||
|
||||
/* Add handler for /B, /B/A, /B/B but not for /B* */
|
||||
mg_set_request_handler(ctx, "/B$", BXHandler, (void *)0);
|
||||
mg_set_request_handler(ctx, "/B/A$", BXHandler, (void *)1);
|
||||
mg_set_request_handler(ctx, "/B/B$", BXHandler, (void *)2);
|
||||
httplib_set_request_handler(ctx, "/B$", BXHandler, (void *)0);
|
||||
httplib_set_request_handler(ctx, "/B/A$", BXHandler, (void *)1);
|
||||
httplib_set_request_handler(ctx, "/B/B$", BXHandler, (void *)2);
|
||||
|
||||
/* Add handler for all files with .foo extention */
|
||||
mg_set_request_handler(ctx, "**.foo$", FooHandler, 0);
|
||||
httplib_set_request_handler(ctx, "**.foo$", FooHandler, 0);
|
||||
|
||||
/* Add handler for /close extention */
|
||||
mg_set_request_handler(ctx, "/close", CloseHandler, 0);
|
||||
httplib_set_request_handler(ctx, "/close", CloseHandler, 0);
|
||||
|
||||
/* Add handler for /form (serve a file outside the document root) */
|
||||
mg_set_request_handler(ctx,
|
||||
"/form",
|
||||
FileHandler,
|
||||
(void *)"../../test/form.html");
|
||||
httplib_set_request_handler(ctx, "/form", FileHandler, (void *)"../../test/form.html");
|
||||
|
||||
/* Add handler for form data */
|
||||
mg_set_request_handler(ctx,
|
||||
httplib_set_request_handler(ctx,
|
||||
"/handle_form.embedded_c.example.callback",
|
||||
FormHandler,
|
||||
(void *)0);
|
||||
|
||||
/* Add a file upload handler for parsing files on the fly */
|
||||
mg_set_request_handler(ctx,
|
||||
"/on_the_fly_form",
|
||||
FileUploadForm,
|
||||
(void *)"/on_the_fly_form.md5.callback");
|
||||
mg_set_request_handler(ctx,
|
||||
"/on_the_fly_form.md5.callback",
|
||||
CheckSumHandler,
|
||||
(void *)0);
|
||||
httplib_set_request_handler(ctx, "/on_the_fly_form", FileUploadForm, (void *)"/on_the_fly_form.md5.callback");
|
||||
httplib_set_request_handler(ctx, "/on_the_fly_form.md5.callback", CheckSumHandler, (void *)0);
|
||||
|
||||
/* Add handler for /cookie example */
|
||||
mg_set_request_handler(ctx, "/cookie", CookieHandler, 0);
|
||||
httplib_set_request_handler(ctx, "/cookie", CookieHandler, 0);
|
||||
|
||||
/* Add HTTP site to open a websocket connection */
|
||||
mg_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0);
|
||||
httplib_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0);
|
||||
|
||||
#ifdef USE_WEBSOCKET
|
||||
/* WS site for the websocket connection */
|
||||
mg_set_websocket_handler(ctx,
|
||||
httplib_set_websocket_handler(ctx,
|
||||
"/websocket",
|
||||
WebSocketConnectHandler,
|
||||
WebSocketReadyHandler,
|
||||
@@ -850,7 +824,7 @@ main(int argc, char *argv[])
|
||||
|
||||
/* List all listening ports */
|
||||
memset(ports, 0, sizeof(ports));
|
||||
port_cnt = mg_get_server_ports(ctx, 32, ports);
|
||||
port_cnt = httplib_get_server_ports(ctx, 32, ports);
|
||||
printf("\n%i listening ports:\n\n", port_cnt);
|
||||
|
||||
for (n = 0; n < port_cnt && n < 32; n++) {
|
||||
@@ -899,7 +873,7 @@ main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
/* Stop the server */
|
||||
mg_stop(ctx);
|
||||
httplib_stop(ctx);
|
||||
printf("Server stopped.\n");
|
||||
printf("Bye!\n");
|
||||
|
||||
|
@@ -23,30 +23,30 @@ class ExampleHandler : public CivetHandler
|
||||
{
|
||||
public:
|
||||
bool
|
||||
handleGet(CivetServer *server, struct mg_connection *conn)
|
||||
handleGet(CivetServer *server, struct httplib_connection *conn)
|
||||
{
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: "
|
||||
"text/html\r\nConnection: close\r\n\r\n");
|
||||
mg_printf(conn, "<html><body>\r\n");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn, "<html><body>\r\n");
|
||||
httplib_printf(conn,
|
||||
"<h2>This is an example text from a C++ handler</h2>\r\n");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the A handler <a "
|
||||
"href=\"a\">click here</a></p>\r\n");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the A handler with a parameter "
|
||||
"<a href=\"a?param=1\">click here</a></p>\r\n");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the A/B handler <a "
|
||||
"href=\"a/b\">click here</a></p>\r\n");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To see a page from the *.foo handler <a "
|
||||
"href=\"xy.foo\">click here</a></p>\r\n");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"<p>To exit <a href=\"%s\">click here</a></p>\r\n",
|
||||
EXIT_URI);
|
||||
mg_printf(conn, "</body></html>\r\n");
|
||||
httplib_printf(conn, "</body></html>\r\n");
|
||||
return true;
|
||||
}
|
||||
};
|
||||
@@ -55,12 +55,12 @@ class ExitHandler : public CivetHandler
|
||||
{
|
||||
public:
|
||||
bool
|
||||
handleGet(CivetServer *server, struct mg_connection *conn)
|
||||
handleGet(CivetServer *server, struct httplib_connection *conn)
|
||||
{
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: "
|
||||
"text/plain\r\nConnection: close\r\n\r\n");
|
||||
mg_printf(conn, "Bye!\n");
|
||||
httplib_printf(conn, "Bye!\n");
|
||||
exitNow = true;
|
||||
return true;
|
||||
}
|
||||
@@ -72,31 +72,31 @@ class AHandler : public CivetHandler
|
||||
bool
|
||||
handleAll(const char *method,
|
||||
CivetServer *server,
|
||||
struct mg_connection *conn)
|
||||
struct httplib_connection *conn)
|
||||
{
|
||||
std::string s = "";
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: "
|
||||
"text/html\r\nConnection: close\r\n\r\n");
|
||||
mg_printf(conn, "<html><body>");
|
||||
mg_printf(conn, "<h2>This is the A handler for \"%s\" !</h2>", method);
|
||||
httplib_printf(conn, "<html><body>");
|
||||
httplib_printf(conn, "<h2>This is the A handler for \"%s\" !</h2>", method);
|
||||
if (CivetServer::getParam(conn, "param", s)) {
|
||||
mg_printf(conn, "<p>param set to %s</p>", s.c_str());
|
||||
httplib_printf(conn, "<p>param set to %s</p>", s.c_str());
|
||||
} else {
|
||||
mg_printf(conn, "<p>param not set</p>");
|
||||
httplib_printf(conn, "<p>param not set</p>");
|
||||
}
|
||||
mg_printf(conn, "</body></html>\n");
|
||||
httplib_printf(conn, "</body></html>\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
bool
|
||||
handleGet(CivetServer *server, struct mg_connection *conn)
|
||||
handleGet(CivetServer *server, struct httplib_connection *conn)
|
||||
{
|
||||
return handleAll("GET", server, conn);
|
||||
}
|
||||
bool
|
||||
handlePost(CivetServer *server, struct mg_connection *conn)
|
||||
handlePost(CivetServer *server, struct httplib_connection *conn)
|
||||
{
|
||||
return handleAll("POST", server, conn);
|
||||
}
|
||||
@@ -106,14 +106,14 @@ class ABHandler : public CivetHandler
|
||||
{
|
||||
public:
|
||||
bool
|
||||
handleGet(CivetServer *server, struct mg_connection *conn)
|
||||
handleGet(CivetServer *server, struct httplib_connection *conn)
|
||||
{
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: "
|
||||
"text/html\r\nConnection: close\r\n\r\n");
|
||||
mg_printf(conn, "<html><body>");
|
||||
mg_printf(conn, "<h2>This is the AB handler!!!</h2>");
|
||||
mg_printf(conn, "</body></html>\n");
|
||||
httplib_printf(conn, "<html><body>");
|
||||
httplib_printf(conn, "<h2>This is the AB handler!!!</h2>");
|
||||
httplib_printf(conn, "</body></html>\n");
|
||||
return true;
|
||||
}
|
||||
};
|
||||
@@ -122,68 +122,64 @@ class FooHandler : public CivetHandler
|
||||
{
|
||||
public:
|
||||
bool
|
||||
handleGet(CivetServer *server, struct mg_connection *conn)
|
||||
handleGet(CivetServer *server, struct httplib_connection *conn)
|
||||
{
|
||||
/* Handler may access the request info using mg_get_request_info */
|
||||
const struct mg_request_info *req_info = mg_get_request_info(conn);
|
||||
/* Handler may access the request info using httplib_get_request_info */
|
||||
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
|
||||
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: "
|
||||
"text/html\r\nConnection: close\r\n\r\n");
|
||||
|
||||
mg_printf(conn, "<html><body>\n");
|
||||
mg_printf(conn, "<h2>This is the Foo GET handler!!!</h2>\n");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn, "<html><body>\n");
|
||||
httplib_printf(conn, "<h2>This is the Foo GET handler!!!</h2>\n");
|
||||
httplib_printf(conn,
|
||||
"<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>\n",
|
||||
req_info->request_method,
|
||||
req_info->uri,
|
||||
req_info->http_version);
|
||||
mg_printf(conn, "</body></html>\n");
|
||||
httplib_printf(conn, "</body></html>\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
bool
|
||||
handlePost(CivetServer *server, struct mg_connection *conn)
|
||||
handlePost(CivetServer *server, struct httplib_connection *conn)
|
||||
{
|
||||
/* Handler may access the request info using mg_get_request_info */
|
||||
const struct mg_request_info *req_info = mg_get_request_info(conn);
|
||||
/* Handler may access the request info using httplib_get_request_info */
|
||||
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
|
||||
long long rlen, wlen;
|
||||
long long nlen = 0;
|
||||
long long tlen = req_info->content_length;
|
||||
char buf[1024];
|
||||
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: "
|
||||
"text/html\r\nConnection: close\r\n\r\n");
|
||||
|
||||
mg_printf(conn, "<html><body>\n");
|
||||
mg_printf(conn, "<h2>This is the Foo POST handler!!!</h2>\n");
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn, "<html><body>\n");
|
||||
httplib_printf(conn, "<h2>This is the Foo POST handler!!!</h2>\n");
|
||||
httplib_printf(conn,
|
||||
"<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>\n",
|
||||
req_info->request_method,
|
||||
req_info->uri,
|
||||
req_info->http_version);
|
||||
mg_printf(conn, "<p>Content Length: %li</p>\n", (long)tlen);
|
||||
mg_printf(conn, "<pre>\n");
|
||||
httplib_printf(conn, "<p>Content Length: %li</p>\n", (long)tlen);
|
||||
httplib_printf(conn, "<pre>\n");
|
||||
|
||||
while (nlen < tlen) {
|
||||
rlen = tlen - nlen;
|
||||
if (rlen > sizeof(buf)) {
|
||||
rlen = sizeof(buf);
|
||||
}
|
||||
rlen = mg_read(conn, buf, (size_t)rlen);
|
||||
if (rlen <= 0) {
|
||||
break;
|
||||
}
|
||||
wlen = mg_write(conn, buf, (size_t)rlen);
|
||||
if (rlen != rlen) {
|
||||
break;
|
||||
}
|
||||
rlen = httplib_read(conn, buf, (size_t)rlen);
|
||||
if (rlen <= 0) break;
|
||||
wlen = httplib_write(conn, buf, (size_t)rlen);
|
||||
if (rlen != rlen) break;
|
||||
nlen += wlen;
|
||||
}
|
||||
|
||||
mg_printf(conn, "\n</pre>\n");
|
||||
mg_printf(conn, "</body></html>\n");
|
||||
httplib_printf(conn, "\n</pre>\n");
|
||||
httplib_printf(conn, "</body></html>\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -191,10 +187,10 @@ class FooHandler : public CivetHandler
|
||||
#define fopen_recursive fopen
|
||||
|
||||
bool
|
||||
handlePut(CivetServer *server, struct mg_connection *conn)
|
||||
handlePut(CivetServer *server, struct httplib_connection *conn)
|
||||
{
|
||||
/* Handler may access the request info using mg_get_request_info */
|
||||
const struct mg_request_info *req_info = mg_get_request_info(conn);
|
||||
/* Handler may access the request info using httplib_get_request_info */
|
||||
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
|
||||
long long rlen, wlen;
|
||||
long long nlen = 0;
|
||||
long long tlen = req_info->content_length;
|
||||
@@ -220,7 +216,7 @@ class FooHandler : public CivetHandler
|
||||
if (rlen > sizeof(buf)) {
|
||||
rlen = sizeof(buf);
|
||||
}
|
||||
rlen = mg_read(conn, buf, (size_t)rlen);
|
||||
rlen = httplib_read(conn, buf, (size_t)rlen);
|
||||
if (rlen <= 0) {
|
||||
fail = 1;
|
||||
break;
|
||||
@@ -236,12 +232,12 @@ class FooHandler : public CivetHandler
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 409 Conflict\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"Connection: close\r\n\r\n");
|
||||
} else {
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 201 Created\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"Connection: close\r\n\r\n");
|
||||
|
@@ -3,9 +3,9 @@
|
||||
#include "civetweb.h"
|
||||
|
||||
// This function will be called by civetweb on every new request.
|
||||
static int begin_request_handler(struct mg_connection *conn)
|
||||
static int begin_request_handler(struct httplib_connection *conn)
|
||||
{
|
||||
const struct mg_request_info *request_info = mg_get_request_info(conn);
|
||||
const struct httplib_request_info *request_info = httplib_get_request_info(conn);
|
||||
char content[100];
|
||||
|
||||
// Prepare the message we're going to send
|
||||
@@ -14,7 +14,7 @@ static int begin_request_handler(struct mg_connection *conn)
|
||||
request_info->remote_port);
|
||||
|
||||
// Send HTTP reply to the client
|
||||
mg_printf(conn,
|
||||
httplib_printf(conn,
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"Content-Length: %d\r\n" // Always set Content-Length
|
||||
@@ -29,8 +29,8 @@ static int begin_request_handler(struct mg_connection *conn)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct mg_context *ctx;
|
||||
struct mg_callbacks callbacks;
|
||||
struct httplib_context *ctx;
|
||||
struct httplib_callbacks callbacks;
|
||||
|
||||
// List of options. Last element must be NULL.
|
||||
const char *options[] = {"listening_ports", "8080", NULL};
|
||||
@@ -40,14 +40,14 @@ int main(void)
|
||||
callbacks.begin_request = begin_request_handler;
|
||||
|
||||
// Start the web server.
|
||||
ctx = mg_start(&callbacks, NULL, options);
|
||||
ctx = httplib_start(&callbacks, NULL, options);
|
||||
|
||||
// Wait until user hits "enter". Server is running in separate thread.
|
||||
// Navigating to http://localhost:8080 will invoke begin_request_handler().
|
||||
getchar();
|
||||
|
||||
// Stop the server.
|
||||
mg_stop(ctx);
|
||||
httplib_stop(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -10,22 +10,22 @@ static const char *html_form =
|
||||
"<input type=\"submit\" />"
|
||||
"</form></body></html>";
|
||||
|
||||
static int begin_request_handler(struct mg_connection *conn)
|
||||
static int begin_request_handler(struct httplib_connection *conn)
|
||||
{
|
||||
const struct mg_request_info *ri = mg_get_request_info(conn);
|
||||
const struct httplib_request_info *ri = httplib_get_request_info(conn);
|
||||
char post_data[1024], input1[sizeof(post_data)], input2[sizeof(post_data)];
|
||||
int post_data_len;
|
||||
|
||||
if (!strcmp(ri->uri, "/handle_post_request")) {
|
||||
// User has submitted a form, show submitted data and a variable value
|
||||
post_data_len = mg_read(conn, post_data, sizeof(post_data));
|
||||
post_data_len = httplib_read(conn, post_data, sizeof(post_data));
|
||||
|
||||
// Parse form data. input1 and input2 are guaranteed to be NUL-terminated
|
||||
mg_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1));
|
||||
mg_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2));
|
||||
httplib_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1));
|
||||
httplib_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2));
|
||||
|
||||
// Send reply to the client, showing submitted form values.
|
||||
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
httplib_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n\r\n"
|
||||
"Submitted data: [%.*s]\n"
|
||||
"Submitted data length: %d bytes\n"
|
||||
@@ -34,7 +34,7 @@ static int begin_request_handler(struct mg_connection *conn)
|
||||
post_data_len, post_data, post_data_len, input1, input2);
|
||||
} else {
|
||||
// Show HTML form.
|
||||
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
httplib_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
"Content-Length: %d\r\n"
|
||||
"Content-Type: text/html\r\n\r\n%s",
|
||||
(int) strlen(html_form), html_form);
|
||||
@@ -44,15 +44,15 @@ static int begin_request_handler(struct mg_connection *conn)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct mg_context *ctx;
|
||||
struct httplib_context *ctx;
|
||||
const char *options[] = {"listening_ports", "8080", NULL};
|
||||
struct mg_callbacks callbacks;
|
||||
struct httplib_callbacks callbacks;
|
||||
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.begin_request = begin_request_handler;
|
||||
ctx = mg_start(&callbacks, NULL, options);
|
||||
ctx = httplib_start(&callbacks, NULL, options);
|
||||
getchar(); // Wait until user hits "enter"
|
||||
mg_stop(ctx);
|
||||
httplib_stop(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -29,7 +29,7 @@ typedef __int64 int64_t;
|
||||
|
||||
|
||||
/* callback: used to generate all content */
|
||||
static int begin_request_handler(struct mg_connection *conn)
|
||||
static int begin_request_handler(struct httplib_connection *conn)
|
||||
{
|
||||
const char * tempPath = ".";
|
||||
#ifdef _WIN32
|
||||
@@ -40,10 +40,10 @@ static int begin_request_handler(struct mg_connection *conn)
|
||||
tempPath = "/tmp";
|
||||
#endif
|
||||
|
||||
if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) {
|
||||
if (!strcmp(httplib_get_request_info(conn)->uri, "/handle_post_request")) {
|
||||
|
||||
mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
|
||||
mg_upload(conn, tempPath);
|
||||
httplib_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
|
||||
httplib_upload(conn, tempPath);
|
||||
} else {
|
||||
/* Show HTML form. */
|
||||
/* See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1 */
|
||||
@@ -60,7 +60,7 @@ static int begin_request_handler(struct mg_connection *conn)
|
||||
""
|
||||
"</body></html>";
|
||||
|
||||
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
httplib_printf(conn, "HTTP/1.0 200 OK\r\n"
|
||||
"Content-Length: %d\r\n"
|
||||
"Content-Type: text/html\r\n\r\n%s",
|
||||
(int) strlen(html_form), html_form);
|
||||
@@ -72,9 +72,9 @@ static int begin_request_handler(struct mg_connection *conn)
|
||||
|
||||
|
||||
/* callback: called after uploading a file is completed */
|
||||
static void upload_handler(struct mg_connection *conn, const char *path)
|
||||
static void upload_handler(struct httplib_connection *conn, const char *path)
|
||||
{
|
||||
mg_printf(conn, "Saved [%s]", path);
|
||||
httplib_printf(conn, "Saved [%s]", path);
|
||||
}
|
||||
|
||||
|
||||
@@ -85,11 +85,11 @@ int main(void)
|
||||
const char * PORT = "8080";
|
||||
|
||||
/* Startup options for the server */
|
||||
struct mg_context *ctx;
|
||||
struct httplib_context *ctx;
|
||||
const char *options[] = {
|
||||
"listening_ports", PORT,
|
||||
NULL};
|
||||
struct mg_callbacks callbacks;
|
||||
struct httplib_callbacks callbacks;
|
||||
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.begin_request = begin_request_handler;
|
||||
@@ -100,11 +100,11 @@ int main(void)
|
||||
printf("Open http://localhost:%s/ in your browser.\n\n", PORT);
|
||||
|
||||
/* Start the server */
|
||||
ctx = mg_start(&callbacks, NULL, options);
|
||||
ctx = httplib_start(&callbacks, NULL, options);
|
||||
|
||||
/* Wait until thr user hits "enter", then stop the server */
|
||||
getchar();
|
||||
mg_stop(ctx);
|
||||
httplib_stop(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -11,49 +11,49 @@
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#define mg_sleep(x) Sleep(x)
|
||||
#define httplib_sleep(x) Sleep(x)
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#define mg_sleep(x) usleep((x)*1000)
|
||||
#define httplib_sleep(x) usleep((x)*1000)
|
||||
#endif
|
||||
|
||||
|
||||
static void
|
||||
send_to_all_websockets(struct mg_context *ctx, const char *data, int data_len)
|
||||
send_to_all_websockets(struct httplib_context *ctx, const char *data, int data_len)
|
||||
{
|
||||
|
||||
int i;
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_lock_context(ctx);
|
||||
for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) {
|
||||
if (ws_ctx->socketList[i]
|
||||
&& (ws_ctx->socketList[i]->webSockState == 2)) {
|
||||
mg_websocket_write(ws_ctx->socketList[i]->conn,
|
||||
httplib_websocket_write(ws_ctx->socketList[i]->conn,
|
||||
WEBSOCKET_OPCODE_TEXT,
|
||||
data,
|
||||
data_len);
|
||||
}
|
||||
}
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
websocket_ready_handler(struct mg_connection *conn, void *_ignored)
|
||||
websocket_ready_handler(struct httplib_connection *conn, void *_ignored)
|
||||
{
|
||||
|
||||
int i;
|
||||
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);
|
||||
const struct httplib_request_info *rq = httplib_get_request_info(conn);
|
||||
struct httplib_context *ctx = httplib_get_context(conn);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
tWebSockInfo *wsock = malloc(sizeof(tWebSockInfo));
|
||||
assert(wsock);
|
||||
wsock->webSockState = 0;
|
||||
mg_set_user_connection_data(conn, wsock);
|
||||
httplib_set_user_connection_data(conn, wsock);
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_lock_context(ctx);
|
||||
for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) {
|
||||
if (0 == ws_ctx->socketList[i]) {
|
||||
ws_ctx->socketList[i] = wsock;
|
||||
@@ -65,7 +65,7 @@ websocket_ready_handler(struct mg_connection *conn, void *_ignored)
|
||||
printf("\nNew websocket attached: %s:%u\n",
|
||||
rq->remote_addr,
|
||||
rq->remote_port);
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
}
|
||||
|
||||
|
||||
@@ -84,33 +84,33 @@ websocket_done(tWebSockContext *ws_ctx, tWebSockInfo *wsock)
|
||||
}
|
||||
}
|
||||
printf("\nClose websocket attached: %s:%u\n",
|
||||
mg_get_request_info(wsock->conn)->remote_addr,
|
||||
mg_get_request_info(wsock->conn)->remote_port);
|
||||
httplib_get_request_info(wsock->conn)->remote_addr,
|
||||
httplib_get_request_info(wsock->conn)->remote_port);
|
||||
free(wsock);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
websocket_data_handler(struct mg_connection *conn,
|
||||
websocket_data_handler(struct httplib_connection *conn,
|
||||
int flags,
|
||||
char *data,
|
||||
size_t data_len,
|
||||
void *_ignored)
|
||||
{
|
||||
|
||||
const struct mg_request_info *rq = mg_get_request_info(conn);
|
||||
const struct httplib_request_info *rq = httplib_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);
|
||||
struct httplib_context *ctx = httplib_get_context(conn);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
char msg[128];
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_lock_context(ctx);
|
||||
if (flags == 136) {
|
||||
// close websock
|
||||
websocket_done(ws_ctx, wsock);
|
||||
mg_set_user_connection_data(conn, NULL);
|
||||
mg_unlock_context(ctx);
|
||||
httplib_set_user_connection_data(conn, NULL);
|
||||
httplib_unlock_context(ctx);
|
||||
return 1;
|
||||
}
|
||||
if (((data_len >= 5) && (data_len < 100) && (flags == 129))
|
||||
@@ -127,42 +127,42 @@ websocket_data_handler(struct mg_connection *conn,
|
||||
if (gid > 0 && chk != NULL && *chk == 0) {
|
||||
wsock->webSockState = 2;
|
||||
}
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// chat message
|
||||
if ((wsock->webSockState == 2) && (!memcmp(data, "msg ", 4))) {
|
||||
send_to_all_websockets(ctx, data, data_len);
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// keep alive
|
||||
if ((data_len == 4) && !memcmp(data, "ping", 4)) {
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
mg_unlock_context(ctx);
|
||||
httplib_unlock_context(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
connection_close_handler(const struct mg_connection *conn, void *_ignored)
|
||||
connection_close_handler(const struct httplib_connection *conn, void *_ignored)
|
||||
{
|
||||
|
||||
const struct mg_request_info *rq = mg_get_request_info(conn);
|
||||
const struct httplib_request_info *rq = httplib_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);
|
||||
struct httplib_context *ctx = httplib_get_context(conn);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
|
||||
mg_lock_context(ctx);
|
||||
httplib_lock_context(ctx);
|
||||
websocket_done(ws_ctx, wsock);
|
||||
mg_set_user_connection_data(conn, NULL);
|
||||
mg_unlock_context(ctx);
|
||||
httplib_set_user_connection_data(conn, NULL);
|
||||
httplib_unlock_context(ctx);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,8 +171,8 @@ eventMain(void *arg)
|
||||
{
|
||||
|
||||
char msg[256];
|
||||
struct mg_context *ctx = (struct mg_context *)arg;
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
|
||||
struct httplib_context *ctx = (struct httplib_context *)arg;
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
|
||||
ws_ctx->runLoop = 1;
|
||||
while (ws_ctx->runLoop) {
|
||||
@@ -181,7 +181,7 @@ eventMain(void *arg)
|
||||
strftime(msg, sizeof(msg), "title %c", timestr);
|
||||
send_to_all_websockets(ctx, msg, strlen(msg));
|
||||
|
||||
mg_sleep(1000);
|
||||
httplib_sleep(1000);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -189,7 +189,7 @@ eventMain(void *arg)
|
||||
|
||||
|
||||
void
|
||||
websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len)
|
||||
websock_send_broadcast(struct httplib_context *ctx, const char *data, int data_len)
|
||||
{
|
||||
|
||||
char buffer[260];
|
||||
@@ -204,22 +204,22 @@ websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len)
|
||||
|
||||
|
||||
void
|
||||
websock_init_lib(const struct mg_context *ctx)
|
||||
websock_init_lib(const struct httplib_context *ctx)
|
||||
{
|
||||
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
memset(ws_ctx, 0, sizeof(*ws_ctx));
|
||||
/* todo: use mg_start_thread_id instead of mg_start_thread */
|
||||
mg_start_thread(eventMain, (void *)ctx);
|
||||
/* todo: use httplib_start_thread_id instead of httplib_start_thread */
|
||||
httplib_start_thread(eventMain, (void *)ctx);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
websock_exit_lib(const struct mg_context *ctx)
|
||||
websock_exit_lib(const struct httplib_context *ctx)
|
||||
{
|
||||
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
|
||||
tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx);
|
||||
ws_ctx->runLoop = 0;
|
||||
/* todo: wait for the thread instead of a timeout */
|
||||
mg_sleep(2000);
|
||||
httplib_sleep(2000);
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@ extern "C" {
|
||||
typedef struct tWebSockInfo {
|
||||
int webSockState;
|
||||
unsigned long initId;
|
||||
struct mg_connection *conn;
|
||||
struct httplib_connection *conn;
|
||||
} tWebSockInfo;
|
||||
|
||||
#define MAX_NUM_OF_WEBSOCKS (256)
|
||||
@@ -22,23 +22,19 @@ typedef struct tWebSockContext {
|
||||
} tWebSockContext;
|
||||
|
||||
|
||||
void websock_init_lib(const struct mg_context *ctx);
|
||||
void websock_exit_lib(const struct mg_context *ctx);
|
||||
void websock_init_lib(const struct httplib_context *ctx);
|
||||
void websock_exit_lib(const struct httplib_context *ctx);
|
||||
|
||||
void
|
||||
websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len);
|
||||
websock_send_broadcast(struct httplib_context *ctx, const char *data, int data_len);
|
||||
|
||||
void websocket_ready_handler(struct mg_connection *conn, void *_ignored);
|
||||
int websocket_data_handler(struct mg_connection *conn,
|
||||
int flags,
|
||||
char *data,
|
||||
size_t data_len,
|
||||
void *_ignored);
|
||||
void connection_close_handler(const struct mg_connection *conn, void *_ignored);
|
||||
void websocket_ready_handler(struct httplib_connection *conn, void *_ignored);
|
||||
int websocket_data_handler(struct httplib_connection *conn, int flags, char *data, size_t data_len, void *_ignored);
|
||||
void connection_close_handler(const struct httplib_connection *conn, void *_ignored);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@@ -13,8 +13,8 @@
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
struct mg_context *ctx = 0;
|
||||
struct mg_callbacks callback_funcs = {0};
|
||||
struct httplib_context *ctx = 0;
|
||||
struct httplib_callbacks callback_funcs = {0};
|
||||
tWebSockContext ws_ctx;
|
||||
char inbuf[4];
|
||||
|
||||
@@ -35,9 +35,9 @@ main(void)
|
||||
callback_funcs.init_context = websock_init_lib;
|
||||
callback_funcs.exit_context = websock_exit_lib;
|
||||
|
||||
ctx = mg_start(&callback_funcs, &ws_ctx, server_options);
|
||||
ctx = httplib_start(&callback_funcs, &ws_ctx, server_options);
|
||||
|
||||
mg_set_websocket_handler(ctx,
|
||||
httplib_set_websocket_handler(ctx,
|
||||
"/MyWebSock",
|
||||
NULL,
|
||||
websocket_ready_handler,
|
||||
@@ -46,7 +46,7 @@ main(void)
|
||||
NULL);
|
||||
|
||||
printf("Connect to localhost:%s/websock.htm\n",
|
||||
mg_get_option(ctx, "listening_ports"));
|
||||
httplib_get_option(ctx, "listening_ports"));
|
||||
|
||||
puts("Enter an (ASCII) character or * to exit:");
|
||||
for (;;) {
|
||||
@@ -59,7 +59,7 @@ main(void)
|
||||
websock_send_broadcast(ctx, inbuf, 1);
|
||||
}
|
||||
|
||||
mg_stop(ctx);
|
||||
httplib_stop(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -40,10 +40,10 @@ const size_t websocket_goodbye_msg_len = 14 /* strlen(websocket_goodbye_msg) */;
|
||||
/*************************************************************************************/
|
||||
#if defined(MG_LEGACY_INTERFACE)
|
||||
int
|
||||
websock_server_connect(const struct mg_connection *conn)
|
||||
websock_server_connect(const struct httplib_connection *conn)
|
||||
#else
|
||||
int
|
||||
websocket_server_connect(const struct mg_connection *conn, void *_ignored)
|
||||
websocket_server_connect(const struct httplib_connection *conn, void *_ignored)
|
||||
#endif
|
||||
{
|
||||
printf("Server: Websocket connected\n");
|
||||
@@ -53,33 +53,33 @@ websocket_server_connect(const struct mg_connection *conn, void *_ignored)
|
||||
|
||||
#if defined(MG_LEGACY_INTERFACE)
|
||||
void
|
||||
websocket_server_ready(struct mg_connection *conn)
|
||||
websocket_server_ready(struct httplib_connection *conn)
|
||||
#else
|
||||
void
|
||||
websocket_server_ready(struct mg_connection *conn, void *_ignored)
|
||||
websocket_server_ready(struct httplib_connection *conn, void *_ignored)
|
||||
#endif
|
||||
{
|
||||
printf("Server: Websocket ready\n");
|
||||
|
||||
/* Send websocket welcome message */
|
||||
mg_lock_connection(conn);
|
||||
mg_websocket_write(conn,
|
||||
httplib_lock_connection(conn);
|
||||
httplib_websocket_write(conn,
|
||||
WEBSOCKET_OPCODE_TEXT,
|
||||
websocket_welcome_msg,
|
||||
websocket_welcome_msg_len);
|
||||
mg_unlock_connection(conn);
|
||||
httplib_unlock_connection(conn);
|
||||
}
|
||||
|
||||
|
||||
#if defined(MG_LEGACY_INTERFACE)
|
||||
int
|
||||
websocket_server_data(struct mg_connection *conn,
|
||||
websocket_server_data(struct httplib_connection *conn,
|
||||
int bits,
|
||||
char *data,
|
||||
size_t data_len)
|
||||
#else
|
||||
int
|
||||
websocket_server_data(struct mg_connection *conn,
|
||||
websocket_server_data(struct httplib_connection *conn,
|
||||
int bits,
|
||||
char *data,
|
||||
size_t data_len,
|
||||
@@ -93,20 +93,20 @@ websocket_server_data(struct mg_connection *conn,
|
||||
|
||||
if (data_len < 3 || 0 != memcmp(data, "bye", 3)) {
|
||||
/* Send websocket acknowledge message */
|
||||
mg_lock_connection(conn);
|
||||
mg_websocket_write(conn,
|
||||
httplib_lock_connection(conn);
|
||||
httplib_websocket_write(conn,
|
||||
WEBSOCKET_OPCODE_TEXT,
|
||||
websocket_acknowledge_msg,
|
||||
websocket_acknowledge_msg_len);
|
||||
mg_unlock_connection(conn);
|
||||
httplib_unlock_connection(conn);
|
||||
} else {
|
||||
/* Send websocket acknowledge message */
|
||||
mg_lock_connection(conn);
|
||||
mg_websocket_write(conn,
|
||||
httplib_lock_connection(conn);
|
||||
httplib_websocket_write(conn,
|
||||
WEBSOCKET_OPCODE_TEXT,
|
||||
websocket_goodbye_msg,
|
||||
websocket_goodbye_msg_len);
|
||||
mg_unlock_connection(conn);
|
||||
httplib_unlock_connection(conn);
|
||||
}
|
||||
|
||||
return 1; /* return 1 to keep the connetion open */
|
||||
@@ -115,10 +115,10 @@ websocket_server_data(struct mg_connection *conn,
|
||||
|
||||
#if defined(MG_LEGACY_INTERFACE)
|
||||
void
|
||||
websocket_server_connection_close(const struct mg_connection *conn)
|
||||
websocket_server_connection_close(const struct httplib_connection *conn)
|
||||
#else
|
||||
void
|
||||
websocket_server_connection_close(const struct mg_connection *conn,
|
||||
websocket_server_connection_close(const struct httplib_connection *conn,
|
||||
void *_ignored)
|
||||
#endif
|
||||
{
|
||||
@@ -129,7 +129,7 @@ websocket_server_connection_close(const struct mg_connection *conn,
|
||||
}
|
||||
|
||||
|
||||
struct mg_context *
|
||||
struct httplib_context *
|
||||
start_websocket_server()
|
||||
{
|
||||
const char *options[] = {"document_root",
|
||||
@@ -141,8 +141,8 @@ start_websocket_server()
|
||||
"request_timeout_ms",
|
||||
"5000",
|
||||
0};
|
||||
struct mg_callbacks callbacks;
|
||||
struct mg_context *ctx;
|
||||
struct httplib_callbacks callbacks;
|
||||
struct httplib_context *ctx;
|
||||
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
|
||||
@@ -153,12 +153,12 @@ start_websocket_server()
|
||||
callbacks.websocket_data = websocket_server_data;
|
||||
callbacks.connection_close = websocket_server_connection_close;
|
||||
|
||||
ctx = mg_start(&callbacks, 0, options);
|
||||
ctx = httplib_start(&callbacks, 0, options);
|
||||
#else
|
||||
/* New interface: */
|
||||
ctx = mg_start(&callbacks, 0, options);
|
||||
ctx = httplib_start(&callbacks, 0, options);
|
||||
|
||||
mg_set_websocket_handler(ctx,
|
||||
httplib_set_websocket_handler(ctx,
|
||||
"/websocket",
|
||||
websocket_server_connect,
|
||||
websocket_server_ready,
|
||||
@@ -181,15 +181,11 @@ struct tclient_data {
|
||||
};
|
||||
|
||||
static int
|
||||
websocket_client_data_handler(struct mg_connection *conn,
|
||||
int flags,
|
||||
char *data,
|
||||
size_t data_len,
|
||||
void *user_data)
|
||||
{
|
||||
struct mg_context *ctx = mg_get_context(conn);
|
||||
websocket_client_data_handler(struct httplib_connection *conn, int flags, char *data, size_t data_len, void *user_data) {
|
||||
|
||||
struct httplib_context *ctx = httplib_get_context(conn);
|
||||
struct tclient_data *pclient_data =
|
||||
(struct tclient_data *)mg_get_user_data(ctx);
|
||||
(struct tclient_data *)httplib_get_user_data(ctx);
|
||||
|
||||
printf("Client received data from server: ");
|
||||
fwrite(data, 1, data_len, stdout);
|
||||
@@ -204,12 +200,12 @@ websocket_client_data_handler(struct mg_connection *conn,
|
||||
}
|
||||
|
||||
static void
|
||||
websocket_client_close_handler(const struct mg_connection *conn,
|
||||
websocket_client_close_handler(const struct httplib_connection *conn,
|
||||
void *user_data)
|
||||
{
|
||||
struct mg_context *ctx = mg_get_context(conn);
|
||||
struct httplib_context *ctx = httplib_get_context(conn);
|
||||
struct tclient_data *pclient_data =
|
||||
(struct tclient_data *)mg_get_user_data(ctx);
|
||||
(struct tclient_data *)httplib_get_user_data(ctx);
|
||||
|
||||
printf("Client: Close handler\n");
|
||||
pclient_data->closed++;
|
||||
@@ -219,13 +215,13 @@ websocket_client_close_handler(const struct mg_connection *conn,
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct mg_context *ctx = NULL;
|
||||
struct httplib_context *ctx = NULL;
|
||||
struct tclient_data client1_data = {NULL, 0, 0};
|
||||
struct tclient_data client2_data = {NULL, 0, 0};
|
||||
struct tclient_data client3_data = {NULL, 0, 0};
|
||||
struct mg_connection *newconn1 = NULL;
|
||||
struct mg_connection *newconn2 = NULL;
|
||||
struct mg_connection *newconn3 = NULL;
|
||||
struct httplib_connection *newconn1 = NULL;
|
||||
struct httplib_connection *newconn2 = NULL;
|
||||
struct httplib_connection *newconn3 = NULL;
|
||||
char ebuf[100] = {0};
|
||||
|
||||
assert(websocket_welcome_msg_len == strlen(websocket_welcome_msg));
|
||||
@@ -236,7 +232,7 @@ main(int argc, char *argv[])
|
||||
printf("Server init\n\n");
|
||||
|
||||
/* Then connect a first client */
|
||||
newconn1 = mg_connect_websocket_client("localhost",
|
||||
newconn1 = httplib_connect_websocket_client("localhost",
|
||||
atoi(PORT),
|
||||
0,
|
||||
ebuf,
|
||||
@@ -266,7 +262,7 @@ main(int argc, char *argv[])
|
||||
client1_data.data = NULL;
|
||||
client1_data.len = 0;
|
||||
|
||||
mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data1", 5);
|
||||
httplib_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data1", 5);
|
||||
|
||||
sleep(1); /* Should get the acknowledge message */
|
||||
assert(client1_data.closed == 0);
|
||||
@@ -283,7 +279,7 @@ main(int argc, char *argv[])
|
||||
client1_data.len = 0;
|
||||
|
||||
/* Now connect a second client */
|
||||
newconn2 = mg_connect_websocket_client("localhost",
|
||||
newconn2 = httplib_connect_websocket_client("localhost",
|
||||
atoi(PORT),
|
||||
0,
|
||||
ebuf,
|
||||
@@ -313,7 +309,7 @@ main(int argc, char *argv[])
|
||||
client2_data.data = NULL;
|
||||
client2_data.len = 0;
|
||||
|
||||
mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data2", 5);
|
||||
httplib_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data2", 5);
|
||||
|
||||
sleep(1); /* Should get the acknowledge message */
|
||||
assert(client1_data.closed == 0);
|
||||
@@ -329,7 +325,7 @@ main(int argc, char *argv[])
|
||||
client1_data.data = NULL;
|
||||
client1_data.len = 0;
|
||||
|
||||
mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "bye", 3);
|
||||
httplib_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "bye", 3);
|
||||
|
||||
sleep(1); /* Should get the goodbye message */
|
||||
assert(client1_data.closed == 0);
|
||||
@@ -345,7 +341,7 @@ main(int argc, char *argv[])
|
||||
client1_data.data = NULL;
|
||||
client1_data.len = 0;
|
||||
|
||||
mg_close_connection(newconn1);
|
||||
httplib_close_connection(newconn1);
|
||||
|
||||
sleep(1); /* Won't get any message */
|
||||
assert(client1_data.closed == 1);
|
||||
@@ -355,7 +351,7 @@ main(int argc, char *argv[])
|
||||
assert(client2_data.data == NULL);
|
||||
assert(client2_data.len == 0);
|
||||
|
||||
mg_websocket_client_write(newconn2, WEBSOCKET_OPCODE_TEXT, "bye", 3);
|
||||
httplib_websocket_client_write(newconn2, WEBSOCKET_OPCODE_TEXT, "bye", 3);
|
||||
|
||||
sleep(1); /* Should get the goodbye message */
|
||||
assert(client1_data.closed == 1);
|
||||
@@ -371,7 +367,7 @@ main(int argc, char *argv[])
|
||||
client2_data.data = NULL;
|
||||
client2_data.len = 0;
|
||||
|
||||
mg_close_connection(newconn2);
|
||||
httplib_close_connection(newconn2);
|
||||
|
||||
sleep(1); /* Won't get any message */
|
||||
assert(client1_data.closed == 1);
|
||||
@@ -382,7 +378,7 @@ main(int argc, char *argv[])
|
||||
assert(client2_data.len == 0);
|
||||
|
||||
/* Connect client 3 */
|
||||
newconn3 = mg_connect_websocket_client("localhost",
|
||||
newconn3 = httplib_connect_websocket_client("localhost",
|
||||
atoi(PORT),
|
||||
0,
|
||||
ebuf,
|
||||
@@ -410,7 +406,7 @@ main(int argc, char *argv[])
|
||||
client3_data.data = NULL;
|
||||
client3_data.len = 0;
|
||||
|
||||
mg_stop(ctx);
|
||||
httplib_stop(ctx);
|
||||
printf("Server shutdown\n");
|
||||
|
||||
sleep(10);
|
||||
|
@@ -12,7 +12,7 @@
|
||||
|
||||
// simple structure for keeping track of websocket connection
|
||||
struct ws_connection {
|
||||
struct mg_connection *conn;
|
||||
struct httplib_connection *conn;
|
||||
int update;
|
||||
int closing;
|
||||
};
|
||||
@@ -36,7 +36,7 @@ static struct ws_connection ws_conn[CONNECTIONS];
|
||||
static void *ws_server_thread(void *parm)
|
||||
{
|
||||
int wsd = (long)parm;
|
||||
struct mg_connection *conn = ws_conn[wsd].conn;
|
||||
struct httplib_connection *conn = ws_conn[wsd].conn;
|
||||
int timer = 0;
|
||||
char tstr[32];
|
||||
int i;
|
||||
@@ -61,7 +61,7 @@ static void *ws_server_thread(void *parm)
|
||||
meter[i].value = meter[i].limit;
|
||||
sprintf(tstr, "meter%d:%d,%d", i+1,
|
||||
meter[i].value, meter[i].limit);
|
||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr));
|
||||
httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr));
|
||||
}
|
||||
|
||||
/* While the connection is open, send periodic updates */
|
||||
@@ -83,7 +83,7 @@ static void *ws_server_thread(void *parm)
|
||||
if (!ws_conn[wsd].closing) {
|
||||
sprintf(tstr, "meter%d:%d,%d", i+1,
|
||||
meter[i].value, meter[i].limit);
|
||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr));
|
||||
httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,7 +91,7 @@ static void *ws_server_thread(void *parm)
|
||||
|
||||
/* Send periodic PING to assure websocket remains connected, except if we are closing */
|
||||
if (timer%100 == 0 && !ws_conn[wsd].closing)
|
||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_PING, NULL, 0);
|
||||
httplib_websocket_write(conn, WEBSOCKET_OPCODE_PING, NULL, 0);
|
||||
}
|
||||
|
||||
fprintf(stderr, "ws_server_thread %d exiting\n", wsd);
|
||||
@@ -108,7 +108,7 @@ static void *ws_server_thread(void *parm)
|
||||
// On new client connection, find next available server connection and store
|
||||
// new connection information. If no more server connections are available
|
||||
// tell civetweb to not accept the client request.
|
||||
static int websocket_connect_handler(const struct mg_connection *conn)
|
||||
static int websocket_connect_handler(const struct httplib_connection *conn)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -117,7 +117,7 @@ static int websocket_connect_handler(const struct mg_connection *conn)
|
||||
for(i=0; i < CONNECTIONS; ++i) {
|
||||
if (ws_conn[i].conn == NULL) {
|
||||
fprintf(stderr, "...prep for server %d\n", i);
|
||||
ws_conn[i].conn = (struct mg_connection *)conn;
|
||||
ws_conn[i].conn = (struct httplib_connection *)conn;
|
||||
ws_conn[i].closing = 0;
|
||||
ws_conn[i].update = 0;
|
||||
break;
|
||||
@@ -133,7 +133,7 @@ static int websocket_connect_handler(const struct mg_connection *conn)
|
||||
|
||||
// websocket_ready_handler()
|
||||
// Once websocket negotiation is complete, start a server for the connection
|
||||
static void websocket_ready_handler(struct mg_connection *conn)
|
||||
static void websocket_ready_handler(struct httplib_connection *conn)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -142,7 +142,7 @@ static void websocket_ready_handler(struct mg_connection *conn)
|
||||
for(i=0; i < CONNECTIONS; ++i) {
|
||||
if (ws_conn[i].conn == conn) {
|
||||
fprintf(stderr, "...start server %d\n", i);
|
||||
mg_start_thread(ws_server_thread, (void *)(long)i);
|
||||
httplib_start_thread(ws_server_thread, (void *)(long)i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -150,7 +150,7 @@ static void websocket_ready_handler(struct mg_connection *conn)
|
||||
|
||||
// websocket_close_handler()
|
||||
// When websocket is closed, tell the associated server to shut down
|
||||
static void websocket_close_handler(struct mg_connection *conn)
|
||||
static void websocket_close_handler(struct httplib_connection *conn)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -168,8 +168,7 @@ static void websocket_close_handler(struct mg_connection *conn)
|
||||
// flags: first byte of websocket frame, see websocket RFC,
|
||||
// http://tools.ietf.org/html/rfc6455, section 5.2
|
||||
// data, data_len: payload data. Mask, if any, is already applied.
|
||||
static int websocket_data_handler(struct mg_connection *conn, int flags,
|
||||
char *data, size_t data_len)
|
||||
static int websocket_data_handler(struct httplib_connection *conn, int flags, char *data, size_t data_len)
|
||||
{
|
||||
int i;
|
||||
int wsd;
|
||||
@@ -198,12 +197,12 @@ static int websocket_data_handler(struct mg_connection *conn, int flags,
|
||||
/* turn on updates */
|
||||
ws_conn[wsd].update = 1;
|
||||
/* echo back */
|
||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
|
||||
httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
|
||||
} else if (strncmp("update off", data, data_len)== 0) {
|
||||
/* turn off updates */
|
||||
ws_conn[wsd].update = 0;
|
||||
/* echo back */
|
||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
|
||||
httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
|
||||
}
|
||||
break;
|
||||
case WEBSOCKET_OPCODE_BINARY:
|
||||
@@ -213,14 +212,14 @@ static int websocket_data_handler(struct mg_connection *conn, int flags,
|
||||
fprintf(stderr, "CLOSE...\n");
|
||||
/* If client initiated close, respond with close message in acknowlegement */
|
||||
if (!ws_conn[wsd].closing) {
|
||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_CONNECTION_CLOSE, data, data_len);
|
||||
httplib_websocket_write(conn, WEBSOCKET_OPCODE_CONNECTION_CLOSE, data, data_len);
|
||||
ws_conn[wsd].closing = 1; /* we should not send addional messages when close requested/acknowledged */
|
||||
}
|
||||
return 0; /* time to close the connection */
|
||||
break;
|
||||
case WEBSOCKET_OPCODE_PING:
|
||||
/* client sent PING, respond with PONG */
|
||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_PONG, data, data_len);
|
||||
httplib_websocket_write(conn, WEBSOCKET_OPCODE_PONG, data, data_len);
|
||||
break;
|
||||
case WEBSOCKET_OPCODE_PONG:
|
||||
/* received PONG to our PING, no action */
|
||||
@@ -238,8 +237,8 @@ static int websocket_data_handler(struct mg_connection *conn, int flags,
|
||||
int main(void)
|
||||
{
|
||||
char server_name[40];
|
||||
struct mg_context *ctx;
|
||||
struct mg_callbacks callbacks;
|
||||
struct httplib_context *ctx;
|
||||
struct httplib_callbacks callbacks;
|
||||
const char *options[] = {
|
||||
"listening_ports", "8080",
|
||||
"document_root", "docroot",
|
||||
@@ -247,9 +246,7 @@ int main(void)
|
||||
};
|
||||
|
||||
/* get simple greeting for the web server */
|
||||
snprintf(server_name, sizeof(server_name),
|
||||
"Civetweb websocket server v. %s",
|
||||
mg_version());
|
||||
snprintf(server_name, sizeof(server_name), "Civetweb websocket server v. %s", httplib_version());
|
||||
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.websocket_connect = websocket_connect_handler;
|
||||
@@ -257,15 +254,15 @@ int main(void)
|
||||
callbacks.websocket_data = websocket_data_handler;
|
||||
callbacks.connection_close = websocket_close_handler;
|
||||
|
||||
ctx = mg_start(&callbacks, NULL, options);
|
||||
ctx = httplib_start(&callbacks, NULL, options);
|
||||
|
||||
/* show the greeting and some basic information */
|
||||
printf("%s started on port(s) %s with web root [%s]\n",
|
||||
server_name, mg_get_option(ctx, "listening_ports"),
|
||||
mg_get_option(ctx, "document_root"));
|
||||
server_name, httplib_get_option(ctx, "listening_ports"),
|
||||
httplib_get_option(ctx, "document_root"));
|
||||
|
||||
getchar(); // Wait until user hits "enter"
|
||||
mg_stop(ctx);
|
||||
httplib_stop(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user