示例#1
0
static bool
thingspeak_channel_update_send(void *data)
{
    struct thingspeak_channel_update_data *mdata = data;
    struct sol_http_params params;
    struct sol_http_client_connection *connection;
    char field_name[] = "fieldX";
    size_t i;
    int r;

    sol_http_params_init(&params);

    if (!sol_http_param_add(&params,
        SOL_HTTP_REQUEST_PARAM_POST_FIELD("api_key", mdata->api_key))) {
        SOL_WRN("Could not add API key");
        goto out;
    }

    if (mdata->status) {
        if (!sol_http_param_add(&params,
            SOL_HTTP_REQUEST_PARAM_POST_FIELD("status", mdata->status))) {
            SOL_WRN("Could not add status field to POST parameters");
            goto out;
        }
    }

    for (i = 0; i < sol_util_array_size(mdata->fields); i++) {
        if (!mdata->fields[i])
            continue;

        field_name[sizeof("field") - 1] = i + '1';
        if (!sol_http_param_add(&params,
            SOL_HTTP_REQUEST_PARAM_POST_FIELD(strdupa(field_name), mdata->fields[i]))) {
            SOL_WRN("Could not add status field to POST %s parameters",
                field_name);
            goto out;
        }
    }

    connection = sol_http_client_request(SOL_HTTP_METHOD_POST,
        mdata->endpoint, &params, thingspeak_channel_update_finished, mdata);
    if (!connection) {
        SOL_WRN("Could not create HTTP request");
        goto out;
    }

    r = sol_ptr_vector_append(&mdata->pending_conns, connection);
    if (r < 0) {
        SOL_WRN("Failed to keep pending connection.");
        sol_http_client_connection_cancel(connection);
    }

out:
    sol_http_params_clear(&params);

    mdata->timeout = NULL;
    return false;
}
示例#2
0
static bool
thingspeak_execute_poll(void *data)
{
    struct thingspeak_execute_data *mdata = data;
    struct sol_http_params params;
    struct sol_http_client_connection *connection;
    int r;

    sol_http_params_init(&params);
    if (!sol_http_param_add(&params,
        SOL_HTTP_REQUEST_PARAM_POST_FIELD("api_key", mdata->talkback.api_key))) {
        SOL_WRN("Could not set API key");
        mdata->timeout = NULL;
        sol_http_params_clear(&params);
        return false;
    }

    connection = sol_http_client_request(SOL_HTTP_METHOD_POST,
        mdata->talkback.endpoint, &params,
        thingspeak_execute_poll_finished, mdata);

    sol_http_params_clear(&params);

    if (!connection) {
        SOL_WRN("Could not create HTTP request");
        mdata->timeout = NULL;
        return false;
    }

    r = sol_ptr_vector_append(&mdata->pending_conns, connection);
    if (r < 0) {
        SOL_WRN("Failed to keep pending connection.");
        sol_http_client_connection_cancel(connection);
        return false;
    }

    return true;
}
示例#3
0
static int
thingspeak_add_in_process(struct sol_flow_node *node, void *data,
    uint16_t port, uint16_t conn_id, const struct sol_flow_packet *packet)
{
    struct thingspeak_add_data *mdata = data;
    const char *cmd_str;
    struct sol_http_params params;
    struct sol_http_client_connection *connection;
    int error_code = 0;
    int r;

    r = sol_flow_packet_get_string(packet, &cmd_str);
    if (r < 0) {
        SOL_WRN("Could not get command string from packet");
        return -EINVAL;
    }

    sol_http_params_init(&params);

    if (!sol_http_param_add(&params,
        SOL_HTTP_REQUEST_PARAM_POST_FIELD("api_key", mdata->talkback.api_key))) {
        SOL_WRN("Could not add API key");
        error_code = -ENOMEM;
        goto out;
    }

    if (!sol_http_param_add(&params,
        SOL_HTTP_REQUEST_PARAM_POST_FIELD("command_string", cmd_str))) {
        SOL_WRN("Could not add command string");
        error_code = -ENOMEM;
        goto out;
    }

    if (mdata->position >= 0) {
        char position_str[3 * sizeof(int)];
        char *pos_str;

        r = snprintf(position_str, sizeof(position_str), "%d", mdata->position);
        if (r < 0 || r >= (int)sizeof(position_str)) {
            SOL_WRN("Could not convert position to string");
            error_code = -ENOMEM;
            goto out;
        }

        /* Use pos_str at SOL_HTTP_REQUEST_PARAM_POST_FIELD macro, otherwise
           the compiler will complain about an "always true" comparison.
         */
        pos_str = position_str;
        if (!sol_http_param_add(&params,
            SOL_HTTP_REQUEST_PARAM_POST_FIELD("position", pos_str))) {
            SOL_WRN("Could not add position");
            error_code = -ENOMEM;
            goto out;
        }
    }

    connection = sol_http_client_request(SOL_HTTP_METHOD_POST,
        mdata->talkback.endpoint,
        &params, thingspeak_add_request_finished, mdata);
    if (!connection) {
        SOL_WRN("Could not create HTTP request");
        error_code = -EINVAL;
        goto out;
    }

    r = sol_ptr_vector_append(&mdata->pending_conns, connection);
    if (r < 0) {
        SOL_WRN("Failed to keep pending connection.");
        sol_http_client_connection_cancel(connection);
        error_code = -ENOMEM;
    }

out:
    sol_http_params_clear(&params);
    return error_code;
}
示例#4
0
static void
test_split_urls(void)
{
#define SET_PARAMS(_url, _scheme, _user, _pass, _host, _path, _query, _fragment, _port, _result, _check_url) \
    { SOL_STR_SLICE_LITERAL(_url), { SOL_STR_SLICE_LITERAL(_scheme), SOL_STR_SLICE_LITERAL(_user), SOL_STR_SLICE_LITERAL(_pass), \
                                     SOL_STR_SLICE_LITERAL(_host), SOL_STR_SLICE_LITERAL(_path), \
                                     SOL_STR_SLICE_LITERAL(_query), SOL_STR_SLICE_LITERAL(_fragment), _port }, _result, _check_url }
    size_t i;
    int r;
    static const struct {
        struct sol_str_slice url;
        struct sol_http_url splitted_url;
        int result;
        bool check_url;
    } test_split[] =  {
        SET_PARAMS("http://[2001:db8::1]", "http", "", "", "2001:db8::1", "", "", "", 0, 0, true),
        SET_PARAMS("http://*****:*****@[::1]:123/a/b?p=1&c=2#/a/b", "foo", "user", "pass", "::1", "/a/b", "p=1&c=2", "/a/b", 123, 0, true),
        SET_PARAMS("foo://user@[::1]:123/a/b?p=1&c=2#/a/b", "foo", "user", "", "::1", "/a/b", "p=1&c=2", "/a/b", 123, 0, true),
        SET_PARAMS("foo://user:@[::1]:123/a/b?p=1&c=2#/a/b", "foo", "user", "", "::1", "/a/b", "p=1&c=2", "/a/b", 123, 0, false),
        SET_PARAMS("foo://[::1]:123/a/b?p=1&c=2#/a/b", "foo", "", "", "::1", "/a/b", "p=1&c=2", "/a/b", 123, 0, true),
        SET_PARAMS("foo://[::1]/a/b?p=1&c=2#/a/b", "foo", "", "", "::1", "/a/b", "p=1&c=2", "/a/b", 0, 0, true),
        SET_PARAMS("foo://[::1]/?p=1&c=2#/a/b", "foo", "", "", "::1", "/", "p=1&c=2", "/a/b", 0, 0, true),
        SET_PARAMS("foo://[::1]/?p=1&c=2", "foo", "", "", "::1", "/", "p=1&c=2", "", 0, 0, true),
        SET_PARAMS("foo://[::1]/#/a/b", "foo", "", "", "::1", "/", "", "/a/b", 0, 0, true),
        SET_PARAMS("foo://[::1]?p=1&c=2", "foo", "", "", "::1", "", "p=1&c=2", "", 0, 0, true),
        SET_PARAMS("foo://[::1]#/a/b", "foo", "", "", "::1", "", "", "/a/b", 0, 0, true),
        SET_PARAMS("foo://[::1]:123/#/a/b", "foo", "", "", "::1", "/", "", "/a/b", 123, 0, true),
        SET_PARAMS("file://[::1]/usr/home/user/hi.txt", "file", "", "", "::1", "/usr/home/user/hi.txt", "", "", 0, 0, true),
        SET_PARAMS("foo://[::1]/?go", "foo", "", "", "::1", "/", "go", "", 0, 0, true),
        SET_PARAMS("foo://:password@[::1]", "foo", "", "password", "::1", "", "", "", 0, 0, true),
        SET_PARAMS("foo://:@[::1]", "foo", "", "", "::1", "", "", "", 0, 0, false),
        SET_PARAMS("foo://@[::1]", "foo", "", "", "::1", "", "", "", 0, 0, false),

        SET_PARAMS("www.intel.com.br", "", "", "", "", "", "", "", 0, -EINVAL, false),
        SET_PARAMS(":www.intel.com", "", "", "", "", "", "", "", 0, -EINVAL, false),
        SET_PARAMS("//www.intel.com", "", "", "", "", "", "", "", 0, -EINVAL, false),
        SET_PARAMS("://www.intel.com", "", "", "", "", "", "", "", 0, -EINVAL, false),
        SET_PARAMS("/a/b", "", "", "", "", "", "", "", 0, -EINVAL, false),
        SET_PARAMS("//a/b", "", "", "", "", "", "", "", 0, -EINVAL, false),
        SET_PARAMS("http://", "", "", "", "", "", "", "", 0, -EINVAL, false),
        SET_PARAMS("http://www.intel.com:/", "http", "", "", "www.intel.com", "/", "", "", 0, 0, false),
        SET_PARAMS("http://intel.com/?go=2", "http", "", "", "intel.com", "/", "go=2", "", 0, 0, true),
        SET_PARAMS("http://www.intel.com:8080", "http", "", "", "www.intel.com", "", "", "", 8080, 0, true),
        SET_PARAMS("http://www.intel.com:1234/", "http", "", "", "www.intel.com", "/", "", "", 1234, 0, true),
        SET_PARAMS("http://www.intel.com/a/b/d?go=2#fragment", "http", "", "", "www.intel.com", "/a/b/d", "go=2", "fragment", 0, 0, true),
        SET_PARAMS("foo://*****:*****@server.com:123/a/b?p=1&c=2#/a/b", "foo", "user", "pass", "server.com", "/a/b", "p=1&c=2", "/a/b", 123, 0, true),
        SET_PARAMS("foo://[email protected]:123/a/b?p=1&c=2#/a/b", "foo", "user", "", "server.com", "/a/b", "p=1&c=2", "/a/b", 123, 0, true),
        /* Do not check the created url for this one, Altought the created one will be correct it will not match,
           as the create url will be foo://user:@server.com:123/a/b?p=1&c=2#/a/b
           This behaviour is acceptable, since ':' can be ommited if the password is not provided.
         */
        SET_PARAMS("foo://user:@server.com:123/a/b?p=1&c=2#/a/b", "foo", "user", "", "server.com", "/a/b", "p=1&c=2", "/a/b", 123, 0, false),
        SET_PARAMS("foo://server.com:123/a/b?p=1&c=2#/a/b", "foo", "", "", "server.com", "/a/b", "p=1&c=2", "/a/b", 123, 0, true),
        SET_PARAMS("foo://server.com/a/b?p=1&c=2#/a/b", "foo", "", "", "server.com", "/a/b", "p=1&c=2", "/a/b", 0, 0, true),
        SET_PARAMS("foo://server.com/?p=1&c=2#/a/b", "foo", "", "", "server.com", "/", "p=1&c=2", "/a/b", 0, 0, true),
        SET_PARAMS("foo://server.com/?p=1&c=2", "foo", "", "", "server.com", "/", "p=1&c=2", "", 0, 0, true),
        SET_PARAMS("foo://server.com/#/a/b", "foo", "", "", "server.com", "/", "", "/a/b", 0, 0, true),
        SET_PARAMS("foo://server.com?p=1&c=2", "foo", "", "", "server.com", "", "p=1&c=2", "", 0, 0, true),
        SET_PARAMS("foo://server.com#/a/b", "foo", "", "", "server.com", "", "", "/a/b", 0, 0, true),
        SET_PARAMS("foo://192.3.3.3:123/#/a/b", "foo", "", "", "192.3.3.3", "/", "", "/a/b", 123, 0, true),
        SET_PARAMS("mailto:[email protected]", "mailto", "", "", "", "*****@*****.**", "", "", 0, 0, true),
        SET_PARAMS("file://localhost/usr/home/user/hi.txt", "file", "", "", "localhost", "/usr/home/user/hi.txt", "", "", 0, 0, true),
        SET_PARAMS("foo://localhost/?go", "foo", "", "", "localhost", "/", "go", "", 0, 0, true),
        SET_PARAMS("foo://:password@localhost", "foo", "", "password", "localhost", "", "", "", 0, 0, true),
        SET_PARAMS("foo://:@localhost", "foo", "", "", "localhost", "", "", "", 0, 0, false),
        SET_PARAMS("foo://@localhost", "foo", "", "", "localhost", "", "", "", 0, 0, false),
    };

    for (i = 0; i < SOL_UTIL_ARRAY_SIZE(test_split); i++) {
        struct sol_http_url splitted;
        struct sol_http_params params;
        struct sol_buffer out_uri = SOL_BUFFER_INIT_EMPTY;

        r = sol_http_split_uri(test_split[i].url, &splitted);
        ASSERT_INT_EQ(r, test_split[i].result);
        if (test_split[i].result < 0)
            continue;
        ASSERT(sol_str_slice_eq(splitted.scheme, test_split[i].splitted_url.scheme));
        ASSERT(sol_str_slice_eq(splitted.host, test_split[i].splitted_url.host));
        ASSERT(sol_str_slice_eq(splitted.path, test_split[i].splitted_url.path));
        ASSERT(sol_str_slice_eq(splitted.fragment, test_split[i].splitted_url.fragment));
        ASSERT(sol_str_slice_eq(splitted.query, test_split[i].splitted_url.query));
        ASSERT(sol_str_slice_eq(splitted.user, test_split[i].splitted_url.user));
        ASSERT(sol_str_slice_eq(splitted.password, test_split[i].splitted_url.password));
        ASSERT_INT_EQ(splitted.port, test_split[i].splitted_url.port);
        if (!test_split[i].check_url)
            continue;
        sol_http_params_init(&params);
        r = sol_http_decode_params(splitted.query, SOL_HTTP_PARAM_QUERY_PARAM, &params);
        ASSERT_INT_EQ(r, 0);
        r = sol_http_create_uri(&out_uri, splitted, &params);
        ASSERT_INT_EQ(r, 0);
        ASSERT(sol_str_slice_eq(test_split[i].url, sol_buffer_get_slice(&out_uri)));
        sol_http_params_clear(&params);
        sol_buffer_fini(&out_uri);
    }

#undef SET_PARAMS
}