2 * ws protocol handler plugin for messageboard "generic sessions" demo
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 #include "../lib/libwebsockets.h"
29 struct per_vhost_data__gs_mb {
31 const struct lws_protocols *gsp;
34 unsigned long last_idx;
37 struct per_session_data__gs_mb {
38 void *pss_gs; /* for use by generic-sessions */
39 struct lws_session_info sinfo;
41 unsigned long last_idx;
42 unsigned int our_form:1;
45 static const char * const param_names[] = {
54 #define MAX_MSG_LEN 512
62 char content[MAX_MSG_LEN];
66 lookup_cb(void *priv, int cols, char **col_val, char **col_name)
68 struct message *m = (struct message *)priv;
71 for (n = 0; n < cols; n++) {
73 if (!strcmp(col_name[n], "idx") ||
74 !strcmp(col_name[n], "MAX(idx)")) {
78 m->idx = atol(col_val[n]);
81 if (!strcmp(col_name[n], "time")) {
82 m->time = atol(col_val[n]);
85 if (!strcmp(col_name[n], "username")) {
86 strncpy(m->username, col_val[n], sizeof(m->username) - 1);
87 m->username[sizeof(m->username) - 1] = '\0';
90 if (!strcmp(col_name[n], "email")) {
91 strncpy(m->email, col_val[n], sizeof(m->email) - 1);
92 m->email[sizeof(m->email) - 1] = '\0';
95 if (!strcmp(col_name[n], "ip")) {
96 strncpy(m->ip, col_val[n], sizeof(m->ip) - 1);
97 m->ip[sizeof(m->ip) - 1] = '\0';
100 if (!strcmp(col_name[n], "content")) {
101 strncpy(m->content, col_val[n], sizeof(m->content) - 1);
102 m->content[sizeof(m->content) - 1] = '\0';
110 get_last_idx(struct per_vhost_data__gs_mb *vhd)
114 if (sqlite3_exec(vhd->pdb, "SELECT MAX(idx) FROM msg;",
115 lookup_cb, &m, NULL) != SQLITE_OK) {
116 lwsl_err("Unable to lookup token: %s\n",
117 sqlite3_errmsg(vhd->pdb));
125 post_message(struct lws *wsi, struct per_vhost_data__gs_mb *vhd,
126 struct per_session_data__gs_mb *pss)
128 struct lws_session_info sinfo;
129 char s[MAX_MSG_LEN + 512];
130 char esc[MAX_MSG_LEN + 256];
132 vhd->gsp->callback(wsi, LWS_CALLBACK_SESSION_INFO,
133 pss->pss_gs, &sinfo, 0);
135 lws_snprintf((char *)s, sizeof(s) - 1,
136 "insert into msg(time, username, email, ip, content)"
137 " values (%lu, '%s', '%s', '%s', '%s');",
138 (unsigned long)lws_now_secs(), sinfo.username, sinfo.email, sinfo.ip,
139 lws_sql_purify(esc, lws_spa_get_string(pss->spa, MBSPA_MSG),
141 if (sqlite3_exec(vhd->pdb, s, NULL, NULL, NULL) != SQLITE_OK) {
142 lwsl_err("Unable to insert msg: %s\n", sqlite3_errmsg(vhd->pdb));
145 vhd->last_idx = get_last_idx(vhd);
147 /* let everybody connected by this protocol on this vhost know */
148 lws_callback_on_writable_all_protocol_vhost(lws_get_vhost(wsi),
149 lws_get_protocol(wsi));
155 callback_messageboard(struct lws *wsi, enum lws_callback_reasons reason,
156 void *user, void *in, size_t len)
158 struct per_session_data__gs_mb *pss = (struct per_session_data__gs_mb *)user;
159 const struct lws_protocol_vhost_options *pvo;
160 struct per_vhost_data__gs_mb *vhd = (struct per_vhost_data__gs_mb *)
161 lws_protocol_vh_priv_get(lws_get_vhost(wsi), lws_get_protocol(wsi));
162 unsigned char *p, *start, *end, buffer[LWS_PRE + 256];
167 case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
168 vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
169 lws_get_protocol(wsi), sizeof(struct per_vhost_data__gs_mb));
172 vhd->vh = lws_get_vhost(wsi);
173 vhd->gsp = lws_vhost_name_to_protocol(vhd->vh,
174 "protocol-generic-sessions");
176 lwsl_err("messageboard: requires generic-sessions\n");
180 pvo = (const struct lws_protocol_vhost_options *)in;
182 if (!strcmp(pvo->name, "message-db"))
183 strncpy(vhd->message_db, pvo->value,
184 sizeof(vhd->message_db) - 1);
187 if (!vhd->message_db[0]) {
188 lwsl_err("messageboard: \"message-db\" pvo missing\n");
192 if (sqlite3_open_v2(vhd->message_db, &vhd->pdb,
193 SQLITE_OPEN_READWRITE |
194 SQLITE_OPEN_CREATE, NULL) != SQLITE_OK) {
195 lwsl_err("Unable to open message db %s: %s\n",
196 vhd->message_db, sqlite3_errmsg(vhd->pdb));
200 if (sqlite3_exec(vhd->pdb, "create table if not exists msg ("
201 " idx integer primary key, time integer,"
202 " username varchar(32), email varchar(100),"
203 " ip varchar(80), content blob);",
204 NULL, NULL, NULL) != SQLITE_OK) {
205 lwsl_err("Unable to create msg table: %s\n",
206 sqlite3_errmsg(vhd->pdb));
211 vhd->last_idx = get_last_idx(vhd);
214 case LWS_CALLBACK_PROTOCOL_DESTROY:
216 sqlite3_close(vhd->pdb);
219 case LWS_CALLBACK_ESTABLISHED:
220 vhd->gsp->callback(wsi, LWS_CALLBACK_SESSION_INFO,
221 pss->pss_gs, &pss->sinfo, 0);
222 if (!pss->sinfo.username[0]) {
223 lwsl_notice("messageboard ws attempt with no session\n");
228 lws_callback_on_writable(wsi);
231 case LWS_CALLBACK_SERVER_WRITEABLE:
234 char j[MAX_MSG_LEN + 512], e[MAX_MSG_LEN + 512],
235 *p = j + LWS_PRE, *start = p,
236 *end = j + sizeof(j) - LWS_PRE;
238 if (pss->last_idx == vhd->last_idx)
241 /* restrict to last 10 */
243 if (vhd->last_idx >= 10)
244 pss->last_idx = vhd->last_idx - 10;
246 sprintf(s, "select idx, time, username, email, ip, content "
247 "from msg where idx > %lu order by idx limit 1;",
249 if (sqlite3_exec(vhd->pdb, s, lookup_cb, &m, NULL) != SQLITE_OK) {
250 lwsl_err("Unable to lookup msg: %s\n",
251 sqlite3_errmsg(vhd->pdb));
256 p += lws_snprintf(p, end - p,
257 "{\"idx\":\"%lu\",\"time\":\"%lu\",",
259 p += lws_snprintf(p, end - p, " \"username\":\"%s\",",
260 lws_json_purify(e, m.username, sizeof(e)));
261 p += lws_snprintf(p, end - p, " \"email\":\"%s\",",
262 lws_json_purify(e, m.email, sizeof(e)));
263 p += lws_snprintf(p, end - p, " \"ip\":\"%s\",",
264 lws_json_purify(e, m.ip, sizeof(e)));
265 p += lws_snprintf(p, end - p, " \"content\":\"%s\"}",
266 lws_json_purify(e, m.content, sizeof(e)));
268 if (lws_write(wsi, (unsigned char *)start, p - start,
272 pss->last_idx = m.idx;
273 if (pss->last_idx == vhd->last_idx)
276 lws_callback_on_writable(wsi); /* more to do */
280 case LWS_CALLBACK_HTTP:
283 /* ie, it's our messageboard new message form */
284 if (!strcmp((const char *)in, "/msg")) {
291 case LWS_CALLBACK_HTTP_BODY:
298 pss->spa = lws_spa_create(wsi, param_names,
299 ARRAY_SIZE(param_names),
300 MAX_MSG_LEN + 1024, NULL, NULL);
305 if (lws_spa_process(pss->spa, in, len)) {
306 lwsl_notice("spa process blew\n");
311 case LWS_CALLBACK_HTTP_BODY_COMPLETION:
315 if (post_message(wsi, vhd, pss))
318 p = buffer + LWS_PRE;
320 end = p + sizeof(buffer) - LWS_PRE;
322 if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
324 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
325 (unsigned char *)"text/plain", 10, &p, end))
327 if (lws_add_http_header_content_length(wsi, 1, &p, end))
329 if (lws_finalize_http_header(wsi, &p, end))
331 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
332 if (n != (p - start)) {
333 lwsl_err("_write returned %d from %ld\n", n, (long)(p - start));
337 n = lws_write(wsi, (unsigned char *)s, 1, LWS_WRITE_HTTP);
343 case LWS_CALLBACK_HTTP_BIND_PROTOCOL:
344 if (!pss || pss->pss_gs)
347 pss->pss_gs = malloc(vhd->gsp->per_session_data_size);
351 memset(pss->pss_gs, 0, vhd->gsp->per_session_data_size);
354 case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
355 if (vhd->gsp->callback(wsi, reason, pss ? pss->pss_gs : NULL, in, len))
358 if (pss && pss->spa) {
359 lws_spa_destroy(pss->spa);
362 if (pss && pss->pss_gs) {
370 return vhd->gsp->callback(wsi, reason, pss ? pss->pss_gs : NULL, in, len);
377 if (lws_http_transaction_completed(wsi))
383 static const struct lws_protocols protocols[] = {
385 "protocol-lws-messageboard",
386 callback_messageboard,
387 sizeof(struct per_session_data__gs_mb),
392 LWS_EXTERN LWS_VISIBLE int
393 init_protocol_lws_messageboard(struct lws_context *context,
394 struct lws_plugin_capability *c)
396 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
397 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
402 c->protocols = protocols;
403 c->count_protocols = ARRAY_SIZE(protocols);
404 c->extensions = NULL;
405 c->count_extensions = 0;
410 LWS_EXTERN LWS_VISIBLE int
411 destroy_protocol_lws_messageboard(struct lws_context *context)