From d4958c1f2f034a42cc37cf13129f1c90b0c303ee Mon Sep 17 00:00:00 2001 From: fujiwara Date: Tue, 5 Jan 2010 19:12:10 +0900 Subject: [PATCH 1/4] $env{"psgi.input"}->read works when offset == 0 --- mod_psgi.c | 3 ++- t/02_input.t | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/mod_psgi.c b/mod_psgi.c index d19774a..7aede5a 100644 --- a/mod_psgi.c +++ b/mod_psgi.c @@ -108,12 +108,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"); } diff --git a/t/02_input.t b/t/02_input.t index e5914dd..a3060f4 100644 --- a/t/02_input.t +++ b/t/02_input.t @@ -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 + From 438313f0e6d4f7faaad44f7e822c4f275f2ef12f Mon Sep 17 00:00:00 2001 From: mattn Date: Wed, 1 Feb 2012 20:20:37 +0900 Subject: [PATCH 2/4] strawberry perl. --- Makefile.msc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile.msc b/Makefile.msc index c2c5a2e..05a15b1 100644 --- a/Makefile.msc +++ b/Makefile.msc @@ -1,6 +1,6 @@ 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 @@ -8,7 +8,7 @@ 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" From acae49af5d77752512e55ef424afc431221c175a Mon Sep 17 00:00:00 2001 From: mattn Date: Wed, 1 Feb 2012 20:21:26 +0900 Subject: [PATCH 3/4] wor for MPM. --- mod_psgi.c | 75 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/mod_psgi.c b/mod_psgi.c index 7aede5a..3bc7684 100644 --- a/mod_psgi.c +++ b/mod_psgi.c @@ -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; @@ -611,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, @@ -626,7 +645,9 @@ 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) { @@ -634,7 +655,8 @@ static int psgi_handler(request_rec *r) 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); @@ -648,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; @@ -663,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); } @@ -693,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; } @@ -708,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) { @@ -716,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; } @@ -753,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; } From 80ca349e8b549d55c2d0db2f89ca60579dde399d Mon Sep 17 00:00:00 2001 From: mattn Date: Wed, 1 Feb 2012 20:21:49 +0900 Subject: [PATCH 4/4] add Makefile for mingw32. --- Makefile.w32 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Makefile.w32 diff --git a/Makefile.w32 b/Makefile.w32 new file mode 100644 index 0000000..e241410 --- /dev/null +++ b/Makefile.w32 @@ -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/."