• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * HTTPD simple CGI example
4  *
5  * This file demonstrates how to add support for basic CGI.
6  */
7 
8  /*
9  * Copyright (c) 2017 Simon Goldschmidt
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without modification,
13  * are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  *    this list of conditions and the following disclaimer in the documentation
19  *    and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
26  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32  * OF SUCH DAMAGE.
33  *
34  * This file is part of the lwIP TCP/IP stack.
35  *
36  * Author: Simon Goldschmidt <goldsimon@gmx.de>
37  *
38  */
39 
40 #include "lwip/opt.h"
41 #include "cgi_example.h"
42 
43 #include "lwip/apps/httpd.h"
44 
45 #include "lwip/def.h"
46 #include "lwip/mem.h"
47 
48 #include <stdio.h>
49 #include <string.h>
50 
51 /** define LWIP_HTTPD_EXAMPLE_CGI_SIMPLE to 1 to enable this cgi example */
52 #ifndef LWIP_HTTPD_EXAMPLE_CGI_SIMPLE
53 #define LWIP_HTTPD_EXAMPLE_CGI_SIMPLE 0
54 #endif
55 
56 #if LWIP_HTTPD_EXAMPLE_CGI_SIMPLE
57 
58 #if !LWIP_HTTPD_CGI
59 #error LWIP_HTTPD_EXAMPLE_CGI_SIMPLE needs LWIP_HTTPD_CGI
60 #endif
61 
62 static const char *cgi_handler_basic(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]);
63 
64 static const tCGI cgi_handlers[] = {
65   {
66     "/basic_cgi",
67     cgi_handler_basic
68   },
69   {
70     "/basic_cgi_2",
71     cgi_handler_basic
72   }
73 };
74 
75 void
cgi_ex_init(void)76 cgi_ex_init(void)
77 {
78   http_set_cgi_handlers(cgi_handlers, LWIP_ARRAYSIZE(cgi_handlers));
79 }
80 
81 /** This basic CGI function can parse param/value pairs and return an url that
82  * is sent as a response by httpd.
83  *
84  * This example function just checks that the input url has two key value
85  * parameter pairs: "foo=bar" and "test=123"
86  * If not, it returns 404
87  */
88 static const char *
cgi_handler_basic(int iIndex,int iNumParams,char * pcParam[],char * pcValue[])89 cgi_handler_basic(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
90 {
91   LWIP_ASSERT("check index", iIndex < LWIP_ARRAYSIZE(cgi_handlers));
92 
93   if (iNumParams == 2) {
94     if (!strcmp(pcParam[0], "foo")) {
95       if (!strcmp(pcValue[0], "bar")) {
96         if (!strcmp(pcParam[1], "test")) {
97           if (!strcmp(pcValue[1], "123")) {
98             return "/index.html";
99           }
100         }
101       }
102     }
103   }
104   return "/404.html";
105 }
106 
107 #endif /* LWIP_HTTPD_EXAMPLE_CGI_SIMPLE */
108