1/* 2 * lws-minimal-http-server-form-get 3 * 4 * Written in 2010-2019 by Andy Green <andy@warmcat.com> 5 * 6 * This file is made available under the Creative Commons CC0 1.0 7 * Universal Public Domain Dedication. 8 * 9 * This demonstrates a minimal http server that performs a form GET with a couple 10 * of parameters. It dumps the parameters to the console log and redirects 11 * to another page. 12 */ 13 14#include <libwebsockets.h> 15#include <string.h> 16#include <signal.h> 17 18static int interrupted; 19 20static const char * param_names[] = { 21 "text1", 22 "send" 23}; 24 25static int 26callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, 27 void *in, size_t len) 28{ 29 uint8_t buf[LWS_PRE + LWS_RECOMMENDED_MIN_HEADER_SPACE], 30 *start = &buf[LWS_PRE], *p = start, 31 *end = &buf[sizeof(buf) - 1]; 32 int n; 33 34 switch (reason) { 35 case LWS_CALLBACK_HTTP: 36 37 if (!lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI)) 38 /* not a GET */ 39 break; 40 lwsl_err("%s: %s\n", __func__, (const char *)in); 41 if (strcmp((const char *)in, "/form1")) 42 /* not our form URL */ 43 break; 44 45 /* we just dump the decoded things to the log */ 46 47 for (n = 0; n < (int)LWS_ARRAY_SIZE(param_names); n++) { 48 int rv = lws_get_urlarg_by_name_safe(wsi, param_names[n], 49 (char *)buf, sizeof(buf)); 50 if (rv < 0) 51 lwsl_user("%s: undefined\n", param_names[n]); 52 else 53 lwsl_user("%s: (len %d) '%s'\n", param_names[n], 54 (int)rv, buf); 55 } 56 57 /* 58 * Our response is to redirect to a static page. We could 59 * have generated a dynamic html page here instead. 60 */ 61 62 if (lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY, 63 (unsigned char *)"after-form1.html", 64 16, &p, end) < 0) 65 return -1; 66 break; 67 68 default: 69 break; 70 } 71 72 return lws_callback_http_dummy(wsi, reason, user, in, len); 73} 74 75static struct lws_protocols protocols[] = { 76 { "http", callback_http, 0, 0, 0, NULL, 0 }, 77 LWS_PROTOCOL_LIST_TERM 78}; 79 80/* default mount serves the URL space from ./mount-origin */ 81 82static const struct lws_http_mount mount = { 83 /* .mount_next */ NULL, /* linked-list "next" */ 84 /* .mountpoint */ "/", /* mountpoint URL */ 85 /* .origin */ "./mount-origin", /* serve from dir */ 86 /* .def */ "index.html", /* default filename */ 87 /* .protocol */ NULL, 88 /* .cgienv */ NULL, 89 /* .extra_mimetypes */ NULL, 90 /* .interpret */ NULL, 91 /* .cgi_timeout */ 0, 92 /* .cache_max_age */ 0, 93 /* .auth_mask */ 0, 94 /* .cache_reusable */ 0, 95 /* .cache_revalidate */ 0, 96 /* .cache_intermediaries */ 0, 97 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */ 98 /* .mountpoint_len */ 1, /* char count */ 99 /* .basic_auth_login_file */ NULL, 100}; 101 102void sigint_handler(int sig) 103{ 104 interrupted = 1; 105} 106 107int main(int argc, const char **argv) 108{ 109 struct lws_context_creation_info info; 110 struct lws_context *context; 111 const char *p; 112 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE 113 /* for LLL_ verbosity above NOTICE to be built into lws, 114 * lws must have been configured and built with 115 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ 116 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ 117 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ 118 /* | LLL_DEBUG */; 119 120 signal(SIGINT, sigint_handler); 121 122 if ((p = lws_cmdline_option(argc, argv, "-d"))) 123 logs = atoi(p); 124 125 lws_set_log_level(logs, NULL); 126 lwsl_user("LWS minimal http server GET | visit http://localhost:7681\n"); 127 128 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ 129 info.port = 7681; 130 info.protocols = protocols; 131 info.mounts = &mount; 132 info.options = 133 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; 134 135 context = lws_create_context(&info); 136 if (!context) { 137 lwsl_err("lws init failed\n"); 138 return 1; 139 } 140 141 while (n >= 0 && !interrupted) 142 n = lws_service(context, 0); 143 144 lws_context_destroy(context); 145 146 return 0; 147} 148