protocol_lws_messageboard.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * ws protocol handler plugin for messageboard "generic sessions" demo
  3. *
  4. * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
  5. *
  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.
  10. *
  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.
  15. *
  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,
  19. * MA 02110-1301 USA
  20. */
  21. #define LWS_DLL
  22. #define LWS_INTERNAL
  23. #include "../lib/libwebsockets.h"
  24. #include <sqlite3.h>
  25. #include <string.h>
  26. struct per_vhost_data__gs_mb {
  27. struct lws_vhost *vh;
  28. const struct lws_protocols *gsp;
  29. sqlite3 *pdb;
  30. char message_db[256];
  31. unsigned long last_idx;
  32. };
  33. struct per_session_data__gs_mb {
  34. void *pss_gs; /* for use by generic-sessions */
  35. struct lws_session_info sinfo;
  36. struct lws_spa *spa;
  37. unsigned long last_idx;
  38. unsigned int our_form:1;
  39. };
  40. static const char * const param_names[] = {
  41. "send",
  42. "msg",
  43. };
  44. enum {
  45. MBSPA_SUBMIT,
  46. MBSPA_MSG,
  47. };
  48. #define MAX_MSG_LEN 512
  49. struct message {
  50. unsigned long idx;
  51. unsigned long time;
  52. char username[32];
  53. char email[100];
  54. char ip[72];
  55. char content[MAX_MSG_LEN];
  56. };
  57. static int
  58. lookup_cb(void *priv, int cols, char **col_val, char **col_name)
  59. {
  60. struct message *m = (struct message *)priv;
  61. int n;
  62. for (n = 0; n < cols; n++) {
  63. if (!strcmp(col_name[n], "idx") ||
  64. !strcmp(col_name[n], "MAX(idx)")) {
  65. if (!col_val[n])
  66. m->idx = 0;
  67. else
  68. m->idx = atol(col_val[n]);
  69. continue;
  70. }
  71. if (!strcmp(col_name[n], "time")) {
  72. m->time = atol(col_val[n]);
  73. continue;
  74. }
  75. if (!strcmp(col_name[n], "username")) {
  76. strncpy(m->username, col_val[n], sizeof(m->username) - 1);
  77. m->username[sizeof(m->username) - 1] = '\0';
  78. continue;
  79. }
  80. if (!strcmp(col_name[n], "email")) {
  81. strncpy(m->email, col_val[n], sizeof(m->email) - 1);
  82. m->email[sizeof(m->email) - 1] = '\0';
  83. continue;
  84. }
  85. if (!strcmp(col_name[n], "ip")) {
  86. strncpy(m->ip, col_val[n], sizeof(m->ip) - 1);
  87. m->ip[sizeof(m->ip) - 1] = '\0';
  88. continue;
  89. }
  90. if (!strcmp(col_name[n], "content")) {
  91. strncpy(m->content, col_val[n], sizeof(m->content) - 1);
  92. m->content[sizeof(m->content) - 1] = '\0';
  93. continue;
  94. }
  95. }
  96. return 0;
  97. }
  98. static unsigned long
  99. get_last_idx(struct per_vhost_data__gs_mb *vhd)
  100. {
  101. struct message m;
  102. if (sqlite3_exec(vhd->pdb, "SELECT MAX(idx) FROM msg;",
  103. lookup_cb, &m, NULL) != SQLITE_OK) {
  104. lwsl_err("Unable to lookup token: %s\n",
  105. sqlite3_errmsg(vhd->pdb));
  106. return 0;
  107. }
  108. return m.idx;
  109. }
  110. static int
  111. post_message(struct lws *wsi, struct per_vhost_data__gs_mb *vhd,
  112. struct per_session_data__gs_mb *pss)
  113. {
  114. struct lws_session_info sinfo;
  115. char s[MAX_MSG_LEN + 512];
  116. char esc[MAX_MSG_LEN + 256];
  117. vhd->gsp->callback(wsi, LWS_CALLBACK_SESSION_INFO,
  118. pss->pss_gs, &sinfo, 0);
  119. lws_snprintf((char *)s, sizeof(s) - 1,
  120. "insert into msg(time, username, email, ip, content)"
  121. " values (%lu, '%s', '%s', '%s', '%s');",
  122. (unsigned long)lws_now_secs(), sinfo.username, sinfo.email, sinfo.ip,
  123. lws_sql_purify(esc, lws_spa_get_string(pss->spa, MBSPA_MSG),
  124. sizeof(esc) - 1));
  125. if (sqlite3_exec(vhd->pdb, s, NULL, NULL, NULL) != SQLITE_OK) {
  126. lwsl_err("Unable to insert msg: %s\n", sqlite3_errmsg(vhd->pdb));
  127. return 1;
  128. }
  129. vhd->last_idx = get_last_idx(vhd);
  130. /* let everybody connected by this protocol on this vhost know */
  131. lws_callback_on_writable_all_protocol_vhost(lws_get_vhost(wsi),
  132. lws_get_protocol(wsi));
  133. return 0;
  134. }
  135. static int
  136. callback_messageboard(struct lws *wsi, enum lws_callback_reasons reason,
  137. void *user, void *in, size_t len)
  138. {
  139. struct per_session_data__gs_mb *pss = (struct per_session_data__gs_mb *)user;
  140. const struct lws_protocol_vhost_options *pvo;
  141. struct per_vhost_data__gs_mb *vhd = (struct per_vhost_data__gs_mb *)
  142. lws_protocol_vh_priv_get(lws_get_vhost(wsi), lws_get_protocol(wsi));
  143. unsigned char *p, *start, *end, buffer[LWS_PRE + 256];
  144. char s[512];
  145. int n;
  146. switch (reason) {
  147. case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
  148. vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  149. lws_get_protocol(wsi), sizeof(struct per_vhost_data__gs_mb));
  150. if (!vhd)
  151. return 1;
  152. vhd->vh = lws_get_vhost(wsi);
  153. vhd->gsp = lws_vhost_name_to_protocol(vhd->vh,
  154. "protocol-generic-sessions");
  155. if (!vhd->gsp) {
  156. lwsl_err("messageboard: requires generic-sessions\n");
  157. return 1;
  158. }
  159. pvo = (const struct lws_protocol_vhost_options *)in;
  160. while (pvo) {
  161. if (!strcmp(pvo->name, "message-db"))
  162. strncpy(vhd->message_db, pvo->value,
  163. sizeof(vhd->message_db) - 1);
  164. pvo = pvo->next;
  165. }
  166. if (!vhd->message_db[0]) {
  167. lwsl_err("messageboard: \"message-db\" pvo missing\n");
  168. return 1;
  169. }
  170. if (sqlite3_open_v2(vhd->message_db, &vhd->pdb,
  171. SQLITE_OPEN_READWRITE |
  172. SQLITE_OPEN_CREATE, NULL) != SQLITE_OK) {
  173. lwsl_err("Unable to open message db %s: %s\n",
  174. vhd->message_db, sqlite3_errmsg(vhd->pdb));
  175. return 1;
  176. }
  177. if (sqlite3_exec(vhd->pdb, "create table if not exists msg ("
  178. " idx integer primary key, time integer,"
  179. " username varchar(32), email varchar(100),"
  180. " ip varchar(80), content blob);",
  181. NULL, NULL, NULL) != SQLITE_OK) {
  182. lwsl_err("Unable to create msg table: %s\n",
  183. sqlite3_errmsg(vhd->pdb));
  184. return 1;
  185. }
  186. vhd->last_idx = get_last_idx(vhd);
  187. break;
  188. case LWS_CALLBACK_PROTOCOL_DESTROY:
  189. if (vhd->pdb)
  190. sqlite3_close(vhd->pdb);
  191. goto passthru;
  192. case LWS_CALLBACK_ESTABLISHED:
  193. vhd->gsp->callback(wsi, LWS_CALLBACK_SESSION_INFO,
  194. pss->pss_gs, &pss->sinfo, 0);
  195. if (!pss->sinfo.username[0]) {
  196. lwsl_notice("messageboard ws attempt with no session\n");
  197. return -1;
  198. }
  199. lws_callback_on_writable(wsi);
  200. break;
  201. case LWS_CALLBACK_SERVER_WRITEABLE:
  202. {
  203. struct message m;
  204. char j[MAX_MSG_LEN + 512], e[MAX_MSG_LEN + 512],
  205. *p = j + LWS_PRE, *start = p,
  206. *end = j + sizeof(j) - LWS_PRE;
  207. if (pss->last_idx == vhd->last_idx)
  208. break;
  209. /* restrict to last 10 */
  210. if (!pss->last_idx)
  211. if (vhd->last_idx >= 10)
  212. pss->last_idx = vhd->last_idx - 10;
  213. sprintf(s, "select idx, time, username, email, ip, content "
  214. "from msg where idx > %lu order by idx limit 1;",
  215. pss->last_idx);
  216. if (sqlite3_exec(vhd->pdb, s, lookup_cb, &m, NULL) != SQLITE_OK) {
  217. lwsl_err("Unable to lookup msg: %s\n",
  218. sqlite3_errmsg(vhd->pdb));
  219. return 0;
  220. }
  221. /* format in JSON */
  222. p += lws_snprintf(p, end - p,
  223. "{\"idx\":\"%lu\",\"time\":\"%lu\",",
  224. m.idx, m.time);
  225. p += lws_snprintf(p, end - p, " \"username\":\"%s\",",
  226. lws_json_purify(e, m.username, sizeof(e)));
  227. p += lws_snprintf(p, end - p, " \"email\":\"%s\",",
  228. lws_json_purify(e, m.email, sizeof(e)));
  229. p += lws_snprintf(p, end - p, " \"ip\":\"%s\",",
  230. lws_json_purify(e, m.ip, sizeof(e)));
  231. p += lws_snprintf(p, end - p, " \"content\":\"%s\"}",
  232. lws_json_purify(e, m.content, sizeof(e)));
  233. if (lws_write(wsi, (unsigned char *)start, p - start,
  234. LWS_WRITE_TEXT) < 0)
  235. return -1;
  236. pss->last_idx = m.idx;
  237. if (pss->last_idx == vhd->last_idx)
  238. break;
  239. lws_callback_on_writable(wsi); /* more to do */
  240. }
  241. break;
  242. case LWS_CALLBACK_HTTP:
  243. pss->our_form = 0;
  244. /* ie, it's our messageboard new message form */
  245. if (!strcmp((const char *)in, "/msg")) {
  246. pss->our_form = 1;
  247. break;
  248. }
  249. goto passthru;
  250. case LWS_CALLBACK_HTTP_BODY:
  251. if (!pss->our_form)
  252. goto passthru;
  253. if (len < 2)
  254. break;
  255. if (!pss->spa) {
  256. pss->spa = lws_spa_create(wsi, param_names,
  257. ARRAY_SIZE(param_names),
  258. MAX_MSG_LEN + 1024, NULL, NULL);
  259. if (!pss->spa)
  260. return -1;
  261. }
  262. if (lws_spa_process(pss->spa, in, len)) {
  263. lwsl_notice("spa process blew\n");
  264. return -1;
  265. }
  266. break;
  267. case LWS_CALLBACK_HTTP_BODY_COMPLETION:
  268. if (!pss->our_form)
  269. goto passthru;
  270. if (post_message(wsi, vhd, pss))
  271. return -1;
  272. p = buffer + LWS_PRE;
  273. start = p;
  274. end = p + sizeof(buffer) - LWS_PRE;
  275. if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
  276. return -1;
  277. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
  278. (unsigned char *)"text/plain", 10, &p, end))
  279. return -1;
  280. if (lws_add_http_header_content_length(wsi, 1, &p, end))
  281. return -1;
  282. if (lws_finalize_http_header(wsi, &p, end))
  283. return -1;
  284. n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
  285. if (n != (p - start)) {
  286. lwsl_err("_write returned %d from %ld\n", n, (long)(p - start));
  287. return -1;
  288. }
  289. s[0] = '0';
  290. n = lws_write(wsi, (unsigned char *)s, 1, LWS_WRITE_HTTP);
  291. if (n != 1)
  292. return -1;
  293. goto try_to_reuse;
  294. case LWS_CALLBACK_HTTP_BIND_PROTOCOL:
  295. if (!pss || pss->pss_gs)
  296. break;
  297. pss->pss_gs = malloc(vhd->gsp->per_session_data_size);
  298. if (!pss->pss_gs)
  299. return -1;
  300. memset(pss->pss_gs, 0, vhd->gsp->per_session_data_size);
  301. break;
  302. case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
  303. if (vhd->gsp->callback(wsi, reason, pss ? pss->pss_gs : NULL, in, len))
  304. return -1;
  305. if (pss && pss->spa) {
  306. lws_spa_destroy(pss->spa);
  307. pss->spa = NULL;
  308. }
  309. if (pss && pss->pss_gs) {
  310. free(pss->pss_gs);
  311. pss->pss_gs = NULL;
  312. }
  313. break;
  314. default:
  315. passthru:
  316. return vhd->gsp->callback(wsi, reason, pss ? pss->pss_gs : NULL, in, len);
  317. }
  318. return 0;
  319. try_to_reuse:
  320. if (lws_http_transaction_completed(wsi))
  321. return -1;
  322. return 0;
  323. }
  324. static const struct lws_protocols protocols[] = {
  325. {
  326. "protocol-lws-messageboard",
  327. callback_messageboard,
  328. sizeof(struct per_session_data__gs_mb),
  329. 4096,
  330. },
  331. };
  332. LWS_EXTERN LWS_VISIBLE int
  333. init_protocol_lws_messageboard(struct lws_context *context,
  334. struct lws_plugin_capability *c)
  335. {
  336. if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
  337. lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
  338. c->api_magic);
  339. return 1;
  340. }
  341. c->protocols = protocols;
  342. c->count_protocols = ARRAY_SIZE(protocols);
  343. c->extensions = NULL;
  344. c->count_extensions = 0;
  345. return 0;
  346. }
  347. LWS_EXTERN LWS_VISIBLE int
  348. destroy_protocol_lws_messageboard(struct lws_context *context)
  349. {
  350. return 0;
  351. }