char* Field::mutable_value_as_null_string() const { Internal::check_type(NULL_STRING, type()); char* cs; throw_if_error(ib_field_mutable_value(ib(), ib_ftype_nulstr_mutable_out(&cs) )); return cs; }
ByteString Field::mutable_value_as_byte_string() const { Internal::check_type(BYTE_STRING, type()); ib_bytestr_t* bs; throw_if_error(ib_field_mutable_value(ib(), ib_ftype_bytestr_mutable_out(&bs) )); return ByteString(bs); }
int64_t& Field::mutable_value_as_number() const { Internal::check_type(NUMBER, type()); ib_num_t* n; throw_if_error(ib_field_mutable_value(ib(), ib_ftype_num_mutable_out(&n) )); return *n; }
long double& Field::mutable_value_as_float() const { Internal::check_type(FLOAT, type()); ib_float_t* n; throw_if_error(ib_field_mutable_value(ib(), ib_ftype_float_mutable_out(&n) )); return *n; }
uint64_t& Field::mutable_value_as_time() const { Internal::check_type(TIME, type()); ib_time_t* n; throw_if_error(ib_field_mutable_value(ib(), ib_ftype_time_mutable_out(&n) )); return *n; }
ib_status_t ib_field_mutable_value_type( ib_field_t *f, void *mutable_out_pval, ib_ftype_t t ) { ib_status_t rc; /* Compare the types */ if (f->type != t) { return IB_EINVAL; } /* Return the value as normal. */ rc = ib_field_mutable_value(f, mutable_out_pval); return rc; }
/** * Get the capture list from a field. * * @param[in] capture Field to get list from. * @param[out] olist If not NULL, pointer to the capture item's list * * @returns * - IB_OK: All OK * - Other if can't fetch field value. */ static ib_status_t get_capture_list( ib_field_t *capture, ib_list_t **olist ) { ib_status_t rc; ib_list_t *list = NULL; assert(capture != NULL); assert(capture->type == IB_FTYPE_LIST); rc = ib_field_mutable_value(capture, ib_ftype_list_mutable_out(&list)); if (rc != IB_OK) { return rc; } if (olist != NULL) { *olist = list; } return rc; }
/// @test Test ironbee library - data provider TEST(TestIronBee, test_data_dynf) { ib_engine_t *ib; ib_data_config_t *dataconfig; ib_data_t *data; ib_field_t *dynf; ib_field_t *f; ib_field_t *f2; ib_status_t rc; ib_num_t n; ib_list_t* l; ibtest_engine_create(&ib); ASSERT_EQ(IB_OK, ib_data_config_create(ib_engine_pool_main_get(ib), & dataconfig)); ASSERT_EQ(IB_OK, ib_data_create(dataconfig, ib_engine_pool_main_get(ib), &data)); ASSERT_TRUE(data); /* Create a field with no initial value. */ ASSERT_EQ( IB_OK, ib_field_create_dynamic( &dynf, ib_engine_pool_main_get(ib), IB_FIELD_NAME("test_dynf"), IB_FTYPE_LIST, dyn_get, (void *)ib_engine_pool_main_get(ib), NULL, NULL ) ); ASSERT_TRUE(dynf); ASSERT_EQ(9UL, dynf->nlen); ASSERT_MEMEQ("test_dynf", dynf->name, 9); /* Add the field to the data store. */ ASSERT_EQ(IB_OK, ib_data_add(data, dynf)); /* Fetch the field from the data store */ ASSERT_EQ(IB_OK, ib_data_get(data, "test_dynf", &f)); ASSERT_TRUE(f); ASSERT_EQ(dynf, f); /* Fetch a dynamic field from the data store */ ASSERT_EQ(IB_OK, ib_data_get(data, "test_dynf:dyn_subkey", &f)); ASSERT_TRUE(f); ASSERT_EQ(9UL, f->nlen); /* Get the list value from the dynamic field. */ rc = ib_field_mutable_value(f, ib_ftype_list_mutable_out(&l)); ASSERT_EQ(IB_OK, rc); ASSERT_EQ(1UL, ib_list_elements(l)); /* Get the single value from the list. */ f2 = (ib_field_t *)ib_list_node_data(ib_list_first(l)); ASSERT_TRUE(f2); ASSERT_EQ(10UL, f2->nlen); rc = ib_field_value(f2, ib_ftype_num_out(&n)); ASSERT_EQ(IB_OK, rc); ASSERT_EQ(5, n); /* Fetch a another subkey */ ASSERT_EQ(IB_OK, ib_data_get(data, "test_dynf:dyn_subkey2", &f)); ASSERT_TRUE(f); ASSERT_EQ(9UL, f->nlen); /* Get the list value from the dynamic field. */ rc = ib_field_mutable_value(f, ib_ftype_list_mutable_out(&l)); ASSERT_EQ(IB_OK, rc); ASSERT_EQ(1UL, ib_list_elements(l)); /* Get the single value from the list. */ f2 = (ib_field_t *)ib_list_node_data(ib_list_first(l)); ASSERT_TRUE(f2); ASSERT_EQ(11UL, f2->nlen); rc = ib_field_value(f2, ib_ftype_num_out(&n)); ASSERT_EQ(IB_OK, rc); ASSERT_EQ(5, n); ibtest_engine_destroy(ib); }
/* * Callback used to generate request header fields. */ static ib_status_t core_gen_request_header_fields(ib_engine_t *ib, ib_tx_t *tx, ib_state_event_type_t event, void *cbdata) { ib_field_t *f; ib_status_t rc; assert(ib != NULL); assert(tx != NULL); assert(event == request_header_finished_event); /** * Alias connection remote and server addresses */ rc = ib_data_get(tx->conn->data, "server_addr", &f); if (rc != IB_OK) { return rc; } rc = ib_data_add(tx->data, f); if (rc != IB_OK) { return rc; } rc = ib_data_get(tx->conn->data, "server_port", &f); if (rc != IB_OK) { return rc; } rc = ib_data_add(tx->data, f); if (rc != IB_OK) { return rc; } rc = ib_data_get(tx->conn->data, "remote_addr", &f); if (rc != IB_OK) { return rc; } rc = ib_data_add(tx->data, f); if (rc != IB_OK) { return rc; } rc = ib_data_get(tx->conn->data, "remote_port", &f); if (rc != IB_OK) { return rc; } rc = ib_data_add(tx->data, f); if (rc != IB_OK) { return rc; } core_gen_tx_numeric_field(tx, "conn_tx_count", tx->conn->tx_count); if (tx->request_line != NULL) { core_gen_tx_bytestr_alias_field(tx, "request_line", tx->request_line->raw); core_gen_tx_bytestr_alias_field(tx, "request_method", tx->request_line->method); core_gen_tx_bytestr_alias_field(tx, "request_uri_raw", tx->request_line->uri); core_gen_tx_bytestr_alias_field(tx, "request_protocol", tx->request_line->protocol); } /* Populate the ARGS collection. */ rc = ib_data_get(tx->data, "ARGS", &f); if (rc == IB_OK) { ib_field_t *param_list; /* Add request URI parameters to ARGS collection. */ rc = ib_data_get(tx->data, "request_uri_params", ¶m_list); if (rc == IB_OK) { ib_list_t *field_list; ib_list_node_t *node = NULL; rc = ib_field_mutable_value( param_list, ib_ftype_list_mutable_out(&field_list) ); if (rc != IB_OK) { return rc; } IB_LIST_LOOP(field_list, node) { ib_field_t *param = (ib_field_t *)ib_list_node_data(node); /* Add the field to the ARGS collection. */ rc = ib_field_list_add(f, param); if (rc != IB_OK) { ib_log_notice_tx(tx, "Failed to add parameter to " "ARGS collection: %s", ib_status_to_string(rc)); } }
/** * Create an alias list collection. * * @param ib Engine. * @param tx Transaction. * @param name Collection name * @param header Header list to alias * * @returns Status code */ static ib_status_t create_header_alias_list( ib_engine_t *ib, ib_tx_t *tx, const char *name, ib_parsed_header_wrapper_t *header) { ib_field_t *f; ib_list_t *header_list; ib_status_t rc; ib_parsed_name_value_pair_list_t *nvpair; assert(ib != NULL); assert(tx != NULL); assert(name != NULL); assert(header != NULL); /* Create the list */ rc = ib_data_get(tx->data, name, &f); if (rc == IB_ENOENT) { rc = ib_data_add_list(tx->data, name, &f); if (rc != IB_OK) { return rc; } } else if (rc != IB_OK) { return rc; } rc = ib_field_mutable_value(f, ib_ftype_list_mutable_out(&header_list)); if (rc != IB_OK) { return rc; } /* Loop through the list & alias everything */ for(nvpair = header->head; nvpair != NULL; nvpair = nvpair->next) { assert(nvpair); assert(nvpair->value); ib_bytestr_t *bs = NULL; if (ib_bytestr_ptr(nvpair->value) != NULL) { rc = ib_bytestr_alias_mem( &bs, tx->mp, ib_bytestr_ptr(nvpair->value), ib_bytestr_length(nvpair->value) ); } else { rc = ib_bytestr_dup_mem(&bs, tx->mp, (const uint8_t *)"", 0); } if (rc != IB_OK) { ib_log_error_tx( tx, "Error creating bytestring of '%.*s' for %s: %s", (int)ib_bytestr_length(nvpair->name), (const char *)ib_bytestr_ptr(nvpair->name), name, ib_status_to_string(rc) ); return rc; } /* Create a byte string field */ rc = ib_field_create( &f, tx->mp, (const char *)ib_bytestr_const_ptr(nvpair->name), ib_bytestr_length(nvpair->name), IB_FTYPE_BYTESTR, ib_ftype_bytestr_in(bs) ); if (rc != IB_OK) { ib_log_error_tx(tx, "Error creating field of '%.*s' for %s: %s", (int)ib_bytestr_length(nvpair->name), (const char *)ib_bytestr_ptr(nvpair->name), name, ib_status_to_string(rc)); return rc; } /* Add the field to the list */ rc = ib_list_push(header_list, f); if (rc != IB_OK) { ib_log_error_tx(tx, "Error adding alias of '%.*s' to %s list: %s", (int)ib_bytestr_length(nvpair->name), (const char *)ib_bytestr_ptr(nvpair->name), name, ib_status_to_string(rc)); return rc; } } return IB_OK; }
/* * Callback used to generate request header fields. */ static ib_status_t core_gen_request_header_fields(ib_engine_t *ib, ib_tx_t *tx, ib_state_event_type_t event, void *cbdata) { ib_field_t *f; ib_status_t rc; ib_conn_t *conn = tx->conn; assert(ib != NULL); assert(tx != NULL); assert(event == handle_request_header_event); core_gen_tx_bytestr_alias2(tx, "server_addr", conn->local_ipstr, strlen(conn->local_ipstr)); core_gen_tx_numeric(tx, "server_port", conn->local_port); core_gen_tx_bytestr_alias2(tx, "remote_addr", conn->remote_ipstr, strlen(conn->remote_ipstr)); core_gen_tx_numeric(tx, "remote_port", conn->remote_port); core_gen_tx_numeric(tx, "conn_tx_count", tx->conn->tx_count); if (tx->request_line != NULL) { core_gen_tx_bytestr_alias(tx, "request_line", tx->request_line->raw); core_gen_tx_bytestr_alias(tx, "request_method", tx->request_line->method); core_gen_tx_bytestr_alias(tx, "request_uri_raw", tx->request_line->uri); core_gen_tx_bytestr_alias(tx, "request_protocol", tx->request_line->protocol); } /* Populate the ARGS collection. */ rc = core_slow_get(&f, tx, "ARGS"); if (rc == IB_OK) { ib_field_t *param_list; rc = core_slow_get(¶m_list, tx, "request_uri_params"); if (rc == IB_OK) { ib_list_t *field_list; ib_list_node_t *node = NULL; rc = ib_field_mutable_value( param_list, ib_ftype_list_mutable_out(&field_list) ); if (rc != IB_OK) { return rc; } IB_LIST_LOOP(field_list, node) { ib_field_t *param = (ib_field_t *)ib_list_node_data(node); /* Add the field to the ARGS collection. */ rc = ib_field_list_add(f, param); if (rc != IB_OK) { ib_log_notice_tx(tx, "Failed to add parameter to " "ARGS collection: %s", ib_status_to_string(rc)); } }