Пример #1
0
static lcb_error_t http_setup(lcb_t instance)
{
    char buffer[1024];
    lcb_ssize_t offset = 0;
    lcb_error_t err;
    lcb_connection_t conn = &instance->bootstrap.via.http.connection;
    struct lcb_http_bootstrap_st *http = &instance->bootstrap.via.http;

    http->weird_things_threshold = LCB_DEFAULT_CONFIG_ERRORS_THRESHOLD;
    http->bummer = 0;

    switch (instance->type) {
    case LCB_TYPE_BUCKET:
        offset = snprintf(buffer, sizeof(buffer),
                          "GET /pools/default/bucketsStreaming/%s HTTP/1.1\r\n",
                          instance->bucket);
        break;
    case LCB_TYPE_CLUSTER:
        offset = snprintf(buffer, sizeof(buffer), "GET /pools/ HTTP/1.1\r\n");
        break;
    default:
        return LCB_EINVAL;
    }

    err = lcb_connection_init(conn, instance);
    if (err != LCB_SUCCESS) {
        return err;
    }
    conn->data = instance;

    if (instance->password) {
        char cred[256];
        char base64[256];
        snprintf(cred, sizeof(cred), "%s:%s", instance->username, instance->password);
        if (lcb_base64_encode(cred, base64, sizeof(base64)) == -1) {
            lcb_destroy(instance);
            return LCB_EINTERNAL;
        }
        offset += snprintf(buffer + offset, sizeof(buffer) - (lcb_size_t)offset,
                           "Authorization: Basic %s\r\n", base64);
    }

    offset += snprintf(buffer + offset, sizeof(buffer) - (lcb_size_t)offset,
                       "%s", LCB_LAST_HTTP_HEADER);

    /* Add space for: Host: \r\n\r\n" */
    http->uri = malloc(strlen(buffer) + strlen(instance->config.backup_nodes[0]) + 80);
    if (http->uri == NULL) {
        lcb_destroy(instance);
        return LCB_CLIENT_ENOMEM;
    }
    strcpy(http->uri, buffer);

    return LCB_SUCCESS;
}
Пример #2
0
static lcb_error_t setup_request_header(http_provider *http)
{
    lcb_settings *settings = http->base.parent->settings;

    char *buf = http->request_buf;
    const lcb_host_t *hostinfo;
    lcb_size_t nbuf = sizeof(http->request_buf);

    lcb_size_t offset = 0;
    http->request_buf[0] = '\0';

    if (settings->conntype == LCB_TYPE_BUCKET) {
        offset = snprintf(buf, nbuf, REQBUCKET_FMT, settings->bucket);

    } else if (settings->conntype == LCB_TYPE_CLUSTER) {
        offset = snprintf(buf, nbuf, REQPOOLS_FMT);

    } else {
        return LCB_EINVAL;
    }

    if (settings->password) {
        char cred[256], b64[256];
        snprintf(cred, sizeof(cred), "%s:%s",
                 settings->username, settings->password);

        if (lcb_base64_encode(cred, b64, sizeof(b64)) == -1) {
            return LCB_EINTERNAL;
        }

        offset += snprintf(buf + offset, nbuf - offset, AUTHDR_FMT, b64);
    }

    hostinfo = lcb_connection_get_host(&http->connection);

    offset += snprintf(buf + offset, nbuf - offset, HOSTHDR_FMT,
                       hostinfo->host, hostinfo->port);

    offset += snprintf(buf + offset, nbuf - offset, "%s\r\n", LAST_HTTP_HEADER);

    return LCB_SUCCESS;
}