Imported Upstream version 3.2.0
[platform/upstream/libwebsockets.git] / plugins / protocol_post_demo.c
index 29145a6..02503c3 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * ws protocol handler plugin for "POST demo"
  *
- * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
+ * Written in 2010-2019 by Andy Green <andy@warmcat.com>
  *
  * This file is made available under the Creative Commons CC0 1.0
  * Universal Public Domain Dedication.
  * may be proprietary.  So unlike the library itself, they are licensed
  * Public Domain.
  */
-#include "../lib/libwebsockets.h"
+
+#if !defined (LWS_PLUGIN_STATIC)
+#define LWS_DLL
+#define LWS_INTERNAL
+#include <libwebsockets.h>
+#endif
+
+#include <stdlib.h>
 #include <string.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifdef WIN32
+#include <io.h>
+#endif
+#include <stdio.h>
 
 struct per_session_data__post_demo {
-       char post_string[256];
-       char result[500 + LWS_PRE];
-       int result_len;
+       struct lws_spa *spa;
+       char result[LWS_PRE + LWS_RECOMMENDED_MIN_HEADER_SPACE];
+       char filename[64];
+       long file_length;
+#if !defined(LWS_WITH_ESP32)
+       lws_filefd_type fd;
+#endif
+       uint8_t completed:1;
+       uint8_t sent_headers:1;
+       uint8_t sent_body:1;
+};
+
+static const char * const param_names[] = {
+       "text",
+       "send",
+       "file",
+       "upload",
 };
 
