From 7fb96c4dd1f7f2c7eb261a785d95f2091d24c389 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Thu, 3 Feb 2022 11:34:45 -0800 Subject: [PATCH] Dropping WebServer in favor of httpd. --- ueforth/common/forth_namespace_tests.fs | 2 -- ueforth/esp32/bindings.fs | 12 ------- ueforth/esp32/builtins.cpp | 22 ------------- ueforth/esp32/builtins.h | 42 ------------------------- ueforth/examples/httpd_sample.fs | 20 ++++++++++++ ueforth/posix/httpd.fs | 8 ++--- ueforth/site/ESP32forth.html | 42 ++++++++++++++++++++++++- 7 files changed, 65 insertions(+), 83 deletions(-) create mode 100755 ueforth/examples/httpd_sample.fs diff --git a/ueforth/common/forth_namespace_tests.fs b/ueforth/common/forth_namespace_tests.fs index 0a6e600..cbfdccf 100644 --- a/ueforth/common/forth_namespace_tests.fs +++ b/ueforth/common/forth_namespace_tests.fs @@ -507,7 +507,6 @@ e: test-esp32-forth-namespace out: SD_MMC out: SD out: WiFi - out: WebServer out: Wire out: editor out: streams @@ -571,7 +570,6 @@ e: test-esp32-forth-namespace out: SD_MMC out: SD out: WiFi - out: WebServer out: Wire out: ok check-esp32-basics diff --git a/ueforth/esp32/bindings.fs b/ueforth/esp32/bindings.fs index 70fbb40..3d73e02 100644 --- a/ueforth/esp32/bindings.fs +++ b/ueforth/esp32/bindings.fs @@ -25,18 +25,6 @@ transfer{ }transfer forth definitions -vocabulary WebServer WebServer definitions -transfer{ - WebServer.arg WebServer.argi WebServer.argName - WebServer.new WebServer.delete - WebServer.begin WebServer.stop - WebServer.on WebServer.hasArg - WebServer.sendHeader WebServer.send WebServer.sendContent - WebServer.method WebServer.handleClient - WebServer.args WebServer.setContentLength -}transfer -forth definitions - vocabulary WiFi WiFi definitions transfer{ diff --git a/ueforth/esp32/builtins.cpp b/ueforth/esp32/builtins.cpp index 3b023c1..0c39564 100644 --- a/ueforth/esp32/builtins.cpp +++ b/ueforth/esp32/builtins.cpp @@ -14,10 +14,6 @@ static char filename[PATH_MAX]; -#ifdef ENABLE_WEBSERVER_SUPPORT -static String string_value; -#endif - {{core}} {{interp}} {{boot}} @@ -53,24 +49,6 @@ static cell_t ResizeFile(cell_t fd, cell_t size) { return 0; } -#ifdef ENABLE_WEBSERVER_SUPPORT -static void InvokeWebServerOn(WebServer *ws, const char *url, cell_t xt) { - ws->on(url, [xt]() { - cell_t code[2]; - code[0] = xt; - code[1] = g_sys.YIELD_XT; - cell_t fstack[INTERRUPT_STACK_CELLS]; - cell_t rstack[INTERRUPT_STACK_CELLS]; - cell_t stack[INTERRUPT_STACK_CELLS]; - cell_t *rp = rstack; - *++rp = (cell_t) (fstack + 1); - *++rp = (cell_t) (stack + 1); - *++rp = (cell_t) code; - forth_run(rp); - }); -} -#endif - #ifdef ENABLE_INTERRUPTS_SUPPORT struct handle_interrupt_args { cell_t xt; diff --git a/ueforth/esp32/builtins.h b/ueforth/esp32/builtins.h index ae41d50..3db6666 100644 --- a/ueforth/esp32/builtins.h +++ b/ueforth/esp32/builtins.h @@ -14,10 +14,6 @@ #ifndef SIM_PRINT_ONLY -# ifdef ENABLE_WEBSERVER_SUPPORT -# include "WebServer.h" -# endif - # include # include # include @@ -48,7 +44,6 @@ static cell_t ResizeFile(cell_t fd, cell_t size); OPTIONAL_SPIFFS_SUPPORT \ OPTIONAL_WIFI_SUPPORT \ OPTIONAL_MDNS_SUPPORT \ - OPTIONAL_WEBSERVER_SUPPORT \ OPTIONAL_SD_SUPPORT \ OPTIONAL_SD_MMC_SUPPORT \ OPTIONAL_I2C_SUPPORT \ @@ -513,43 +508,6 @@ static cell_t FromIP(IPAddress ip) { X("MDNS.begin", MDNS_BEGIN, n0 = MDNS.begin(c0)) #endif -#ifndef ENABLE_WEBSERVER_SUPPORT -# define OPTIONAL_WEBSERVER_SUPPORT -#else -# ifndef SIM_PRINT_ONLY -# include -static void InvokeWebServerOn(WebServer *ws, const char *url, cell_t xt); -# define ws0 ((WebServer *) a0) -# endif -# define OPTIONAL_WEBSERVER_SUPPORT \ - /* WebServer */ \ - X("WebServer.new", WEBSERVER_NEW, PUSH new WebServer(tos)) \ - X("WebServer.delete", WEBSERVER_DELETE, delete ws0; DROP) \ - X("WebServer.begin", WEBSERVER_BEGIN, ws0->begin(n1); DROPn(2)) \ - X("WebServer.stop", WEBSERVER_STOP, ws0->stop(); DROP) \ - X("WebServer.on", WEBSERVER_ON, InvokeWebServerOn(ws0, c2, n1); DROPn(3)) \ - X("WebServer.hasArg", WEBSERVER_HAS_ARG, n0 = ws0->hasArg(c1); DROP) \ - X("WebServer.arg", WEBSERVER_ARG, \ - string_value = ws0->arg(c1); \ - c1 = &string_value[0]; n0 = string_value.length()) \ - X("WebServer.argi", WEBSERVER_ARGI, \ - string_value = ws0->arg(n1); \ - c1 = &string_value[0]; n0 = string_value.length()) \ - X("WebServer.argName", WEBSERVER_ARG_NAME, \ - string_value = ws0->argName(n1); \ - c1 = &string_value[0]; n0 = string_value.length()) \ - X("WebServer.args", WEBSERVER_ARGS, n0 = ws0->args()) \ - X("WebServer.setContentLength", WEBSERVER_SET_CONTENT_LENGTH, \ - ws0->setContentLength(n1); DROPn(2)) \ - X("WebServer.sendHeader", WEBSERVER_SEND_HEADER, \ - ws0->sendHeader(c3, c2, n1); DROPn(4)) \ - X("WebServer.send", WEBSERVER_SEND, ws0->send(n3, c2, c1); DROPn(4)) \ - X("WebServer.sendContent", WEBSERVER_SEND_CONTENT, \ - ws0->sendContent(c1); DROPn(2)) \ - X("WebServer.method", WEBSERVER_METHOD, n0 = ws0->method()) \ - X("WebServer.handleClient", WEBSERVER_HANDLE_CLIENT, ws0->handleClient(); DROP) -#endif - #ifndef ENABLE_OLED_SUPPORT # define OPTIONAL_OLED_SUPPORT #else diff --git a/ueforth/examples/httpd_sample.fs b/ueforth/examples/httpd_sample.fs new file mode 100755 index 0000000..2e643d7 --- /dev/null +++ b/ueforth/examples/httpd_sample.fs @@ -0,0 +1,20 @@ +#! /usr/bin/env ueforth + +also httpd + +: handle-index + s" text/html" ok-response r| + + Hi! + +| send ; + +: handle1 + ." Got a request for: " path type cr + path s" /" str= if handle-index exit then + notfound-response +; + +: run 8080 server + begin handleClient if handle1 then pause again ; +run diff --git a/ueforth/posix/httpd.fs b/ueforth/posix/httpd.fs index 0afce9b..47f6c09 100644 --- a/ueforth/posix/httpd.fs +++ b/ueforth/posix/httpd.fs @@ -45,7 +45,7 @@ sockaddr httpd-port sockaddr client variable client-len : server ( port -- ) httpd-port ->port! ." Listening on port " httpd-port ->port@ . cr AF_INET SOCK_STREAM 0 socket to sockfd - ( sockfd SOL_SOCKET SO_REUSEADDR 1 >r rp@ 4 setsockopt rdrop throw ) +( sockfd SOL_SOCKET SO_REUSEADDR 1 >r rp@ 4 setsockopt rdrop throw ) sockfd non-block throw sockfd httpd-port sizeof(sockaddr_in) bind throw sockfd max-connections listen throw @@ -80,14 +80,14 @@ variable goal variable goal# : path ( -- a n ) 0 bl skipover bl eat rot drop ; : send ( a n -- ) client-type ; -: response ( mime$ result$ status mime$ -- ) +: response ( mime$ result$ status -- ) s" HTTP/1.0 " client-type <# #s #> client-type bl client-emit client-type client-cr s" Content-type: " client-type client-type client-cr client-cr ; : ok-response ( mime$ -- ) s" OK" 200 response ; -: bad-response ( mime$ -- ) s" text/plain" s" Bad Request" 400 response ; -: notfound-response ( mime$ -- ) s" text/plain" s" Not Found" 404 response ; +: bad-response ( -- ) s" text/plain" s" Bad Request" 400 response ; +: notfound-response ( -- ) s" text/plain" s" Not Found" 404 response ; only forth definitions httpd diff --git a/ueforth/site/ESP32forth.html b/ueforth/site/ESP32forth.html index 86e4664..07984e1 100644 --- a/ueforth/site/ESP32forth.html +++ b/ueforth/site/ESP32forth.html @@ -500,8 +500,9 @@ SPIFFS.totalBytes ( -- n ) SPIFFS.usedBytes ( -- n ) -
WebServer
+
WebServer (DEPRECATED)
These words are inside the WebServer vocabulary. +

Removed in 7.0.6.14, use httpd instead.

 WebServer.new ( port -- ws )   Allocate new webserver object
 WebServer.delete ( ws -- )   Delete webserver object
@@ -521,6 +522,45 @@ WebServer.sendContent ( z ws -- )
 WebServer.method ( ws -- n )   GET / POST etc.
 
+
httpd
+These words are inside the httpd vocabulary. +
+server ( port -- ) Run a webserver on a port (in background, returns immediately).
+handleClient ( -- f ) Wait for a client to connect, true if connected.
+path ( -- a n ) Get request path.
+method ( -- a n ) Get request method (GET/POST).
+header ( a n -- a n ) Get header value or empty string.
+body ( -- a n ) Get request body.
+response ( mime mime# result result# status -- ) Respond with a particular mime type result and status.
+ok-response ( mime mime# ) Response with a HTTP 200.
+bad-response ( -- ) Response with HTTP 400 Bad request.
+notfound-response ( -- ) Response with HTTP 404 Not found.
+send ( a n -- ) Send response bytes.
+
+NOTE: You may want to use this feature in tandem with tasks / streams.
+
+Example:
+
+also httpd
+
+: handle-index
+  s" text/html" ok-response r|
+
+  Hi!
+
+| send ;
+
+: handle1
+  ." Got a request for: " path type cr
+  path s" /" str= if handle-index exit then
+  notfound-response
+;
+
+: run  8080 server
+       begin handleClient if handle1 then pause again ;
+run
+
+
Wire
These words are inside the Wire vocabulary.