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 struct per_session_data__post_demo {
29 char result[LWS_PRE + 500];
37 static const char * const param_names[] = {
44 enum enum_param_names {
52 file_upload_cb(void *data, const char *name, const char *filename,
53 char *buf, int len, enum lws_spa_fileupload_states state)
55 struct per_session_data__post_demo *pss =
56 (struct per_session_data__post_demo *)data;
61 strncpy(pss->filename, filename, sizeof(pss->filename) - 1);
62 /* we get the original filename in @filename arg, but for
63 * simple demo use a fixed name so we don't have to deal with
65 pss->fd = open("/tmp/post-file",
66 O_CREAT | O_TRUNC | O_RDWR, 0600);
68 case LWS_UFS_FINAL_CONTENT:
71 pss->file_length += len;
73 /* if the file length is too big, drop it */
74 if (pss->file_length > 100000)
77 n = write(pss->fd, buf, len);
78 lwsl_notice("%s: write %d says %d\n", __func__, len, n);
80 if (state == LWS_UFS_CONTENT)
83 pss->fd = LWS_INVALID_FILE;
91 callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
92 void *user, void *in, size_t len)
94 struct per_session_data__post_demo *pss =
95 (struct per_session_data__post_demo *)user;
96 unsigned char buffer[LWS_PRE + 512];
97 unsigned char *p, *start, *end;
101 case LWS_CALLBACK_HTTP_BODY:
102 /* create the POST argument parser if not already existing */
104 pss->spa = lws_spa_create(wsi, param_names,
105 ARRAY_SIZE(param_names), 1024,
106 file_upload_cb, pss);
110 pss->filename[0] = '\0';
111 pss->file_length = 0;
114 /* let it parse the POST data */
115 if (lws_spa_process(pss->spa, in, len))
119 case LWS_CALLBACK_HTTP_BODY_COMPLETION:
120 lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
121 /* call to inform no more payload data coming */
122 lws_spa_finalize(pss->spa);
124 p = (unsigned char *)pss->result + LWS_PRE;
125 end = p + sizeof(pss->result) - LWS_PRE - 1;
126 p += sprintf((char *)p,
127 "<html><body><h1>Form results (after urldecoding)</h1>"
128 "<table><tr><td>Name</td><td>Length</td><td>Value</td></tr>");
130 for (n = 0; n < ARRAY_SIZE(param_names); n++)
131 p += snprintf((char *)p, end - p,
132 "<tr><td><b>%s</b></td><td>%d</td><td>%s</td></tr>",
134 lws_spa_get_length(pss->spa, n),
135 lws_spa_get_string(pss->spa, n));
137 p += snprintf((char *)p, end - p, "</table><br><b>filename:</b> %s, <b>length</b> %ld",
138 pss->filename, pss->file_length);
140 p += snprintf((char *)p, end - p, "</body></html>");
141 pss->result_len = p - (unsigned char *)(pss->result + LWS_PRE);
143 p = buffer + LWS_PRE;
145 end = p + sizeof(buffer) - LWS_PRE;
147 if (lws_add_http_header_status(wsi, 200, &p, end))
150 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
151 (unsigned char *)"text/html", 9, &p, end))
153 if (lws_add_http_header_content_length(wsi, pss->result_len, &p, end))
155 if (lws_finalize_http_header(wsi, &p, end))
158 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
162 lws_callback_on_writable(wsi);
165 case LWS_CALLBACK_HTTP_WRITEABLE:
166 lwsl_debug("LWS_CALLBACK_HTTP_WRITEABLE: sending %d\n",
168 n = lws_write(wsi, (unsigned char *)pss->result + LWS_PRE,
169 pss->result_len, LWS_WRITE_HTTP);
174 case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
175 /* called when our wsi user_space is going to be destroyed */
177 lws_spa_destroy(pss->spa);
189 if (lws_http_transaction_completed(wsi))
195 static const struct lws_protocols protocols[] = {
197 "protocol-post-demo",
199 sizeof(struct per_session_data__post_demo),
204 LWS_EXTERN LWS_VISIBLE int
205 init_protocol_post_demo(struct lws_context *context,
206 struct lws_plugin_capability *c)
208 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
209 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
214 c->protocols = protocols;
215 c->count_protocols = ARRAY_SIZE(protocols);
216 c->extensions = NULL;
217 c->count_extensions = 0;
222 LWS_EXTERN LWS_VISIBLE int
223 destroy_protocol_post_demo(struct lws_context *context)