From f041d1095ddf7541c4b275e16d2ed3355a8e2be9 Mon Sep 17 00:00:00 2001 From: Unbit Date: Wed, 5 Feb 2014 11:21:01 +0100 Subject: [PATCH] perl refactoring --- plugins/coroae/coroae.c | 18 ++-------- plugins/psgi/psgi_loader.c | 73 ++++++++++++++++++++++++++++++-------- 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/plugins/coroae/coroae.c b/plugins/coroae/coroae.c index 766c2813..585b16d4 100644 --- a/plugins/coroae/coroae.c +++ b/plugins/coroae/coroae.c @@ -386,21 +386,9 @@ static void coroae_loop() { exit(1); } - perl_eval_pv("use Coro;", 0); - if (SvTRUE(ERRSV)) { - uwsgi_log("unable to load Coro module\n"); - exit(1); - } - perl_eval_pv("use AnyEvent;", 0); - if (SvTRUE(ERRSV)) { - uwsgi_log("unable to load AnyEvent module\n"); - exit(1); - } - perl_eval_pv("use Coro::AnyEvent;", 0); - if (SvTRUE(ERRSV)) { - uwsgi_log("unable to load Coro::AnyEvent module\n"); - exit(1); - } + perl_eval_pv("use Coro;", 1); + perl_eval_pv("use AnyEvent;", 1); + perl_eval_pv("use Coro::AnyEvent;", 1); uwsgi.current_wsgi_req = coroae_current_wsgi_req; uwsgi.wait_write_hook = coroae_wait_fd_write; diff --git a/plugins/psgi/psgi_loader.c b/plugins/psgi/psgi_loader.c index 30c5ff45..8b9ec508 100644 --- a/plugins/psgi/psgi_loader.c +++ b/plugins/psgi/psgi_loader.c @@ -107,11 +107,62 @@ XS(XS_input_read) { SV *read_buf = ST(1); unsigned long arg_len = SvIV(ST(2)); + long offset = 0; + uwsgi_log("items = %d\n", items); + if (items > 2) { + uwsgi_log("OFFSET\n"); + offset = (long) SvIV(ST(3)); + } + + uwsgi_log("offset = %lld\n", offset); + ssize_t rlen = 0; char *buf = uwsgi_request_body_read(wsgi_req, arg_len, &rlen); if (buf) { - sv_setpvn(read_buf, buf, rlen); + if (rlen > 0 && offset != 0) { + STRLEN orig_len; + // get data from original string + char *orig = SvPV(read_buf, orig_len); + size_t new_size = orig_len; + // check for negative case first + if (offset < 0) { + // first of all get the new orig_len; + offset = abs(offset); + if (offset > (long) orig_len) { + new_size = offset; + offset = 0; + } + else { + offset = orig_len - offset; + } + } + // still valid ? + if (offset > 0) { + // if the new string is bigger than the old one, allocate a bigger chunk + if ((size_t) rlen + offset > orig_len) { + new_size += rlen; + new_size -= offset; + } + // if offset is bigger than orig_len, pad with "\0", so we use (slower) calloc + char *new_buf = uwsgi_calloc(new_size); + // put back older value + memcpy(new_buf, orig, orig_len); + // put the new value + memcpy(new_buf + offset, buf, rlen); + // free the old value + free(buf); + sv_setpvn(read_buf, new_buf, new_size); + } + // fallback + else { + goto zerofallback; + } + } + else { +zerofallback: + sv_setpvn(read_buf, buf, rlen); + } goto ret; } @@ -352,16 +403,12 @@ int init_psgi_app(struct wsgi_request *wsgi_req, char *app, uint16_t app_len, Pe } } - perl_eval_pv("use IO::Handle;", 0); - perl_eval_pv("use IO::File;", 0); - perl_eval_pv("use IO::Socket;", 0); - perl_eval_pv("use Scalar::Util;", 0); + perl_eval_pv("use IO::Handle;", 1); + perl_eval_pv("use IO::File;", 1); + perl_eval_pv("use IO::Socket;", 1); + perl_eval_pv("use Scalar::Util;", 1); if (!uperl.no_die_catch) { - perl_eval_pv("use Devel::StackTrace;", 0); - if (!SvTRUE(ERRSV)) { - uperl.stacktrace_available = 1; - perl_eval_pv("$SIG{__DIE__} = \\&uwsgi::stacktrace;", 0); - } + perl_eval_pv("use Devel::StackTrace; $SIG{__DIE__} = \\&uwsgi::stacktrace;", 0); } if (uperl.argv_items || uperl.argv_item) { @@ -507,10 +554,6 @@ int uwsgi_perl_mule(char *opt) { void uwsgi_perl_exec(char *filename) { size_t size = 0; char *buf = uwsgi_open_and_read(filename, &size, 1, NULL); - perl_eval_pv(buf, 0); + perl_eval_pv(buf, 1); free(buf); - if (SvTRUE(ERRSV)) { - uwsgi_log("%s", SvPV_nolen(ERRSV)); - exit(1); - } }