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
23 #include "../lib/libwebsockets.h"
27 #include <sys/types.h>
34 struct per_session_data__post_demo {
36 char result[LWS_PRE + 500];
44 static const char * const param_names[] = {
51 enum enum_param_names {
59 file_upload_cb(void *data, const char *name, const char *filename,
60 char *buf, int len, enum lws_spa_fileupload_states state)
62 struct per_session_data__post_demo *pss =
63 (struct per_session_data__post_demo *)data;
68 strncpy(pss->filename, filename, sizeof(pss->filename) - 1);
69 /* we get the original filename in @filename arg, but for
70 * simple demo use a fixed name so we don't have to deal with
72 pss->fd = open("/tmp/post-file",
73 O_CREAT | O_TRUNC | O_RDWR, 0600);
75 case LWS_UFS_FINAL_CONTENT:
78 pss->file_length += len;
80 /* if the file length is too big, drop it */
81 if (pss->file_length > 100000)
84 n = write(pss->fd, buf, len);
85 lwsl_notice("%s: write %d says %d\n", __func__, len, n);
87 if (state == LWS_UFS_CONTENT)
90 pss->fd = LWS_INVALID_FILE;
98 callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
99 void *user, void *in, size_t len)
101 struct per_session_data__post_demo *pss =
102 (struct per_session_data__post_demo *)user;
103 unsigned char buffer[LWS_PRE + 512];
104 unsigned char *p, *start, *end;
108 case LWS_CALLBACK_HTTP_BODY:
109 /* create the POST argument parser if not already existing */
111 pss->spa = lws_spa_create(wsi, param_names,
112 ARRAY_SIZE(param_names), 1024,
113 file_upload_cb, pss);
117 pss->filename[0] = '\0';
118 pss->file_length = 0;
121 /* let it parse the POST data */
122 if (lws_spa_process(pss->spa, in, len))
126 case LWS_CALLBACK_HTTP_BODY_COMPLETION:
127 lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
128 /* call to inform no more payload data coming */
129 lws_spa_finalize(pss->spa);
131 p = (unsigned char *)pss->result + LWS_PRE;
132 end = p + sizeof(pss->result) - LWS_PRE - 1;
133 p += sprintf((char *)p,
134 "<html><body><h1>Form results (after urldecoding)</h1>"
135 "<table><tr><td>Name</td><td>Length</td><td>Value</td></tr>");
137 for (n = 0; n < ARRAY_SIZE(param_names); n++)
138 p += snprintf((char *)p, end - p,
139 "<tr><td><b>%s</b></td><td>%d</td><td>%s</td></tr>",
141 lws_spa_get_length(pss->spa, n),
142 lws_spa_get_string(pss->spa, n));
144 p += snprintf((char *)p, end - p, "</table><br><b>filename:</b> %s, <b>length</b> %ld",
145 pss->filename, pss->file_length);
147 p += snprintf((char *)p, end - p, "</body></html>");
148 pss->result_len = p - (unsigned char *)(pss->result + LWS_PRE);
150 p = buffer + LWS_PRE;
152 end = p + sizeof(buffer) - LWS_PRE;
154 if (lws_add_http_header_status(wsi, 200, &p, end))
157 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
158 (unsigned char *)"text/html", 9, &p, end))
160 if (lws_add_http_header_content_length(wsi, pss->result_len, &p, end))
162 if (lws_finalize_http_header(wsi, &p, end))
165 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
169 lws_callback_on_writable(wsi);
172 case LWS_CALLBACK_HTTP_WRITEABLE:
173 lwsl_debug("LWS_CALLBACK_HTTP_WRITEABLE: sending %d\n",
175 n = lws_write(wsi, (unsigned char *)pss->result + LWS_PRE,
176 pss->result_len, LWS_WRITE_HTTP);
181 case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
182 /* called when our wsi user_space is going to be destroyed */
184 lws_spa_destroy(pss->spa);
196 if (lws_http_transaction_completed(wsi))
202 static const struct lws_protocols protocols[] = {
204 "protocol-post-demo",
206 sizeof(struct per_session_data__post_demo),
211 LWS_EXTERN LWS_VISIBLE int
212 init_protocol_post_demo(struct lws_context *context,
213 struct lws_plugin_capability *c)
215 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
216 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
221 c->protocols = protocols;
222 c->count_protocols = ARRAY_SIZE(protocols);
223 c->extensions = NULL;
224 c->count_extensions = 0;
229 LWS_EXTERN LWS_VISIBLE int
230 destroy_protocol_post_demo(struct lws_context *context)