From 85ec883d11e7d06351f7de5db7eb5aacb631eb25 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 15 Feb 2012 23:54:42 +0900 Subject: [PATCH] Lower-case names in name/value pairs in spdylay_submit_{request, response} --- lib/includes/spdylay/spdylay.h | 21 +++++++++++++-------- lib/spdylay_frame.c | 12 ++++++++++++ lib/spdylay_frame.h | 5 +++++ lib/spdylay_submit.c | 2 ++ tests/main.c | 2 ++ tests/spdylay_frame_test.c | 17 +++++++++++++++++ tests/spdylay_frame_test.h | 1 + tests/spdylay_session_test.c | 10 ++++++++-- 8 files changed, 60 insertions(+), 10 deletions(-) diff --git a/lib/includes/spdylay/spdylay.h b/lib/includes/spdylay/spdylay.h index c3edd08..aa5c86f 100644 --- a/lib/includes/spdylay/spdylay.h +++ b/lib/includes/spdylay/spdylay.h @@ -394,9 +394,10 @@ void* spdylay_session_get_stream_user_data(spdylay_session *session, int32_t stream_id); /* - * Submits SYN_STREAM frame. |pri| is priority of this request and it - * must be in the range of [0, 3]. 0 means the higest priority. |nv| - * must include following name/value pairs: + * Submits SYN_STREAM frame and optionally one or more DATA + * frames. |pri| is priority of this request and it must be in the + * range of [0, 3]. 0 means the higest priority. |nv| must include + * following name/value pairs: * * "method": HTTP method (e.g., "GET" or "POST") * "scheme": URI scheme (e.g., "https") @@ -405,7 +406,8 @@ void* spdylay_session_get_stream_user_data(spdylay_session *session, * * "host" name/value pair is also required by some hosts. * - * This function creates copies of all name/value pairs in |nv|. + * This function creates copies of all name/value pairs in |nv|. It + * also lower-cases all names in |nv|. * * If |data_prd| is not NULL, it provides data which will be sent in * subsequent DATA frames. In this case, "POST" must be specified with @@ -426,14 +428,17 @@ int spdylay_submit_request(spdylay_session *session, uint8_t pri, void *stream_user_data); /* - * Submits SYN_REPLY frame against stream |stream_id|. |nv| must - * include following name/value pairs: + * Submits SYN_REPLY frame and optionally one or more DATA frames + * against stream |stream_id|. |nv| must include following name/value + * pairs: * * "status": HTTP status code (e.g., "200" or "200 OK") * "version": HTTP response version (e.g., "HTTP/1.1") * - * This function creates copies of all name/value pairs in |nv|. If - * |data_prd| is not NULL, it provides data which will be sent in + * This function creates copies of all name/value pairs in |nv|. It + * also lower-cases all names in |nv|. + * + * If |data_prd| is not NULL, it provides data which will be sent in * subsequent DATA frames. If |data_prd| is NULL, SYN_REPLY will have * FLAG_FIN. * diff --git a/lib/spdylay_frame.c b/lib/spdylay_frame.c index f5972f0..567e54e 100644 --- a/lib/spdylay_frame.c +++ b/lib/spdylay_frame.c @@ -315,6 +315,18 @@ void spdylay_frame_nv_sort(char **nv) qsort(nv, n/2, 2*sizeof(char*), spdylay_string_compar); } +void spdylay_frame_nv_downcase(char **nv) +{ + int i, j; + for(i = 0; nv[i]; i += 2) { + for(j = 0; nv[i][j] != '\0'; ++j) { + if('A' <= nv[i][j] && nv[i][j] <= 'Z') { + nv[i][j] += 'a'-'A'; + } + } + } +} + void spdylay_frame_syn_stream_init(spdylay_syn_stream *frame, uint8_t flags, int32_t stream_id, int32_t assoc_stream_id, uint8_t pri, char **nv) diff --git a/lib/spdylay_frame.h b/lib/spdylay_frame.h index a880a60..fcdcbe6 100644 --- a/lib/spdylay_frame.h +++ b/lib/spdylay_frame.h @@ -293,6 +293,11 @@ char** spdylay_frame_nv_copy(const char **nv); */ void spdylay_frame_nv_sort(char **nv); +/* + * Makes names in |nv| lower cased. + */ +void spdylay_frame_nv_downcase(char **nv); + /* * Makes copy of |iv| and return the copy. The |niv| is the number of * entries in |iv|. This function returns the pointer to the copy if diff --git a/lib/spdylay_submit.c b/lib/spdylay_submit.c index c083c02..86245a0 100644 --- a/lib/spdylay_submit.c +++ b/lib/spdylay_submit.c @@ -71,6 +71,7 @@ int spdylay_submit_response(spdylay_session *session, free(data_prd_copy); return SPDYLAY_ERR_NOMEM; } + spdylay_frame_nv_downcase(nv_copy); spdylay_frame_nv_sort(nv_copy); if(data_prd == NULL) { flags |= SPDYLAY_FLAG_FIN; @@ -151,6 +152,7 @@ int spdylay_submit_request(spdylay_session *session, uint8_t pri, free(data_prd_copy); return SPDYLAY_ERR_NOMEM; } + spdylay_frame_nv_downcase(nv_copy); spdylay_frame_nv_sort(nv_copy); if(data_prd == NULL) { flags |= SPDYLAY_FLAG_FIN; diff --git a/tests/main.c b/tests/main.c index 1a1892d..f144a84 100644 --- a/tests/main.c +++ b/tests/main.c @@ -132,6 +132,8 @@ int main(int argc, char* argv[]) !CU_add_test(pSuite, "frame_pack_settings", test_spdylay_frame_pack_settings) || !CU_add_test(pSuite, "frame_nv_sort", test_spdylay_frame_nv_sort) || + !CU_add_test(pSuite, "frame_nv_downcase", + test_spdylay_frame_nv_downcase) || !CU_add_test(pSuite, "stream_add_pushed_stream", test_spdylay_stream_add_pushed_stream)) { CU_cleanup_registry(); diff --git a/tests/spdylay_frame_test.c b/tests/spdylay_frame_test.c index 9f32268..73a22e9 100644 --- a/tests/spdylay_frame_test.c +++ b/tests/spdylay_frame_test.c @@ -230,3 +230,20 @@ void test_spdylay_frame_nv_sort() CU_ASSERT(strcmp("version", nv[4]) == 0); CU_ASSERT(strcmp("HTTP/1.1", nv[5]) == 0); } + +void test_spdylay_frame_nv_downcase() +{ + const char *nv_src[] = { + "VERSION", "HTTP/1.1", + "Content-Length", "1000000007", + NULL + }; + char **nv; + nv = spdylay_frame_nv_copy(nv_src); + spdylay_frame_nv_downcase(nv); + CU_ASSERT(0 == strcmp("version", nv[0])); + CU_ASSERT(0 == strcmp("HTTP/1.1", nv[1])); + CU_ASSERT(0 == strcmp("content-length", nv[2])); + CU_ASSERT(0 == strcmp("1000000007", nv[3])); + spdylay_frame_nv_del(nv); +} diff --git a/tests/spdylay_frame_test.h b/tests/spdylay_frame_test.h index 26c35da..6484e56 100644 --- a/tests/spdylay_frame_test.h +++ b/tests/spdylay_frame_test.h @@ -33,5 +33,6 @@ void test_spdylay_frame_pack_goaway(); void test_spdylay_frame_pack_headers(); void test_spdylay_frame_pack_settings(); void test_spdylay_frame_nv_sort(); +void test_spdylay_frame_nv_downcase(); #endif /* SPDYLAY_FRAME_TEST_H */ diff --git a/tests/spdylay_session_test.c b/tests/spdylay_session_test.c index 331847f..f7bc8fe 100644 --- a/tests/spdylay_session_test.c +++ b/tests/spdylay_session_test.c @@ -527,10 +527,11 @@ void test_spdylay_submit_response() NULL, NULL }; - const char *nv[] = { NULL }; + const char *nv[] = { "Content-Length", "1024", NULL }; int32_t stream_id = 2; spdylay_data_provider data_prd; my_user_data ud; + spdylay_outbound_item *item; data_prd.read_callback = fixed_length_data_source_read_callback; ud.data_source_length = 64*1024; @@ -538,6 +539,8 @@ void test_spdylay_submit_response() spdylay_session_open_stream(session, stream_id, SPDYLAY_FLAG_NONE, 3, SPDYLAY_STREAM_OPENING, NULL); CU_ASSERT(0 == spdylay_submit_response(session, stream_id, nv, &data_prd)); + item = spdylay_session_get_next_ob_item(session); + CU_ASSERT(0 == strcmp("content-length", item->frame->syn_reply.nv[0])); CU_ASSERT(0 == spdylay_session_send(session)); spdylay_session_del(session); } @@ -551,14 +554,17 @@ void test_spdylay_submit_request_with_data() NULL, NULL }; - const char *nv[] = { NULL }; + const char *nv[] = { "Version", "HTTP/1.1", NULL }; spdylay_data_provider data_prd; my_user_data ud; + spdylay_outbound_item *item; data_prd.read_callback = fixed_length_data_source_read_callback; ud.data_source_length = 64*1024; CU_ASSERT(0 == spdylay_session_client_new(&session, &callbacks, &ud)); CU_ASSERT(0 == spdylay_submit_request(session, 3, nv, &data_prd, NULL)); + item = spdylay_session_get_next_ob_item(session); + CU_ASSERT(0 == strcmp("version", item->frame->syn_stream.nv[0])); CU_ASSERT(0 == spdylay_session_send(session)); CU_ASSERT(0 == ud.data_source_length);