+enum enum_param_names {
+       EPN_TEXT,
+       EPN_SEND,
+       EPN_FILE,
+       EPN_UPLOAD,
+};
+
+static int
+file_upload_cb(void *data, const char *name, const char *filename,
+              char *buf, int len, enum lws_spa_fileupload_states state)
+{
+       struct per_session_data__post_demo *pss =
+                       (struct per_session_data__post_demo *)data;
+#if !defined(LWS_WITH_ESP32)
+       int n;
+
+       (void)n;
+#endif
+
+       switch (state) {
+       case LWS_UFS_OPEN:
+               lws_strncpy(pss->filename, filename, sizeof(pss->filename));
+               /* we get the original filename in @filename arg, but for
+                * simple demo use a fixed name so we don't have to deal with
+                * attacks  */
+#if !defined(LWS_WITH_ESP32)
+               pss->fd = (lws_filefd_type)(long long)lws_open("/tmp/post-file",
+                              O_CREAT | O_TRUNC | O_RDWR, 0600);
+#endif
+               break;
+       case LWS_UFS_FINAL_CONTENT:
+       case LWS_UFS_CONTENT:
+               if (len) {
+                       pss->file_length += len;
+
+                       /* if the file length is too big, drop it */
+                       if (pss->file_length > 100000)
+                               return 1;
+
+#if !defined(LWS_WITH_ESP32)
+                       n = write((int)(long long)pss->fd, buf, len);
+                       lwsl_info("%s: write %d says %d\n", __func__, len, n);
+#else
+                       lwsl_notice("%s: Received chunk size %d\n", __func__, len);
+#endif
+               }
+               if (state == LWS_UFS_CONTENT)
+                       break;
+#if !defined(LWS_WITH_ESP32)
+               close((int)(long long)pss->fd);
+               pss->fd = LWS_INVALID_FILE;
+#endif
+               break;
+       case LWS_UFS_CLOSE:
+               break;
+       }
+
+       return 0;
+}
+
+/*
+ * returns length in bytes
+ */
+
+static int
+format_result(struct per_session_data__post_demo *pss)
+{
+       unsigned char *p, *start, *end;
+       int n;
+
+       p = (unsigned char *)pss->result + LWS_PRE;
+       start = p;
+       end = p + sizeof(pss->result) - LWS_PRE - 1;
+
+       p += lws_snprintf((char *)p, end -p,
+                       "<!DOCTYPE html><html lang=\"en\"><head>"
+                       "<meta charset=utf-8 http-equiv=\"Content-Language\" "
+                       "content=\"en\"/>"
+         "<title>LWS Server Status</title>"
+         "</head><body><h1>Form results (after urldecoding)</h1>"
+         "<table><tr><td>Name</td><td>Length</td><td>Value</td></tr>");
+
+       for (n = 0; n < (int)LWS_ARRAY_SIZE(param_names); n++) {
+               if (!lws_spa_get_string(pss->spa, n))
+                       p += lws_snprintf((char *)p, end - p,
+                           "<tr><td><b>%s</b></td><td>0"
+                           "</td><td>NULL</td></tr>",
+                           param_names[n]);
+               else
+                       p += lws_snprintf((char *)p, end - p,
+                           "<tr><td><b>%s</b></td><td>%d"
+                           "</td><td>%s</td></tr>",
+                           param_names[n],
+                           lws_spa_get_length(pss->spa, n),
+                           lws_spa_get_string(pss->spa, n));
+       }
+
+       p += lws_snprintf((char *)p, end - p,
+                       "</table><br><b>filename:</b> %s, "
+                       "<b>length</b> %ld",
+                       pss->filename, pss->file_length);
+
+       p += lws_snprintf((char *)p, end - p, "</body></html>");
+
+       return (int)lws_ptr_diff(p, start);
+}
+
 static int
 callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
                   void *user, void *in, size_t len)
 {
        struct per_session_data__post_demo *pss =
                        (struct per_session_data__post_demo *)user;
-       unsigned char buffer[LWS_PRE + 512];
        unsigned char *p, *start, *end;
        int n;
 
        switch (reason) {
-
        case LWS_CALLBACK_HTTP_BODY:
-               lwsl_debug("LWS_CALLBACK_HTTP_BODY: len %d\n", (int)len);
-               strncpy(pss->post_string, in, sizeof (pss->post_string) -1);
-               pss->post_string[sizeof(pss->post_string) - 1] = '\0';
+               /* create the POST argument parser if not already existing */
+               if (!pss->spa) {
+                       pss->spa = lws_spa_create(wsi, param_names,
+                                       LWS_ARRAY_SIZE(param_names), 1024,
+                                       file_upload_cb, pss);
+                       if (!pss->spa)
+                               return -1;
+
+                       pss->filename[0] = '\0';
+                       pss->file_length = 0;
+               }
 
-               if (len < sizeof(pss->post_string) - 1)
-                       pss->post_string[len] = '\0';
+               /* let it parse the POST data */
+               if (lws_spa_process(pss->spa, in, (int)len))
+                       return -1;
+               break;
+
+       case LWS_CALLBACK_HTTP_BODY_COMPLETION:
+               lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION: %p\n", wsi);
+               /* call to inform no more payload data coming */
+               lws_spa_finalize(pss->spa);
+
+               pss->completed = 1;
+               lws_callback_on_writable(wsi);
                break;
 
        case LWS_CALLBACK_HTTP_WRITEABLE:
-               lwsl_debug("LWS_CALLBACK_HTTP_WRITEABLE: sending %d\n", pss->result_len);
-               n = lws_write(wsi, (unsigned char *)pss->result + LWS_PRE,
-                             pss->result_len, LWS_WRITE_HTTP);
-               if (n < 0)
-                       return 1;
-               goto try_to_reuse;
+               if (!pss->completed)
+                       break;
 
-       case LWS_CALLBACK_HTTP_BODY_COMPLETION:
-               lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
-               /*
-                * the whole of the sent body arrived,
-                * respond to the client with a redirect to show the
-                * results
-                */
-               pss->result_len = sprintf((char *)pss->result + LWS_PRE,
-                           "<html><body><h1>Form results</h1>'%s'<br>"
-                           "</body></html>", pss->post_string);
-
-               p = buffer + LWS_PRE;
+               p = (unsigned char *)pss->result + LWS_PRE;
                start = p;
-               end = p + sizeof(buffer) - LWS_PRE;
-
-               if (lws_add_http_header_status(wsi, 200, &p, end))
-                       return 1;
-
-               if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
-                               (unsigned char *)"text/html", 9, &p, end))
-                       return 1;
-               if (lws_add_http_header_content_length(wsi, pss->result_len, &p, end))
-                       return 1;
-               if (lws_finalize_http_header(wsi, &p, end))
-                       return 1;
-
-               n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
-               if (n < 0)
-                       return 1;
-
-               /*
-                *  send the payload next time, in case would block after
-                * headers
-                */
-               lws_callback_on_writable(wsi);
+               end = p + sizeof(pss->result) - LWS_PRE - 1;
+
+               if (!pss->sent_headers) {
+                       n = format_result(pss);
+
+                       if (lws_add_http_header_status(wsi, HTTP_STATUS_OK,
+                                                      &p, end))
+                               goto bail;
+
+                       if (lws_add_http_header_by_token(wsi,
+                                       WSI_TOKEN_HTTP_CONTENT_TYPE,
+                                       (unsigned char *)"text/html", 9,
+                                       &p, end))
+                               goto bail;
+                       if (lws_add_http_header_content_length(wsi, n, &p, end))
+                               goto bail;
+                       if (lws_finalize_http_header(wsi, &p, end))
+                               goto bail;
+
+                       /* first send the headers ... */
+                       n = lws_write(wsi, start, lws_ptr_diff(p, start),
+                                     LWS_WRITE_HTTP_HEADERS);
+                       if (n < 0)
+                               goto bail;
+
+                       pss->sent_headers = 1;
+                       lws_callback_on_writable(wsi);
+                       break;
+               }
+
+               if (!pss->sent_body) {
+                       n = format_result(pss);
+
+                       n = lws_write(wsi, (unsigned char *)start, n,
+                                     LWS_WRITE_HTTP_FINAL);
+
+                       pss->sent_body = 1;
+                       if (n < 0)
+                               return 1;
+                       goto try_to_reuse;
+               }
+               break;
+
+       case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
+               /* called when our wsi user_space is going to be destroyed */
+               if (pss->spa) {
+                       lws_spa_destroy(pss->spa);
+                       pss->spa = NULL;
+               }
                break;
 
        default:
