summaryrefslogtreecommitdiff
path: root/request_info.c
blob: 330599f1dabfdf52b0e3e155766e5d84b099e0cb (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
   +----------------------------------------------------------------------+
   | PHP HTML Embedded Scripting Language Version 3.0                     |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997,1998 PHP Development Team (See Credits file)      |
   +----------------------------------------------------------------------+
   | This program is free software; you can redistribute it and/or modify |
   | it under the terms of one of the following licenses:                 |
   |                                                                      |
   |  A) the GNU General Public License as published by the Free Software |
   |     Foundation; either version 2 of the License, or (at your option) |
   |     any later version.                                               |
   |                                                                      |
   |  B) the PHP License as published by the PHP Development Team and     |
   |     included in the distribution in the file: LICENSE                |
   |                                                                      |
   | This program is distributed in the hope that it will be useful,      |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of both licenses referred to here.   |
   | If you did not, or have any questions about PHP licensing, please    |
   | contact core@php.net.                                                |
   +----------------------------------------------------------------------+
   | Author: Jim Winstead (jimw@php.net)                                  |
   +----------------------------------------------------------------------+
 */

#include "php.h"

#ifndef THREAD_SAFE
php3_request_info request_info;
#endif

#if CGI_BINARY
int php3_init_request_info(void *conf)
{
	char *buf;	/* temporary buffers */
	TLS_VARS;

	GLOBAL(request_info).path_info = getenv("PATH_INFO");
	GLOBAL(request_info).path_translated = getenv("PATH_TRANSLATED");
	GLOBAL(request_info).query_string = getenv("QUERY_STRING");
	GLOBAL(request_info).current_user = NULL;
	GLOBAL(request_info).current_user_length = 0;
	GLOBAL(request_info).request_method = getenv("REQUEST_METHOD");
	GLOBAL(request_info).script_name = getenv("SCRIPT_NAME");
	buf = getenv("CONTENT_LENGTH");
	GLOBAL(request_info).content_length = (buf ? atoi(buf) : 0);
	GLOBAL(request_info).content_type = getenv("CONTENT_TYPE");
	GLOBAL(request_info).cookies = getenv("HTTP_COOKIE");
	GLOBAL(request_info).script_filename = getenv("SCRIPT_FILENAME");
	/* Hack for annoying servers that do not set SCRIPT_FILENAME for us */
	if (!GLOBAL(request_info).script_filename) {
		GLOBAL(request_info).script_filename = GLOBAL(request_info).php_argv0;
	}
#if WIN32|WINNT
	/* FIXME WHEN APACHE NT IS FIXED */
	/* a hack for apache nt because it does not appear to set argv[1] and sets
	   script filename to php.exe thus makes us parse php.exe instead of file.php
	   requires we get the info from path translated.  This can be removed at
	   such a time taht apache nt is fixed */
	else if (GLOBAL(request_info).path_translated) {
		GLOBAL(request_info).script_filename = GLOBAL(request_info).path_translated;
	} else {
		GLOBAL(request_info).script_filename = NULL;
	}
#endif

	/* doc_root configuration variable is currently ignored,
	   as it is with every other access method currently also. */

	/* We always need to emalloc() filename, since it gets placed into
	   the include file hash table, and gets freed with that table.
	   Notice that this means that we don't need to efree() it in
	   php3_destroy_request_info()! */
#if DISCARD_PATH
	if (GLOBAL(request_info).script_filename) {
		GLOBAL(request_info).filename = estrdup(GLOBAL(request_info).script_filename);
	} else {
		GLOBAL(request_info).filename = NULL;
	}
#else
	if (GLOBAL(request_info).path_translated) {
		GLOBAL(request_info).filename = estrdup(GLOBAL(request_info).path_translated);
	} else {
		GLOBAL(request_info).filename = NULL;
	}
#endif
	return SUCCESS;
}

int php3_destroy_request_info(void *conf)
{
	STR_FREE(GLOBAL(request_info).current_user);
	return SUCCESS;
}
#endif

#if FHTTPD
char script_name_resolved_buffer[2048];
const char *method_names[] =
{"Unknown", "GET", "HEAD", "POST", "PUT"};

