minimal-http-server-form-get

This commit is contained in:
Andy Green 2018-03-29 09:11:27 +08:00
parent 020a8a2c38
commit 764d0d3b45
9 changed files with 289 additions and 0 deletions

View file

@ -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

View file

@ -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 <libwebsockets.h>\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()

View file

@ -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.

View file

@ -0,0 +1,153 @@
/*
* lws-minimal-http-server-form-get
*
* Copyright (C) 2018 Andy Green <andy@warmcat.com>
*
* 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 <libwebsockets.h>
#include <string.h>
#include <signal.h>
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;
}

View file

@ -0,0 +1,9 @@
<meta charset="UTF-8">
<html>
<body>
<img src="libwebsockets.org-logo.png"><br>
<h1>404</h1>
Sorry, that file doesn't exist.
</body>
</html>

View file

@ -0,0 +1,9 @@
<meta charset="UTF-8">
<html>
<body>
<img src="libwebsockets.org-logo.png"><br>
Thanks for posting the form.
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -0,0 +1,20 @@
<meta charset="UTF-8">
<html>
<body>
<img src="libwebsockets.org-logo.png"><br>
Hello from the <b>minimal http form GET example</b>.
<p>
This is a static page served from ./mount-origin/index.html.
<p>
When you submit the form below, you will see the values of the<br>
form parameters reported on the console log.
<p>
<form action="/form1" method="get">
Type some text:<br>
<input type="text" name="text1"><br>
<input type="submit" name="send" value="Submit">
</form>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB