summaryrefslogtreecommitdiff
path: root/sapi/fhttpd/fhttpd.c
blob: 762251df3d98c92c3ec0d639a48fd474c07b1bf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2001 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: Alex Belits <abelits@phobos.illtel.denver.co.us>             |
   +----------------------------------------------------------------------+
 */

/* $Id$ */
#include "php.h"

#include <stdlib.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#include <errno.h>
#include <ctype.h>

#if FHTTPD
#include <servproc.h>
#include <signal.h>

struct http_server *server = NULL;
struct request *req = NULL;
struct httpresponse *response = NULL;
int headermade = 0;
int global_alarmflag = 0;
int idle_timeout = IDLE_TIMEOUT;
int exit_status = 0;
char **currentheader = NULL;
char *headerfirstline = NULL;
int headerlines = 0;
static int headerlinesallocated = 0;

void alarmhandler(SIGACTARGS)
{
	global_alarmflag = 1;
}

void setalarm(int t)
{
	struct sigaction tmpsigaction;
	global_alarmflag = 0;
	if (t){
		bzero((char *) &tmpsigaction, sizeof(struct sigaction));
		tmpsigaction.sa_handler = alarmhandler;
		sigaddset(&tmpsigaction.sa_mask, SIGALRM);
		tmpsigaction.sa_flags = 0;
		sigaction(SIGALRM, &tmpsigaction, NULL);
		alarm(t);
	}
}

int checkinput(int h)
{
	fd_set readfd;
	FD_ZERO(&readfd);
	FD_SET(h, &readfd);
	return select(h + 1, &readfd, NULL, NULL, NULL) > 0;
}


PHPAPI void php_fhttpd_free_header(void)
{
	int i;

	if (headerfirstline) {
		free(headerfirstline);
		headerfirstline = NULL;
	}
	if (currentheader) {
		for (i = 0; i < headerlines; i++) {
			free(currentheader[i]);
		}
		free(currentheader);
		currentheader = NULL;
	}
	headerlines = 0;
	headerlinesallocated = 0;
	headermade = 0;
}


PHPAPI void php_fhttpd_puts_header(char *s)
{
	char *p0, *p1, *p2, *p3, **p;
	int l;

	if (!s || !*s || *s == '\r' || *s == '\n')
		return;
	l = strlen(s);
	p2 = strchr(s, '\r');
	p3 = strchr(s, '\n');
	p0 = strchr(s, ':');
	p1 = strchr(s, ' ');
	if (p0 && (!p1 || p1 > p0)) {
		if (!headerlinesallocated) {
			currentheader = (char **) malloc(10 * sizeof(char *));
			if (currentheader)
				headerlinesallocated = 10;
		} else {
			if (headerlinesallocated <= headerlines) {
				p = (char **) realloc(currentheader, (headerlinesallocated + 10) * sizeof(char *));
				if (p) {
					currentheader = p;
					headerlinesallocated += 10;
				}
			}
		}

		if (headerlinesallocated > headerlines) {
			currentheader[headerlines] = malloc(l + 3);
			if (currentheader[headerlines]) {
				strcpy(currentheader[headerlines], s);
				if (!p3) {
					if (p2) {
						(currentheader[headerlines] + (p2 - s))[1] = '\n';
						(currentheader[headerlines] + (p2 - s))[2] = 0;
					} else {
						currentheader[headerlines][l] = '\r';
						currentheader[headerlines][l + 1] = '\n';
						currentheader[headerlines][l + 2] = 0;
					}
				}
				headerlines++;
				headermade = 1;
			}
		}
	} else {
		if (headerfirstline)
			free(headerfirstline);
		headerfirstline = malloc(l + 3);
		if (headerfirstline) {
			strcpy(headerfirstline, s);
			if (!p3) {
				if (p2) {
					(headerfirstline + (p2 - s))[1] = '\n';
					(headerfirstline + (p2 - s))[2] = 0;
				} else {
					headerfirstline[l] = '\r';
					headerfirstline[l + 1] = '\n';
					headerfirstline[l + 2] = 0;
				}
			}
		}
		headermade = 1;
	}
}

void fhttpd_flush(void)
{
}

PHPAPI void php_fhttpd_puts(char *s)
{
	putlinetoresponse(response, s);
}

PHPAPI void php_fhttpd_putc(char c)
{
	writetoresponse(response, &c, 1);
}

PHPAPI int php_fhttpd_write(char *a, int n)
{
	return writetoresponse(response, a, n);
}
#endif
/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */