Ejemplo n.º 1
0
// mruby_run for not request phase.
static int ap_mruby_run_nr(const char *mruby_code_file)
{

    int n;
    struct mrb_parser_state* p;
    FILE *mrb_file;
    mrb_state *mrb = mrb_open();
    ap_mruby_class_init(mrb);

    if ((mrb_file = fopen(mruby_code_file, "r")) == NULL) {
        ap_log_error(APLOG_MARK
            , APLOG_ERR
            , 0
            , ap_server_conf
            , "%s ERROR %s: mrb file oepn failed: %s"
            , MODULE_NAME
            , __func__
            , mruby_code_file
        );
        mrb_close(mrb);
        return -1;
    }

   ap_log_error(APLOG_MARK
       , APLOG_DEBUG
       , 0
       , ap_server_conf
       , "%s DEBUG %s: cache nothing on pid %d, compile code: %s"
       , MODULE_NAME
       , __func__
       , getpid()
       , mruby_code_file
   );

    p = mrb_parse_file(mrb, mrb_file, NULL);
    fclose(mrb_file);
    n = mrb_generate_code(mrb, p);

    mrb_pool_close(p->pool);

    ap_log_error(APLOG_MARK
        , APLOG_DEBUG
        , 0
        , ap_server_conf
        , "%s DEBUG %s: run mruby code: %s"
        , MODULE_NAME
        , __func__
        , mruby_code_file
    );

    ap_mrb_set_status_code(OK);
    mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));

    if (mrb->exc)
        ap_mrb_raise_file_error_nr(mrb, mrb_obj_value(mrb->exc), mruby_code_file);

    mrb_close(mrb);

    return APR_SUCCESS;
}
Ejemplo n.º 2
0
mrb_value ap_mrb_return(mrb_state *mrb, mrb_value self)
{

    mrb_int ret;

    mrb_get_args(mrb, "i", &ret);
    ap_mrb_set_status_code((int)ret);

    return self;
}
Ejemplo n.º 3
0
static int ap_mruby_run(mrb_state *mrb, request_rec *r, mod_mruby_code_t *code, int module_status)
{

    int ai, i, last_idx;
    jmp_buf mod_mruby_jmp;
    TRACER;

    // mutex lock
    if (apr_thread_mutex_lock(mod_mruby_mutex) != APR_SUCCESS) {
        ap_log_rerror(APLOG_MARK
            , APLOG_ERR
            , 0
            , r
            , "%s ERROR %s: mod_mruby_mutex lock failed"
            , MODULE_NAME
            , __func__
        );
        return DECLINED;
    }

    ap_mrb_push_request(r);
    if (code->cache == CACHE_DISABLE) {
        mod_mruby_compile_code(mrb, code, r->server);
    }
    ai = mrb_gc_arena_save(mrb);
    ap_log_rerror(APLOG_MARK
        , APLOG_DEBUG
        , 0
        , r
        , "%s DEBUG %s: run mruby code: path=[%s] irep_idx=[%d]~[%d] cache=[%d]"
        , MODULE_NAME
        , __func__
        , code->path
        , code->irep_idx_start
        , code->irep_idx_end
        , code->cache
    );

    ap_mrb_set_status_code(OK);
    if (!setjmp(mod_mruby_jmp)) {
        mrb->jmp = &mod_mruby_jmp;
        mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[code->irep_idx_start]), mrb_top_self(mrb));
    }

    mrb->jmp = 0;
    mrb_gc_arena_restore(mrb, ai);

    if (mrb->exc)
        ap_mrb_raise_file_error(mrb, mrb_obj_value(mrb->exc), r, code->path);

    ap_log_rerror(APLOG_MARK
        , APLOG_DEBUG
        , 0
        , r
        , "%s DEBUG %s: irep_idx=%d return mruby code(%d): %s"
        , MODULE_NAME
        , __func__
        , code->irep_idx_start
        , ap_mrb_get_status_code()
        , code->path
    );
    if (code->cache == CACHE_DISABLE) {
        last_idx = mrb->irep_len;
        mrb->irep_len = code->irep_idx_start;
        for (i = code->irep_idx_start; i < last_idx; i++) {
            ap_log_rerror(APLOG_MARK
                , APLOG_DEBUG
                , 0
                , r
                , "%s DEBUG %s: irep[%d] cleaning"
                , MODULE_NAME
                , __func__
                , i
            );
            ap_mruby_irep_clean(mrb, mrb->irep[i], r);
        }
    }
    ap_mruby_state_clean(mrb);

    // mutex unlock
    if (apr_thread_mutex_unlock(mod_mruby_mutex) != APR_SUCCESS){
        ap_log_rerror(APLOG_MARK
            , APLOG_ERR
            , 0
            , r
            , "%s ERROR %s: mod_mruby_mutex unlock failed"
            , MODULE_NAME
            , __func__
        );
        return OK;
    }

    return ap_mrb_get_status_code();
}