static int extract_name_value(const char *src, h2o_iovec_t **name, h2o_iovec_t *value) { const char *colon = strchr(src, ':'); if (colon == NULL) return -1; if (extract_name(src, colon - src, name) != 0) return -1; *value = h2o_str_stripws(colon + 1, strlen(colon + 1)); *value = h2o_strdup(NULL, value->base, value->len); return 0; }
static int extract_name(const char *src, size_t len, h2o_iovec_t **_name) { h2o_iovec_t name; const h2o_token_t *name_token; name = h2o_str_stripws(src, len); if (name.len == 0) return -1; name = h2o_strdup(NULL, name.base, name.len); h2o_strtolower(name.base, name.len); if ((name_token = h2o_lookup_token(name.base, name.len)) != NULL) { *_name = (h2o_iovec_t *)&name_token->buf; free(name.base); } else { *_name = h2o_mem_alloc(sizeof(**_name)); **_name = name; } return 0; }
static void test_stripws(void) { h2o_iovec_t t; t = h2o_str_stripws(H2O_STRLIT("")); ok(h2o_memis(t.base, t.len, H2O_STRLIT(""))); t = h2o_str_stripws(H2O_STRLIT("hello world")); ok(h2o_memis(t.base, t.len, H2O_STRLIT("hello world"))); t = h2o_str_stripws(H2O_STRLIT(" hello world")); ok(h2o_memis(t.base, t.len, H2O_STRLIT("hello world"))); t = h2o_str_stripws(H2O_STRLIT("hello world ")); ok(h2o_memis(t.base, t.len, H2O_STRLIT("hello world"))); t = h2o_str_stripws(H2O_STRLIT(" hello world ")); ok(h2o_memis(t.base, t.len, H2O_STRLIT("hello world"))); t = h2o_str_stripws(H2O_STRLIT(" ")); ok(h2o_memis(t.base, t.len, H2O_STRLIT(""))); }