httpd_ssi.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <dev/board.h>
  40. #include <dev/urom.h>
  41. #include <pro/dhcp.h>
  42. #endif
  43. #include <isc/list.h>
  44. #include <pro/uhttp/mediatypes.h>
  45. #include <pro/uhttp/envvars.h>
  46. #include <pro/uhttp/modules/mod_redir.h>
  47. #include <pro/uhttp/modules/mod_cgi_func.h>
  48. #include <pro/uhttp/modules/mod_ssi.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51. #define MAJOR_VERSION 1
  52. #define MINOR_VERSION 0
  53. enum appvar_t {
  54. APPVAR_VERSION = 1,
  55. APPVAR_NAME
  56. };
  57. typedef struct _APPVAR {
  58. char *var_name;
  59. enum appvar_t var_index;
  60. } APPVAR;
  61. APPVAR appvar_list[] = {
  62. { "APPNAME", APPVAR_NAME },
  63. { "APPVERSION", APPVAR_VERSION }
  64. };
  65. #define APPVAR_NUM (sizeof(appvar_list) / sizeof(APPVAR))
  66. static const char *AppVarHandler(HTTPD_SESSION *hs, int item)
  67. {
  68. static char value[128];
  69. value[0] = '\0';
  70. switch (item) {
  71. case APPVAR_NAME:
  72. strcpy(value, "SSI Sample");
  73. break;
  74. case APPVAR_VERSION:
  75. sprintf(value, "%d.%d", MAJOR_VERSION, MINOR_VERSION);
  76. break;
  77. }
  78. return value;
  79. }
  80. static void AppVarInit(APPVAR *list)
  81. {
  82. int i;
  83. for (i = 0; i < APPVAR_NUM; i++) {
  84. EnvRegisterVariable(list[i].var_name, AppVarHandler, list[i].var_index);
  85. }
  86. }
  87. static int CgiVarList(HTTPD_SESSION *hs)
  88. {
  89. extern ISC_LIST(HTTP_ENVVAR_ENTRY) envVarList;
  90. HTTP_ENVVAR_ENTRY *env;
  91. /* Locate the entry with the given name. */
  92. for (env = ISC_LIST_HEAD(envVarList); env; env = ISC_LIST_NEXT(env, env_link)) {
  93. s_printf(hs->s_stream, "<option>%s</option>\r\n", env->env_name);
  94. }
  95. return 0;
  96. }
  97. int main(void)
  98. {
  99. #ifdef NUT_OS
  100. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  101. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  102. #endif
  103. puts("uHTTP server side include sample\nBuild " __DATE__ " " __TIME__);
  104. #ifdef NUT_OS
  105. NutRegisterDevice(&DEV_ETHER, 0, 0);
  106. NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 60000);
  107. NutRegisterDevice(&devUrom, 0, 0);
  108. #endif
  109. StreamInit();
  110. MediaTypeInitDefaults();
  111. HttpRegisterRedir("", "/index.shtml", 301);
  112. HttpRegisterCgiFunction("varlist.cgi", CgiVarList);
  113. HttpRegisterMediaType("cgi", NULL, NULL, HttpCgiFunctionHandler);
  114. EnvInitDefaults();
  115. AppVarInit(appvar_list);
  116. HttpRegisterSsiVarHandler(EnvHandler);
  117. StreamClientAccept(HttpdClientHandler, NULL);
  118. puts("Exit");
  119. #ifdef NUT_OS
  120. for (;;) ;
  121. #endif
  122. return 0;
  123. }