post_example.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @file
  3. * HTTPD example for simple POST
  4. */
  5. /*
  6. * Copyright (c) 2017 Simon Goldschmidt
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Simon Goldschmidt <goldsimon@gmx.de>
  34. *
  35. */
  36. #include "lwip/opt.h"
  37. #include "lwip/apps/httpd.h"
  38. #include "lwip/def.h"
  39. #include "lwip/mem.h"
  40. #include <stdio.h>
  41. #include <string.h>
  42. /** define LWIP_HTTPD_EXAMPLE_GENERATEDFILES to 1 to enable this file system */
  43. #ifndef LWIP_HTTPD_EXAMPLE_SIMPLEPOST
  44. #define LWIP_HTTPD_EXAMPLE_SIMPLEPOST 0
  45. #endif
  46. #if LWIP_HTTPD_EXAMPLE_SIMPLEPOST
  47. #if !LWIP_HTTPD_SUPPORT_POST
  48. #error This needs LWIP_HTTPD_SUPPORT_POST
  49. #endif
  50. #define USER_PASS_BUFSIZE 16
  51. static void *current_connection;
  52. static void *valid_connection;
  53. static char last_user[USER_PASS_BUFSIZE];
  54. err_t
  55. httpd_post_begin(void *connection, const char *uri, const char *http_request,
  56. u16_t http_request_len, int content_len, char *response_uri,
  57. u16_t response_uri_len, u8_t *post_auto_wnd)
  58. {
  59. LWIP_UNUSED_ARG(connection);
  60. LWIP_UNUSED_ARG(http_request);
  61. LWIP_UNUSED_ARG(http_request_len);
  62. LWIP_UNUSED_ARG(content_len);
  63. LWIP_UNUSED_ARG(post_auto_wnd);
  64. if (!memcmp(uri, "/login.cgi", 11)) {
  65. if (current_connection != connection) {
  66. current_connection = connection;
  67. valid_connection = NULL;
  68. /* default page is "login failed" */
  69. snprintf(response_uri, response_uri_len, "/loginfail.html");
  70. /* e.g. for large uploads to slow flash over a fast connection, you should
  71. manually update the rx window. That way, a sender can only send a full
  72. tcp window at a time. If this is required, set 'post_aut_wnd' to 0.
  73. We do not need to throttle upload speed here, so: */
  74. *post_auto_wnd = 1;
  75. return ERR_OK;
  76. }
  77. }
  78. return ERR_VAL;
  79. }
  80. err_t
  81. httpd_post_receive_data(void *connection, struct pbuf *p)
  82. {
  83. err_t ret;
  84. LWIP_ASSERT("NULL pbuf", p != NULL);
  85. if (current_connection == connection) {
  86. u16_t token_user = pbuf_memfind(p, "user=", 5, 0);
  87. u16_t token_pass = pbuf_memfind(p, "pass=", 5, 0);
  88. if ((token_user != 0xFFFF) && (token_pass != 0xFFFF)) {
  89. u16_t value_user = token_user + 5;
  90. u16_t value_pass = token_pass + 5;
  91. u16_t len_user = 0;
  92. u16_t len_pass = 0;
  93. u16_t tmp;
  94. /* find user len */
  95. tmp = pbuf_memfind(p, "&", 1, value_user);
  96. if (tmp != 0xFFFF) {
  97. len_user = tmp - value_user;
  98. } else {
  99. len_user = p->tot_len - value_user;
  100. }
  101. /* find pass len */
  102. tmp = pbuf_memfind(p, "&", 1, value_pass);
  103. if (tmp != 0xFFFF) {
  104. len_pass = tmp - value_pass;
  105. } else {
  106. len_pass = p->tot_len - value_pass;
  107. }
  108. if ((len_user > 0) && (len_user < USER_PASS_BUFSIZE) &&
  109. (len_pass > 0) && (len_pass < USER_PASS_BUFSIZE)) {
  110. /* provide contiguous storage if p is a chained pbuf */
  111. char buf_user[USER_PASS_BUFSIZE];
  112. char buf_pass[USER_PASS_BUFSIZE];
  113. char *user = (char *)pbuf_get_contiguous(p, buf_user, sizeof(buf_user), len_user, value_user);
  114. char *pass = (char *)pbuf_get_contiguous(p, buf_pass, sizeof(buf_pass), len_pass, value_pass);
  115. if (user && pass) {
  116. user[len_user] = 0;
  117. pass[len_pass] = 0;
  118. if (!strcmp(user, "lwip") && !strcmp(pass, "post")) {
  119. /* user and password are correct, create a "session" */
  120. valid_connection = connection;
  121. memcpy(last_user, user, sizeof(last_user));
  122. }
  123. }
  124. }
  125. }
  126. /* not returning ERR_OK aborts the connection, so return ERR_OK unless the
  127. connection is unknown */
  128. ret = ERR_OK;
  129. } else {
  130. ret = ERR_VAL;
  131. }
  132. /* this function must ALWAYS free the pbuf it is passed or it will leak memory */
  133. pbuf_free(p);
  134. return ret;
  135. }
  136. void
  137. httpd_post_finished(void *connection, char *response_uri, u16_t response_uri_len)
  138. {
  139. /* default page is "login failed" */
  140. snprintf(response_uri, response_uri_len, "/loginfail.html");
  141. if (current_connection == connection) {
  142. if (valid_connection == connection) {
  143. /* login succeeded */
  144. snprintf(response_uri, response_uri_len, "/session.html");
  145. }
  146. current_connection = NULL;
  147. valid_connection = NULL;
  148. }
  149. }
  150. #endif /* LWIP_HTTPD_EXAMPLE_SIMPLEPOST*/