1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-07 16:02:55 +03:00

Renamed mg_ to httplib_

This commit is contained in:
Lammert Bies
2016-12-13 17:32:31 +01:00
parent 647139558b
commit d317ce3e82
178 changed files with 2173 additions and 2513 deletions

View File

@@ -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;