web.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. * This file is a modified version of the lwIP web server demo. The original
  15. * author is unknown because the file didn't contain any license information.
  16. */
  17. /**
  18. * @file web.c
  19. * @brief HTTP server wrapper thread code.
  20. * @addtogroup WEB_THREAD
  21. * @{
  22. */
  23. #include "ch.h"
  24. #include "lwip/opt.h"
  25. #include "lwip/arch.h"
  26. #include "lwip/api.h"
  27. #include "web.h"
  28. #if LWIP_NETCONN
  29. static const char http_html_hdr[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
  30. static const char http_index_html[] = "<html><head><title>Congrats!</title></head><body><h1>Welcome to our lwIP HTTP server!</h1><p>This is a small test page.</body></html>";
  31. static void http_server_serve(struct netconn *conn) {
  32. struct netbuf *inbuf;
  33. char *buf;
  34. u16_t buflen;
  35. err_t err;
  36. /* Read the data from the port, blocking if nothing yet there.
  37. We assume the request (the part we care about) is in one netbuf */
  38. err = netconn_recv(conn, &inbuf);
  39. if (err == ERR_OK) {
  40. netbuf_data(inbuf, (void **)&buf, &buflen);
  41. /* Is this an HTTP GET command? (only check the first 5 chars, since
  42. there are other formats for GET, and we're keeping it very simple )*/
  43. if (buflen>=5 &&
  44. buf[0]=='G' &&
  45. buf[1]=='E' &&
  46. buf[2]=='T' &&
  47. buf[3]==' ' &&
  48. buf[4]=='/' ) {
  49. /* Send the HTML header
  50. * subtract 1 from the size, since we dont send the \0 in the string
  51. * NETCONN_NOCOPY: our data is const static, so no need to copy it
  52. */
  53. netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
  54. /* Send our HTML page */
  55. netconn_write(conn, http_index_html, sizeof(http_index_html)-1, NETCONN_NOCOPY);
  56. }
  57. }
  58. /* Close the connection (server closes in HTTP) */
  59. netconn_close(conn);
  60. /* Delete the buffer (netconn_recv gives us ownership,
  61. so we have to make sure to deallocate the buffer) */
  62. netbuf_delete(inbuf);
  63. }
  64. /**
  65. * Stack area for the http thread.
  66. */
  67. THD_WORKING_AREA(wa_http_server, WEB_THREAD_STACK_SIZE);
  68. /**
  69. * HTTP server thread.
  70. */
  71. THD_FUNCTION(http_server, p) {
  72. struct netconn *conn, *newconn;
  73. err_t err;
  74. (void)p;
  75. chRegSetThreadName("http");
  76. /* Create a new TCP connection handle */
  77. conn = netconn_new(NETCONN_TCP);
  78. LWIP_ERROR("http_server: invalid conn", (conn != NULL), chThdExit(MSG_RESET););
  79. /* Bind to port 80 (HTTP) with default IP address */
  80. netconn_bind(conn, NULL, WEB_THREAD_PORT);
  81. /* Put the connection into LISTEN state */
  82. netconn_listen(conn);
  83. /* Goes to the final priority after initialization.*/
  84. chThdSetPriority(WEB_THREAD_PRIORITY);
  85. while (true) {
  86. err = netconn_accept(conn, &newconn);
  87. if (err != ERR_OK)
  88. continue;
  89. http_server_serve(newconn);
  90. netconn_delete(newconn);
  91. }
  92. }
  93. #endif /* LWIP_NETCONN */
  94. /** @} */