Skip to content

Commit ec68ed4

Browse files
author
Ian Holsman
committed
initial checkin of the apache2 SAPI using a handler instead of filters.
TBD: src highlighting better post handling
1 parent f3ac7fc commit ec68ed4

File tree

9 files changed

+1661
-0
lines changed

9 files changed

+1661
-0
lines changed

sapi/apache2handler/CREDITS

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Apache 2.0
2+
Sascha Schumann, Aaron Bannert original work
3+
Ian Holsman & Justin Erenkrantz is for converting this to a handler

sapi/apache2handler/README

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
WHAT IS THIS?
2+
3+
This module exploits the layered I/O support in Apache 2.0.
4+
5+
HOW DOES IT WORK?
6+
7+
In Apache 2.0, you have handlers which generate content (like
8+
reading a script from disk). The content goes then through
9+
a chain of filters. PHP can be such a filter, so that it processes
10+
your script and hands the output to the next filter (which will
11+
usually cause a write to the network).
12+
13+
DOES IT WORK?
14+
15+
Currently the issues with the module are:
16+
* Thread safety of external PHP modules
17+
* The lack of re-entrancy of PHP. due to this I have disabled the 'virtual'
18+
function, and tried to stop any method where a php script can run another php
19+
script while it is being run.
20+
21+
22+
HOW TO INSTALL
23+
24+
This SAPI module is known to work with Apache 2.0.44.
25+
26+
$ cd apache-2.x
27+
$ cd src
28+
$ ./configure --enable-so
29+
$ make install
30+
31+
For testing purposes, you might want to use --with-mpm=prefork.
32+
(Albeit PHP also works with threaded MPMs. See Thread Safety note above)
33+
34+
Configure PHP 4:
35+
36+
$ cd php-4.x
37+
$ ./configure --with-apxs2=/path/to/apache-2.0/bin/apxs
38+
$ make install
39+
40+
At the end of conf/httpd.conf, add:
41+
42+
AddType application/x-httpd-php .php
43+
44+
If you would like to enable source code highlighting functionality add:
45+
46+
AddType application/x-httpd-php-source .phps
47+
48+
That's it. Now start bin/httpd.
49+
50+
HOW TO CONFIGURE
51+
52+
The Apache 2.0 PHP module supports a new configuration directive that
53+
allows an admin to override the php.ini search path. For example,
54+
place your php.ini file in Apache's ServerRoot/conf directory and
55+
add this to your httpd.conf file:
56+
57+
PHPINIDir "conf"
58+
59+
DEBUGGING APACHE AND PHP
60+
61+
To debug Apache, we recommened:
62+
63+
1. Use the Prefork MPM (Apache 1.3-like process model) by
64+
configuring Apache with '--with-mpm=prefork'.
65+
2. Start httpd using -DONE_PROCESS (e.g. (gdb) r -DONE_PROCESS).
66+
67+
If you want to debug a part of the PHP startup procedure, set a
68+
breakpoint on 'load_module'. Step through it until apr_dso_load() is
69+
done. Then you can set a breakpoint on any PHP-related symbol.
70+
71+
TODO
72+
73+
PHP functions like apache_sub_req (see php_functions.c)
74+
Source Code Highlighting
75+
Protocol handlers
76+

sapi/apache2handler/apache_config.c

