示例#1
0
TEST(TestIronBee, test_data_indexed)
{
    ib_engine_t *ib;
    ib_data_config_t *dataconfig;
    ib_data_t *data;
    ib_field_t *f;
    size_t i;
    size_t j;
    ib_num_t n;

    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_register_indexed_ex(dataconfig, "foo", 3, &i));
    ASSERT_EQ(IB_OK, ib_data_lookup_index(dataconfig, "foo", &j));
    ASSERT_EQ(i, j);
    ASSERT_EQ(IB_ENOENT, ib_data_lookup_index(dataconfig, "bar", NULL));
    ASSERT_EQ(IB_EINVAL, ib_data_register_indexed(dataconfig, "foo"));

    ASSERT_EQ(IB_OK, ib_data_create(dataconfig, ib_engine_pool_main_get(ib), &data));
    ASSERT_TRUE(data);

    ASSERT_EQ(IB_OK, ib_data_add_num(data, "foo", 5, NULL));
    ASSERT_EQ(IB_OK, ib_data_get_indexed(data, i, &f));
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_num_out(&n)));
    ASSERT_EQ(5, n);
    ASSERT_EQ(IB_OK, ib_data_get(data, "foo", &f));
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_num_out(&n)));
    ASSERT_EQ(5, n);
}
示例#2
0
TEST_F(TestIBUtilField, Alias)
{
    ib_num_t   num1;
    ib_num_t   num2;
    ib_float_t flt1;
    ib_float_t flt2;
    char *s = NULL;
    const char *v;
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_field_create_alias(&f, MM(), "foo", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&s));
    ASSERT_EQ(IB_OK, rc);
    v = "hello";
    rc = ib_field_setv(f, ib_ftype_nulstr_in(v));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_STREQ(v, s);

    /*
     * Alias a numeric field
     */
    num1 = 1;
    rc = ib_field_create_alias(&f, MM(), "num", 3,
                               IB_FTYPE_NUM,
                               ib_ftype_num_in(&num1));
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_value(f, ib_ftype_num_out(&num2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(num1, num2);

    num1 = 3;
    rc = ib_field_value(f, ib_ftype_num_out(&num2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(num1, num2);


    /*
     * Alias a floating point field
     */
    flt1 = 1.1;
    rc = ib_field_create_alias(&f, MM(), "flt", 3,
                               IB_FTYPE_FLOAT,
                               ib_ftype_float_in(&flt1));
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_value(f, ib_ftype_float_out(&flt2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(flt1, flt2);

    flt1 = 1.5;
    rc = ib_field_value(f, ib_ftype_float_out(&flt2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(flt1, flt2);
}
示例#3
0
/// @test Test util field library - ib_field_from_string()
TEST_F(TestIBUtilField, test_field_from_string)
{
    ib_field_t *f;
    ib_status_t rc;
    ib_num_t fnum;
    ib_float_t ffloat;
    const char *fnulstr;

    rc = ib_field_from_string(MM(), IB_S2SL("test_num"),
                              "11", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_NUM, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_num_out(&fnum)));
    ASSERT_EQ(11, fnum);

    rc = ib_field_from_string(MM(), IB_S2SL("test_num"),
                              "-11", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_NUM, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_num_out(&fnum)));
    ASSERT_EQ(-11, fnum);

    rc = ib_field_from_string(MM(), IB_S2SL("test_float"),
                              "1.0", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_FLOAT, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_float_out(&ffloat)));
    ASSERT_EQ(1.0, ffloat);

    rc = ib_field_from_string(MM(), IB_S2SL("test_num"),
                              "-1.0", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_FLOAT, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_float_out(&ffloat)));
    ASSERT_EQ(-1.0, ffloat);

    rc = ib_field_from_string(MM(), IB_S2SL("test_str"),
                              "x", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_NULSTR, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_nulstr_out(&fnulstr)));
    ASSERT_STREQ("x", fnulstr);

    rc = ib_field_from_string(MM(), IB_S2SL("test_str"),
                              "-1.1x", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_NULSTR, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_nulstr_out(&fnulstr)));
    ASSERT_STREQ("-1.1x", fnulstr);
}
示例#4
0
int64_t ConstField::value_as_number() const
{
    Internal::check_type(NUMBER, type());
    int64_t v;
    throw_if_error(ib_field_value(ib(), ib_ftype_num_out(&v)));
    return v;
}
示例#5
0
TEST_F(XRulesTest, SetFlag) {
    ib_field_t       *field;
    ib_num_t          num;
    ib_var_target_t  *target;
    const ib_list_t  *list;

    std::string config =
        std::string(
            "LogLevel DEBUG\n"
            "LoadModule \"ibmod_persistence_framework.so\"\n"
            "LoadModule \"ibmod_init_collection.so\"\n"
            "LoadModule \"ibmod_xrules.so\"\n"
            "InitCollection GeoIP vars: country_code=US\n"
            "SensorId B9C1B52B-C24A-4309-B9F9-0EF4CD577A3E\n"
            "SensorName UnitTesting\n"
            "SensorHostname unit-testing.sensor.tld\n"
            /* Note that both rules should fire and result in a single entry. */
            "XRulePath /  EnableRequestParamInspection priority=1\n"
            "XRuleGeo US EnableRequestParamInspection priority=1\n"
            "<Site test-site>\n"
            "   SiteId AAAABBBB-1111-2222-3333-000000000000\n"
            "   Hostname somesite.com\n"
            "</Site>\n"
        );

    configureIronBeeByString(config.c_str());
    performTx();
    ASSERT_TRUE(ib_tx);
    ASSERT_TRUE(ib_tx->flags & IB_TX_FINSPECT_REQPARAMS);

    ASSERT_EQ(
        IB_OK,
        ib_var_target_acquire_from_string(
            &target,
            ib_tx->mp,
            ib_var_store_config(ib_tx->var_store),
            "FLAGS:inspectRequestParams",
            strlen("FLAGS:inspectRequestParams"),
            NULL,
            NULL)
    );
    ASSERT_EQ(
        IB_OK,
        ib_var_target_get_const(
            target,
            &list,
            ib_tx->mp,
            ib_tx->var_store)
    );
    ASSERT_EQ(1U, ib_list_elements(list));
    field = (ib_field_t *)ib_list_node_data_const(ib_list_first_const(list));
    ASSERT_EQ(IB_FTYPE_NUM, field->type);
    ASSERT_EQ(
        IB_OK,
        ib_field_value(field, ib_ftype_num_out(&num))
    );
    ASSERT_EQ(1, num);
}
示例#6
0
TEST_F(CoreActionTest, setVarMult) {
    ib_field_t *f;
    ib_num_t n;

    ASSERT_EQ(IB_OK, ib_data_get(ib_conn->tx->dpi, "c", &f));

    ASSERT_EQ(IB_FTYPE_NUM, f->type);

    ib_field_value(f, ib_ftype_num_out(&n));

    ASSERT_EQ(2, n);
}
示例#7
0
int64_t ConstField::value_as_number(
    const char* arg,
    size_t      arg_length
) const
{
    Internal::check_type(NUMBER, type());
    int64_t v;
    throw_if_error(ib_field_value_ex(
        ib(), ib_ftype_num_out(&v),
        arg, arg_length
    ));
    return v;
}
示例#8
0
/**
 * Do a larger integration test.
 */
TEST_F(CoreActionTest, integration) {
    ib_field_t *f;
    ib_num_t n;

    ASSERT_EQ(IB_OK, ib_data_get(ib_conn->tx->dpi, "r1", &f));
    ASSERT_EQ(IB_FTYPE_NUM, f->type);
    ib_field_value(f, ib_ftype_num_out(&n));
    ASSERT_EQ(1, n);

    ASSERT_EQ(IB_OK, ib_data_get(ib_conn->tx->dpi, "r2", &f));
    ASSERT_EQ(IB_FTYPE_NUM, f->type);
    ib_field_value(f, ib_ftype_num_out(&n));
    ASSERT_EQ(1, n);

    ASSERT_EQ(IB_OK, ib_data_get(ib_conn->tx->dpi, "r3", &f));
    ASSERT_EQ(IB_FTYPE_NUM, f->type);
    ib_field_value(f, ib_ftype_num_out(&n));
    ASSERT_EQ(1, n);

    ASSERT_EQ(IB_OK, ib_data_get(ib_conn->tx->dpi, "r4", &f));
    ASSERT_EQ(IB_FTYPE_NUM, f->type);
    ib_field_value(f, ib_ftype_num_out(&n));
    ASSERT_EQ(1, n);
}
示例#9
0
文件: field.c 项目: aburan28/ironbee
/**
 * Attempt to convert a single field.
 *
 * If the desired type matches the in_field type, out_field is set to NULL
 * and IB_OK is returned.
 *
 * @param[in,out] mp Memory pool to use.
 * @param[in] desired_type The type to try to convert this to.
 * @param[in] in_field The input field.
 * @param[out] out_field The output field to write to.
 *
 * @returns
 *   - IB_OK On success.
 *   - IB_EINVAL If a string cannot be converted to a number type
 *               or some other invalid type conversion is requested.
 *   - IB_EALLOC Memory allocation error.
 */
ib_status_t ib_field_convert(
    ib_mpool_t *mp,
    const ib_ftype_t desired_type,
    const ib_field_t *in_field,
    ib_field_t **out_field)
{
    assert(mp);
    assert(in_field);

    ib_status_t rc;

    /* Holder values for in_field values before being set in out_field. */
    size_t sz;
    const char *str;
    const ib_bytestr_t *bstr;
    ib_num_t num;
    ib_float_t flt;
    void *new_field_value;


    if (in_field->type == desired_type) {
        *out_field = NULL;
        return IB_OK;
    }

    switch (in_field->type) {
        case IB_FTYPE_NULSTR:

            /* Extract string. */
            rc = ib_field_value(in_field, ib_ftype_nulstr_out(&str));
            if (rc!=IB_OK){
                return rc;
            }

            switch(desired_type) {
                case IB_FTYPE_BYTESTR:
                    rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
                    if (rc!=IB_OK){
                        return rc;
                    }
                    new_field_value = ib_ftype_bytestr_in(bstr);
                    break;
                case IB_FTYPE_NUM:
                    rc = ib_string_to_num(str, 0, &num);
                    new_field_value = ib_ftype_num_in(&num);
                    break;
                case IB_FTYPE_FLOAT:
                    rc = ib_string_to_float(str, &flt);
                    new_field_value = ib_ftype_float_in(&flt);
                    break;
                default:
                    return IB_EINVAL;
            }
            break;
        case IB_FTYPE_BYTESTR:

            /* Extract bytestr. */
            rc = ib_field_value(in_field, ib_ftype_bytestr_out(&bstr));
            if (rc!=IB_OK){
                return rc;
            }
            sz = ib_bytestr_length(bstr);

            /* Convert byte str. */
            switch(desired_type) {
                case IB_FTYPE_NULSTR:
                    str = ib_mpool_memdup_to_str(mp, bstr, sz);
                    if (!str) {
                        return rc;
                    }
                    new_field_value = ib_ftype_nulstr_in(str);
                    break;
                case IB_FTYPE_NUM:
                    rc = ib_string_to_num_ex((char *)bstr, sz, 0, &num);
                    new_field_value = ib_ftype_num_in(&num);
                    break;
                case IB_FTYPE_FLOAT:
                    rc = ib_string_to_float_ex((char *)bstr, sz, &flt);
                    new_field_value = ib_ftype_float_in(&flt);
                    break;
                default:
                    return IB_EINVAL;
            }
            break;
        case IB_FTYPE_NUM:

            /* Extract unum. */
            rc = ib_field_value(in_field, ib_ftype_num_out(&num));
            if (rc!=IB_OK){
                return rc;
            }

            switch(desired_type) {
                case IB_FTYPE_NULSTR:
                    str = ib_num_to_string(mp, num);
                    if (!str) {
                        return IB_EINVAL;
                    }
                    new_field_value = ib_ftype_nulstr_in(str);
                    break;
                case IB_FTYPE_BYTESTR:
                    str = ib_num_to_string(mp, num);
                    if (!str) {
                        return IB_EINVAL;
                    }
                    rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
                    if (rc!=IB_OK){
                        return rc;
                    }
                    new_field_value = ib_ftype_bytestr_in(bstr);
                    break;
                case IB_FTYPE_FLOAT:
                    flt = (ib_float_t)num;
                    new_field_value = ib_ftype_float_in(&flt);
                    break;
                default:
                    return IB_EINVAL;
            }
            break;
        case IB_FTYPE_FLOAT:

            /* Extract unum. */
            rc = ib_field_value(in_field, ib_ftype_float_out(&flt));
            if (rc!=IB_OK){
                return rc;
            }

            switch(desired_type) {
                case IB_FTYPE_NULSTR:
                    str = ib_float_to_string(mp, flt);
                    if (!str) {
                        return IB_EINVAL;
                    }
                    new_field_value = ib_ftype_nulstr_in(str);
                    break;
                case IB_FTYPE_BYTESTR:
                    str = ib_float_to_string(mp, flt);
                    if (!str) {
                        return IB_EINVAL;
                    }
                    rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
                    if (rc!=IB_OK){
                        return rc;
                    }
                    new_field_value = ib_ftype_bytestr_in(bstr);
                    break;
                case IB_FTYPE_NUM:
                    num = (ib_float_t)flt;
                    new_field_value = ib_ftype_num_in(&num);
                    break;
                default:
                    return IB_EINVAL;
            }
            break;
        default:
            return IB_EINVAL;
    }

    rc = ib_field_create(
        out_field,
        mp,
        in_field->name,
        in_field->nlen,
        desired_type,
        new_field_value);
    return rc;
}
示例#10
0
文件: field.c 项目: aburan28/ironbee
void ib_field_util_log_debug(
    const char       *prefix,
    const ib_field_t *f
)
{
    assert(prefix != NULL);
    assert(f != NULL);

    ib_status_t rc;

    if (ib_util_get_log_level() < IB_LOG_DEBUG) {
        return;
    }

    if (ib_field_is_dynamic(f)) {
        ib_util_log_debug(
            "%s is dynamic: fn_get=%p cbdata_get=%p fn_set=%p cbdata_set=%p",

          prefix,
            f->val->fn_get, f->val->cbdata_get,
            f->val->fn_set, f->val->cbdata_set
        );
    }

    ib_util_log_debug("%s name=%.*s type=%d",
                      prefix, (int)f->nlen, f->name, f->type
    );

    if (ib_field_is_dynamic(f)) {
        return;
    }

    assert(f->val->pval);

    if (*(void **)(f->val->pval) == NULL) {
        ib_util_log_debug(
            "%s has no value.",
            prefix
        );
    }
    else {
        switch (f->type) {
        case IB_FTYPE_GENERIC:
        {
            void *v;
            rc = ib_field_value(f, ib_ftype_generic_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s value=%p", prefix, v);
            }
            break;
        }
        case IB_FTYPE_NUM:
        {
            ib_num_t v;
            rc = ib_field_value(f, ib_ftype_num_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s value=%"PRId64, prefix, v);
            }
            break;
        }
        case IB_FTYPE_FLOAT:
        {
            ib_float_t v;
            rc = ib_field_value(f, ib_ftype_float_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s value=%Lf", prefix, v);
            }
            break;
        }
        case IB_FTYPE_NULSTR:
        {
            const char *v;
            rc = ib_field_value(f, ib_ftype_nulstr_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s value=%s", prefix, v);
            }
            break;
        }
        case IB_FTYPE_BYTESTR:
        {
            const ib_bytestr_t *v;
            rc = ib_field_value(f, ib_ftype_bytestr_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s value=%" IB_BYTESTR_FMT,
                                  prefix, IB_BYTESTR_FMT_PARAM(v));
            }
            break;
        }
        case IB_FTYPE_LIST:
        {
            const ib_list_t* v;
            rc = ib_field_value(f, ib_ftype_list_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s &value=%p", prefix, v);
            }
            break;
        }
        case IB_FTYPE_SBUFFER:
        {
            const ib_stream_t* v;
            rc = ib_field_value(f, ib_ftype_sbuffer_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s &value=%p", prefix, v);
            }
            break;
        }
        default:
            ib_util_log_debug("%s Unknown field type: %u",
                              prefix, f->type
            );
        }
    }
}
示例#11
0
TEST_F(TestIBUtilField, ConvertString)
{
    ib_field_t *f1;
    ib_field_t *f2;
    ib_status_t rc;
    ib_num_t    num;
    ib_float_t  flt;

    /*
     * Convert numeric string to number
     */

    /* Create the field */
    rc = ib_field_create(&f1, MM(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_num_out(&num));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(1, num);

    /*
     * Convert floating-point string to float
     */

    /* Create the field */
    rc = ib_field_create(&f1, MM(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("1.1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_float_out(&flt));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FLOAT_EQ(1.1, flt);

    /*
     * Convert non-numeric string to number
     */

    /* Create the field */
    rc = ib_field_create(&f1, MM(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("x1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);

    /*
     * Convert floating-point string to number
     */

    /* Create the field */
    rc = ib_field_create(&f1, MM(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("1.1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);


    /*
     * Convert non-numeric string to float
     */

    /* Create the field */
    rc = ib_field_create(&f1, MM(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("x1.1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);
}
示例#12
0
TEST_F(TestIBUtilField, AliasConvert)
{
    char       *str;
    ib_field_t *f1;
    ib_field_t *f2;
    ib_status_t rc;
    ib_num_t    num;
    ib_float_t  flt;

    /*
     * Convert numeric string to number
     */

    /* Copy a number into the string */
    str = ib_mm_strdup(MM(), "1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MM(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_num_out(&num));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(1, num);

    /*
     * Convert floating-point string to float
     */

    /* Copy a number into the string */
    str = ib_mm_strdup(MM(), "1.1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MM(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_float_out(&flt));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FLOAT_EQ(1.1, flt);

    /*
     * Convert non-numeric string to number
     */

    /* Copy a number into the string */
    str = ib_mm_strdup(MM(), "x1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MM(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);

    /*
     * Convert floating-point string to number
     */

    /* Copy a number into the string */
    str = ib_mm_strdup(MM(), "1.1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MM(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);


    /*
     * Convert non-numeric string to float
     */

    /* Copy a number into the string */
    str = ib_mm_strdup(MM(), "1.1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MM(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_float_out(&flt));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FLOAT_EQ(1.1, flt);
}
示例#13
0
文件: field.c 项目: niubl/ironbee
ib_status_t ib_field_convert(
    ib_mpool_t        *mp,
    const ib_ftype_t   desired_type,
    const ib_field_t  *in_field,
    ib_field_t       **out_field
)
{
    assert(mp);
    assert(in_field);

    ib_status_t rc;

    /* Holder values for in_field values before being set in out_field. */
    size_t sz;
    const char *str;
    const ib_bytestr_t *bstr;
    ib_num_t num;
    ib_time_t tme;
    ib_float_t flt;
    void *new_field_value;

    if (in_field->type == desired_type) {
        *out_field = NULL;
        return IB_OK;
    }

    switch (in_field->type) {
    case IB_FTYPE_NULSTR:

        /* Extract string.  Note that a zero-length nulstr field can
         * have a NULL value in the union. */
        if ( (in_field->val->u.nulstr == NULL) &&
             (in_field->val->pval == NULL) )
        {
            str = "";
        }
        else {
            rc = ib_field_value(in_field, ib_ftype_nulstr_out(&str));
            if (rc != IB_OK){
                return rc;
            }
        }

        switch (desired_type) {
        case IB_FTYPE_BYTESTR:
            rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_bytestr_in(bstr);
            break;
        case IB_FTYPE_TIME:
            rc = ib_string_to_time(str, &tme);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_time_in(&tme);
            break;
        case IB_FTYPE_NUM:
            rc = ib_string_to_num(str, 0, &num);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_num_in(&num);
            break;
        case IB_FTYPE_FLOAT:
            rc = ib_string_to_float(str, &flt);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_float_in(&flt);
            break;
        default:
            return IB_EINVAL;
        }
        break;

    case IB_FTYPE_BYTESTR:

        /* Extract bytestr. */
        rc = ib_field_value(in_field, ib_ftype_bytestr_out(&bstr));
        if (rc != IB_OK){
            return rc;
        }
        str = (const char *)ib_bytestr_const_ptr(bstr);
        sz = ib_bytestr_length(bstr);

        /* Convert byte str. */
        switch(desired_type) {
        case IB_FTYPE_NULSTR:
            str = ib_mpool_memdup_to_str(mp, str, sz);
            if (!str) {
                return rc;
            }
            new_field_value = ib_ftype_nulstr_in(str);
            break;
        case IB_FTYPE_TIME:
            rc = ib_string_to_time_ex(str, sz, &tme);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_time_in(&tme);
            break;
        case IB_FTYPE_NUM:
            rc = ib_string_to_num_ex(str, sz, 0, &num);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_num_in(&num);
            break;
        case IB_FTYPE_FLOAT:
            rc = ib_string_to_float_ex(str, sz, &flt);
            if (rc != IB_OK) {
                return rc;
            }
            new_field_value = ib_ftype_float_in(&flt);
            break;
        default:
            return IB_EINVAL;
        }
        break;

    case IB_FTYPE_TIME:

        /* Extract time. */
        rc = ib_field_value(in_field, ib_ftype_time_out(&tme));
        if (rc != IB_OK){
            return rc;
        }

        switch (desired_type) {
        case IB_FTYPE_NULSTR:
            str = ib_time_to_string(mp, tme);
            if (! str) {
                return IB_EINVAL;
            }
            new_field_value = ib_ftype_nulstr_in(str);
            break;
        case IB_FTYPE_BYTESTR:
            str = ib_time_to_string(mp, tme);
            if (! str) {
                return IB_EINVAL;
            }
            rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
            if (rc != IB_OK){
                return rc;
            }
            new_field_value = ib_ftype_bytestr_in(bstr);
            break;
        case IB_FTYPE_FLOAT:
            flt = (ib_float_t)tme;
            /* Check that our assignment is within error=1, or fail. */
            if (llabs(tme - flt) > 1) {
                return IB_EINVAL;
            }
            new_field_value = ib_ftype_float_in(&flt);
            break;
        default:
            return IB_EINVAL;
        }
        break;

    case IB_FTYPE_NUM:

        /* Extract unum. */
        rc = ib_field_value(in_field, ib_ftype_num_out(&num));
        if (rc != IB_OK){
            return rc;
        }

        switch (desired_type) {
        case IB_FTYPE_NULSTR:
            str = ib_num_to_string(mp, num);
            if (! str) {
                return IB_EINVAL;
            }
            new_field_value = ib_ftype_nulstr_in(str);
            break;
        case IB_FTYPE_BYTESTR:
            str = ib_num_to_string(mp, num);
            if (! str) {
                return IB_EINVAL;
            }
            rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
            if (rc != IB_OK){
                return rc;
            }
            new_field_value = ib_ftype_bytestr_in(bstr);
            break;
        case IB_FTYPE_TIME:
            if (num < 0) {
                return IB_EINVAL;
            }
            tme = (ib_time_t)num;
            new_field_value = ib_ftype_time_in(&tme);
            break;
        case IB_FTYPE_FLOAT:
            flt = (ib_float_t)num;
            if (llabs(flt - num) > 1) {
                return IB_EINVAL;
            }
            new_field_value = ib_ftype_float_in(&flt);
            break;
        default:
            return IB_EINVAL;
        }
        break;

    case IB_FTYPE_FLOAT:

        /* Extract unum. */
        rc = ib_field_value(in_field, ib_ftype_float_out(&flt));
        if (rc != IB_OK){
            return rc;
        }

        switch (desired_type) {
        case IB_FTYPE_NULSTR:
            str = ib_float_to_string(mp, flt);
            if (!str) {
                return IB_EINVAL;
            }
            new_field_value = ib_ftype_nulstr_in(str);
            break;
        case IB_FTYPE_BYTESTR:
            str = ib_float_to_string(mp, flt);
            if (!str) {
                return IB_EINVAL;
            }
            rc = ib_bytestr_dup_nulstr((ib_bytestr_t **)&bstr, mp, str);
            if (rc != IB_OK){
                return rc;
            }
            new_field_value = ib_ftype_bytestr_in(bstr);
            break;
        case IB_FTYPE_TIME:
            if (flt < 0) {
                return IB_EINVAL;
            }
            tme = (ib_float_t)flt;
            new_field_value = ib_ftype_time_in(&tme);
            break;
        case IB_FTYPE_NUM:
            num = (ib_float_t)flt;
            new_field_value = ib_ftype_num_in(&num);
            break;
        default:
            return IB_EINVAL;
        }
        break;

    default:
        return IB_EINVAL;
    }

    rc = ib_field_create(
        out_field,
        mp,
        in_field->name,
        in_field->nlen,
        desired_type,
        new_field_value
    );
    return rc;
}
示例#14
0
/**
 * JSON YAJL encode: Encode a list
 *
 * @param[in,out] handle YAJL generator handle
 * @param[in] name Name of @a list (or NULL)
 * @param[in] nlen Length of @a name
 * @param[in] list IronBee list to encode
 *
 * @returns IronBee status code
 */
static ib_status_t encode_list(
    yajl_gen         handle,
    const char      *name,
    size_t           nlen,
    const ib_list_t *list)
{
    ib_status_t           rc = IB_OK;
    const ib_list_node_t *node;
    yajl_gen_status       status;
    int                   errors = 0;

    /* Encode the name */
    if ( (name != NULL) && (nlen != 0) ) {
        status = yajl_gen_string(handle, (const unsigned char *)name, nlen);
        if (status != yajl_gen_status_ok) {
            return IB_EUNKNOWN;
        }
    }

    /* Encode the map start */
    status = yajl_gen_map_open(handle);
    if (status != yajl_gen_status_ok) {
        return IB_EUNKNOWN;
    }

    IB_LIST_LOOP_CONST(list, node) {
        const ib_field_t *field = (const ib_field_t *)node->data;
        ib_status_t       tmprc;

        status = yajl_gen_string(handle,
                                 (const unsigned char *)field->name,
                                 field->nlen);
        if (status != yajl_gen_status_ok) {
            rc = IB_EUNKNOWN;
            ++errors;
            continue;
        }

        switch(field->type) {
        case IB_FTYPE_LIST:
        {
            const ib_list_t *list2;
            tmprc = ib_field_value(field, ib_ftype_list_out(&list2));
            if ( (tmprc != IB_OK) && (rc == IB_OK) ) {
                rc = tmprc;
                ++errors;
            }
            else {
                tmprc = encode_list(handle, NULL, 0, list2);
                if ( (tmprc != IB_OK) && (rc == IB_OK) ) {
                    rc = tmprc;
                    ++errors;
                }
            }
            break;
        }

        case IB_FTYPE_NUM:
        {
            ib_num_t num;
            tmprc = ib_field_value(field, ib_ftype_num_out(&num));
            if ( (tmprc != IB_OK) && (rc == IB_OK) ) {
                rc = tmprc;
                ++errors;
            }
            else {
                status = yajl_gen_integer(handle, num);
                if (status != yajl_gen_status_ok) {
                    if (rc != IB_OK) {
                        rc = IB_EUNKNOWN;
                    }
                    ++errors;
                }
            }
            break;
        }

        case IB_FTYPE_FLOAT:
        {
            ib_float_t fnum;
            tmprc = ib_field_value(field, ib_ftype_float_out(&fnum));
            if ( (tmprc != IB_OK) && (rc == IB_OK) ) {
                rc = tmprc;
                ++errors;
            }
            else {
                char buf[float_buf_size+1];
                snprintf(buf, float_buf_size, "%#.8Lg", fnum);
                status = yajl_gen_number(handle, buf, strlen(buf));
                if (status != yajl_gen_status_ok) {
                    if (rc != IB_OK) {
                        rc = IB_EUNKNOWN;
                    }
                    ++errors;
                }
            }
            break;
        }

        case IB_FTYPE_NULSTR:
        {
            const char *str;
            tmprc = ib_field_value(field, ib_ftype_nulstr_out(&str));
            if ( (tmprc != IB_OK) && (rc == IB_OK) ) {
                rc = tmprc;
                ++errors;
            }
            else {
                status = yajl_gen_string(handle,
                                         (unsigned char *)str,
                                         strlen(str));
                if (status != yajl_gen_status_ok) {
                    if (rc != IB_OK) {
                        rc = IB_EUNKNOWN;
                    }
                    ++errors;
                }
            }
            break;
        }

        case IB_FTYPE_BYTESTR:
        {
            const ib_bytestr_t *bs;
            tmprc = ib_field_value(field, ib_ftype_bytestr_out(&bs));
            if ( (tmprc != IB_OK) && (rc == IB_OK) ) {
                rc = tmprc;
                ++errors;
            }
            else {
                status = yajl_gen_string(handle,
                                         ib_bytestr_const_ptr(bs),
                                         ib_bytestr_length(bs));
                if (status != yajl_gen_status_ok) {
                    if (rc != IB_OK) {
                        rc = IB_EUNKNOWN;
                    }
                    ++errors;
                }
            }
            break;
        }

        default: /* Just ignore it */
            break;

        } /* switch(f->type) */
    }

    /* Encode the map end */
    status = yajl_gen_map_close(handle);
    if (status != yajl_gen_status_ok) {
        return IB_EUNKNOWN;
    }

    return IB_OK;
}
示例#15
0
文件: expand.c 项目: moon2l/ironbee
/**
 * Join a field with strings before and after it
 *
 * @param[in] mp Memory pool
 * @param[in] f Field to join
 * @param[in] iptr Pointer to initial string
 * @param[in] ilen Length of @a iptr
 * @param[in] fptr Pointer to final string
 * @param[in] flen Length of @a fptr
 * @param[in] nul true if NUL byte should be tacked on, false if not
 * @param[out] out Pointer to output block
 * @param[out] olen Length of the output block
 *
 * @returns status code
 */
static ib_status_t join_parts(ib_mpool_t *mp,
                              const ib_field_t *f,
                              const char *iptr,
                              size_t ilen,
                              const char *fptr,
                              size_t flen,
                              bool nul,
                              char **out,
                              size_t *olen)
{
    IB_FTRACE_INIT();
    ib_status_t rc;
    char numbuf[NUM_BUF_LEN+1]; /* Buffer used to convert number to str */
    assert(NUM_BUF_LEN <= 256);

    switch(f->type) {
    case IB_FTYPE_NULSTR:
    {
        /* Field is a NUL-terminated string */
        const char *s;
        rc = ib_field_value(f, ib_ftype_nulstr_out(&s));
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }
        size_t slen = strlen(s);
        rc = join3(mp,
                   iptr, ilen,
                   s, slen,
                   fptr, flen,
                   nul,
                   out, olen);
        break;
    }

    case IB_FTYPE_BYTESTR:
    {
        /* Field is a byte string */
        const ib_bytestr_t *bs;
        rc = ib_field_value(f, ib_ftype_bytestr_out(&bs));
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }
        rc = join3(mp,
                   iptr, ilen,
                   (const char *)ib_bytestr_const_ptr(bs),
                   ib_bytestr_length(bs),
                   fptr, flen,
                   nul,
                   out, olen);
        break;
    }

    case IB_FTYPE_NUM:
    {
        /* Field is a number; convert it to a string */
        ib_num_t n;
        rc = ib_field_value(f, ib_ftype_num_out(&n));
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }
        snprintf(numbuf, NUM_BUF_LEN, "%"PRId64, n);
        rc = join3(mp,
                   iptr, ilen,
                   numbuf, strlen(numbuf),
                   fptr, flen,
                   nul,
                   out, olen);
        break;
    }

    case IB_FTYPE_UNUM:
    {
        /* Field is an unsigned number; convert it to a string */
        ib_unum_t n;
        rc = ib_field_value(f, ib_ftype_unum_out(&n));
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }
        snprintf(numbuf, NUM_BUF_LEN, "%"PRIu64, n);
        rc = join3(mp,
                   iptr, ilen,
                   numbuf, strlen(numbuf),
                   fptr, flen,
                   nul,
                   out, olen);
        break;
    }

    case IB_FTYPE_LIST:
    {
        /* Field is a list: use the first element in the list */
        const ib_list_t *list;
        const ib_list_node_t *node;
        const ib_field_t *element;

        rc = ib_field_value(f, ib_ftype_list_out(&list));
        if (rc != IB_OK) {
            IB_FTRACE_RET_STATUS(rc);
        }

        node = ib_list_first_const(list);
        if (node == NULL) {
            rc = join2(mp, iptr, ilen, fptr, flen, nul, out, olen);
            break;
        }

        element = (const ib_field_t *)ib_list_node_data_const(node);
        rc = join_parts(mp, element, iptr, ilen, fptr, flen, nul, out, olen);
        break;
    }

    default:
        /* Something else: replace with "" */
        rc = join2(mp, iptr, ilen, fptr, flen, nul, out, olen);
        break;
    }

    IB_FTRACE_RET_STATUS(rc);
}
示例#16
0
/**
 * A body filter to intercept response body and feed it to Ironbee,
 * and to buffer the data if required by Ironbee configuration.
 *
 * @param[in]  r     The nginx request object.
 * @param[in]  in    The data to filter.
 * @return     status propagated from next filter, or OK/Error
 */
static ngx_int_t ironbee_body_out(ngx_http_request_t *r, ngx_chain_t *in)
{
    ngx_log_t *prev_log;
    ngxib_req_ctx *ctx;
    ngx_chain_t *link;
    ib_status_t rc;
    ib_num_t num;
    ngx_int_t rv = NGX_OK;
    ib_txdata_t itxdata;

    if (r->internal)
        return ngx_http_next_body_filter(r, in);

    prev_log = ngxib_log(r->connection->log);

    ctx = ngx_http_get_module_ctx(r, ngx_ironbee_module);
    assert((ctx != NULL) && (ctx->tx != NULL));
    ib_log_debug_tx(ctx->tx, "ironbee_body_out");
    if (in == NULL) {
        /* FIXME: could this happen in circumstances when we should
         * notify Ironbee of end-of-response ?
         */
        ib_log_debug_tx(ctx->tx, "ironbee_body_out: input was null");
        cleanup_return(prev_log) ngx_http_next_body_filter(r, in);
    }
    ctx = ngx_http_get_module_ctx(r, ngx_ironbee_module);
    if (ctx->output_filter_done) {
        ib_log_debug_tx(ctx->tx, "ironbee_body_out: already done");
        cleanup_return(prev_log) ngx_http_next_body_filter(r, in);
    }
    if (!ctx->output_filter_init) {
        ctx->output_filter_init = 1;

        if (ctx->internal_errordoc) {
            /* If it's our own errordoc, pass it straight through */
            /* Should we log anything here?  The error will already
             * have been logged.
             */
            ctx->output_buffering = IOBUF_NOBUF;
            ctx->response_buf = NULL;
            ib_log_debug_tx(ctx->tx, "ironbee_body_out: in internal errordoc");
        }
        else {
            /* Determine whether we're configured to buffer */
            rc = ib_context_get(ctx->tx->ctx, "buffer_res",
                                ib_ftype_num_out(&num), NULL);
            ib_log_debug_tx(ctx->tx, "ironbee_body_out: buffer_res is %d", (int)num);
            if (rc != IB_OK)
                ib_log_error_tx(ctx->tx,
                                "Can't determine output buffer configuration!");
            if (num == 0) {
                ib_log_debug_tx(ctx->tx, "ironbee_body_out: NOBUF");
                ctx->output_buffering = IOBUF_NOBUF;
                ctx->response_buf = NULL;
            }
            else {
                /* If we're buffering, initialise the buffer */
                ib_log_debug_tx(ctx->tx, "ironbee_body_out: BUFFER");
                ctx->output_buffering = IOBUF_BUFFER;
            }
        }
    }

    ngx_regex_malloc_init(r->pool);

    for (link = in; link != NULL; link = link->next) {
        /* Feed the data to ironbee */
        itxdata.data = link->buf->pos;
        itxdata.dlen = link->buf->last - link->buf->pos;
        ib_log_debug_tx(ctx->tx, "ironbee_body_out: %d bytes",
                        (int)itxdata.dlen);
        if (itxdata.dlen > 0) {
            ib_state_notify_response_body_data(ironbee, ctx->tx, &itxdata);
        }

        /* If Ironbee just signalled an error, switch to discard data mode,
         * and dump anything we already have buffered,
         */
        if ( (STATUS_IS_ERROR(ctx->status)) &&
             !ctx->internal_errordoc &&
             (ctx->output_buffering != IOBUF_DISCARD) ) {
            ib_log_debug_tx(ctx->tx, "ironbee_body_out: error %d", ctx->status);
            free_chain(r->pool, ctx->response_buf);
            ctx->response_buf = NULL;
            ctx->output_buffering = IOBUF_DISCARD;
        }
        else if (ctx->output_buffering == IOBUF_BUFFER) {
            /* Copy any data to our buffer */
            if (ctx->response_buf == NULL) {
                ctx->response_buf = ngx_pcalloc(r->pool, sizeof(ngx_chain_t));
                ctx->response_ptr = ctx->response_buf;
            }
            else {
                ctx->response_ptr->next = ngx_pcalloc(r->pool, sizeof(ngx_chain_t));
                ctx->response_ptr = ctx->response_ptr->next;
            }
            /* Not sure if any data types need setaside, but let's be safe */
#if NO_COPY_REQUIRED
            ctx->response_ptr->buf = link->buf;
#else
            if (itxdata.dlen > 0) {
                ctx->response_ptr->buf = ngx_create_temp_buf(r->pool, itxdata.dlen);
                memcpy(ctx->response_ptr->buf->pos, link->buf->pos, itxdata.dlen);
                ctx->response_ptr->buf->last += itxdata.dlen;
            }
            else {
                ctx->response_ptr->buf = ngx_palloc(r->pool, sizeof(ngx_buf_t));
                memcpy(ctx->response_ptr->buf, link->buf, sizeof(ngx_buf_t));
            }
#endif
        }

        if (link->buf->last_buf) {
            ib_log_debug_tx(ctx->tx, "ironbee_body_out: last_buf");
            ctx->output_filter_done = 1;
        }
    }

    if (ctx->output_buffering == IOBUF_NOBUF) {
        /* Normal operation - pass it down the chain */
        ib_log_debug_tx(ctx->tx, "ironbee_body_out: passing on");
        ctx->start_response = 1;
        rv = ngx_http_next_body_filter(r, in);
    }
    else if (ctx->output_buffering == IOBUF_BUFFER) {
        ib_log_debug_tx(ctx->tx, "ironbee_body_out: buffering");
        if (ctx->output_filter_done) {
            /* We can pass on the buffered data all at once */
            ib_log_debug_tx(ctx->tx, "ironbee_body_out: passing buffer");
            ctx->start_response = 1;
            rv = ngx_http_next_body_filter(r, ctx->response_buf);
        }
    }
    else if (ctx->output_buffering == IOBUF_DISCARD) {
        ib_log_debug_tx(ctx->tx, "ironbee_body_out: discarding");
        if (ctx->output_filter_done) {
            /* Pass a last bucket with no data */
            //ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
            //              "ironbee_body_out: passing NULL last-buffer");
            //ctx->response_buf = ngx_pcalloc(r->pool, sizeof(ngx_chain_t));
            //ctx->response_buf->buf = ngx_calloc_buf(r->pool);
            //ctx->response_buf->buf->last_buf = ctx->response_buf->buf->last_in_chain = 1;
            //rv = ngx_http_next_body_filter(r, ctx->response_buf);
            /* FIXME: Is setting rv enough to serve error page */
            rv = ctx->status;
        }
    }
    if (ctx->output_filter_done) {
        ib_log_debug_tx(ctx->tx, "ironbee_body_out: notify_postprocess");
        rc = ib_state_notify_postprocess(ironbee, ctx->tx);
        if ((rv == NGX_OK) && (rc != IB_OK)) {
            rv = NGX_HTTP_INTERNAL_SERVER_ERROR;
        }
        rc = ib_state_notify_logging(ironbee, ctx->tx);
        if ((rv == NGX_OK) && (rc != IB_OK)) {
            rv = NGX_HTTP_INTERNAL_SERVER_ERROR;
        }
    }
    cleanup_return(prev_log) rv;
}
示例#17
0
文件: field.c 项目: aburan28/ironbee
ib_status_t ib_field_copy(
    ib_field_t       **pf,
    ib_mpool_t        *mp,
    const char        *name,
    size_t             nlen,
    const ib_field_t  *src
)
{
    ib_status_t rc;

    if (ib_field_is_dynamic(src)) {
        rc = ib_field_create_dynamic(
            pf, mp, name, nlen,
            src->type,
            src->val->fn_get,
            src->val->cbdata_get,
            src->val->fn_set,
            src->val->cbdata_set
        );
    }
    else {
        switch (src->type) {
        case IB_FTYPE_NUM:
        {
            ib_num_t v;
            rc = ib_field_value(src, ib_ftype_num_out(&v));
            if (rc != IB_OK) {
                goto failed;
            }
            rc = ib_field_create(
                pf, mp, name, nlen,
                src->type,
                ib_ftype_num_in(&v)
            );
            break;
        }
        case IB_FTYPE_FLOAT:
        {
            ib_float_t v;
            rc = ib_field_value(src, ib_ftype_float_out(&v));
            if (rc != IB_OK) {
                goto failed;
            }
            rc = ib_field_create(
                pf, mp, name, nlen,
                src->type,
                ib_ftype_float_in(&v)
            );
            break;
        }
        default:
        {
            void *v;
            rc = ib_field_value(src, &v);
            if (rc != IB_OK) {
                goto failed;
            }
            rc = ib_field_create(
                pf, mp, name, nlen,
                src->type,
                v
            );
        }
        }
    }

    if (rc != IB_OK) {
        goto failed;
    }

    ib_field_util_log_debug("FIELD_COPY", (*pf));

    return rc;

failed:
    *pf = NULL;
    return rc;
}
示例#18
0
文件: field.c 项目: aburan28/ironbee
const char *ib_field_format(
    const ib_field_t  *field,
    bool               quote,
    bool               escape,
    const char       **type_name,
    char              *buf,
    size_t             bufsize
)
{
    ib_status_t rc;
    const char *tname = NULL;

    assert(buf != NULL);
    assert(bufsize > 0);

    *buf = '\0';
    if (field == NULL) {
        tname = "NULL";
        if (quote) {
            strncpy(buf, "\"\"", bufsize-1);
            *(buf+bufsize) = '\0';
        }
        else {
            *buf = '\0';
        }
    }
    else {
        switch (field->type) {

        case IB_FTYPE_NULSTR :
        {
            const char *s;
            tname = "NULSTR";
            rc = ib_field_value(field, ib_ftype_nulstr_out(&s));
            if (rc != IB_OK) {
                break;
            }
            if (escape) {
                ib_string_escape_json_buf(s, quote, buf, bufsize, NULL, NULL);
            }
            else if (quote) {
                snprintf(buf, bufsize, "\"%s\"", (s?s:""));
            }
            else {
                strncpy(buf, s, bufsize-1);
                *(buf+bufsize-1) = '\0';
            }
            break;
        }

        case IB_FTYPE_BYTESTR:
        {
            const ib_bytestr_t *bs;

            tname = "BYTESTR";
            rc = ib_field_value(field, ib_ftype_bytestr_out(&bs));
            if (rc != IB_OK) {
                break;
            }

            if (escape) {
                ib_string_escape_json_buf_ex(ib_bytestr_const_ptr(bs),
                                             ib_bytestr_length(bs),
                                             true,
                                             quote,
                                             buf, bufsize, NULL,
                                             NULL);
            }
            else if (quote) {
                snprintf(buf, bufsize, "\"%.*s\"",
                         (int)ib_bytestr_length(bs),
                         (const char *)ib_bytestr_const_ptr(bs));
            }
            else {
                size_t len = ib_bytestr_length(bs);
                if (len > (bufsize - 1) ) {
                    len = bufsize - 1;
                }
                strncpy(buf, (const char *)ib_bytestr_const_ptr(bs), len);
                *(buf+len) = '\0';
            }
            break;
        }

        case IB_FTYPE_NUM :          /**< Numeric value */
        {
            ib_num_t n;
            tname = "NUM";
            rc = ib_field_value(field, ib_ftype_num_out(&n));
            if (rc != IB_OK) {
                break;
            }
            snprintf(buf, bufsize, "%"PRId64, n);
            break;
        }

        case IB_FTYPE_FLOAT :        /**< Float numeric value */
        {
            ib_float_t f;
            tname = "FLOAT";
            rc = ib_field_value(field, ib_ftype_float_out(&f));
            if (rc != IB_OK) {
                break;
            }
            snprintf(buf, bufsize, "%Lf", f);
            break;
        }

        case IB_FTYPE_LIST :         /**< List */
        {
            const ib_list_t *lst;
            size_t len;

            tname = "LIST";
            rc = ib_field_value(field, ib_ftype_list_out(&lst));
            if (rc != IB_OK) {
                break;
            }
            len = IB_LIST_ELEMENTS(lst);
            if (len == 0) {
                snprintf(buf, bufsize, "list[%zd]", len);
            }
            else {
                const ib_list_node_t *node;
                node = ib_list_last_const(lst);
                if (node == NULL) {
                    snprintf(buf, bufsize, "list[%zd]", len);
                }
                else {
                    ib_field_format((const ib_field_t *)node->data,
                                    quote, escape,
                                    &tname, buf, bufsize);
                }
            }
            break;
        }

        default:
            tname = buf;
            snprintf(buf, bufsize, "type = %d", field->type);
            break;
        }
    }

    /* Store the type name */
    if (type_name != NULL) {
        *type_name = tname;
    }

    /* Return the buffer */
    return buf;
}
示例#19
0
/// @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);
}