httpserv.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (C) 2001-2004 by egnite Software GmbH
  3. * Copyright (C) 2009 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * \example httpd_upnp/httpserv.c
  37. *
  38. * Very simple multithreaded HTTP daemon with DHCP and UPnP functionality.
  39. *
  40. * Note: A DHCP server must be available on the network.
  41. * Tested on the Elektor Internet Radio.
  42. */
  43. #include <cfg/os.h>
  44. #include <string.h>
  45. #include <io.h>
  46. #include <fcntl.h>
  47. #include <time.h>
  48. #include <dev/board.h>
  49. #include <dev/urom.h>
  50. #include <dev/nplmmc.h>
  51. #include <dev/sbimmc.h>
  52. #include <fs/phatfs.h>
  53. #include <sys/version.h>
  54. #include <sys/thread.h>
  55. #include <sys/timer.h>
  56. #include <sys/heap.h>
  57. #include <sys/confnet.h>
  58. #include <sys/socket.h>
  59. #include <arpa/inet.h>
  60. #include <net/route.h>
  61. #include <netinet/in.h>
  62. #include <pro/httpd.h>
  63. #include <pro/dhcp.h>
  64. #include <pro/ssi.h>
  65. #include <pro/asp.h>
  66. #include <pro/discover.h>
  67. #include "upnp.h"
  68. /* Server thread stack size. */
  69. #ifndef HTTPD_SERVICE_STACK
  70. #define HTTPD_SERVICE_STACK ((1024 * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD)
  71. #endif
  72. /*! \fn Service(void *arg)
  73. * \brief HTTP service thread.
  74. *
  75. * The endless loop in this thread waits for a client connect,
  76. * processes the HTTP request and disconnects. Nut/Net doesn't
  77. * support a server backlog. If one client has established a
  78. * connection, further connect attempts will be rejected.
  79. * Typically browsers open more than one connection in order
  80. * to load images concurrently. So we run this routine by
  81. * several threads.
  82. *
  83. */
  84. THREAD(Service, arg)
  85. {
  86. TCPSOCKET *sock;
  87. FILE *stream;
  88. uint8_t id = (uint8_t) ((uintptr_t) arg);
  89. /*
  90. * Now loop endless for connections.
  91. */
  92. for (;;) {
  93. /*
  94. * Create a socket.
  95. */
  96. if ((sock = NutTcpCreateSocket()) == 0) {
  97. printf("[%u] Creating socket failed\n", id);
  98. NutSleep(5000);
  99. continue;
  100. }
  101. /*
  102. * Listen on port 80. This call will block until we get a connection
  103. * from a client.
  104. */
  105. NutTcpAccept(sock, 80);
  106. printf("[%u] Connected, %lu bytes free\n", id, NutHeapAvailable());
  107. /*
  108. * Wait until at least 4 kByte of free RAM is available. This will
  109. * keep the client connected in low memory situations.
  110. */
  111. while (NutHeapAvailable() < 4096) {
  112. printf("[%u] Low mem\n", id);
  113. NutSleep(1000);
  114. }
  115. /*
  116. * Associate a stream with the socket so we can use standard I/O calls.
  117. */
  118. if ((stream = _fdopen((int) ((uintptr_t) sock), "r+b")) == 0) {
  119. printf("[%u] Creating stream device failed\n", id);
  120. } else {
  121. /*
  122. * This API call saves us a lot of work. It will parse the
  123. * client's HTTP request, send any requested file from the
  124. * registered file system or handle CGI requests by calling
  125. * our registered CGI routine.
  126. */
  127. NutHttpProcessRequest(stream);
  128. /*
  129. * Destroy the virtual stream device.
  130. */
  131. fclose(stream);
  132. }
  133. /*
  134. * Close our socket.
  135. */
  136. NutTcpCloseSocket(sock);
  137. printf("[%u] Disconnected\n", id);
  138. }
  139. }
  140. /*!
  141. * \brief Main application routine.
  142. *
  143. * Nut/OS automatically calls this entry after initialization.
  144. */
  145. int main(void)
  146. {
  147. uint32_t baud = 115200;
  148. uint8_t i;
  149. /*
  150. * Initialize the uart device.
  151. */
  152. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  153. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  154. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  155. NutSleep(200);
  156. printf("\n\nNut/OS %s HTTP Daemon...\n", NutVersionString());
  157. /*
  158. * Register Ethernet controller.
  159. */
  160. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  161. puts("Registering device failed");
  162. } else {
  163. /*
  164. * Add "All Host" multicast address.
  165. */
  166. printf("Add \"All Host\" multicast address...");
  167. if (NutNetIfAddMcastAddr(DEV_ETHER_NAME, INADDR_ALLHOSTS_GROUP) != 0) {
  168. /*
  169. * It could be possible that your ethernet
  170. * driver does not support multicast.
  171. */
  172. puts("failed");
  173. } else {
  174. puts("OK");
  175. }
  176. }
  177. printf("Configure %s...", DEV_ETHER_NAME);
  178. if (NutNetLoadConfig(DEV_ETHER_NAME)) {
  179. puts("failed");
  180. } else {
  181. if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {
  182. puts("failed");
  183. } else {
  184. puts("OK");
  185. }
  186. }
  187. printf("%s ready\n", inet_ntoa(confnet.cdn_ip_addr));
  188. /*
  189. * Register our device for the file system.
  190. */
  191. NutRegisterDevice(&devUrom, 0, 0);
  192. NutRegisterCgiBinPath("cgi-bin/");
  193. /*
  194. * Start four server threads.
  195. */
  196. for (i = 1; i <= 4; i++) {
  197. char thname[] = "httpd0";
  198. thname[5] = '0' + i;
  199. NutThreadCreate(thname, Service, (void *) (uintptr_t) i, HTTPD_SERVICE_STACK);
  200. }
  201. /*
  202. * Start the UPnP functionality
  203. */
  204. upnp_Init();
  205. /*
  206. * We could do something useful here, like serving a watchdog.
  207. */
  208. NutThreadSetPriority(254);
  209. for (;;) {
  210. NutSleep(60000);
  211. }
  212. return 0;
  213. }
  214. /*** EOF ***/