1999-12-20 15:11:14 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP version 4.0 |
|
|
|
|
+----------------------------------------------------------------------+
|
2001-02-26 14:11:02 +08:00
|
|
|
| Copyright (c) 1997-2001 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"
|
|
|
|
|
2001-03-14 01:14:47 +08:00
|
|
|
#include "ext/standard/php_smart_str.h"
|
|
|
|
|
2001-02-19 18:28:08 +08:00
|
|
|
#include <sys/uio.h>
|
|
|
|
|
1999-12-20 15:11:14 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2001-06-24 00:27:56 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
static int thttpd_globals_id;
|
2001-08-05 18:32:48 +08:00
|
|
|
#define TG(v) TSRMG(thttpd_globals_id, php_thttpd_globals *, v)
|
2001-06-24 00:27:56 +08:00
|
|
|
#else
|
|
|
|
static php_thttpd_globals thttpd_globals;
|
1999-12-20 15:11:14 +08:00
|
|
|
#define TG(v) (thttpd_globals.v)
|
2001-06-24 00:27:56 +08:00
|
|
|
#endif
|
1999-12-20 15:11:14 +08:00
|
|
|
|
|
|
|
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;
|
2001-07-28 19:36:37 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-12-20 15:11:14 +08:00
|
|
|
|
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-04-22 22:22:40 +08:00
|
|
|
TG(hc)->bytes_sent += n;
|
2001-01-10 21:51:58 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-03-03 01:01:52 +08:00
|
|
|
#define COMBINE_HEADERS 30
|
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
static int sapi_thttpd_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
|
1999-12-20 15:11:14 +08:00
|
|
|
{
|
|
|
|
char buf[1024];
|
2001-03-03 01:01:52 +08:00
|
|
|
struct iovec vec[COMBINE_HEADERS];
|
|
|
|
int n = 0;
|
|
|
|
zend_llist_position pos;
|
|
|
|
sapi_header_struct *h;
|
2001-04-25 14:42:24 +08:00
|
|
|
size_t len;
|
2001-03-03 01:01:52 +08:00
|
|
|
|
1999-12-20 15:11:14 +08:00
|
|
|
if (!SG(sapi_headers).http_status_line) {
|
|
|
|
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);
|
2001-03-03 01:01:52 +08:00
|
|
|
vec[n].iov_base = buf;
|
2001-04-25 14:42:24 +08:00
|
|
|
vec[n].iov_len = len;
|
|
|
|
} else {
|
|
|
|
vec[n].iov_base = SG(sapi_headers).http_status_line;
|
|
|
|
len = strlen(vec[n].iov_base);
|
|
|
|
vec[n].iov_len = len;
|
|
|
|
vec[++n].iov_base = "\r\n";
|
|
|
|
vec[n].iov_len = 2;
|
|
|
|
len += 2;
|
1999-12-20 15:11:14 +08:00
|
|
|
}
|
2001-04-25 14:42:24 +08:00
|
|
|
TG(hc)->status = SG(sapi_headers).http_response_code;
|
|
|
|
TG(hc)->bytes_sent += len;
|
|
|
|
n++;
|
1999-12-20 15:11:14 +08:00
|
|
|
|
2001-03-03 01:01:52 +08:00
|
|
|
h = zend_llist_get_first_ex(&sapi_headers->headers, &pos);
|
|
|
|
while (h) {
|
|
|
|
vec[n].iov_base = h->header;
|
|
|
|
vec[n++].iov_len = h->header_len;
|
|
|
|
if (n >= COMBINE_HEADERS - 1) {
|
|
|
|
if (writev(TG(hc)->conn_fd, vec, n) == -1 && errno == EPIPE)
|
|
|
|
php_handle_aborted_connection();
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
vec[n].iov_base = "\r\n";
|
|
|
|
vec[n++].iov_len = 2;
|
|
|
|
|
|
|
|
h = zend_llist_get_next_ex(&sapi_headers->headers, &pos);
|
2001-01-09 16:45:05 +08:00
|
|
|
}
|
2001-03-03 01:01:52 +08:00
|
|
|
|
2001-02-19 03:03:36 +08:00
|
|
|
vec[n].iov_base = "\r\n";
|
|
|
|
vec[n++].iov_len = 2;
|
2001-03-03 01:01:52 +08:00
|
|
|
|
|
|
|
if (n) {
|
|
|
|
if (writev(TG(hc)->conn_fd, vec, n) == -1 && errno == EPIPE)
|
|
|
|
php_handle_aborted_connection();
|
|
|
|
}
|
2001-02-19 03:03:36 +08:00
|
|
|
|
2001-03-03 01:01:52 +08:00
|
|
|
return SAPI_HEADER_SENT_SUCCESSFULLY;
|
1999-12-20 15:11:14 +08:00
|
|
|
}
|
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
static int sapi_thttpd_read_post(char *buffer, uint count_bytes TSRMLS_DC)
|
1999-12-20 15:11:14 +08:00
|
|
|
{
|
|
|
|
size_t read_bytes = 0, tmp;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
static char *sapi_thttpd_read_cookies(TSRMLS_D)
|
1999-12-20 15:11:14 +08:00
|
|
|
{
|
|
|
|
return TG(hc)->cookie;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BUF_SIZE 512
|
|
|
|
#define ADD_STRING(name) \
|
2001-07-28 19:36:37 +08:00
|
|
|
php_register_variable(name, buf, track_vars_array TSRMLS_CC)
|
2000-08-25 16:46:32 +08:00
|
|
|
|
2001-08-05 18:32:48 +08:00
|
|
|
static void sapi_thttpd_register_variables(zval *track_vars_array TSRMLS_DC)
|
1999-12-20 15:11:14 +08:00
|
|
|
{
|
|
|
|
char buf[BUF_SIZE + 1];
|
2001-05-03 17:04:15 +08:00
|
|
|
char *p;
|
2000-08-25 16:46:32 +08:00
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
php_register_variable("PHP_SELF", SG(request_info).request_uri, track_vars_array TSRMLS_CC);
|
|
|
|
php_register_variable("SERVER_SOFTWARE", SERVER_SOFTWARE, track_vars_array TSRMLS_CC);
|
|
|
|
php_register_variable("GATEWAY_INTERFACE", "CGI/1.1", track_vars_array TSRMLS_CC);
|
|
|
|
php_register_variable("REQUEST_METHOD", (char *) SG(request_info).request_method, track_vars_array TSRMLS_CC);
|
|
|
|
php_register_variable("REQUEST_URI", SG(request_info).request_uri, track_vars_array TSRMLS_CC);
|
|
|
|
php_register_variable("PATH_TRANSLATED", SG(request_info).path_translated, track_vars_array TSRMLS_CC);
|
1999-12-20 15:11:14 +08:00
|
|
|
|
2001-05-03 17:04:15 +08:00
|
|
|
p = inet_ntoa(TG(hc)->client_addr.sa_in.sin_addr);
|
|
|
|
/* string representation of IPs are never larger than 512 bytes */
|
|
|
|
if (p) {
|
|
|
|
memcpy(buf, p, strlen(p) + 1);
|
|
|
|
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]) { \
|
2001-07-28 19:36:37 +08:00
|
|
|
php_register_variable(#name, TG(hc)->field, track_vars_array TSRMLS_CC); \
|
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])
|
2001-07-28 19:36:37 +08:00
|
|
|
php_register_variable("AUTH_TYPE", "Basic", track_vars_array TSRMLS_CC);
|
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,
|
2001-03-03 01:01:52 +08:00
|
|
|
NULL,
|
2000-08-25 16:46:32 +08:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2001-08-05 18:32:48 +08:00
|
|
|
static void thttpd_module_main(TSRMLS_D)
|
1999-12-20 15:11:14 +08:00
|
|
|
{
|
|
|
|
zend_file_handle file_handle;
|
|
|
|
|
|
|
|
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
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
if (php_request_startup(TSRMLS_C) == FAILURE) {
|
1999-12-20 15:11:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
php_execute_script(&file_handle TSRMLS_CC);
|
1999-12-20 15:11:14 +08:00
|
|
|
php_request_shutdown(NULL);
|
|
|
|
}
|
|
|
|
|
2001-08-05 22:17:55 +08:00
|
|
|
static void thttpd_request_ctor(TSRMLS_D)
|
1999-12-20 15:11:14 +08:00
|
|
|
{
|
|
|
|
int offset;
|
2001-03-14 01:14:47 +08:00
|
|
|
smart_str s = {0};
|
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
|
|
|
|
2001-03-14 01:14:47 +08:00
|
|
|
smart_str_appends_ex(&s, TG(hc)->hs->cwd, 1);
|
|
|
|
smart_str_appends_ex(&s, TG(hc)->expnfilename, 1);
|
|
|
|
smart_str_0(&s);
|
|
|
|
SG(request_info).path_translated = s.c;
|
1999-12-20 15:11:14 +08:00
|
|
|
|
2001-03-14 01:14:47 +08:00
|
|
|
s.c = NULL;
|
|
|
|
smart_str_appendc_ex(&s, '/', 1);
|
|
|
|
smart_str_appends_ex(&s, TG(hc)->origfilename, 1);
|
|
|
|
smart_str_0(&s);
|
|
|
|
SG(request_info).request_uri = s.c;
|
1999-12-20 15:11:14 +08:00
|
|
|
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;
|
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
php_handle_auth_data(TG(hc)->authorization TSRMLS_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-05 22:17:55 +08:00
|
|
|
static void thttpd_request_dtor(TSRMLS_D)
|
1999-12-20 15:11:14 +08:00
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
2001-06-24 00:27:56 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
|
|
|
|
#ifdef TSRM_ST
|
|
|
|
#define thread_create_simple_detached(n) st_thread_create(n, NULL, 0, 0)
|
|
|
|
#define thread_usleep(n) st_usleep(n)
|
|
|
|
#define thread_exit() st_thread_exit(NULL)
|
|
|
|
/* No preemption, simple operations are safe */
|
|
|
|
#define thread_atomic_inc(n) (++n)
|
|
|
|
#define thread_atomic_dec(n) (--n)
|
|
|
|
#else
|
|
|
|
#error No thread primitives available
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We might want to replace this with a STAILQ */
|
|
|
|
typedef struct qreq {
|
|
|
|
httpd_conn *hc;
|
|
|
|
struct qreq *next;
|
|
|
|
} qreq_t;
|
|
|
|
|
|
|
|
static MUTEX_T qr_lock;
|
|
|
|
static qreq_t *queued_requests;
|
|
|
|
static qreq_t *last_qr;
|
|
|
|
static int nr_free_threads;
|
|
|
|
static int nr_threads;
|
|
|
|
static int max_threads = 50;
|
|
|
|
|
|
|
|
#define HANDLE_STRINGS() { \
|
|
|
|
HANDLE_STR(encodedurl); \
|
|
|
|
HANDLE_STR(decodedurl); \
|
|
|
|
HANDLE_STR(origfilename); \
|
|
|
|
HANDLE_STR(expnfilename); \
|
|
|
|
HANDLE_STR(pathinfo); \
|
|
|
|
HANDLE_STR(query); \
|
|
|
|
HANDLE_STR(referer); \
|
|
|
|
HANDLE_STR(useragent); \
|
|
|
|
HANDLE_STR(accept); \
|
|
|
|
HANDLE_STR(accepte); \
|
|
|
|
HANDLE_STR(acceptl); \
|
|
|
|
HANDLE_STR(cookie); \
|
|
|
|
HANDLE_STR(contenttype); \
|
|
|
|
HANDLE_STR(authorization); \
|
|
|
|
HANDLE_STR(remoteuser); \
|
|
|
|
}
|
|
|
|
|
|
|
|
static httpd_conn *duplicate_conn(httpd_conn *hc, httpd_conn *nhc)
|
|
|
|
{
|
|
|
|
memcpy(nhc, hc, sizeof(*nhc));
|
|
|
|
|
|
|
|
#define HANDLE_STR(m) nhc->m = nhc->m ? strdup(nhc->m) : NULL
|
|
|
|
HANDLE_STRINGS();
|
|
|
|
#undef HANDLE_STR
|
|
|
|
|
|
|
|
return nhc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_conn(httpd_conn *hc)
|
|
|
|
{
|
|
|
|
#define HANDLE_STR(m) if (hc->m) free(hc->m)
|
|
|
|
HANDLE_STRINGS();
|
|
|
|
#undef HANDLE_STR
|
|
|
|
}
|
|
|
|
|
|
|
|
static httpd_conn *dequeue_request(void)
|
|
|
|
{
|
|
|
|
httpd_conn *ret = NULL;
|
|
|
|
qreq_t *m;
|
|
|
|
|
|
|
|
tsrm_mutex_lock(qr_lock);
|
|
|
|
if (queued_requests) {
|
|
|
|
m = queued_requests;
|
|
|
|
ret = m->hc;
|
|
|
|
if (!(queued_requests = m->next))
|
|
|
|
last_qr = NULL;
|
|
|
|
free(m);
|
|
|
|
}
|
|
|
|
tsrm_mutex_unlock(qr_lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *worker_thread(void *);
|
|
|
|
|
|
|
|
static void queue_request(httpd_conn *hc)
|
|
|
|
{
|
|
|
|
qreq_t *m;
|
|
|
|
httpd_conn *nhc;
|
|
|
|
|
|
|
|
/* Mark as long-running request */
|
|
|
|
hc->file_address = (char *) 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We cannot synchronously revoke accesses to hc in the worker
|
|
|
|
* thread, so we need to pass a copy of hc to the worker thread.
|
|
|
|
*/
|
|
|
|
nhc = malloc(sizeof *nhc);
|
|
|
|
duplicate_conn(hc, nhc);
|
|
|
|
|
|
|
|
/* Allocate request queue container */
|
|
|
|
m = malloc(sizeof *m);
|
|
|
|
m->hc = nhc;
|
|
|
|
m->next = NULL;
|
|
|
|
|
|
|
|
tsrm_mutex_lock(qr_lock);
|
|
|
|
/* Create new threads when reaching a certain threshhold */
|
|
|
|
if (nr_threads < max_threads && nr_free_threads < 2) {
|
|
|
|
nr_threads++; /* protected by qr_lock */
|
|
|
|
|
|
|
|
thread_atomic_inc(nr_free_threads);
|
|
|
|
thread_create_simple_detached(worker_thread);
|
|
|
|
}
|
|
|
|
/* Insert container into request queue */
|
|
|
|
if (queued_requests)
|
|
|
|
last_qr->next = m;
|
|
|
|
else
|
|
|
|
queued_requests = m;
|
|
|
|
last_qr = m;
|
|
|
|
tsrm_mutex_unlock(qr_lock);
|
|
|
|
}
|
|
|
|
|
2001-08-05 18:32:48 +08:00
|
|
|
static off_t thttpd_real_php_request(httpd_conn *hc TSRMLS_DC);
|
2001-06-24 00:27:56 +08:00
|
|
|
|
|
|
|
static void *worker_thread(void *dummy)
|
1999-12-20 15:11:14 +08:00
|
|
|
{
|
2001-06-24 00:27:56 +08:00
|
|
|
int do_work = 50;
|
|
|
|
httpd_conn *hc;
|
1999-12-20 15:11:14 +08:00
|
|
|
|
2001-06-24 00:27:56 +08:00
|
|
|
while (do_work) {
|
|
|
|
hc = dequeue_request();
|
|
|
|
|
|
|
|
if (!hc) {
|
|
|
|
/* do_work--; */
|
|
|
|
thread_usleep(500000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* do_work = 50; */
|
|
|
|
|
|
|
|
thread_atomic_dec(nr_free_threads);
|
|
|
|
|
2001-08-05 21:26:54 +08:00
|
|
|
thttpd_real_php_request(hc TSRMLS_CC);
|
2001-06-24 00:27:56 +08:00
|
|
|
shutdown(hc->conn_fd, 0);
|
|
|
|
destroy_conn(hc);
|
|
|
|
free(hc);
|
|
|
|
|
|
|
|
thread_atomic_inc(nr_free_threads);
|
|
|
|
}
|
|
|
|
thread_atomic_dec(nr_free_threads);
|
|
|
|
thread_atomic_dec(nr_threads);
|
|
|
|
thread_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_dead_conn(int fd)
|
|
|
|
{
|
|
|
|
qreq_t *m, *prev = NULL;
|
|
|
|
|
|
|
|
tsrm_mutex_lock(qr_lock);
|
|
|
|
m = queued_requests;
|
|
|
|
while (m) {
|
|
|
|
if (m->hc->conn_fd == fd) {
|
|
|
|
if (prev)
|
|
|
|
if (!(prev->next = m->next))
|
|
|
|
last_qr = prev;
|
|
|
|
else
|
|
|
|
if (!(queued_requests = m->next))
|
|
|
|
last_qr = NULL;
|
|
|
|
destroy_conn(m->hc);
|
|
|
|
free(m->hc);
|
|
|
|
free(m);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = m;
|
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
tsrm_mutex_unlock(qr_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2001-08-05 21:26:54 +08:00
|
|
|
static off_t thttpd_real_php_request(httpd_conn *hc TSRMLS_DC)
|
2001-06-24 00:27:56 +08:00
|
|
|
{
|
1999-12-20 15:11:14 +08:00
|
|
|
TG(hc) = hc;
|
2001-04-22 22:22:40 +08:00
|
|
|
hc->bytes_sent = 0;
|
1999-12-20 15:11:14 +08:00
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
thttpd_request_ctor(TSRMLS_C);
|
1999-12-20 15:11:14 +08:00
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
thttpd_module_main(TSRMLS_C);
|
1999-12-20 15:11:14 +08:00
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
thttpd_request_dtor(TSRMLS_C);
|
1999-12-20 15:11:14 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-06-24 00:27:56 +08:00
|
|
|
off_t thttpd_php_request(httpd_conn *hc)
|
|
|
|
{
|
|
|
|
#ifdef ZTS
|
|
|
|
queue_request(hc);
|
|
|
|
#else
|
2001-07-28 19:36:37 +08:00
|
|
|
TSRMLS_FETCH();
|
2001-08-05 18:32:48 +08:00
|
|
|
return thttpd_real_php_request(hc TSRMLS_CC);
|
2001-06-24 00:27:56 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-11-13 21:23:56 +08:00
|
|
|
void thttpd_register_on_close(void (*arg)(int))
|
|
|
|
{
|
2001-07-28 19:36:37 +08:00
|
|
|
TSRMLS_FETCH();
|
2000-11-13 21:23:56 +08:00
|
|
|
TG(on_close) = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thttpd_closed_conn(int fd)
|
|
|
|
{
|
2001-07-28 19:36:37 +08:00
|
|
|
TSRMLS_FETCH();
|
2000-11-13 21:23:56 +08:00
|
|
|
if (TG(on_close)) TG(on_close)(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int thttpd_get_fd(void)
|
|
|
|
{
|
2001-07-28 19:36:37 +08:00
|
|
|
TSRMLS_FETCH();
|
2000-11-13 21:23:56 +08:00
|
|
|
return TG(hc)->conn_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thttpd_set_dont_close(void)
|
|
|
|
{
|
2001-07-28 19:36:37 +08:00
|
|
|
TSRMLS_FETCH();
|
2000-11-13 21:23:56 +08:00
|
|
|
TG(hc)->file_address = (char *) 1;
|
|
|
|
}
|
|
|
|
|
2001-06-24 00:27:56 +08:00
|
|
|
|
1999-12-20 15:11:14 +08:00
|
|
|
void thttpd_php_init(void)
|
|
|
|
{
|
2001-06-24 00:27:56 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
tsrm_startup(1, 1, 0, NULL);
|
2001-07-27 18:16:41 +08:00
|
|
|
ts_allocate_id(&thttpd_globals_id, sizeof(php_thttpd_globals), NULL, NULL);
|
2001-06-24 00:27:56 +08:00
|
|
|
qr_lock = tsrm_mutex_alloc();
|
|
|
|
thttpd_register_on_close(remove_dead_conn);
|
|
|
|
#endif
|
2001-01-03 06:49:31 +08:00
|
|
|
sapi_startup(&thttpd_sapi_module);
|
|
|
|
thttpd_sapi_module.startup(&thttpd_sapi_module);
|
2001-06-24 00:27:56 +08:00
|
|
|
{
|
2001-07-28 19:36:37 +08:00
|
|
|
TSRMLS_FETCH();
|
2001-08-05 18:32:48 +08:00
|
|
|
|
2001-06-24 00:27:56 +08:00
|
|
|
SG(server_context) = (void *) 1;
|
|
|
|
}
|
1999-12-20 15:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void thttpd_php_shutdown(void)
|
|
|
|
{
|
2001-07-28 19:36:37 +08:00
|
|
|
TSRMLS_FETCH();
|
2001-06-24 00:27:56 +08:00
|
|
|
|
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();
|
2001-06-24 00:27:56 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
tsrm_shutdown();
|
|
|
|
#endif
|
2000-10-12 03:40:56 +08:00
|
|
|
}
|
1999-12-20 15:11:14 +08:00
|
|
|
}
|