envvars.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #if !defined(HTTPD_EXCLUDE_DATE)
  38. #include <pro/rfctime.h>
  39. #endif
  40. #include <pro/uhttp/streamio.h>
  41. #include <pro/uhttp/utils.h>
  42. #include <pro/uhttp/envvars.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <memdebug.h>
  46. /*@cond */
  47. ISC_LIST(HTTP_ENVVAR_ENTRY) envVarList = ISC_LIST_INITIAL_TYPE(HTTP_ENVVAR_ENTRY);
  48. /*@endcond */
  49. const char* HttpSessionInfo(HTTPD_SESSION *hs, int item)
  50. {
  51. static char *env_value;
  52. const char *vp = NULL;
  53. #if !defined(HTTPD_EXCLUDE_DATE)
  54. time_t now;
  55. #endif
  56. switch (item) {
  57. #if !defined(HTTPD_EXCLUDE_DATE)
  58. case HSITEM_DATE_GMT:
  59. time(&now);
  60. vp = Rfc1123TimeString(gmtime(&now));
  61. break;
  62. case HSITEM_DATE_LOCAL:
  63. time(&now);
  64. vp = Rfc1123TimeString(localtime(&now));
  65. break;
  66. #endif
  67. case HSITEM_DOCUMENT_NAME:
  68. vp = strrchr(hs->s_req.req_url, '/');
  69. if (vp) {
  70. vp++;
  71. } else {
  72. vp = hs->s_req.req_url;
  73. }
  74. break;
  75. case HSITEM_DOCUMENT_ROOT:
  76. vp = HTTP_ROOT;
  77. break;
  78. #if HTTP_VERSION >= 0x10
  79. case HSITEM_HTTP_ACCEPT_ENCODING:
  80. vp = hs->s_req.req_encoding;
  81. break;
  82. case HSITEM_HTTP_CONNECTION:
  83. if (hs->s_req.req_connection == HTTP_CONN_KEEP_ALIVE) {
  84. vp = ct_Keep_Alive;
  85. } else {
  86. vp = ct_close;
  87. }
  88. break;
  89. case HSITEM_HTTP_COOKIE:
  90. break;
  91. case HSITEM_HTTP_HOST:
  92. vp = hs->s_req.req_host;
  93. break;
  94. case HSITEM_HTTP_REFERER:
  95. vp = hs->s_req.req_referer;
  96. break;
  97. case HSITEM_HTTP_USER_AGENT:
  98. vp = hs->s_req.req_agent;
  99. break;
  100. #endif
  101. case HSITEM_QUERY_STRING:
  102. vp = hs->s_req.req_query;
  103. break;
  104. case HSITEM_QUERY_STRING_UNESCAPED:
  105. vp = hs->s_req.req_query;
  106. break;
  107. case HSITEM_REQUEST_METHOD:
  108. if (hs->s_req.req_method == HTTP_METHOD_GET) {
  109. vp = ct_GET;
  110. }
  111. #if HTTP_VERSION >= 0x10
  112. else if (hs->s_req.req_method == HTTP_METHOD_HEAD) {
  113. vp = ct_HEAD;
  114. }
  115. else if (hs->s_req.req_method == HTTP_METHOD_POST) {
  116. vp = ct_POST;
  117. }
  118. #endif
  119. break;
  120. case HSITEM_REQUEST_URI:
  121. vp = hs->s_req.req_url;
  122. break;
  123. }
  124. if (vp == NULL) {
  125. vp = "";
  126. }
  127. free(env_value);
  128. env_value = strdup(vp);
  129. if (item == HSITEM_QUERY_STRING) {
  130. HttpUrlUnescape(env_value);
  131. }
  132. return env_value;
  133. }
  134. const char* HttpStreamInfo(HTTPD_SESSION *hs, int item)
  135. {
  136. return StreamInfo(hs->s_stream, item);
  137. }
  138. const char* EnvHandler(HTTPD_SESSION *hs, const char *name)
  139. {
  140. static const char empty;
  141. HTTP_ENVVAR_ENTRY *env;
  142. const char *rp = &empty;
  143. int i;
  144. /* Locate the entry with the given name. */
  145. for (env = ISC_LIST_HEAD(envVarList); env; env = ISC_LIST_NEXT(env, env_link)) {
  146. i = strcasecmp(env->env_name, name);
  147. if (i <= 0) {
  148. if (i) {
  149. env = NULL;
  150. }
  151. break;
  152. }
  153. }
  154. if (env) {
  155. /* Found an entry, call the registered handler. */
  156. rp = (* env->env_handler)(hs, env->env_index < 0 ? -env->env_index : env->env_index);
  157. }
  158. return rp;
  159. }