httpd_form.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2012 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * $Id$
  36. */
  37. #ifdef NUT_OS
  38. #include <sys/version.h>
  39. #include <dev/board.h>
  40. #include <dev/urom.h>
  41. #include <pro/dhcp.h>
  42. #endif
  43. #include <pro/uhttp/mediatypes.h>
  44. #include <pro/uhttp/modules/mod_redir.h>
  45. #include <pro/uhttp/modules/mod_cgi_func.h>
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include <string.h>
  49. static int SendResult(HTTPD_SESSION *hs, char *first, char *last)
  50. {
  51. static const char head[] =
  52. "<html>"
  53. "<head>"
  54. "<title>Form Result</title>"
  55. "</head>";
  56. static const char body[] =
  57. "<body>"
  58. "<p>Hello %s %s!</p>"
  59. "<a href=\"/index.html\">back</a>"
  60. "</body>"
  61. "<html>";
  62. HttpSendHeaderTop(hs, 200);
  63. HttpSendHeaderBottom(hs, "text", "html", -1);
  64. s_puts(head, hs->s_stream);
  65. s_printf(hs->s_stream, body, first, last);
  66. s_flush(hs->s_stream);
  67. return 0;
  68. }
  69. static int CgiGetForm(HTTPD_SESSION *hs)
  70. {
  71. char *arg;
  72. char *val;
  73. char *first = NULL;
  74. char *last = NULL;
  75. for (arg = HttpArgParseFirst(&hs->s_req); arg; arg = HttpArgParseNext(&hs->s_req)) {
  76. val = HttpArgValue(&hs->s_req);
  77. if (val) {
  78. if (strcmp(arg, "firstname") == 0) {
  79. first = strdup(val);
  80. }
  81. else if (strcmp(arg, "familyname") == 0) {
  82. last = strdup(val);
  83. }
  84. }
  85. }
  86. SendResult(hs, first, last);
  87. free(first);
  88. free(last);
  89. return 0;
  90. }
  91. static int CgiPostForm(HTTPD_SESSION *hs)
  92. {
  93. char *arg;
  94. char *val;
  95. char *first = NULL;
  96. char *last = NULL;
  97. long avail;
  98. avail = hs->s_req.req_length;
  99. while (avail) {
  100. arg = HttpArgReadNext(hs, &avail);
  101. if (arg) {
  102. val = HttpArgValue(&hs->s_req);
  103. if (val) {
  104. if (strcmp(arg, "firstname") == 0) {
  105. first = strdup(val);
  106. }
  107. else if (strcmp(arg, "familyname") == 0) {
  108. last = strdup(val);
  109. }
  110. }
  111. }
  112. }
  113. SendResult(hs, first, last);
  114. free(first);
  115. free(last);
  116. return 0;
  117. }
  118. int main(void)
  119. {
  120. #ifdef NUT_OS
  121. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  122. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  123. #endif
  124. puts("uHTTP form sample\nBuild " __DATE__ " " __TIME__);
  125. #ifdef NUT_OS
  126. NutRegisterDevice(&DEV_ETHER, 0, 0);
  127. NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 60000);
  128. NutRegisterDevice(&devUrom, 0, 0);
  129. #endif
  130. StreamInit();
  131. MediaTypeInitDefaults();
  132. HttpRegisterRedir("", "/index.html", 301);
  133. HttpRegisterCgiFunction("getform.cgi", CgiGetForm);
  134. HttpRegisterCgiFunction("postform.cgi", CgiPostForm);
  135. HttpRegisterMediaType("cgi", NULL, NULL, HttpCgiFunctionHandler);
  136. StreamClientAccept(HttpdClientHandler, NULL);
  137. puts("Exit");
  138. #ifdef NUT_OS
  139. for (;;) ;
  140. #endif
  141. return 0;
  142. }