Ejemplo n.º 1
0
void
cb_http_complete_callback(lcb_http_request_t request, lcb_t handle, const void *cookie, lcb_error_t error, const lcb_http_resp_t *resp)
{
    struct cb_context_st *ctx = (struct cb_context_st *)cookie;
    struct cb_bucket_st *bucket = ctx->bucket;
    VALUE key, val, res, exc;
    lcb_http_status_t status;

    ctx->request->completed = 1;

    if (bucket->destroying) {
        cb_context_free(ctx);
        return;
    }

    key = STR_NEW((const char*)resp->v.v0.path, resp->v.v0.npath);
    val = resp->v.v0.nbytes ? STR_NEW((const char*)resp->v.v0.bytes, resp->v.v0.nbytes) : Qnil;
    exc = ctx->exception;
    if (!RTEST(exc)) {
        exc = cb_check_error_with_status(error, "failed to execute HTTP request", key, resp->v.v0.status);
        if (exc != Qnil && val != Qnil) {
            rb_ivar_set(exc, cb_id_iv_body, val);
        }
    }
    if (RTEST(exc)) {
        if (rb_obj_is_kind_of(exc, cb_eHTTPError)) {
            rb_funcall(exc, cb_id_parse_body_bang, 0);
        }
        ctx->exception = exc;
    }
    status = resp->v.v0.status;
    if (resp->v.v0.headers) {
        cb_build_headers(ctx, resp->v.v0.headers);
        ctx->headers_val = Qnil;
    }
    if (ctx->extended) {
        res = rb_class_new_instance(0, NULL, cb_cResult);
        rb_ivar_set(res, cb_id_iv_error, ctx->exception);
        rb_ivar_set(res, cb_id_iv_status, status ? INT2FIX(status) : Qnil);
        rb_ivar_set(res, cb_id_iv_operation, cb_sym_http_request);
        rb_ivar_set(res, cb_id_iv_key, key);
        rb_ivar_set(res, cb_id_iv_value, val);
        rb_ivar_set(res, cb_id_iv_completed, Qtrue);
        rb_ivar_set(res, cb_id_iv_headers, ctx->headers_val);
    } else {
        res = val;
    }
    if (ctx->proc != Qnil) {
        cb_proc_call(bucket, ctx->proc, 1, res);
        ctx->proc = Qnil;
    }
    if (!bucket->async && ctx->exception == Qnil) {
        ctx->rv = res;
    }
    if (bucket->async) {
        cb_context_free(ctx);
    }
    (void)handle;
    (void)request;
}
Ejemplo n.º 2
0
    void
http_data_callback(lcb_http_request_t request, lcb_t handle, const void *cookie, lcb_error_t error, const lcb_http_resp_t *resp)
{
    struct context_st *ctx = (struct context_st *)cookie;
    struct bucket_st *bucket = ctx->bucket;
    VALUE key, val, res;

    key = STR_NEW((const char*)resp->v.v0.path, resp->v.v0.npath);
    ctx->exception = cb_check_error_with_status(error,
            "failed to execute HTTP request", key, resp->v.v0.status);
    val = resp->v.v0.nbytes ? STR_NEW((const char*)resp->v.v0.bytes, resp->v.v0.nbytes) : Qnil;
    if (ctx->exception != Qnil) {
        cb_gc_protect(bucket, ctx->exception);
        lcb_cancel_http_request(bucket->handle, request);
    }
    if (resp->v.v0.headers) {
        cb_build_headers(ctx, resp->v.v0.headers);
    }
    if (ctx->proc != Qnil) {
        if (ctx->extended) {
            res = rb_class_new_instance(0, NULL, cResult);
            rb_ivar_set(res, id_iv_error, ctx->exception);
            rb_ivar_set(res, id_iv_operation, sym_http_request);
            rb_ivar_set(res, id_iv_key, key);
            rb_ivar_set(res, id_iv_value, val);
            rb_ivar_set(res, id_iv_completed, Qfalse);
            rb_ivar_set(res, id_iv_headers, ctx->headers_val);
        } else {
            res = val;
        }
        cb_proc_call(ctx->proc, 1, res);
    }
    (void)handle;
}
Ejemplo n.º 3
0
void
cb_http_data_callback(lcb_http_request_t request, lcb_t handle, const void *cookie, lcb_error_t error, const lcb_http_resp_t *resp)
{
    struct cb_context_st *ctx = (struct cb_context_st *)cookie;
    struct cb_bucket_st *bucket = ctx->bucket;
    VALUE key, val, res;
    lcb_http_status_t status;

    key = STR_NEW((const char*)resp->v.v0.path, resp->v.v0.npath);
    val = resp->v.v0.nbytes ? STR_NEW((const char*)resp->v.v0.bytes, resp->v.v0.nbytes) : Qnil;
    status = resp->v.v0.status;
    if (NIL_P(ctx->exception)) {
        ctx->exception = cb_check_error_with_status(error,
                         "failed to execute HTTP request", key, resp->v.v0.status);
        if (ctx->exception != Qnil) {
            VALUE body_str = rb_ivar_get(ctx->exception, cb_id_iv_body);
            if (NIL_P(body_str)) {
                rb_ivar_set(ctx->exception, cb_id_iv_body, val);
            } else {
                rb_str_concat(body_str, val);
            }
            return;
        }
    }
    if (resp->v.v0.headers) {
        cb_build_headers(ctx, resp->v.v0.headers);
    }
    if (ctx->proc != Qnil) {
        if (ctx->extended) {
            res = rb_class_new_instance(0, NULL, cb_cResult);
            rb_ivar_set(res, cb_id_iv_error, Qnil);
            rb_ivar_set(res, cb_id_iv_status, status ? INT2FIX(status) : Qnil);
            rb_ivar_set(res, cb_id_iv_operation, cb_sym_http_request);
            rb_ivar_set(res, cb_id_iv_key, key);
            rb_ivar_set(res, cb_id_iv_value, val);
            rb_ivar_set(res, cb_id_iv_completed, Qfalse);
            rb_ivar_set(res, cb_id_iv_headers, ctx->headers_val);
        } else {
            res = val;
        }
        cb_proc_call(bucket, ctx->proc, 1, res);
    }
    (void)handle;
}