aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/nghttp2/lib/nghttp2_npn.c
blob: ce097ab446c628f71dba44f3f17cc98609f0d28e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* 
 * nghttp2 - HTTP/2 C 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 "nghttp2_npn.h" 
 
#include <string.h> 
 
static int select_next_protocol(unsigned char **out, unsigned char *outlen, 
                                const unsigned char *in, unsigned int inlen, 
                                const char *key, unsigned int keylen) { 
  unsigned int i; 
  for (i = 0; i + keylen <= inlen; i += (unsigned int)(in[i] + 1)) { 
    if (memcmp(&in[i], key, keylen) == 0) { 
      *out = (unsigned char *)&in[i + 1]; 
      *outlen = in[i]; 
      return 0; 
    } 
  } 
  return -1; 
} 
 
#define NGHTTP2_HTTP_1_1_ALPN "\x8http/1.1" 
#define NGHTTP2_HTTP_1_1_ALPN_LEN (sizeof(NGHTTP2_HTTP_1_1_ALPN) - 1) 
 
int nghttp2_select_next_protocol(unsigned char **out, unsigned char *outlen, 
                                 const unsigned char *in, unsigned int inlen) { 
  if (select_next_protocol(out, outlen, in, inlen, NGHTTP2_PROTO_ALPN, 
                           NGHTTP2_PROTO_ALPN_LEN) == 0) { 
    return 1; 
  } 
  if (select_next_protocol(out, outlen, in, inlen, NGHTTP2_HTTP_1_1_ALPN, 
                           NGHTTP2_HTTP_1_1_ALPN_LEN) == 0) { 
    return 0; 
  } 
  return -1; 
}