2 * ws protocol handler plugin for "POST demo"
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * The person who associated a work with this deed has dedicated
10 * the work to the public domain by waiving all of his or her rights
11 * to the work worldwide under copyright law, including all related
12 * and neighboring rights, to the extent allowed by law. You can copy,
13 * modify, distribute and perform the work, even for commercial purposes,
14 * all without asking permission.
16 * These test plugins are intended to be adapted for use in your code, which
17 * may be proprietary. So unlike the library itself, they are licensed
20 #include "../lib/libwebsockets.h"
23 struct per_session_data__post_demo {
24 char post_string[256];
25 char result[500 + LWS_PRE];
30 callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
31 void *user, void *in, size_t len)
33 struct per_session_data__post_demo *pss =
34 (struct per_session_data__post_demo *)user;
35 unsigned char buffer[LWS_PRE + 512];
36 unsigned char *p, *start, *end;
41 case LWS_CALLBACK_HTTP_BODY:
42 lwsl_debug("LWS_CALLBACK_HTTP_BODY: len %d\n", (int)len);
43 strncpy(pss->post_string, in, sizeof (pss->post_string) -1);
44 pss->post_string[sizeof(pss->post_string) - 1] = '\0';
46 if (len < sizeof(pss->post_string) - 1)
47 pss->post_string[len] = '\0';
50 case LWS_CALLBACK_HTTP_WRITEABLE:
51 lwsl_debug("LWS_CALLBACK_HTTP_WRITEABLE: sending %d\n", pss->result_len);
52 n = lws_write(wsi, (unsigned char *)pss->result + LWS_PRE,
53 pss->result_len, LWS_WRITE_HTTP);
58 case LWS_CALLBACK_HTTP_BODY_COMPLETION:
59 lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
61 * the whole of the sent body arrived,
62 * respond to the client with a redirect to show the
65 pss->result_len = sprintf((char *)pss->result + LWS_PRE,
66 "<html><body><h1>Form results</h1>'%s'<br>"
67 "</body></html>", pss->post_string);
71 end = p + sizeof(buffer) - LWS_PRE;
73 if (lws_add_http_header_status(wsi, 200, &p, end))
76 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
77 (unsigned char *)"text/html", 9, &p, end))
79 if (lws_add_http_header_content_length(wsi, pss->result_len, &p, end))
81 if (lws_finalize_http_header(wsi, &p, end))
84 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
89 * send the payload next time, in case would block after
92 lws_callback_on_writable(wsi);
102 if (lws_http_transaction_completed(wsi))
108 static const struct lws_protocols protocols[] = {
110 "protocol-post-demo",
112 sizeof(struct per_session_data__post_demo),
118 init_protocol_post_demo(struct lws_context *context,
119 struct lws_plugin_capability *c)
121 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
122 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
127 c->protocols = protocols;
128 c->count_protocols = ARRAY_SIZE(protocols);
129 c->extensions = NULL;
130 c->count_extensions = 0;
136 destroy_protocol_post_demo(struct lws_context *context)