Added spdylay_npn_get_version()

This commit is contained in:
Tatsuhiro Tsujikawa 2012-02-26 01:30:41 +09:00
parent 8693874340
commit 0a723aa10f
2 changed files with 45 additions and 5 deletions

View File

@ -705,6 +705,16 @@ int spdylay_submit_goaway(spdylay_session *session);
int spdylay_select_next_protocol(unsigned char **out, unsigned char *outlen, int spdylay_select_next_protocol(unsigned char **out, unsigned char *outlen,
const unsigned char *in, unsigned int inlen); const unsigned char *in, unsigned int inlen);
/*
* Returns spdy version which spdylay library supports from given
* protocol name. The |proto| is the pointer to the protocol name and
* |protolen| is its length. Currently, "spdy/2" and "spdy/3" are
* supported.
*
* This function returns nonzero spdy version if it succeeds, or 0.
*/
uint16_t spdylay_npn_get_version(const unsigned char *proto, size_t protolen);
/* /*
* Put back previously deferred DATA frame in the stream |stream_id| * Put back previously deferred DATA frame in the stream |stream_id|
* to outbound queue. * to outbound queue.

View File

@ -26,17 +26,31 @@
#include <string.h> #include <string.h>
typedef struct {
const unsigned char *proto;
uint8_t len;
} spdylay_npn_proto;
int spdylay_select_next_protocol(unsigned char **out, unsigned char *outlen, int spdylay_select_next_protocol(unsigned char **out, unsigned char *outlen,
const unsigned char *in, unsigned int inlen) const unsigned char *in, unsigned int inlen)
{ {
int http_selected = 0; int http_selected = 0;
unsigned int i = 0; unsigned int i = 0;
static const spdylay_npn_proto proto_list[] = {
{ (const unsigned char*)"spdy/2", 6 },
{ (const unsigned char*)"spdy/3", 6 }
};
for(; i < inlen; i += in[i]+1) { for(; i < inlen; i += in[i]+1) {
if(in[i] == 6 && memcmp(&in[i+1], "spdy/2", in[i]) == 0) { int j;
for(j = 0; j < sizeof(proto_list)/sizeof(spdylay_npn_proto); ++j) {
if(in[i] == proto_list[j].len &&
memcmp(&in[i+1], proto_list[j].proto, in[i]) == 0) {
*out = (unsigned char*)&in[i+1]; *out = (unsigned char*)&in[i+1];
*outlen = in[i]; *outlen = in[i];
return 1; return 1;
} else if(in[i] == 8 && memcmp(&in[i+1], "http/1.1", in[i]) == 0) { }
}
if(in[i] == 8 && memcmp(&in[i+1], "http/1.1", in[i]) == 0) {
http_selected = 1; http_selected = 1;
*out = (unsigned char*)&in[i+1]; *out = (unsigned char*)&in[i+1];
*outlen = in[i]; *outlen = in[i];
@ -50,3 +64,19 @@ int spdylay_select_next_protocol(unsigned char **out, unsigned char *outlen,
return -1; return -1;
} }
} }
uint16_t spdylay_npn_get_version(const unsigned char *proto, size_t protolen)
{
if(proto == NULL) {
return 0;
} else {
if(protolen == 6) {
if(memcmp("spdy/2", proto, 6) == 0) {
return SPDYLAY_PROTO_SPDY2;
} else if(memcmp("spdy/3", proto, 6) == 0) {
return SPDYLAY_PROTO_SPDY3;
}
}
return 0;
}
}