+228
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 4 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2003 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 2.02 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available at through the world-wide-web at |
10+
| http://www.php.net/license/2_02.txt. |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Sascha Schumann <sascha@schumann.cx> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#include "php.h"
22+
#include "php_ini.h"
23+
#include "php_apache.h"
24+
25+
#include "apr_strings.h"
26+
#include "ap_config.h"
27+
#include "util_filter.h"
28+
#include "httpd.h"
29+
#include "http_config.h"
30+
#include "http_request.h"
31+
#include "http_core.h"
32+
#include "http_protocol.h"
33+
#include "http_log.h"
34+
#include "http_main.h"
35+
#include "util_script.h"
36+
#include "http_core.h"
37+
38+
#ifdef PHP_AP_DEBUG
39+
#define phpapdebug(a) fprintf a
40+
#else
41+
#define phpapdebug(a)
42+
#endif
43+
44+
typedef struct {
45+
HashTable config;
46+
} php_conf_rec;
47+
48+
typedef struct {
49+
char *value;
50+
size_t value_len;
51+
char status;
52+
} php_dir_entry;
53+
54+
static const char *real_value_hnd(cmd_parms *cmd, void *dummy,
55+
const char *name, const char *value, int status)
56+
{
57+
php_conf_rec *d = dummy;
58+
php_dir_entry e;
59+
60+
phpapdebug((stderr, "Getting %s=%s for %p (%d)\n", name, value, dummy,
61+
zend_hash_num_elements(&d->config)));
62+
63+
if (!strncasecmp(value, "none", sizeof("none"))) {
64+
value = "";
65+
}
66+
67+
e.value = apr_pstrdup(cmd->pool, value);
68+
e.value_len = strlen(value);
69+
e.status = status;
70+
71+
zend_hash_update(&d->config, (char *) name, strlen(name) + 1, &e,
72+
sizeof(e), NULL);
73+
return NULL;
74+
}
75+
76+
static const char *php_apache_value_handler(cmd_parms *cmd, void *dummy,
77+
const char *name, const char *value)
78+
{
79+
return real_value_hnd(cmd, dummy, name, value, PHP_INI_PERDIR);
80+
}
81+
82+
static const char *php_apache_admin_value_handler(cmd_parms *cmd, void *dummy,
83+
const char *name, const char *value)
84+
{
85+
return real_value_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
86+
}
87+
88+
static const char *real_flag_hnd(cmd_parms *cmd, void *dummy, const char *arg1,
89+
const char *arg2, int status)
90+
{
91+
char bool_val[2];
92+
93+
if (!strcasecmp(arg2, "On") || (arg2[0] == '1' && arg2[1] == '\0')) {
94+
bool_val[0] = '1';
95+
} else {
96+
bool_val[0] = '0';
97+
}
98+
bool_val[1] = 0;
99+
100+
return real_value_hnd(cmd, dummy, arg1, bool_val, status);
101+
}
102+
103+
static const char *php_apache_flag_handler(cmd_parms *cmd, void *dummy,
104+
const char *name, const char *value)
105+
{
106+
return real_flag_hnd(cmd, dummy, name, value, PHP_INI_PERDIR);
107+
}
108+
109+
static const char *php_apache_admin_flag_handler(cmd_parms *cmd, void *dummy,
110+
const char *name, const char *value)
111+
{
112+
return real_flag_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
113+
}
114+
115+
static const char *php_apache_phpini_set(cmd_parms *cmd, void *mconfig,
116+
const char *arg)
117+
{
118+
if (apache2_php_ini_path_override) {
119+
return "Only first PHPINIDir directive honored per configuration tree "
120+
"- subsequent ones ignored";
121+
}
122+
apache2_php_ini_path_override = ap_server_root_relative(cmd->pool, arg);
123+
return NULL;
124+
}
125+
126+
127+
void *merge_php_config(apr_pool_t *p, void *base_conf, void *new_conf)
128+
{
129+
php_conf_rec *d = base_conf, *e = new_conf;
130+
php_dir_entry *pe;
131+
php_dir_entry *data;
132+
char *str;
133+
uint str_len;
134+
ulong num_index;
135+
136+
phpapdebug((stderr, "Merge dir (%p) (%p)\n", base_conf, new_conf));
137+
for (zend_hash_internal_pointer_reset(&d->config);
138+
zend_hash_get_current_key_ex(&d->config, &str, &str_len,
139+
&num_index, 0, NULL) == HASH_KEY_IS_STRING;
140+
zend_hash_move_forward(&d->config)) {
141+
pe = NULL;
142+
zend_hash_get_current_data(&d->config, (void **) &data);
143+
if (zend_hash_find(&e->config, str, str_len, (void **) &pe) == SUCCESS) {
144+
if (pe->status >= data->status) continue;
145+
}
146+
zend_hash_update(&e->config, str, str_len, data, sizeof(*data), NULL);
147+
phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", str,
148+
data->status, pe?pe->status:-1));
149+
}
150+
return new_conf;
151+
}
152+
153+
char *get_php_config(void *conf, char *name, size_t name_len)
154+
{
155+
php_conf_rec *d = conf;
156+
php_dir_entry *pe;
157+
158+
if (zend_hash_find(&d->config, name, name_len, (void **) &pe) == SUCCESS) {
159+
return pe->value;
160+
}
161+
162+
return "";
163+
}
164+
165+
void apply_config(void *dummy)
166+
{
167+
php_conf_rec *d = dummy;
168+
char *str;
169+
uint str_len;
170+
php_dir_entry *data;
171+
172+
for (zend_hash_internal_pointer_reset(&d->config);
173+
zend_hash_get_current_key_ex(&d->config, &str, &str_len, NULL, 0,
174+
NULL) == HASH_KEY_IS_STRING;
175+
zend_hash_move_forward(&d->config)) {
176+
zend_hash_get_current_data(&d->config, (void **) &data);
177+
phpapdebug((stderr, "APPLYING (%s)(%s)\n", str, data->value));
178+
if (zend_alter_ini_entry(str, str_len, data->value, data->value_len,
179+
data->status, PHP_INI_STAGE_RUNTIME) == FAILURE) {
180+
phpapdebug((stderr, "..FAILED\n"));
181+
}
182+
}
183+
}
184+
185+
const command_rec php_dir_cmds[] =
186+
{
187+
AP_INIT_TAKE2("php_value", php_apache_value_handler, NULL, OR_OPTIONS,
188+
"PHP Value Modifier"),
189+
AP_INIT_TAKE2("php_flag", php_apache_flag_handler, NULL, OR_OPTIONS,
190+
"PHP Flag Modifier"),
191+
AP_INIT_TAKE2("php_admin_value", php_apache_admin_value_handler, NULL,
192+
ACCESS_CONF|RSRC_CONF, "PHP Value Modifier (Admin)"),
193+
AP_INIT_TAKE2("php_admin_flag", php_apache_admin_flag_handler, NULL,
194+
ACCESS_CONF|RSRC_CONF, "PHP Flag Modifier (Admin)"),
195+
AP_INIT_TAKE1("PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF,
196+
"Directory containing the php.ini file"),
197+
{NULL}
198+
};
199+
200+
static apr_status_t destroy_php_config(void *data)
201+
{
202+
php_conf_rec *d = data;
203+
204+
phpapdebug((stderr, "Destroying config %p\n", data));
205+
zend_hash_destroy(&d->config);
206+
207+
return APR_SUCCESS;
208+
}
209+
210+
void *create_php_config(apr_pool_t *p, char *dummy)
211+
{
212+
php_conf_rec *newx =
213+
(php_conf_rec *) apr_pcalloc(p, sizeof(*newx));
214+
215+
phpapdebug((stderr, "Creating new config (%p) for %s\n", newx, dummy));
216+
zend_hash_init(&newx->config, 0, NULL, NULL, 1);
217+
apr_pool_cleanup_register(p, newx, destroy_php_config, apr_pool_cleanup_null);
218+
return (void *) newx;
219+
}
220+
221+
/*
222+
* Local variables:
223+
* tab-width: 4
224+
* c-basic-offset: 4
225+
* End:
226+
* vim600: sw=4 ts=4 fdm=marker
227+
* vim<600: sw=4 ts=4
228+
*/

0 commit comments

Comments
 (0)