httpd_tiny.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2012 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * $Id$
  36. */
  37. #ifdef NUT_OS
  38. /* Include Nut/OS specific headers. */
  39. #include <sys/version.h>
  40. #include <dev/board.h>
  41. #include <sys/confnet.h>
  42. #include <dev/urom.h>
  43. #include <arpa/inet.h>
  44. #include <pro/dhcp.h>
  45. #endif
  46. #include <pro/uhttp/mediatypes.h>
  47. #include <stdio.h>
  48. /*!
  49. * \example uhttpd_tiny/httpd_tiny.c
  50. *
  51. * Minimalist HTTP server sample.
  52. *
  53. * This is a simple HTTP server based on the MicroHTTP library. It
  54. * should compile and run without modification on any operating system
  55. * that is supported by the MicroHTTP library.
  56. *
  57. * It does not include any redirection. Thus, it is required to specify
  58. * the full URL in the browser request, e.g. 127.0.0.1/index.html.
  59. *
  60. * Only a single server thread is running. Concurrent requests may
  61. * fail.
  62. *
  63. * When used on Nut/OS, make sure, that the macro NUT_OS has been
  64. * defined on the compile command, which can done by adding
  65. * HWDEF += -DNUT_OS
  66. * either in the Makefile or in app/UserConf.mk. It is further required,
  67. * that a valid network configuration exists in non-volatile memory. You
  68. * can run the sample application editconf to check this.
  69. */
  70. /*
  71. * Target specific initialization.
  72. */
  73. static int TargetInit(void)
  74. {
  75. /* On Nut/OS we need to register some drivers, assign stdout to the
  76. * default console and configure the network interface. */
  77. #ifdef NUT_OS
  78. /* Register the default console device. */
  79. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  80. /* Assign the console device to stdout. */
  81. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  82. /* Register the UROM file system. */
  83. printf("\nRegister file system...");
  84. if (NutRegisterDevice(&devUrom, 0, 0)) {
  85. return -1;
  86. }
  87. /* Register the default network device. */
  88. printf("OK\nRegister %s...", DEV_ETHER_NAME);
  89. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  90. return -1;
  91. }
  92. /* Configure the network interface. */
  93. printf("OK\nConfigure %s...", DEV_ETHER_NAME);
  94. if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 60000)) {
  95. return -1;
  96. }
  97. puts(inet_ntoa(confnet.cdn_ip_addr));
  98. #endif
  99. return 0;
  100. }
  101. int main(void)
  102. {
  103. /* Target-specific initialization. */
  104. if (TargetInit()) {
  105. puts("failed");
  106. for (;;);
  107. }
  108. /* Write a banner to our console. */
  109. puts("\nTiny uHTTP sample running");
  110. /* Initialize the TCP socket interface. */
  111. StreamInit();
  112. /* Register media type defaults. These are configurable in
  113. * include/cfg/http.h or the Nut/OS Configurator. */
  114. MediaTypeInitDefaults();
  115. /* Wait for a client (browser) and handle its request. This function
  116. will only return on unrecoverable errors. */
  117. StreamClientAccept(HttpdClientHandler, NULL);
  118. /* Typically this point will be never reached. */
  119. return 0;
  120. }