1999-12-20 15:11:14 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP version 4.0 |
|
|
|
|
+----------------------------------------------------------------------+
|
2000-01-01 09:32:05 +08:00
|
|
|
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
|
1999-12-20 15:11:14 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-05-18 23:34:45 +08:00
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
1999-12-20 15:11:14 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
2000-05-18 23:34:45 +08:00
|
|
|
| http://www.php.net/license/2_02.txt. |
|
1999-12-20 15:11:14 +08:00
|
|
|
| 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: Sascha Schumann <sascha@schumann.cx> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "SAPI.h"
|
2000-06-06 07:24:42 +08:00
|
|
|
#include "php_main.h"
|
1999-12-20 15:11:14 +08:00
|
|
|
#include "php_thttpd.h"
|
2000-08-25 16:46:32 +08:00
|
|
|
#include "php_variables.h"
|
1999-12-20 15:11:14 +08:00
|
|
|
#include "version.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
httpd_conn *hc;
|
|
|
|
int post_off;
|
2000-11-13 21:23:56 +08:00
|
|
|
void (*on_close)(int);
|
1999-12-20 15:11:14 +08:00
|
|
|
} php_thttpd_globals;
|
|
|
|
|
|
|
|
static php_thttpd_globals thttpd_globals;
|
|
|
|
|
|
|
|
#define TLS_D
|
|
|
|
#define TLS_DC
|
|
|
|
#define TLS_C
|
|
|
|
#define TLS_CC
|
|
|
|
#define TG(v) (thttpd_globals.v)
|
|
|
|
#define TLS_FETCH()
|
|
|
|
|
|
|
|
static int sapi_thttpd_ub_write(const char *str, uint str_length)
|
|
|
|
{
|
2000-10-30 00:01:02 +08:00
|
|
|
int n;
|
2001-01-10 21:51:58 +08:00
|
|
|
uint sent = 0;
|
1999-12-20 15:11:14 +08:00
|
|
|
TLS_FETCH();
|
|
|
|
|
2001-01-10 21:51:58 +08:00
|
|
|
while (str_length > 0) {
|
|
|
|
n = send(TG(hc)->conn_fd, str, str_length, 0);
|
2000-10-30 00:01:02 +08:00
|
|
|
|
2001-01-10 21:51:58 +08:00
|
|
|
if (n == -1 && errno == EPIPE)
|
|
|
|
php_handle_aborted_connection();
|
|
|
|
if (n == -1 && errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
if (n <= 0)
|
|
|
|
return n;
|
2000-10-30 00:01:02 +08:00
|
|
|
|
2001-01-10 21:51:58 +08:00
|
|
|
TG(hc)->bytes += n;
|
|
|
|
str += n;
|
|
|
|
sent += n;
|
|
|
|
str_length -= n;
|
|
|
|
}
|
2001-01-09 16:45:05 +08:00
|
|
|
|
2001-01-10 21:51:58 +08:00
|
|
|
return sent;
|
1999-12-20 15:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sapi_thttpd_send_headers(sapi_headers_struct *sapi_headers SLS_DC)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
if (!SG(sapi_headers).http_status_line) {
|
2001-01-09 16:45:05 +08:00
|
|
|
size_t len;
|
|
|
|
|
1999-12-20 15:11:14 +08:00
|
|
|
snprintf(buf, 1023, "HTTP/1.0 %d Something\r\n", SG(sapi_headers).http_response_code);
|
2001-01-09 16:45:05 +08:00
|
|
|
len = strlen(buf);
|
|
|
|
send(TG(hc)->conn_fd, buf, len, 0);
|
2001-01-09 17:01:33 +08:00
|
|
|
TG(hc)->status = SG(sapi_headers).http_response_code;
|
2001-01-09 16:45:05 +08:00
|
|
|
TG(hc)->bytes += len;
|
1999-12-20 15:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return SAPI_HEADER_DO_SEND;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sapi_thttpd_send_header(sapi_header_struct *sapi_header, void *server_context)
|
|
|
|
{
|
2001-02-19 03:03:36 +08:00
|
|
|
struct iovec vec[2];
|
|
|
|
int n = 0;
|
1999-12-20 15:11:14 +08:00
|
|
|
TLS_FETCH();
|
|
|
|
|
2001-01-09 16:45:05 +08:00
|
|
|
if (sapi_header) {
|
2001-02-19 03:03:36 +08:00
|
|
|
vec[n].iov_base = sapi_header->header;
|
|
|
|
vec[n++].iov_len = sapi_header->header_len;
|
2001-01-09 16:45:05 +08:00
|
|
|
TG(hc)->bytes += sapi_header->header_len;
|
|
|
|
}
|
2001-02-19 03:03:36 +08:00
|
|
|
vec[n].iov_base = "\r\n";
|
|
|
|
vec[n++].iov_len = 2;
|
2001-01-09 16:45:05 +08:00
|
|
|
TG(hc)->bytes += 2;
|
2001-02-19 03:03:36 +08:00
|
|
|
|
|
|
|
writev(TG(hc)->conn_fd, vec, n);
|
1999-12-20 15:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sapi_thttpd_read_post(char *buffer, uint count_bytes SLS_DC)
|
|
|
|
{
|
|
|
|
size_t read_bytes = 0, tmp;
|
|
|
|
int c;
|
|
|
|
TLS_FETCH();
|
|
|
|
|
|
|
|
/* to understand this, read cgi_interpose_input() in libhttpd.c */
|
|
|
|
c = TG(hc)->read_idx - TG(hc)->checked_idx;
|
|
|
|
if (c > 0) {
|
|
|
|
read_bytes = MIN(c, count_bytes);
|
|
|
|
memcpy(buffer, TG(hc)->read_buf + TG(hc)->checked_idx, read_bytes);
|
|
|
|
TG(hc)->checked_idx += read_bytes;
|
|
|
|
count_bytes -= read_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
count_bytes = MIN(count_bytes,
|
|
|
|
SG(request_info).content_length - SG(read_post_bytes) - TG(post_off));
|
|
|
|
|
|
|
|
while (read_bytes < count_bytes) {
|
|
|
|
tmp = recv(TG(hc)->conn_fd, buffer + read_bytes,
|
|
|
|
count_bytes - read_bytes, 0);
|
|
|
|
if (tmp <= 0)
|
|
|
|
break;
|
|
|
|
read_bytes += tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return read_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *sapi_thttpd_read_cookies(SLS_D)
|
|
|
|
{
|
|
|
|
TLS_FETCH();
|
|
|
|
|
|
|
|
return TG(hc)->cookie;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BUF_SIZE 512
|
|
|
|
#define ADD_STRING(name) \
|
2000-08-25 17:15:00 +08:00
|
|
|
php_register_variable(name, buf, track_vars_array ELS_CC PLS_CC)
|
2000-08-25 16:46:32 +08:00
|
|
|
|
|
|
|
static void sapi_thttpd_register_variables(zval *track_vars_array ELS_DC SLS_DC PLS_DC)
|
1999-12-20 15:11:14 +08:00
|
|
|
{
|
|
|
|
char buf[BUF_SIZE + 1];
|
2000-08-25 16:46:32 +08:00
|
|
|
TLS_FETCH();
|
|
|
|
|
2000-08-25 18:00:48 +08:00
|
|
|
php_register_variable("PHP_SELF", SG(request_info).request_uri, track_vars_array ELS_CC PLS_CC);
|
2000-08-25 17:15:00 +08:00
|
|
|
php_register_variable("SERVER_SOFTWARE", SERVER_SOFTWARE, track_vars_array ELS_CC PLS_CC);
|
|
|
|
php_register_variable("GATEWAY_INTERFACE", "CGI/1.1", track_vars_array ELS_CC PLS_CC);
|
2001-01-09 16:45:05 +08:00
|
|
|
php_register_variable("REQUEST_METHOD", (char *) SG(request_info).request_method, track_vars_array ELS_CC PLS_CC);
|
2000-08-25 17:15:00 +08:00
|
|
|
php_register_variable("REQUEST_URI", SG(request_info).request_uri, track_vars_array ELS_CC PLS_CC);
|
|
|
|
php_register_variable("PATH_TRANSLATED", SG(request_info).path_translated, track_vars_array ELS_CC PLS_CC);
|
1999-12-20 15:11:14 +08:00
|
|
|
|
|
|
|
buf[BUF_SIZE] = '\0';
|
|
|
|
|
2000-08-25 16:46:32 +08:00
|
|
|
strncpy(buf, inet_ntoa(TG(hc)->client_addr.sa_in.sin_addr), BUF_SIZE);
|
|
|
|
ADD_STRING("REMOTE_ADDR");
|
|
|
|
ADD_STRING("REMOTE_HOST");
|
1999-12-20 15:11:14 +08:00
|
|
|
|
|
|
|
snprintf(buf, BUF_SIZE, "%d", TG(hc)->hs->port);
|
|
|
|
ADD_STRING("SERVER_PORT");
|
|
|
|
|
|
|
|
snprintf(buf, BUF_SIZE, "/%s", TG(hc)->pathinfo);
|
|
|
|
ADD_STRING("PATH_INFO");
|
|
|
|
|
|
|
|
snprintf(buf, BUF_SIZE, "/%s", TG(hc)->origfilename);
|
|
|
|
ADD_STRING("SCRIPT_NAME");
|
|
|
|
|
|
|
|
#define CONDADD(name, field) \
|
|
|
|
if (TG(hc)->field[0]) { \
|
2000-08-25 17:15:00 +08:00
|
|
|
php_register_variable(#name, TG(hc)->field, track_vars_array ELS_CC PLS_C); \
|
1999-12-20 15:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CONDADD(HTTP_REFERER, referer);
|
|
|
|
CONDADD(HTTP_USER_AGENT, useragent);
|
|
|
|
CONDADD(HTTP_ACCEPT, accept);
|
|
|
|
CONDADD(HTTP_ACCEPT_ENCODING, accepte);
|
|
|
|
CONDADD(HTTP_COOKIE, cookie);
|
|
|
|
CONDADD(CONTENT_TYPE, contenttype);
|
|
|
|
CONDADD(REMOTE_USER, remoteuser);
|
|
|
|
CONDADD(SERVER_PROTOCOL, protocol);
|
|
|
|
|
|
|
|
if (TG(hc)->contentlength != -1) {
|
|
|
|
sprintf(buf, "%ld", (long) TG(hc)->contentlength);
|
|
|
|
ADD_STRING("CONTENT_LENGTH");
|
|
|
|
}
|
|
|
|
|
2000-08-25 16:46:32 +08:00
|
|
|
if (TG(hc)->authorization[0])
|
2000-08-25 17:15:00 +08:00
|
|
|
php_register_variable("AUTH_TYPE", "Basic", track_vars_array ELS_CC PLS_C);
|
1999-12-20 15:11:14 +08:00
|
|
|
}
|
|
|
|
|
2001-01-03 06:49:31 +08:00
|
|
|
static sapi_module_struct thttpd_sapi_module = {
|
2000-08-25 16:46:32 +08:00
|
|
|
"thttpd",
|
|
|
|
"thttpd",
|
|
|
|
|
|
|
|
php_module_startup,
|
|
|
|
php_module_shutdown_wrapper,
|
|
|
|
|
|
|
|
NULL, /* activate */
|
|
|
|
NULL, /* deactivate */
|
|
|
|
|
|
|
|
sapi_thttpd_ub_write,
|
|
|
|
NULL,
|
|
|
|
NULL, /* get uid */
|
|
|
|
NULL, /* getenv */
|
|
|
|
|
|
|
|
php_error,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
sapi_thttpd_send_headers,
|
|
|
|
sapi_thttpd_send_header,
|
|
|
|
sapi_thttpd_read_post,
|
|
|
|
sapi_thttpd_read_cookies,
|
|
|
|
|
|
|
|
sapi_thttpd_register_variables,
|
|
|
|
NULL, /* Log message */
|
|
|
|
|
|
|
|
NULL, /* Block interruptions */
|
|
|
|
NULL, /* Unblock interruptions */
|
|
|
|
|
|
|
|
STANDARD_SAPI_MODULE_PROPERTIES
|
|
|
|
};
|
|
|
|
|
1999-12-20 15:11:14 +08:00
|
|
|
static void thttpd_module_main(TLS_D SLS_DC)
|
|
|
|
{
|
|
|
|
zend_file_handle file_handle;
|
|
|
|
CLS_FETCH();
|
|
|
|
ELS_FETCH();
|
|
|
|
PLS_FETCH();
|
|
|
|
|
|
|
|
file_handle.type = ZEND_HANDLE_FILENAME;
|
2000-08-29 23:09:44 +08:00
|
|
|
file_handle.filename = SG(request_info).path_translated;
|
1999-12-20 15:11:14 +08:00
|
|
|
file_handle.free_filename = 0;
|
2000-08-14 02:00:50 +08:00
|
|
|
file_handle.opened_path = NULL;
|
1999-12-20 15:11:14 +08:00
|
|
|
|
|
|
|
if (php_request_startup(CLS_C ELS_CC PLS_CC SLS_CC) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
php_execute_script(&file_handle CLS_CC ELS_CC PLS_CC);
|
|
|
|
php_request_shutdown(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void thttpd_request_ctor(TLS_D SLS_DC)
|
|
|
|
{
|
2000-08-29 23:09:44 +08:00
|
|
|
char *cp;
|
|
|
|
size_t cp_len;
|
1999-12-20 15:11:14 +08:00
|
|
|
char buf[1024];
|
|
|
|
int offset;
|
2000-08-29 23:09:44 +08:00
|
|
|
size_t filename_len;
|
1999-12-20 15:11:14 +08:00
|
|
|
size_t cwd_len;
|
|
|
|
|
2000-11-13 05:10:09 +08:00
|
|
|
|
|
|
|
SG(request_info).query_string = TG(hc)->query?strdup(TG(hc)->query):NULL;
|
2000-08-29 23:09:44 +08:00
|
|
|
|
|
|
|
filename_len = strlen(TG(hc)->expnfilename);
|
|
|
|
cwd_len = strlen(TG(hc)->hs->cwd);
|
|
|
|
|
|
|
|
cp_len = cwd_len + filename_len;
|
|
|
|
cp = (char *) malloc(cp_len + 1);
|
|
|
|
/* cwd always ends in "/", so this is safe */
|
|
|
|
memcpy(cp, TG(hc)->hs->cwd, cwd_len);
|
|
|
|
memcpy(cp + cwd_len, TG(hc)->expnfilename, filename_len);
|
|
|
|
cp[cp_len] = '\0';
|
1999-12-20 15:11:14 +08:00
|
|
|
|
2000-08-29 23:09:44 +08:00
|
|
|
SG(request_info).path_translated = cp;
|
1999-12-20 15:11:14 +08:00
|
|
|
|
|
|
|
snprintf(buf, 1023, "/%s", TG(hc)->origfilename);
|
|
|
|
SG(request_info).request_uri = strdup(buf);
|
|
|
|
SG(request_info).request_method = httpd_method_str(TG(hc)->method);
|
2000-08-03 06:48:45 +08:00
|
|
|
SG(sapi_headers).http_response_code = 200;
|
1999-12-20 15:11:14 +08:00
|
|
|
SG(request_info).content_type = TG(hc)->contenttype;
|
|
|
|
SG(request_info).content_length = TG(hc)->contentlength;
|
|
|
|
|
2000-10-27 18:19:06 +08:00
|
|
|
php_handle_auth_data(TG(hc)->authorization SLS_CC);
|
2000-10-12 03:40:56 +08:00
|
|
|
|
1999-12-20 15:11:14 +08:00
|
|
|
TG(post_off) = TG(hc)->read_idx - TG(hc)->checked_idx;
|
|
|
|
|
|
|
|
/* avoid feeding \r\n from POST data to SAPI */
|
|
|
|
offset = TG(post_off) - SG(request_info).content_length;
|
|
|
|
|
|
|
|
if (offset > 0) {
|
|
|
|
TG(post_off) -= offset;
|
|
|
|
TG(hc)->read_idx -= offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void thttpd_request_dtor(TLS_D SLS_DC)
|
|
|
|
{
|
2000-11-13 05:10:09 +08:00
|
|
|
if (SG(request_info).query_string)
|
|
|
|
free(SG(request_info).query_string);
|
1999-12-20 15:11:14 +08:00
|
|
|
free(SG(request_info).request_uri);
|
|
|
|
free(SG(request_info).path_translated);
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t thttpd_php_request(httpd_conn *hc)
|
|
|
|
{
|
|
|
|
SLS_FETCH();
|
|
|
|
TLS_FETCH();
|
|
|
|
|
|
|
|
TG(hc) = hc;
|
2001-01-09 16:45:05 +08:00
|
|
|
hc->bytes = 0;
|
1999-12-20 15:11:14 +08:00
|
|
|
|
|
|
|
thttpd_request_ctor(TLS_C SLS_CC);
|
|
|
|
|
|
|
|
thttpd_module_main(TLS_C SLS_CC);
|
|
|
|
|
|
|
|
thttpd_request_dtor(TLS_C SLS_CC);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-11-13 21:23:56 +08:00
|
|
|
void thttpd_register_on_close(void (*arg)(int))
|
|
|
|
{
|
|
|
|
TG(on_close) = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thttpd_closed_conn(int fd)
|
|
|
|
{
|
|
|
|
if (TG(on_close)) TG(on_close)(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int thttpd_get_fd(void)
|
|
|
|
{
|
|
|
|
return TG(hc)->conn_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thttpd_set_dont_close(void)
|
|
|
|
{
|
|
|
|
TG(hc)->file_address = (char *) 1;
|
|
|
|
}
|
|
|
|
|
1999-12-20 15:11:14 +08:00
|
|
|
void thttpd_php_init(void)
|
|
|
|
{
|
2001-01-03 06:49:31 +08:00
|
|
|
sapi_startup(&thttpd_sapi_module);
|
|
|
|
thttpd_sapi_module.startup(&thttpd_sapi_module);
|
1999-12-20 15:11:14 +08:00
|
|
|
SG(server_context) = (void *) 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thttpd_php_shutdown(void)
|
|
|
|
{
|
2000-10-12 03:40:56 +08:00
|
|
|
if (SG(server_context) != NULL) {
|
2001-01-03 06:49:31 +08:00
|
|
|
thttpd_sapi_module.shutdown(&thttpd_sapi_module);
|
2000-10-12 03:40:56 +08:00
|
|
|
sapi_shutdown();
|
|
|
|
}
|
1999-12-20 15:11:14 +08:00
|
|
|
}
|