httpserver-netconn.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "lwip/opt.h"
  2. #include "lwip/arch.h"
  3. #include "lwip/api.h"
  4. #include "httpserver-netconn.h"
  5. #if LWIP_NETCONN
  6. #ifndef HTTPD_DEBUG
  7. #define HTTPD_DEBUG LWIP_DBG_OFF
  8. #endif
  9. static const char http_html_hdr[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
  10. 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, served by httpserver-netconn.</body></html>";
  11. /** Serve one HTTP connection accepted in the http thread */
  12. static void
  13. http_server_netconn_serve(struct netconn *conn)
  14. {
  15. struct netbuf *inbuf;
  16. char *buf;
  17. u16_t buflen;
  18. err_t err;
  19. /* Read the data from the port, blocking if nothing yet there.
  20. We assume the request (the part we care about) is in one netbuf */
  21. err = netconn_recv(conn, &inbuf);
  22. if (err == ERR_OK) {
  23. netbuf_data(inbuf, (void**)&buf, &buflen);
  24. /* Is this an HTTP GET command? (only check the first 5 chars, since
  25. there are other formats for GET, and we're keeping it very simple )*/
  26. if (buflen>=5 &&
  27. buf[0]=='G' &&
  28. buf[1]=='E' &&
  29. buf[2]=='T' &&
  30. buf[3]==' ' &&
  31. buf[4]=='/' ) {
  32. /* Send the HTML header
  33. * subtract 1 from the size, since we don't send the \0 in the string
  34. * NETCONN_NOCOPY: our data is const static, so no need to copy it
  35. */
  36. netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
  37. /* Send our HTML page */
  38. netconn_write(conn, http_index_html, sizeof(http_index_html)-1, NETCONN_NOCOPY);
  39. }
  40. }
  41. /* Close the connection (server closes in HTTP) */
  42. netconn_close(conn);
  43. /* Delete the buffer (netconn_recv gives us ownership,
  44. so we have to make sure to deallocate the buffer) */
  45. netbuf_delete(inbuf);
  46. }
  47. /** The main function, never returns! */
  48. static void
  49. http_server_netconn_thread(void *arg)
  50. {
  51. struct netconn *conn, *newconn;
  52. err_t err;
  53. LWIP_UNUSED_ARG(arg);
  54. /* Create a new TCP connection handle */
  55. /* Bind to port 80 (HTTP) with default IP address */
  56. #if LWIP_IPV6
  57. conn = netconn_new(NETCONN_TCP_IPV6);
  58. netconn_bind(conn, IP6_ADDR_ANY, 80);
  59. #else /* LWIP_IPV6 */
  60. conn = netconn_new(NETCONN_TCP);
  61. netconn_bind(conn, IP_ADDR_ANY, 80);
  62. #endif /* LWIP_IPV6 */
  63. LWIP_ERROR("http_server: invalid conn", (conn != NULL), return;);
  64. /* Put the connection into LISTEN state */
  65. netconn_listen(conn);
  66. do {
  67. err = netconn_accept(conn, &newconn);
  68. if (err == ERR_OK) {
  69. http_server_netconn_serve(newconn);
  70. netconn_delete(newconn);
  71. }
  72. } while(err == ERR_OK);
  73. LWIP_DEBUGF(HTTPD_DEBUG,
  74. ("http_server_netconn_thread: netconn_accept received error %d, shutting down",
  75. err));
  76. netconn_close(conn);
  77. netconn_delete(conn);
  78. }
  79. /** Initialize the HTTP server (start its thread) */
  80. void
  81. http_server_netconn_init(void)
  82. {
  83. sys_thread_new("http_server_netconn", http_server_netconn_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
  84. }
  85. #endif /* LWIP_NETCONN*/