httpd_ajax.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <sys/version.h>
  39. #include <sys/timer.h>
  40. #include <dev/board.h>
  41. #include <dev/urom.h>
  42. #include <pro/dhcp.h>
  43. #define tm _tm
  44. #endif
  45. #include <pro/uhttp/mediatypes.h>
  46. #include <pro/uhttp/modules/mod_redir.h>
  47. #include <pro/uhttp/modules/mod_cgi_func.h>
  48. #include <stdio.h>
  49. #include <time.h>
  50. static int CgiClock(HTTPD_SESSION *hs)
  51. {
  52. time_t now;
  53. const struct tm *ltm;
  54. #if defined(NUT_OS)
  55. NutSleep(1000);
  56. #elif defined(_WIN32)
  57. Sleep(1000);
  58. #else
  59. #warning Unknown OS, sleep missing
  60. #endif
  61. HttpSendHeaderTop(hs, 200);
  62. s_puts("Cache-Control: no-cache, must-revalidate\r\n", hs->s_stream);
  63. s_puts("Content-Type: text/html; charset=iso-8859-1\r\n", hs->s_stream);
  64. s_puts("Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n", hs->s_stream);
  65. HttpSendHeaderBottom(hs, "text", "html", -1);
  66. time(&now);
  67. ltm = localtime(&now);
  68. s_printf(hs->s_stream, "{\"time\": \"%02d:%02d:%02d\"}\r\n", ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
  69. s_flush(hs->s_stream);
  70. return 0;
  71. }
  72. int main(void)
  73. {
  74. #ifdef NUT_OS
  75. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  76. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  77. #endif
  78. puts("uHTTP AJAX sample\nBuild " __DATE__ " " __TIME__);
  79. #ifdef NUT_OS
  80. NutRegisterDevice(&DEV_ETHER, 0, 0);
  81. NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 60000);
  82. NutRegisterDevice(&devUrom, 0, 0);
  83. #endif
  84. StreamInit();
  85. MediaTypeInitDefaults();
  86. HttpRegisterRedir("", "/index.html", 301);
  87. HttpRegisterCgiFunction("clock.cgi", CgiClock);
  88. HttpRegisterMediaType("cgi", NULL, NULL, HttpCgiFunctionHandler);
  89. StreamClientAccept(HttpdClientHandler, NULL);
  90. puts("Exit");
  91. #ifdef NUT_OS
  92. for (;;) ;
  93. #endif
  94. return 0;
  95. }