cgi_example.c 3.2 KB

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