Merge branch 'master' of https://github.com/sorced-jim/spdylay into sorced-jim-master

Conflicts:
	examples/spdylay_ssl.cc
	tests/Makefile.am
	tests/main.c

Changes:
        spdylay_select_next_protocol() returns -1 if it fails.
        Use cunit without pkg-config because debian does not provide .pc file.
        Some doc updates to suite my taste.
        Added spdylay_npn.h
This commit is contained in:
Tatsuhiro Tsujikawa 2012-02-03 23:53:43 +09:00
commit 876c33c562
10 changed files with 213 additions and 13 deletions

2
.gitignore vendored
View File

@ -25,3 +25,5 @@ ltmain.sh
stamp-h1 stamp-h1
.deps/ .deps/
INSTALL INSTALL
.DS_STORE
tests/main

View File

@ -346,8 +346,6 @@ int select_next_proto_cb(SSL* ssl,
const unsigned char *in, unsigned int inlen, const unsigned char *in, unsigned int inlen,
void *arg) void *arg)
{ {
*out = (unsigned char*)in+1;
*outlen = in[0];
if(ssl_debug) { if(ssl_debug) {
print_timer(); print_timer();
std::cout << " NPN select next protocol: the remote server offers:" std::cout << " NPN select next protocol: the remote server offers:"
@ -359,10 +357,15 @@ int select_next_proto_cb(SSL* ssl,
std::cout.write(reinterpret_cast<const char*>(&in[i+1]), in[i]); std::cout.write(reinterpret_cast<const char*>(&in[i+1]), in[i]);
std::cout << std::endl; std::cout << std::endl;
} }
if(in[i] == 6 && memcmp(&in[i+1], "spdy/2", in[i]) == 0) { }
*out = (unsigned char*)in+i+1; if(spdylay_select_next_protocol(out, outlen, in, inlen) == -1) {
*outlen = in[i]; std::cerr << "Invalid protocol: "
} << std::string((const char*)*out, (size_t)*outlen) << std::endl;
abort();
}
if(ssl_debug) {
std::cout << " NPN selected the protocol: "
<< std::string((const char*)*out, (size_t)*outlen) << std::endl;
} }
return SSL_TLSEXT_ERR_OK; return SSL_TLSEXT_ERR_OK;
} }

View File

@ -33,11 +33,12 @@ lib_LTLIBRARIES = libspdylay.la
OBJECTS = spdylay_pq.c spdylay_map.c spdylay_queue.c \ OBJECTS = spdylay_pq.c spdylay_map.c spdylay_queue.c \
spdylay_buffer.c spdylay_frame.c spdylay_zlib.c \ spdylay_buffer.c spdylay_frame.c spdylay_zlib.c \
spdylay_session.c spdylay_helper.c spdylay_stream.c spdylay_session.c spdylay_helper.c spdylay_stream.c spdylay_npn.c
HFILES = spdylay_pq.h spdylay_int.h spdylay_map.h spdylay_queue.h \ HFILES = spdylay_pq.h spdylay_int.h spdylay_map.h spdylay_queue.h \
spdylay_buffer.h spdylay_frame.h spdylay_zlib.h \ spdylay_buffer.h spdylay_frame.h spdylay_zlib.h \
spdylay_session.h spdylay_helper.h spdylay_stream.h spdylay_int.h spdylay_session.h spdylay_helper.h spdylay_stream.h spdylay_int.h \
spdylay_npn.h
libspdylay_la_SOURCES = $(HFILES) $(OBJECTS) libspdylay_la_SOURCES = $(HFILES) $(OBJECTS)
libspdylay_la_LDFLAGS = -no-undefined \ libspdylay_la_LDFLAGS = -no-undefined \

View File

@ -414,6 +414,27 @@ int spdylay_submit_ping(spdylay_session *session);
*/ */
int spdylay_submit_goaway(spdylay_session *session); int spdylay_submit_goaway(spdylay_session *session);
/*
* A helper function for dealing with NPN. This function returns the
* version of spdy that was negotiated, or -1. To use this method you
* should do something like:
*
* static int select_next_proto_cb(SSL* ssl,
* unsigned char **out, unsigned char *outlen,
* const unsigned char *in, unsigned int inlen,
* void *arg)
* {
* if (spdylay_select_next_protocol(out, outlen, in, inlen) > 0) {
* ((MyType*)arg)->spdy = 1;
* }
* return SSL_TLSEXT_ERR_OK;
* }
* ...
* SSL_CTX_set_next_proto_select_cb(ssl_ctx, select_next_proto_cb, my_obj);
*/
int spdylay_select_next_protocol(unsigned char **out, unsigned char *outlen,
const unsigned char *in, unsigned int inlen);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

42
lib/spdylay_npn.c Normal file
View File

@ -0,0 +1,42 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "spdylay_npn.h"
#include <string.h>
int spdylay_select_next_protocol(unsigned char **out, unsigned char *outlen,
const unsigned char *in, unsigned int inlen)
{
unsigned int i = 0;
for(; i < inlen; i += in[i]+1) {
/* Always assign to *out so that the last one is picked. */
*out = (unsigned char*)in+i+1;
*outlen = in[i];
if(in[i] == 6 && memcmp(&in[i+1], "spdy/2", in[i]) == 0) {
return 2;
}
}
return -1;
}

34
lib/spdylay_npn.h Normal file
View File

