httpd_simple.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2009-2011 by egnite GmbH
  3. * Copyright (C) 2001-2004 by egnite Software 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. * $Id$
  37. */
  38. /*!
  39. * \example httpd_simple/httpd_simple.c
  40. *
  41. * Simple Webserver
  42. *
  43. */
  44. #include <dev/board.h>
  45. #include <dev/urom.h>
  46. #include <sys/version.h>
  47. #include <sys/timer.h>
  48. #include <sys/confnet.h>
  49. #include <sys/socket.h>
  50. #include <arpa/inet.h>
  51. #include <pro/dhcp.h>
  52. #include <pro/httpd.h>
  53. #include <io.h>
  54. #include <errno.h>
  55. /*!
  56. * \brief Main application routine.
  57. *
  58. * Nut/OS automatically calls this entry after initialization.
  59. */
  60. int main(void)
  61. {
  62. uint32_t baud = 115200;
  63. TCPSOCKET *sock;
  64. FILE *stream;
  65. /*
  66. * Initialize the console.
  67. */
  68. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  69. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  70. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  71. printf("\n\nSimple HTTP Daemon running on Nut/OS %s\n",
  72. NutVersionString());
  73. /*
  74. * Initialize the network interface.
  75. */
  76. printf("Configure %s...", DEV_ETHER_NAME);
  77. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  78. puts("failed. Cannot register Ethernet device.");
  79. for (;;);
  80. }
  81. if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 60000)) {
  82. puts("failed. Cannot configure network.\nUse editconf.");
  83. for (;;);
  84. }
  85. printf("%s ready\n", inet_ntoa(confnet.cdn_ip_addr));
  86. /*
  87. * Initialize the file system.
  88. */
  89. printf("Register UROM file system...");
  90. if (NutRegisterDevice(&devUrom, 0, 0)) {
  91. puts("failed.");
  92. for (;;);
  93. }
  94. puts("OK.");
  95. /*
  96. * Now loop endless for connections.
  97. */
  98. for (;;) {
  99. /* Create a socket. */
  100. sock = NutTcpCreateSocket();
  101. if (sock == NULL) {
  102. printf("Error %d creating socket.\n", errno);
  103. NutSleep(1000);
  104. continue;
  105. }
  106. /* Listen on port 80. NutTcpAccept() will block
  107. until we get a connection from a client. */
  108. printf("Listening...");
  109. NutTcpAccept(sock, 80);
  110. printf("Connected...");
  111. /* Associate a binary stdio stream with the socket. */
  112. stream = _fdopen((int) ((uintptr_t) sock), "r+b");
  113. if (stream == NULL) {
  114. printf("Error %d creating stream.\n", errno);
  115. } else {
  116. /* This API call saves us a lot of work. It will parse
  117. the client's HTTP request, send the requested file. */
  118. NutHttpProcessRequest(stream);
  119. /* Destroy the associated stream. */
  120. fclose(stream);
  121. }
  122. /* Close the socket. */
  123. NutTcpCloseSocket(sock);
  124. puts("Disconnected");
  125. }
  126. return 0;
  127. }