Esempio n. 1
0
void
lcbvrow_reset(lcbvrow_PARSER* ctx)
{
    /**
     * We create a copy, and set its relevant fields. All other
     * fields are zeroed implicitly. Then we copy the object back.
     */
    jsonsl_reset(ctx->jsn);
    jsonsl_reset(ctx->jsn_rdetails);

    lcb_string_clear(&ctx->current_buf);
    lcb_string_clear(&ctx->meta_buf);
    lcb_string_clear(&ctx->last_hk);

    /* Initially all callbacks are enabled so that we can search for the
     * rows array. */
    ctx->jsn->action_callback_POP = initial_pop_callback;
    ctx->jsn->action_callback_PUSH = initial_push_callback;
    ctx->jsn->error_callback = parse_error_callback;
    ctx->jsn->max_callback_level = 4;
    ctx->jsn->data = ctx;
    jsonsl_enable_all_callbacks(ctx->jsn);

    ctx->have_error = 0;
    ctx->initialized = 0;
    ctx->meta_complete = 0;
    ctx->rowcount = 0;
    ctx->min_pos = 0;
    ctx->keep_pos = 0;
    ctx->header_len = 0;
    ctx->last_row_endpos = 0;
}
Esempio n. 2
0
void
lcb_n1p_reset(lcb_N1QLPARAMS *params)
{
    lcb_string_clear(&params->form);
    lcb_string_clear(&params->posargs);
    lcb_string_clear(&params->scanvec);
    params->scanvec_type = SCANVEC_NONE;
}
Esempio n. 3
0
static void reset_stream_state(http_provider *http)
{
    lcb_string_clear(&http->stream.chunk);
    lcb_string_clear(&http->stream.input);
    lcb_string_clear(&http->stream.header);

    if (http->stream.config) {
        lcb_clconfig_decref(http->stream.config);
    }

    http->stream.config = NULL;

    lcb_assert(LCB_SUCCESS == lcb_connection_reset_buffers(&http->connection));
}
Esempio n. 4
0
static void
initial_pop_callback(jsonsl_t jsn, jsonsl_action_t action,
    struct jsonsl_state_st *state, const jsonsl_char_t *at)
{
    lcbvrow_PARSER *ctx = jsn->data;
    char *key;
    unsigned long len;

    if (ctx->have_error) {
        return;
    }
    if (JSONSL_STATE_IS_CONTAINER(state)) {
        return;
    }
    if (state->type != JSONSL_T_HKEY) {
        return;
    }

    key = ctx->current_buf.base + state->pos_begin;
    len = jsn->pos - state->pos_begin;
    NORMALIZE_OFFSETS(key, len);

    lcb_string_clear(&ctx->last_hk);
    buffer_append(&ctx->last_hk, key, len);

    (void)action; (void)at;
}
Esempio n. 5
0
static int
ensure_scratch(lcb_t instance, lcb_SIZE capacity)
{
    if (!instance->scratch) {
        instance->scratch = calloc(1, sizeof(*instance->scratch));
        if (!instance->scratch) {
            return 0;
        }
        lcb_string_init(instance->scratch);
    } else {
        lcb_string_clear(instance->scratch);
    }

    if (0 != lcb_string_reserve(instance->scratch, capacity)) {
        return 0;
    }
    return 1;
}
Esempio n. 6
0
/**
 * Called to retrive the mechlist from the packet.
 * @return 0 to continue authentication, 1 if no authentication needed, or
 * -1 on error.
 */
static int
set_chosen_mech(mc_pSESSREQ sreq, lcb_string *mechlist, const char **data,
                unsigned int *ndata)
{
    cbsasl_error_t saslerr;
    const char *chosenmech;
    mc_pSESSINFO ctx = sreq->inner;

    lcb_assert(sreq->inner);
    if (ctx->settings->sasl_mech_force) {
        char *forcemech = ctx->settings->sasl_mech_force;
        if (!strstr(mechlist->base, forcemech)) {
            /** Requested mechanism not found */
            set_error_ex(sreq, LCB_SASLMECH_UNAVAILABLE, mechlist->base);
            return -1;
        }

        lcb_string_clear(mechlist);
        if (lcb_string_appendz(mechlist, forcemech)) {
            set_error_ex(sreq, LCB_CLIENT_ENOMEM, NULL);
            return -1;
        }
    }

    saslerr = cbsasl_client_start(ctx->sasl, mechlist->base,
                                  NULL, data, ndata, &chosenmech);
    switch (saslerr) {
    case SASL_OK:
        ctx->nmech = strlen(chosenmech);
        if (! (ctx->mech = strdup(chosenmech)) ) {
            set_error_ex(sreq, LCB_CLIENT_ENOMEM, NULL);
            return -1;
        }
        return 0;
    case SASL_NOMECH:
        lcb_log(LOGARGS(sreq, INFO), SESSREQ_LOGFMT "Server does not support SASL (no mechanisms supported)", SESSREQ_LOGID(sreq));
        return 1;
        break;
    default:
        lcb_log(LOGARGS(sreq, INFO), SESSREQ_LOGFMT "cbsasl_client_start returned %d", SESSREQ_LOGID(sreq), saslerr);
        set_error_ex(sreq, LCB_EINTERNAL, "Couldn't start SASL client");
        return -1;
    }
}
Esempio n. 7
0
/**
 * This is called for the first few tokens, where we are still searching
 * for the row set.
 */
