Skip to content
Snippets Groups Projects
Commit 7cace648 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot
Browse files

Improve cookie auth example

Use templated index page and SSI calls instead of hard-coded html

PUBLISHED_FROM=eade9dfe283952ed5465cd0ff1924f61a14dea0c
parent 447bf4ad
No related branches found
No related tags found
No related merge requests found
...@@ -197,37 +197,48 @@ void check_sessions() { ...@@ -197,37 +197,48 @@ void check_sessions() {
/* Main event handler. */ /* Main event handler. */
static void ev_handler(struct mg_connection *nc, int ev, void *p) { static void ev_handler(struct mg_connection *nc, int ev, void *p) {
/* Perform session maintenance. */ switch (ev) {
if (ev == MG_EV_TIMER) { case MG_EV_HTTP_REQUEST: {
check_sessions(); struct http_message *hm = (struct http_message *) p;
mg_set_timer(nc, mg_time() + SESSION_CHECK_INTERVAL); struct session *s = get_session(hm);
return; /* Ask the user to log in if they did not present a valid cookie. */
} if (s == NULL) {
if (ev != MG_EV_HTTP_REQUEST) return; mg_printf(nc,
"HTTP/1.0 302 Found\r\n"
nc->flags |= MG_F_SEND_AND_CLOSE; "Location: /login.html\r\n"
struct http_message *hm = (struct http_message *) p; "\r\n"
struct session *s = get_session(hm); "Please log in");
/* Ask the user to log in if they did not present a valid cookie. */ nc->flags |= MG_F_SEND_AND_CLOSE;
if (s == NULL) { break;
mg_printf(nc, }
"HTTP/1.0 302 Found\r\n" /*
"Location: /login.html\r\n" * Serve the page that was requested.
"\r\n" * Save session in user_data for use by SSI calls.
"Please log in"); */
return; fprintf(stderr, "%s (sid %" INT64_X_FMT ") requested %.*s\n", s->user,
s->id, (int) hm->uri.len, hm->uri.p);
nc->user_data = s;
mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
break;
}
case MG_EV_SSI_CALL: {
/* Expand variables in a page by using session data. */
const char *var = (const char *) p;
const struct session *s = (const struct session *) nc->user_data;
if (strcmp(var, "user") == 0) {
mg_printf_html_escape(nc, "%s", s->user);
} else if (strcmp(var, "lucky_number") == 0) {
mg_printf_html_escape(nc, "%d", s->lucky_number);
}
break;
}
case MG_EV_TIMER: {
/* Perform session maintenance. */
check_sessions();
mg_set_timer(nc, mg_time() + SESSION_CHECK_INTERVAL);
break;
}
} }
/* Application logic that uses session data goes here. */
fprintf(stderr, "%s (sid %" INT64_X_FMT ") requested %.*s\n", s->user, s->id,
(int) hm->uri.len, hm->uri.p);
mg_printf(nc,
"HTTP/1.0 200 Ok\r\n"
"COntent-Type: text/html\r\n"
"\r\n"
"<h1>Hello, %s!</h1>\r\n"
"<p>Your lucky number is %d.</p>\r\n"
"<p><a href=/logout>Log out</a>",
s->user, s->lucky_number);
} }
int main(void) { int main(void) {
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Main - Mongoose cookie_auth</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
* { outline: none; }
body {
background-color: #789; margin: 0;
padding: 0; font: 16px/1.4 Helvetica, Arial, sans-serif;
font: 16px/1.4 Helvetica, Arial, sans-serif;
}
div.content {
width: 600px; margin: 2em auto; padding: 20px 50px;
background-color: #fff; border-radius: 1em;
}
label { display: inline-block; min-width: 7em; }
input { border: 1px solid #ccc; padding: 0.2em; }
a:link, a:visited { color: #69c; text-decoration: none; }
@media (max-width: 700px) {
body { background-color: #fff; }
div.content { width: auto; margin: 0 auto; padding: 1em; }
}
</style>
</head>
<body>
<div class="content">
<h1>Hello, <!--#call user -->!</h1>
<p>Your lucky number is <!--#call lucky_number -->.</p>
<p><a href="/logout">Log out</a>
</div>
</body>
</html>
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>Please log in</title> <title>Log in - Mongoose cookie_auth</title>
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css"> <style type="text/css">
* { outline: none; } * { outline: none; }
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
font: 16px/1.4 Helvetica, Arial, sans-serif; font: 16px/1.4 Helvetica, Arial, sans-serif;
} }
div.content { div.content {
width: 800px; margin: 2em auto; padding: 20px 50px; width: 600px; margin: 2em auto; padding: 20px 50px;
background-color: #fff; border-radius: 1em; background-color: #fff; border-radius: 1em;
} }
label { display: inline-block; min-width: 7em; } label { display: inline-block; min-width: 7em; }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment