/*1* nghttp2 - HTTP/2 C Library2*3* Copyright (c) 2012 Tatsuhiro Tsujikawa4*5* Permission is hereby granted, free of charge, to any person obtaining6* a copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sublicense, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice shall be14* included in all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND19* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE20* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION21* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION22* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23*/24#include "nghttp2_npn.h"2526#include <string.h>2728static int select_next_protocol(unsigned char **out, unsigned char *outlen,29const unsigned char *in, unsigned int inlen,30const char *key, unsigned int keylen) {31unsigned int i;32for (i = 0; i + keylen <= inlen; i += (unsigned int)(in[i] + 1)) {33if (memcmp(&in[i], key, keylen) == 0) {34*out = (unsigned char *)&in[i + 1];35*outlen = in[i];36return 0;37}38}39return -1;40}4142#define NGHTTP2_HTTP_1_1_ALPN "\x8http/1.1"43#define NGHTTP2_HTTP_1_1_ALPN_LEN (sizeof(NGHTTP2_HTTP_1_1_ALPN) - 1)4445int nghttp2_select_next_protocol(unsigned char **out, unsigned char *outlen,46const unsigned char *in, unsigned int inlen) {47if (select_next_protocol(out, outlen, in, inlen, NGHTTP2_PROTO_ALPN,48NGHTTP2_PROTO_ALPN_LEN) == 0) {49return 1;50}51if (select_next_protocol(out, outlen, in, inlen, NGHTTP2_HTTP_1_1_ALPN,52NGHTTP2_HTTP_1_1_ALPN_LEN) == 0) {53return 0;54}55return -1;56}575859