static void
initial_push_callback(jsonsl_t jsn, jsonsl_action_t action,
    struct jsonsl_state_st *state, const jsonsl_char_t *at)
{
    lcbvrow_PARSER *ctx = (lcbvrow_PARSER*)jsn->data;
    jsonsl_jpr_match_t match = JSONSL_MATCH_UNKNOWN;

    if (ctx->have_error) {
        return;
    }

    if (JSONSL_STATE_IS_CONTAINER(state)) {
        jsonsl_jpr_match_state(jsn, state, ctx->last_hk.base, ctx->last_hk.nused,
            &match);
    }

    lcb_string_clear(&ctx->last_hk);

    if (ctx->initialized == 0) {
        if (state->type != JSONSL_T_OBJECT) {
            ctx->have_error = 1;
            return;
        }

        if (match != JSONSL_MATCH_POSSIBLE) {
            ctx->have_error = 1;
            return;
        }
        /* tag the state */
        state->data = JOBJ_RESPONSE_ROOT;
        ctx->initialized = 1;
        return;
    }

    if (state->type == JSONSL_T_LIST && match == JSONSL_MATCH_POSSIBLE) {
        /* we have a match, e.g. "rows:[]" */
        jsn->action_callback_POP = row_pop_callback;
        jsn->action_callback_PUSH = meta_header_complete_callback;
        state->data = JOBJ_ROWSET;
    }

    (void)action; /* always PUSH */
    (void)at;
}
Esempio n. 8
0
LIBCOUCHBASE_API
const char *
lcb_n1p_encode(lcb_N1QLPARAMS *params, lcb_error_t *err)
{
    lcb_error_t err_s = LCB_SUCCESS;
    if (!err) {
        err = &err_s;
    }
    /* Build the query */

    lcb_string_clear(&params->reqbuf);

    if (!params->form.nused) {
        *err = LCB_EINVAL;
        return NULL;
    }

    if (-1 == lcb_string_append(
                &params->reqbuf, params->form.base, params->form.nused)) {
        *err = LCB_CLIENT_ENOMEM;
        return NULL;
    }

    if ((*err = finalize_field(params, &params->posargs, "args", "]"))
            != LCB_SUCCESS) {
        return NULL;
    }

    if (params->scanvec.nused) {
        if (params->consist_type != LCB_N1P_CONSISTENCY_RYOW) {
            *err = LCB_OPTIONS_CONFLICT;
            return NULL;
        }
        if ((*err = finalize_field(params, &params->scanvec, "scan_vector", "}"))
                != LCB_SUCCESS) {
            return NULL;
        }
    }

    if (params->consist_type) {
        if (-1 == lcb_string_reserve(&params->reqbuf, 15)) {
            *err = LCB_CLIENT_ENOMEM;
            return NULL;
        }
    }

    if (params->consist_type == LCB_N1P_CONSISTENCY_RYOW) {
        if (!params->scanvec.nused) {
            *err = LCB_OPTIONS_CONFLICT;
            return NULL;
        } else {
            set_reqbuf_optz(params, PARAM_CONSISTENT, "at_plus");
        }
    } else if (params->consist_type == LCB_N1P_CONSISTENCY_REQUEST) {
        set_reqbuf_optz(params, PARAM_CONSISTENT, "request_plus");
    } else if (params->consist_type == LCB_N1P_CONSISTENCY_STATEMENT) {
        set_reqbuf_optz(params, PARAM_CONSISTENT, "statement_plus");
    } else if (params->consist_type == LCB_N1P_CONSISTENCY_NONE) {
        /* Nothing */
    } else {
        *err = LCB_EINVAL;
        return NULL;
    }

    return params->reqbuf.base;
}