Imported Upstream version 3.2
[platform/upstream/libwebsockets.git] / minimal-examples / http-server / minimal-http-server-form-get / minimal-http-server-form-get.c
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
18 static int interrupted;
19
20 static const char * param_names[] = {
21         "text1",
22         "send"
23 };
24
25 static int
26 callback_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         const char *val;
33         int n;
34
35         switch (reason) {
36         case LWS_CALLBACK_HTTP:
37
38                 if (!lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI))
39                         /* not a GET */
40                         break;
41                 lwsl_err("%s: %s\n", __func__, (const char *)in);
42                 if (strcmp((const char *)in, "/form1"))
43                         /* not our form URL */
44                         break;
45
46                 /* we just dump the decoded things to the log */
47
48                 for (n = 0; n < (int)LWS_ARRAY_SIZE(param_names); n++) {
49                         val = lws_get_urlarg_by_name(wsi, param_names[n],
50                                         (char *)buf, sizeof(buf));
51                         if (!val)
52                                 lwsl_user("%s: undefined\n", param_names[n]);
53                         else
54                                 lwsl_user("%s: (len %d) '%s'\n", param_names[n],
55                                           (int)strlen((const char *)buf),buf);
56                 }
57
58                 /*
59                  * Our response is to redirect to a static page.  We could
60                  * have generated a dynamic html page here instead.
61                  */
62
63                 if (lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
64                                       (unsigned char *)"after-form1.html",
65                                       16, &p, end) < 0)
66                         return -1;
67                 break;
68
69         default:
70                 break;
71         }
72
73         return lws_callback_http_dummy(wsi, reason, user, in, len);
74 }
75
76 static struct lws_protocols protocols[] = {
77         { "http", callback_http, 0, 0 },
78         { NULL, NULL, 0, 0 } /* terminator */
79 };
80
81 /* default mount serves the URL space from ./mount-origin */
82
83 static const struct lws_http_mount mount = {
84         /* .mount_next */              NULL,            /* linked-list "next" */
85         /* .mountpoint */               "/",            /* mountpoint URL */
86         /* .origin */           "./mount-origin",       /* serve from dir */
87         /* .def */                      "index.html",   /* default filename */
88         /* .protocol */                 NULL,
89         /* .cgienv */                   NULL,
90         /* .extra_mimetypes */          NULL,
91         /* .interpret */                NULL,
92         /* .cgi_timeout */              0,
93         /* .cache_max_age */            0,
94         /* .auth_mask */                0,
95         /* .cache_reusable */           0,
96         /* .cache_revalidate */         0,
97         /* .cache_intermediaries */     0,
98         /* .origin_protocol */          LWSMPRO_FILE,   /* files in a dir */
99         /* .mountpoint_len */           1,              /* char count */
100         /* .basic_auth_login_file */    NULL,
101 };
102
103 void sigint_handler(int sig)
104 {
105         interrupted = 1;
106 }
107
108 int main(int argc, const char **argv)
109 {
110         struct lws_context_creation_info info;
111         struct lws_context *context;
112         const char *p;
113         int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
114                         /* for LLL_ verbosity above NOTICE to be built into lws,
115                          * lws must have been configured and built with
116                          * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
117                         /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
118                         /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
119                         /* | LLL_DEBUG */;
120
121         signal(SIGINT, sigint_handler);
122
123         if ((p = lws_cmdline_option(argc, argv, "-d")))
124                 logs = atoi(p);
125
126         lws_set_log_level(logs, NULL);
127         lwsl_user("LWS minimal http server GET | visit http://localhost:7681\n");
128
129         memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
130         info.port = 7681;
131         info.protocols = protocols;
132         info.mounts = &mount;
133         info.options =
134                 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
135
136         context = lws_create_context(&info);
137         if (!context) {
138                 lwsl_err("lws init failed\n");
139                 return 1;
140         }
141
142         while (n >= 0 && !interrupted)
143                 n = lws_service(context, 0);
144
145         lws_context_destroy(context);
146
147         return 0;
148 }