Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work for MPM. #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile.msc
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
MOD_PSGI_VERSION=0.0.1
APACHE_ROOT=c:/progra~1/apache~1/apache2.2
PERL_ROOT=c:/perl
PERL_ROOT=c:/strawberry/perl

all : mod_psgi.so

mod_psgi.obj : mod_psgi.c ppport.h
cl /MD /c mod_psgi.c -DMP_SYS_DL_DLOPEN=1 -DNDEBUG -DWIN32 -DNO_STRICT -DHAVE_DES_FCRYPT -DUSE_SITECUSTOMIZE -DPRIVLIB_LAST_IN_INC -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFIX /I"$(PERL_ROOT)/lib/CORE" /I"$(APACHE_ROOT)/include" -DMOD_PSGI_VERSION=\"$(MOD_PSGI_VERSION)\"

mod_psgi.so : mod_psgi.obj
link /DLL /OUT:mod_psgi.so mod_psgi.obj /LIBPATH:"$(APACHE_ROOT)/lib" "$(PERL_ROOT)/lib/core/perl510.lib" "$(APACHE_ROOT)/lib/libapr-1.lib" "$(APACHE_ROOT)/lib/libaprutil-1.lib" "$(APACHE_ROOT)/lib/libhttpd.lib"
link /DLL /OUT:mod_psgi.so mod_psgi.obj /LIBPATH:"$(APACHE_ROOT)/lib" "$(PERL_ROOT)/lib/core/libperl512.a" "$(APACHE_ROOT)/lib/libapr-1.lib" "$(APACHE_ROOT)/lib/libaprutil-1.lib" "$(APACHE_ROOT)/lib/libhttpd.lib"

ppport.h :
perl -MDevel::PPPort -e "Devel::PPPort::WriteFile"
Expand Down
14 changes: 14 additions & 0 deletions Makefile.w32
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
MOD_PSGI_VERSION=0.0.1
APACHE_ROOT=c:/progra~1/apache~1/apache2.2
PERL_ROOT=c:/strawberry/perl

all : mod_psgi.so

mod_psgi.o : mod_psgi.c
gcc -g -shared -c mod_psgi.c -DMP_SYS_DL_DLOPEN=1 -MD -DNDEBUG -O1 -DWIN32 -DNO_STRICT -DHAVE_DES_FCRYPT -DUSE_SITECUSTOMIZE -DPRIVLIB_LAST_IN_INC -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFIX -I"$(PERL_ROOT)/lib/CORE" -I"$(APACHE_ROOT)/include" -DMOD_PSGI_VERSION=\"$(MOD_PSGI_VERSION)\"

mod_psgi.so : mod_psgi.o
gcc -g -shared -o mod_psgi.so mod_psgi.o -L"$(APACHE_ROOT)/lib" -L"$(PERL_ROOT)/lib/core" -lperl512 -llibapr-1 -llibaprutil-1 -llibhttpd

install :
cp mod_psgi.so "$(APACHE_ROOT)/modules/."
78 changes: 59 additions & 19 deletions mod_psgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,16 @@ typedef struct {
char *location;
} psgi_dir_config;

static PerlInterpreter *perlinterp = NULL;
typedef struct {
apr_hash_t *apps;
} psgi_apps_t;

static apr_hash_t *psgi_apps = NULL;
static apr_shm_t *psgi_shm = NULL;
static char *shm_name = NULL;
static apr_global_mutex_t *psgi_mutex = NULL;
static char *mutex_name = NULL;

static PerlInterpreter *perlinterp = NULL;

static int psgi_multiprocess = 0;

Expand Down Expand Up @@ -108,12 +115,13 @@ XS(ModPSGI_Input_read)
SV *buf = ST(1);
request_rec *r = (request_rec *) mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj;
apr_size_t len = SvIV(ST(2));
int offset = items >= 4 ? SvIV(ST(3)) : 0;
apr_bucket_brigade *bb;
apr_size_t nread = 0;
char *pv, *tmp;
int eos = 0;

if (items >= 4) {
if (offset > 0) {
croak("$env->{'psgi.input'}->read: mod_psgi can't handle offset");
}

Expand Down Expand Up @@ -610,10 +618,22 @@ static int psgi_handler(request_rec *r)
SV *app, *env, *res;
psgi_dir_config *c;
int rc;
psgi_apps_t *psgi_apps;
int locked = 0;

if (strcmp(r->handler, PSGI_HANDLER_NAME)) {
return DECLINED;
}

rc = apr_global_mutex_lock(psgi_mutex);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server,
"apr_global_mutex_lock() failed");
rc = HTTP_INTERNAL_SERVER_ERROR;
goto exit;
}
locked = 1;

c = (psgi_dir_config *) ap_get_module_config(r->per_dir_config, &psgi_module);
if (c->file == NULL) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server,
Expand All @@ -625,15 +645,18 @@ static int psgi_handler(request_rec *r)
ENTER;
SAVETMPS;

