You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.3 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP HTML Embedded Scripting Language Version 3.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
  6. +----------------------------------------------------------------------+
  7. | This program is free software; you can redistribute it and/or modify |
  8. | it under the terms of one of the following licenses: |
  9. | |
  10. | A) the GNU General Public License as published by the Free Software |
  11. | Foundation; either version 2 of the License, or (at your option) |
  12. | any later version. |
  13. | |
  14. | B) the PHP License as published by the PHP Development Team and |
  15. | included in the distribution in the file: LICENSE |
  16. | |
  17. | This program is distributed in the hope that it will be useful, |
  18. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  19. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  20. | GNU General Public License for more details. |
  21. | |
  22. | You should have received a copy of both licenses referred to here. |
  23. | If you did not, or have any questions about PHP licensing, please |
  24. | contact core@php.net. |
  25. +----------------------------------------------------------------------+
  26. | Author: Alex Belits <abelits@phobos.illtel.denver.co.us> |
  27. +----------------------------------------------------------------------+
  28. */
  29. /* $Id$ */
  30. #ifdef THREAD_SAFE
  31. #include "tls.h"
  32. #endif
  33. #include "php.h"
  34. #include <stdlib.h>
  35. #if HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #include <string.h>
  39. #include <errno.h>
  40. #include <ctype.h>
  41. #if FHTTPD
  42. #include <servproc.h>
  43. #include <signal.h>
  44. struct http_server *server = NULL;
  45. struct request *req = NULL;
  46. struct httpresponse *response = NULL;
  47. int headermade = 0;
  48. int global_alarmflag = 0;
  49. int idle_timeout = IDLE_TIMEOUT;
  50. int exit_status = 0;
  51. char **currentheader = NULL;
  52. char *headerfirstline = NULL;
  53. int headerlines = 0;
  54. static int headerlinesallocated = 0;
  55. void alarmhandler(SIGACTARGS)
  56. {
  57. global_alarmflag = 1;
  58. }
  59. void setalarm(int t)
  60. {
  61. struct sigaction tmpsigaction;
  62. global_alarmflag = 0;
  63. if (t){
  64. bzero((char *) &tmpsigaction, sizeof(struct sigaction));
  65. tmpsigaction.sa_handler = alarmhandler;
  66. sigaddset(&tmpsigaction.sa_mask, SIGALRM);
  67. tmpsigaction.sa_flags = 0;
  68. sigaction(SIGALRM, &tmpsigaction, NULL);
  69. alarm(t);
  70. }
  71. }
  72. int checkinput(int h)
  73. {
  74. fd_set readfd;
  75. FD_ZERO(&readfd);
  76. FD_SET(h, &readfd);
  77. return select(h + 1, &readfd, NULL, NULL, NULL) > 0;
  78. }
  79. PHPAPI void php3_fhttpd_free_header(void)
  80. {
  81. int i;
  82. if (headerfirstline) {
  83. free(headerfirstline);
  84. headerfirstline = NULL;
  85. }
  86. if (currentheader) {
  87. for (i = 0; i < headerlines; i++) {
  88. free(currentheader[i]);
  89. }
  90. free(currentheader);
  91. currentheader = NULL;
  92. }
  93. headerlines = 0;
  94. headerlinesallocated = 0;
  95. headermade = 0;
  96. }
  97. PHPAPI void php3_fhttpd_puts_header(char *s)
  98. {
  99. char *p0, *p1, *p2, *p3, **p;
  100. int l;
  101. if (!s || !*s || *s == '\r' || *s == '\n')
  102. return;
  103. l = strlen(s);
  104. p2 = strchr(s, '\r');
  105. p3 = strchr(s, '\n');
  106. p0 = strchr(s, ':');
  107. p1 = strchr(s, ' ');
  108. if (p0 && (!p1 || p1 > p0)) {
  109. if (!headerlinesallocated) {
  110. currentheader = (char **) malloc(10 * sizeof(char *));
  111. if (currentheader)
  112. headerlinesallocated = 10;
  113. } else {
  114. if (headerlinesallocated <= headerlines) {
  115. p = (char **) realloc(currentheader, (headerlinesallocated + 10) * sizeof(char *));
  116. if (p) {
  117. currentheader = p;
  118. headerlinesallocated += 10;
  119. }
  120. }
  121. }
  122. if (headerlinesallocated > headerlines) {
  123. currentheader[headerlines] = malloc(l + 3);
  124. if (currentheader[headerlines]) {
  125. strcpy(currentheader[headerlines], s);
  126. if (!p3) {
  127. if (p2) {
  128. (currentheader[headerlines] + (p2 - s))[1] = '\n';
  129. (currentheader[headerlines] + (p2 - s))[2] = 0;
  130. } else {
  131. currentheader[headerlines][l] = '\r';
  132. currentheader[headerlines][l + 1] = '\n';
  133. currentheader[headerlines][l + 2] = 0;
  134. }
  135. }
  136. headerlines++;
  137. headermade = 1;
  138. }
  139. }
  140. } else {
  141. if (headerfirstline)
  142. free(headerfirstline);
  143. headerfirstline = malloc(l + 3);
  144. if (headerfirstline) {
  145. strcpy(headerfirstline, s);
  146. if (!p3) {
  147. if (p2) {
  148. (headerfirstline + (p2 - s))[1] = '\n';
  149. (headerfirstline + (p2 - s))[2] = 0;
  150. } else {
  151. headerfirstline[l] = '\r';
  152. headerfirstline[l + 1] = '\n';
  153. headerfirstline[l + 2] = 0;
  154. }
  155. }
  156. }
  157. headermade = 1;
  158. }
  159. }
  160. void fhttpd_flush( /*GLOBAL(php3_rqst)->connection */ void)
  161. {
  162. }
  163. PHPAPI void php3_fhttpd_puts(char *s)
  164. {
  165. putlinetoresponse(response, s);
  166. }
  167. PHPAPI void php3_fhttpd_putc(char c)
  168. {
  169. writetoresponse(response, &c, 1);
  170. }
  171. PHPAPI int php3_fhttpd_write(char *a, int n)
  172. {
  173. return writetoresponse(response, a, n);
  174. }
  175. #endif
  176. /*
  177. * Local variables:
  178. * tab-width: 4
  179. * c-basic-offset: 4
  180. * End:
  181. */