@ -0,0 +1,34 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SPDYLAY_NPN_H
#define SPDYLAY_NPN_H
#ifdef HAVE_CONFIG
# include <config.h>
#endif /* HAVE_CONFIG */
#include <spdylay/spdylay.h>
#endif /* SPDYLAY_NPN_H */

View File

@ -27,17 +27,18 @@ check_PROGRAMS = main
OBJECTS = main.c spdylay_pq_test.c spdylay_map_test.c spdylay_queue_test.c \ OBJECTS = main.c spdylay_pq_test.c spdylay_map_test.c spdylay_queue_test.c \
spdylay_buffer_test.c spdylay_zlib_test.c spdylay_session_test.c \ spdylay_buffer_test.c spdylay_zlib_test.c spdylay_session_test.c \
spdylay_frame_test.c spdylay_stream_test.c spdylay_frame_test.c spdylay_stream_test.c spdylay_npn_test.c
HFILES = spdylay_pq_test.h spdylay_map_test.h spdylay_queue_test.h \ HFILES = spdylay_pq_test.h spdylay_map_test.h spdylay_queue_test.h \
spdylay_buffer_test.h spdylay_zlib_test.h spdylay_session_test.h \ spdylay_buffer_test.h spdylay_zlib_test.h spdylay_session_test.h \
spdylay_frame_test.h spdylay_stream_test.h spdylay_frame_test.h spdylay_stream_test.h spdylay_npn_test.h
main_SOURCES = $(HFILES) $(OBJECTS) main_SOURCES = $(HFILES) $(OBJECTS)
main_LDADD = ${top_builddir}/lib/libspdylay.la -lcunit main_LDADD = ${top_builddir}/lib/libspdylay.la
main_LDFLAGS = -static main_LDFLAGS = -static -lcunit -lncurses
AM_CFLAGS = -Wall -g -O2 -I${top_srcdir}/lib -I${top_srcdir}/lib/includes AM_CFLAGS = -Wall -g -O2 -I${top_srcdir}/lib -I${top_srcdir}/lib/includes
TESTS = main TESTS = main
endif # HAVE_CUNIT endif # HAVE_CUNIT

View File

@ -34,6 +34,7 @@
#include "spdylay_session_test.h" #include "spdylay_session_test.h"
#include "spdylay_frame_test.h" #include "spdylay_frame_test.h"
#include "spdylay_stream_test.h" #include "spdylay_stream_test.h"
#include "spdylay_npn_test.h"
int init_suite1(void) int init_suite1(void)
{ {
@ -46,7 +47,7 @@ int clean_suite1(void)
} }
int main() int main(int argc, char* argv[])
{ {
CU_pSuite pSuite = NULL; CU_pSuite pSuite = NULL;
@ -67,6 +68,7 @@ int main()
!CU_add_test(pSuite, "queue", test_spdylay_queue) || !CU_add_test(pSuite, "queue", test_spdylay_queue) ||
!CU_add_test(pSuite, "buffer", test_spdylay_buffer) || !CU_add_test(pSuite, "buffer", test_spdylay_buffer) ||
!CU_add_test(pSuite, "zlib", test_spdylay_zlib) || !CU_add_test(pSuite, "zlib", test_spdylay_zlib) ||
!CU_add_test(pSuite, "npn", test_spdylay_npn) ||
!CU_add_test(pSuite, "session_recv", test_spdylay_session_recv) || !CU_add_test(pSuite, "session_recv", test_spdylay_session_recv) ||
!CU_add_test(pSuite, "session_recv_invalid_stream_id", !CU_add_test(pSuite, "session_recv_invalid_stream_id",
test_spdylay_session_recv_invalid_stream_id) || test_spdylay_session_recv_invalid_stream_id) ||

64
tests/spdylay_npn_test.c Normal file
View File

@ -0,0 +1,64 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Twist Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <CUnit/CUnit.h>
#include <spdylay/spdylay.h>
#include <string.h>
static void spdy2()
{
const unsigned char spdy[] = {
8, 'h', 't', 't', 'p', '/', '1', '.', '0',
6, 's', 'p', 'd', 'y', '/', '2',
6, 's', 'p', 'd', 'y', '/', '3'
};
unsigned char outlen;
unsigned char* out;
CU_ASSERT(2 == spdylay_select_next_protocol(&out, &outlen,
spdy, sizeof(spdy)));
CU_ASSERT(6 == outlen);
CU_ASSERT(memcmp("spdy/2", out, 6) == 0);
}
static void spdy4()
{
const unsigned char spdy[] = {
6, 's', 'p', 'd', 'y', '/', '4',
8, 's', 'p', 'd', 'y', '/', '2', '.', '1',
8, 'h', 't', 't', 'p', '/', '1', '.', '0',
};
unsigned char outlen;
unsigned char* out;
CU_ASSERT(-1 == spdylay_select_next_protocol(&out, &outlen,
spdy, sizeof(spdy)));
CU_ASSERT(8 == outlen);
CU_ASSERT(memcmp("http/1.0", out, outlen) == 0);
}
void test_spdylay_npn()
{
spdy2();
spdy4();
}

30
tests/spdylay_npn_test.h Normal file
View File

@ -0,0 +1,30 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Twist Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SPDYLAY_NPN_TEST_H
#define SPDYLAY_NPN_TEST_H
void test_spdylay_npn();
#endif // SPDYLAY_NPN_TEST_H