app = apr_hash_get(psgi_apps, c->file, APR_HASH_KEY_STRING);
psgi_apps = (psgi_apps_t *)apr_shm_baseaddr_get(psgi_shm);

app = apr_hash_get(psgi_apps->apps, c->file, APR_HASH_KEY_STRING);
if (app == NULL) {
app = load_psgi(r->pool, c->file);
if (app == NULL) {
server_error(r, "%s had compilation errors.", c->file);
rc = HTTP_INTERNAL_SERVER_ERROR;
goto exit;
}
apr_hash_set(psgi_apps, c->file, APR_HASH_KEY_STRING, app);
SvREFCNT_inc(app);
apr_hash_set(psgi_apps->apps, c->file, APR_HASH_KEY_STRING, app);
}

env = make_env(r, c);
Expand All @@ -647,6 +670,10 @@ static int psgi_handler(request_rec *r)
SvREFCNT_dec(res);

exit:
if (locked) {
apr_global_mutex_unlock(psgi_mutex);
}

FREETMPS;
LEAVE;
return rc;
Expand All @@ -662,11 +689,13 @@ static apr_status_t psgi_child_exit(void *p)
PERL_SYS_TERM();
perlinterp = NULL;
}

return OK;
}

static void psgi_child_init(apr_pool_t *p, server_rec *s)
{
apr_global_mutex_child_init(&psgi_mutex, (const char *) mutex_name, p);
apr_pool_cleanup_register(p, NULL, psgi_child_exit, psgi_child_exit);
}

Expand All @@ -692,8 +721,6 @@ psgi_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
ap_mpm_query(AP_MPMQ_IS_FORKED, &psgi_multiprocess);
psgi_multiprocess = (psgi_multiprocess != AP_MPMQ_NOT_SUPPORTED);

psgi_apps = apr_hash_make(pconf);

return OK;
}

Expand All @@ -707,6 +734,8 @@ psgi_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_
apr_hash_index_t *hi;
void *data;
const char *userdata_key = "psgi_post_config";
psgi_apps_t *psgi_apps = NULL;
apr_status_t rc;

apr_pool_userdata_get(&data, userdata_key, s->process->pool);
if (data == NULL) {
Expand All @@ -715,19 +744,31 @@ psgi_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_
return OK;
}

for (hi = apr_hash_first(pconf, psgi_apps); hi; hi = apr_hash_next(hi)) {
apr_hash_this(hi, &key, NULL, NULL);
file = (char *) key;
app = load_psgi(pconf, file);
if (app == NULL) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, NULL,
"%s had compilation errors.", file);
return DONE;
}
apr_hash_set(psgi_apps, file, APR_HASH_KEY_STRING, app);
ap_add_version_component(pconf, apr_psprintf(pconf, "mod_psgi/%s", MOD_PSGI_VERSION));

mutex_name = apr_psprintf(pconf, "/tmp/psgi_mutex.%ld", (long int) getpid());
rc = apr_global_mutex_create(&psgi_mutex,
(const char *) mutex_name, APR_LOCK_DEFAULT, pconf);
if (rc != APR_SUCCESS) {
return DECLINED;
}
rc = apr_global_mutex_lock(psgi_mutex);
if (rc != APR_SUCCESS) {
return DECLINED;
}

ap_add_version_component(pconf, apr_psprintf(pconf, "mod_psgi/%s", MOD_PSGI_VERSION));
/* shared name to store apps */
shm_name = apr_pstrdup(pconf, "/tmp/psgi_shm");
rc = apr_shm_attach(&psgi_shm, (const char *) shm_name, pconf);
if (rc != APR_SUCCESS) {
rc = apr_shm_create(&psgi_shm, sizeof(psgi_apps_t),
(const char *) shm_name, pconf);
}
if (rc == APR_SUCCESS) {
psgi_apps = (psgi_apps_t *)apr_shm_baseaddr_get(psgi_shm);
psgi_apps->apps = apr_hash_make(pconf);
}
apr_global_mutex_unlock(psgi_mutex);

return OK;
}
Expand All @@ -752,7 +793,6 @@ static const char *cmd_psgi_app(cmd_parms *cmd, void *conf, const char *v)
{
psgi_dir_config *c = (psgi_dir_config *) conf;
c->file = (char *) apr_pstrdup(cmd->pool, v);
apr_hash_set(psgi_apps, c->file, APR_HASH_KEY_STRING, c->file);
return NULL;
}

Expand Down
23 changes: 23 additions & 0 deletions t/02_input.t
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,26 @@ args:
is_success: ok
content: foo=bar

=== read offset 0
--- request
method: POST
code: |
$env->{'psgi.input'}->read(my $buf, 1, 0);
$buf;
args:
- foo: bar
--- response
is_success: ok
content: f

=== read offset 1
--- request
method: POST
code: |
$env->{'psgi.input'}->read(my $buf, 1, 1);
$buf;
args:
- foo: bar
--- response
is_success: not ok