mod_cgi_func.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include <pro/uhttp/modules/mod_cgi_func.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. static ISC_LIST(HTTP_CGI_FUNCTION) cgiFunctionList = ISC_LIST_INITIAL_TYPE(HTTP_CGI_FUNCTION);
  41. HTTP_CGI_FUNCTION *HttpCgiFunctionLookup(const char *uri)
  42. {
  43. HTTP_CGI_FUNCTION *cgi;
  44. int i;
  45. HTTP_ASSERT(uri != NULL);
  46. for (cgi = ISC_LIST_HEAD(cgiFunctionList); cgi; cgi = ISC_LIST_NEXT(cgi, cgi_link)) {
  47. i = strcasecmp(cgi->cgi_uri, uri);
  48. if (i <= 0) {
  49. if (i) {
  50. cgi = NULL;
  51. }
  52. break;
  53. }
  54. }
  55. return cgi;
  56. }
  57. int HttpRegisterCgiFunction(const char *uri, HTTP_CGI_HANDLER handler)
  58. {
  59. int rc = -1;
  60. HTTP_CGI_FUNCTION *cur = NULL;
  61. int i = -1;
  62. HTTP_ASSERT(uri != NULL);
  63. for (cur = ISC_LIST_HEAD(cgiFunctionList); cur; cur = ISC_LIST_NEXT(cur, cgi_link)) {
  64. i = strcasecmp(cur->cgi_uri, uri);
  65. if (i <= 0) {
  66. break;
  67. }
  68. }
  69. if (i) {
  70. HTTP_CGI_FUNCTION *cgi;
  71. cgi = calloc(1, sizeof(HTTP_CGI_FUNCTION));
  72. if (cgi) {
  73. cgi->cgi_uri = strdup(uri);
  74. if (cgi->cgi_uri) {
  75. cgi->cgi_handler = handler;
  76. if (cur) {
  77. ISC_LIST_INSERTBEFORE(cgiFunctionList, cur, cgi, cgi_link);
  78. } else {
  79. ISC_LIST_APPEND(cgiFunctionList, cgi, cgi_link);
  80. }
  81. rc = 0;
  82. } else {
  83. free(cgi);
  84. }
  85. }
  86. }
  87. else if (handler) {
  88. /* Override registered handler. */
  89. cur->cgi_handler = handler;
  90. }
  91. else {
  92. /* Remove registered handler. */
  93. ISC_LIST_UNLINK_TYPE(cgiFunctionList, cur, cgi_link, HTTP_CGI_FUNCTION);
  94. free(cur->cgi_uri);
  95. free(cur);
  96. }
  97. return rc;
  98. }
  99. int HttpCgiFunctionHandler(HTTPD_SESSION *hs, const MEDIA_TYPE_ENTRY *mt, const char *filepath)
  100. {
  101. HTTP_CGI_FUNCTION *cgi;
  102. const char *pp;
  103. (void)mt;
  104. HTTP_ASSERT(filepath != NULL);
  105. if (strncasecmp(filepath, HTTP_ROOT, strlen(HTTP_ROOT)) == 0) {
  106. pp = filepath + strlen(HTTP_ROOT);
  107. }
  108. else {
  109. pp = filepath + (*filepath == '/');
  110. }
  111. cgi = HttpCgiFunctionLookup(pp);
  112. if (cgi) {
  113. (*cgi->cgi_handler)(hs);
  114. }
  115. return 0;
  116. }