mirror of
https://github.com/lammertb/libhttp.git
synced 2025-08-06 05:02:40 +03:00
Renamed mg_ to httplib_
This commit is contained in:
@@ -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");
|
||||
|
||||
|
Reference in New Issue
Block a user