@@ -98,6 +261,10 @@ callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
 
        return 0;
 
+bail:
+
+       return 1;
+
 try_to_reuse:
        if (lws_http_transaction_completed(wsi))
                return -1;
@@ -105,16 +272,22 @@ try_to_reuse:
        return 0;
 }
 
+#define LWS_PLUGIN_PROTOCOL_POST_DEMO \
+       { \
+               "protocol-post-demo", \
+               callback_post_demo, \
+               sizeof(struct per_session_data__post_demo), \
+               1024, \
+               0, NULL, 0 \
+       }
+
+#if !defined (LWS_PLUGIN_STATIC)
+
 static const struct lws_protocols protocols[] = {
-       {
-               "protocol-post-demo",
-               callback_post_demo,
-               sizeof(struct per_session_data__post_demo),
-               1024,
-       },
+       LWS_PLUGIN_PROTOCOL_POST_DEMO
 };
 
-LWS_VISIBLE int
+LWS_EXTERN LWS_VISIBLE int
 init_protocol_post_demo(struct lws_context *context,
                        struct lws_plugin_capability *c)
 {
@@ -125,15 +298,17 @@ init_protocol_post_demo(struct lws_context *context,
        }
 
        c->protocols = protocols;
-       c->count_protocols = ARRAY_SIZE(protocols);
+       c->count_protocols = LWS_ARRAY_SIZE(protocols);
        c->extensions = NULL;
        c->count_extensions = 0;
 
        return 0;
 }
 
-LWS_VISIBLE int
+LWS_EXTERN LWS_VISIBLE int
 destroy_protocol_post_demo(struct lws_context *context)
 {
        return 0;
 }
+
+#endif