diff --git a/minimal-examples/http-server/README.md b/minimal-examples/http-server/README.md index b8959784..7854e14c 100644 --- a/minimal-examples/http-server/README.md +++ b/minimal-examples/http-server/README.md @@ -1,6 +1,7 @@ |Example|Demonstrates| ---|--- minimal-http-server-dynamic|Serves both static and dynamically generated http content +minimal-http-server-form-get|Process a GET form minimal-http-server-form-post|Process a POST form (no file transfer) minimal-http-server-libuv|Same as minimal-http-server but libuv event loop minimal-http-server-multivhost|Same as minimal-http-server but three different vhosts diff --git a/minimal-examples/http-server/minimal-http-server-form-get/CMakeLists.txt b/minimal-examples/http-server/minimal-http-server-form-get/CMakeLists.txt new file mode 100644 index 00000000..582809fb --- /dev/null +++ b/minimal-examples/http-server/minimal-http-server-form-get/CMakeLists.txt @@ -0,0 +1,76 @@ +cmake_minimum_required(VERSION 2.8) +include(CheckCSourceCompiles) + +set(SAMP lws-minimal-http-server-form-get) +set(SRCS minimal-http-server-form-get.c) + +# If we are being built as part of lws, confirm current build config supports +# reqconfig, else skip building ourselves. +# +# If we are being built externally, confirm installed lws was configured to +# support reqconfig, else error out with a helpful message about the problem. +# +MACRO(require_lws_config reqconfig _val result) + + if (DEFINED ${reqconfig}) + if (${reqconfig}) + set (rq 1) + else() + set (rq 0) + endif() + else() + set(rq 0) + endif() + + if (${_val} EQUAL ${rq}) + set(SAME 1) + else() + set(SAME 0) + endif() + + if (LWS_WITH_MINIMAL_EXAMPLES AND NOT ${SAME}) + if (${_val}) + message("${SAMP}: skipping as lws being built without ${reqconfig}") + else() + message("${SAMP}: skipping as lws built with ${reqconfig}") + endif() + set(${result} 0) + else() + if (LWS_WITH_MINIMAL_EXAMPLES) + set(MET ${SAME}) + else() + CHECK_C_SOURCE_COMPILES("#include \nint main(void) {\n#if defined(${reqconfig})\n return 0;\n#else\n fail;\n#endif\n return 0;\n}\n" HAS_${reqconfig}) + if (NOT DEFINED HAS_${reqconfig} OR NOT HAS_${reqconfig}) + set(HAS_${reqconfig} 0) + else() + set(HAS_${reqconfig} 1) + endif() + if ((HAS_${reqconfig} AND ${_val}) OR (NOT HAS_${reqconfig} AND NOT ${_val})) + set(MET 1) + else() + set(MET 0) + endif() + endif() + if (NOT MET) + if (${_val}) + message(FATAL_ERROR "This project requires lws must have been configured with ${reqconfig}") + else() + message(FATAL_ERROR "Lws configuration of ${reqconfig} is incompatible with this project") + endif() + endif() + endif() +ENDMACRO() + +set(requirements 1) +require_lws_config(LWS_WITHOUT_SERVER 0 requirements) + +if (requirements) + add_executable(${SAMP} ${SRCS}) + + if (websockets_shared) + target_link_libraries(${SAMP} websockets_shared) + add_dependencies(${SAMP} websockets_shared) + else() + target_link_libraries(${SAMP} websockets) + endif() +endif() diff --git a/minimal-examples/http-server/minimal-http-server-form-get/README.md b/minimal-examples/http-server/minimal-http-server-form-get/README.md new file mode 100644 index 00000000..a22d8c26 --- /dev/null +++ b/minimal-examples/http-server/minimal-http-server-form-get/README.md @@ -0,0 +1,21 @@ +# lws minimal http server form GET + +## build + +``` + $ cmake . && make +``` + +## usage + +``` + $ ./lws-minimal-http-server-form-get +[2018/03/29 08:29:41:7044] USER: LWS minimal http server form GET | visit http://localhost:7681 +[2018/03/29 08:29:41:7044] NOTICE: Creating Vhost 'default' port 7681, 1 protocols, IPv6 off +[2018/03/29 08:29:49:8601] USER: text1: (len 4) 'xxxx' +[2018/03/29 08:29:49:8601] USER: send: (len 6) 'Submit' +``` + +Visit http://localhost:7681, submit the form. + +The form parameters are dumped to the log and you are redirected to a different page. diff --git a/minimal-examples/http-server/minimal-http-server-form-get/minimal-http-server-form-get.c b/minimal-examples/http-server/minimal-http-server-form-get/minimal-http-server-form-get.c new file mode 100644 index 00000000..64cb1aac --- /dev/null +++ b/minimal-examples/http-server/minimal-http-server-form-get/minimal-http-server-form-get.c @@ -0,0 +1,153 @@ +/* + * lws-minimal-http-server-form-get + * + * Copyright (C) 2018 Andy Green + * + * This file is made available under the Creative Commons CC0 1.0 + * Universal Public Domain Dedication. + * + * This demonstrates a minimal http server that performs a form GET with a couple + * of parameters. It dumps the parameters to the console log and redirects + * to another page. + */ + +#include +#include +#include + +static int interrupted; + +static const char * param_names[] = { + "text1", + "send" +}; + +static int +callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, + void *in, size_t len) +{ + uint8_t buf[LWS_PRE + 256], *start = &buf[LWS_PRE], *p = start, + *end = &buf[sizeof(buf) - 1]; + const char *val; + int n; + + switch (reason) { + case LWS_CALLBACK_HTTP: + + if (!lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI)) + /* not a GET */ + break; + if (strcmp((const char *)in, "/form1")) + /* not our form URL */ + break; + + /* we just dump the decoded things to the log */ + + for (n = 0; n < (int)ARRAY_SIZE(param_names); n++) { + val = lws_get_urlarg_by_name(wsi, param_names[n], + (char *)buf, sizeof(buf)); + if (!val) + lwsl_user("%s: undefined\n", param_names[n]); + else + lwsl_user("%s: (len %d) '%s'\n", param_names[n], + (int)strlen((const char *)buf),buf); + } + + /* + * Our response is to redirect to a static page. We could + * have generated a dynamic html page here instead. + */ + + if (lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY, + (unsigned char *)"after-form1.html", + 16, &p, end)) + return -1; + + /* we could add more headers here */ + + if (lws_finalize_http_header(wsi, &p, end)) + return -1; + + n = lws_write(wsi, start, lws_ptr_diff(p, start), + LWS_WRITE_HTTP_HEADERS | + LWS_WRITE_H2_STREAM_END); + if (n < 0) + return -1; + + break; + + default: + break; + } + + return lws_callback_http_dummy(wsi, reason, user, in, len); +} + +static struct lws_protocols protocols[] = { + { "http", callback_http, 0, 0 }, + { NULL, NULL, 0, 0 } /* terminator */ +}; + +/* default mount serves the URL space from ./mount-origin */ + +static const struct lws_http_mount mount = { + /* .mount_next */ NULL, /* linked-list "next" */ + /* .mountpoint */ "/", /* mountpoint URL */ + /* .origin */ "./mount-origin", /* serve from dir */ + /* .def */ "index.html", /* default filename */ + /* .protocol */ NULL, + /* .cgienv */ NULL, + /* .extra_mimetypes */ NULL, + /* .interpret */ NULL, + /* .cgi_timeout */ 0, + /* .cache_max_age */ 0, + /* .auth_mask */ 0, + /* .cache_reusable */ 0, + /* .cache_revalidate */ 0, + /* .cache_intermediaries */ 0, + /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */ + /* .mountpoint_len */ 1, /* char count */ + /* .basic_auth_login_file */ NULL, +}; + +void sigint_handler(int sig) +{ + interrupted = 1; +} + +int main(int argc, char **argv) +{ + struct lws_context_creation_info info; + struct lws_context *context; + int n = 0; + + signal(SIGINT, sigint_handler); + + memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ + info.port = 7681; + info.protocols = protocols; + info.mounts = &mount; + + lws_set_log_level(LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE + /* for LLL_ verbosity above NOTICE to be built into lws, + * lws must have been configured and built with + * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ + /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ + /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ + /* | LLL_DEBUG */, NULL); + + lwsl_user("LWS minimal http server GET | visit http://localhost:7681\n"); + + context = lws_create_context(&info); + if (!context) { + lwsl_err("lws init failed\n"); + return 1; + } + + while (n >= 0 && !interrupted) + n = lws_service(context, 1000); + + lws_context_destroy(context); + + return 0; +} diff --git a/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/404.html b/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/404.html new file mode 100644 index 00000000..9ad5a334 --- /dev/null +++ b/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/404.html @@ -0,0 +1,9 @@ + + + +
+

404

+ Sorry, that file doesn't exist. + + + diff --git a/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/after-form1.html b/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/after-form1.html new file mode 100644 index 00000000..6009273a --- /dev/null +++ b/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/after-form1.html @@ -0,0 +1,9 @@ + + + +
+ + Thanks for posting the form. + + + diff --git a/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/favicon.ico b/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/favicon.ico new file mode 100644 index 00000000..c0cc2e3d Binary files /dev/null and b/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/favicon.ico differ diff --git a/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/index.html b/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/index.html new file mode 100644 index 00000000..c74a9535 --- /dev/null +++ b/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/index.html @@ -0,0 +1,20 @@ + + + +
+ + Hello from the minimal http form GET example. +

+ This is a static page served from ./mount-origin/index.html. +

+ When you submit the form below, you will see the values of the
+ form parameters reported on the console log. +

+

+ Type some text:
+
+ +
+ + + diff --git a/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/libwebsockets.org-logo.png b/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/libwebsockets.org-logo.png new file mode 100644 index 00000000..2060a10c Binary files /dev/null and b/minimal-examples/http-server/minimal-http-server-form-get/mount-origin/libwebsockets.org-logo.png differ