int php3_init_request_info(void *conf)
{
	static int exit_requested = 0;
	int i, len;
	TLS_VARS;
	req = NULL;

	setalarm(idle_timeout);
	while (checkinput(server->infd) && (req = readrequest(server))) {
		alarm(0);
		if (req->reqtype == FHTTPD_REQUEST) {
			if (headermade)
				php3_fhttpd_free_header();
			response = createresponse(1024, req->id, req->fd, req->ver_major > 0);
			if (response) {
				if (req->script_name && req->script_name_resolved) {
					len = strlen(req->script_name);
					strncpy(script_name_resolved_buffer, req->script_name_resolved, 2047);
					script_name_resolved_buffer[2047] = 0;

					GLOBAL(request_info).path_info = NULL;	/* Not supported */
					GLOBAL(request_info).path_translated = script_name_resolved_buffer;
					GLOBAL(request_info).query_string = req->query_string;
					GLOBAL(request_info).current_user = NULL;
					GLOBAL(request_info).current_user_length = 0;
					GLOBAL(request_info).request_method = method_names[req->method];
					GLOBAL(request_info).script_name = req->script_name;
					GLOBAL(request_info).content_length = req->databuffsize;
					GLOBAL(request_info).content_type = req->content_type;
					GLOBAL(request_info).cookies = NULL;
					for (i = 0; i < req->nlines; i++) {
						if (req->lines[i].paramc > 1) {
							if (req->lines[i].params[0]) {
								if (!strcasecmp(req->lines[i].params[0], "HTTP_COOKIE")) {
									if (req->lines[i].params[1]) {
										GLOBAL(request_info).cookies = req->lines[i].params[1];
									}
								}
							}
						}
					}
					/* doc_root configuration variable is currently ignored,
					   as it is with every other access method currently also. */

					/* We always need to emalloc() filename, since it gets placed into
					   the include file hash table, and gets freed with that table.
					   Notice that this means that we don't need to efree() it in
					   php3_destroy_request_info()! */
					if (GLOBAL(request_info).path_translated)
						GLOBAL(request_info).filename = estrdup(GLOBAL(request_info).path_translated);
					else
						GLOBAL(request_info).filename = NULL;

					return SUCCESS;
				} else {
					deleterequest(req);
					req = NULL;
					setalarm(idle_timeout);
				}
			} else {
				deleterequest(req);
				req = NULL;
				setalarm(idle_timeout);
			}
		} else {
			if (req->reqtype == FHTTPD_EXITOK) {
				exit_status = 1;
				deleterequest(req);
				req = NULL;
				setalarm(idle_timeout);
				return FAILURE;
			}
			deleterequest(req);
			req = NULL;
			setalarm(idle_timeout);
		}
	}
	if (global_alarmflag) {
		if (!exit_requested) {
			requestexit(server, 1);
			exit_requested = 1;
		}
	} else {
	exit_status = 1; 
	}
	return FAILURE;
}
#endif							/* FHTTPD */

#if APACHE
int php3_init_request_info(void *conf)
{
	const char *buf;
	TLS_VARS;

	GLOBAL(request_info).current_user = NULL;
	GLOBAL(request_info).current_user_length = 0;

	GLOBAL(request_info).filename = GLOBAL(php3_rqst)->filename;
	GLOBAL(request_info).request_method = GLOBAL(php3_rqst)->method;
	GLOBAL(request_info).query_string = GLOBAL(php3_rqst)->args;
	GLOBAL(request_info).content_type = table_get(GLOBAL(php3_rqst)->subprocess_env, "CONTENT_TYPE");

	buf = table_get(GLOBAL(php3_rqst)->subprocess_env, "CONTENT_LENGTH");
	GLOBAL(request_info).content_length = (buf ? atoi(buf) : 0);

	GLOBAL(request_info).cookies = table_get(GLOBAL(php3_rqst)->subprocess_env, "HTTP_COOKIE");

	return SUCCESS;
}

#endif

#if USE_SAPI
/* temporary until I figure a beter way to do it */
int php3_init_request_info(void *conf)
{
	TLS_VARS;
	if (GLOBAL(sapi_rqst)->filename)
		GLOBAL(request_info).filename = estrdup(GLOBAL(sapi_rqst)->filename);
	else
		GLOBAL(request_info).filename = NULL;
	GLOBAL(request_info).path_info = GLOBAL(sapi_rqst)->path_info;
	GLOBAL(request_info).path_translated = GLOBAL(sapi_rqst)->path_translated;
	GLOBAL(request_info).query_string = GLOBAL(sapi_rqst)->query_string;
	GLOBAL(request_info).current_user = GLOBAL(sapi_rqst)->current_user;
	GLOBAL(request_info).current_user_length = GLOBAL(sapi_rqst)->current_user_length;
	GLOBAL(request_info).request_method = GLOBAL(sapi_rqst)->request_method;
	GLOBAL(request_info).script_name = GLOBAL(sapi_rqst)->script_name;
	GLOBAL(request_info).content_length = GLOBAL(sapi_rqst)->content_length;
	GLOBAL(request_info).content_type = GLOBAL(sapi_rqst)->content_type;
	GLOBAL(request_info).cookies = GLOBAL(sapi_rqst)->cookies;

	return SUCCESS;
}
#endif

#if !CGI_BINARY
int php3_destroy_request_info(void *conf)
{
	/* see above for why we don't want to efree() request_info.filename */
	return SUCCESS;
}
#endif
/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */