From 97648d257fc61bbcfe485ba0c25e79aa06d20fdf Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 21 Apr 2015 23:48:45 +0900 Subject: [PATCH 01/13] mkhufftbl.py: Refactor --- mkhufftbl.py | 432 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 347 insertions(+), 85 deletions(-) diff --git a/mkhufftbl.py b/mkhufftbl.py index c243abca..86960428 100755 --- a/mkhufftbl.py +++ b/mkhufftbl.py @@ -10,8 +10,271 @@ from __future__ import unicode_literals import re import sys +import StringIO + +# From [1] +HUFFMAN_CODE_TABLE = """\ + ( 0) |11111111|11000 1ff8 [13] + ( 1) |11111111|11111111|1011000 7fffd8 [23] + ( 2) |11111111|11111111|11111110|0010 fffffe2 [28] + ( 3) |11111111|11111111|11111110|0011 fffffe3 [28] + ( 4) |11111111|11111111|11111110|0100 fffffe4 [28] + ( 5) |11111111|11111111|11111110|0101 fffffe5 [28] + ( 6) |11111111|11111111|11111110|0110 fffffe6 [28] + ( 7) |11111111|11111111|11111110|0111 fffffe7 [28] + ( 8) |11111111|11111111|11111110|1000 fffffe8 [28] + ( 9) |11111111|11111111|11101010 ffffea [24] + ( 10) |11111111|11111111|11111111|111100 3ffffffc [30] + ( 11) |11111111|11111111|11111110|1001 fffffe9 [28] + ( 12) |11111111|11111111|11111110|1010 fffffea [28] + ( 13) |11111111|11111111|11111111|111101 3ffffffd [30] + ( 14) |11111111|11111111|11111110|1011 fffffeb [28] + ( 15) |11111111|11111111|11111110|1100 fffffec [28] + ( 16) |11111111|11111111|11111110|1101 fffffed [28] + ( 17) |11111111|11111111|11111110|1110 fffffee [28] + ( 18) |11111111|11111111|11111110|1111 fffffef [28] + ( 19) |11111111|11111111|11111111|0000 ffffff0 [28] + ( 20) |11111111|11111111|11111111|0001 ffffff1 [28] + ( 21) |11111111|11111111|11111111|0010 ffffff2 [28] + ( 22) |11111111|11111111|11111111|111110 3ffffffe [30] + ( 23) |11111111|11111111|11111111|0011 ffffff3 [28] + ( 24) |11111111|11111111|11111111|0100 ffffff4 [28] + ( 25) |11111111|11111111|11111111|0101 ffffff5 [28] + ( 26) |11111111|11111111|11111111|0110 ffffff6 [28] + ( 27) |11111111|11111111|11111111|0111 ffffff7 [28] + ( 28) |11111111|11111111|11111111|1000 ffffff8 [28] + ( 29) |11111111|11111111|11111111|1001 ffffff9 [28] + ( 30) |11111111|11111111|11111111|1010 ffffffa [28] + ( 31) |11111111|11111111|11111111|1011 ffffffb [28] +' ' ( 32) |010100 14 [ 6] +'!' ( 33) |11111110|00 3f8 [10] +'"' ( 34) |11111110|01 3f9 [10] +'#' ( 35) |11111111|1010 ffa [12] +'$' ( 36) |11111111|11001 1ff9 [13] +'%' ( 37) |010101 15 [ 6] +'&' ( 38) |11111000 f8 [ 8] +''' ( 39) |11111111|010 7fa [11] +'(' ( 40) |11111110|10 3fa [10] +')' ( 41) |11111110|11 3fb [10] +'*' ( 42) |11111001 f9 [ 8] +'+' ( 43) |11111111|011 7fb [11] +',' ( 44) |11111010 fa [ 8] +'-' ( 45) |010110 16 [ 6] +'.' ( 46) |010111 17 [ 6] +'/' ( 47) |011000 18 [ 6] +'0' ( 48) |00000 0 [ 5] +'1' ( 49) |00001 1 [ 5] +'2' ( 50) |00010 2 [ 5] +'3' ( 51) |011001 19 [ 6] +'4' ( 52) |011010 1a [ 6] +'5' ( 53) |011011 1b [ 6] +'6' ( 54) |011100 1c [ 6] +'7' ( 55) |011101 1d [ 6] +'8' ( 56) |011110 1e [ 6] +'9' ( 57) |011111 1f [ 6] +':' ( 58) |1011100 5c [ 7] +';' ( 59) |11111011 fb [ 8] +'<' ( 60) |11111111|1111100 7ffc [15] +'=' ( 61) |100000 20 [ 6] +'>' ( 62) |11111111|1011 ffb [12] +'?' ( 63) |11111111|00 3fc [10] +'@' ( 64) |11111111|11010 1ffa [13] +'A' ( 65) |100001 21 [ 6] +'B' ( 66) |1011101 5d [ 7] +'C' ( 67) |1011110 5e [ 7] +'D' ( 68) |1011111 5f [ 7] +'E' ( 69) |1100000 60 [ 7] +'F' ( 70) |1100001 61 [ 7] +'G' ( 71) |1100010 62 [ 7] +'H' ( 72) |1100011 63 [ 7] +'I' ( 73) |1100100 64 [ 7] +'J' ( 74) |1100101 65 [ 7] +'K' ( 75) |1100110 66 [ 7] +'L' ( 76) |1100111 67 [ 7] +'M' ( 77) |1101000 68 [ 7] +'N' ( 78) |1101001 69 [ 7] +'O' ( 79) |1101010 6a [ 7] +'P' ( 80) |1101011 6b [ 7] +'Q' ( 81) |1101100 6c [ 7] +'R' ( 82) |1101101 6d [ 7] +'S' ( 83) |1101110 6e [ 7] +'T' ( 84) |1101111 6f [ 7] +'U' ( 85) |1110000 70 [ 7] +'V' ( 86) |1110001 71 [ 7] +'W' ( 87) |1110010 72 [ 7] +'X' ( 88) |11111100 fc [ 8] +'Y' ( 89) |1110011 73 [ 7] +'Z' ( 90) |11111101 fd [ 8] +'[' ( 91) |11111111|11011 1ffb [13] +'\' ( 92) |11111111|11111110|000 7fff0 [19] +']' ( 93) |11111111|11100 1ffc [13] +'^' ( 94) |11111111|111100 3ffc [14] +'_' ( 95) |100010 22 [ 6] +'`' ( 96) |11111111|1111101 7ffd [15] +'a' ( 97) |00011 3 [ 5] +'b' ( 98) |100011 23 [ 6] +'c' ( 99) |00100 4 [ 5] +'d' (100) |100100 24 [ 6] +'e' (101) |00101 5 [ 5] +'f' (102) |100101 25 [ 6] +'g' (103) |100110 26 [ 6] +'h' (104) |100111 27 [ 6] +'i' (105) |00110 6 [ 5] +'j' (106) |1110100 74 [ 7] +'k' (107) |1110101 75 [ 7] +'l' (108) |101000 28 [ 6] +'m' (109) |101001 29 [ 6] +'n' (110) |101010 2a [ 6] +'o' (111) |00111 7 [ 5] +'p' (112) |101011 2b [ 6] +'q' (113) |1110110 76 [ 7] +'r' (114) |101100 2c [ 6] +'s' (115) |01000 8 [ 5] +'t' (116) |01001 9 [ 5] +'u' (117) |101101 2d [ 6] +'v' (118) |1110111 77 [ 7] +'w' (119) |1111000 78 [ 7] +'x' (120) |1111001 79 [ 7] +'y' (121) |1111010 7a [ 7] +'z' (122) |1111011 7b [ 7] +'{' (123) |11111111|1111110 7ffe [15] +'|' (124) |11111111|100 7fc [11] +'}' (125) |11111111|111101 3ffd [14] +'~' (126) |11111111|11101 1ffd [13] + (127) |11111111|11111111|11111111|1100 ffffffc [28] + (128) |11111111|11111110|0110 fffe6 [20] + (129) |11111111|11111111|010010 3fffd2 [22] + (130) |11111111|11111110|0111 fffe7 [20] + (131) |11111111|11111110|1000 fffe8 [20] + (132) |11111111|11111111|010011 3fffd3 [22] + (133) |11111111|11111111|010100 3fffd4 [22] + (134) |11111111|11111111|010101 3fffd5 [22] + (135) |11111111|11111111|1011001 7fffd9 [23] + (136) |11111111|11111111|010110 3fffd6 [22] + (137) |11111111|11111111|1011010 7fffda [23] + (138) |11111111|11111111|1011011 7fffdb [23] + (139) |11111111|11111111|1011100 7fffdc [23] + (140) |11111111|11111111|1011101 7fffdd [23] + (141) |11111111|11111111|1011110 7fffde [23] + (142) |11111111|11111111|11101011 ffffeb [24] + (143) |11111111|11111111|1011111 7fffdf [23] + (144) |11111111|11111111|11101100 ffffec [24] + (145) |11111111|11111111|11101101 ffffed [24] + (146) |11111111|11111111|010111 3fffd7 [22] + (147) |11111111|11111111|1100000 7fffe0 [23] + (148) |11111111|11111111|11101110 ffffee [24] + (149) |11111111|11111111|1100001 7fffe1 [23] + (150) |11111111|11111111|1100010 7fffe2 [23] + (151) |11111111|11111111|1100011 7fffe3 [23] + (152) |11111111|11111111|1100100 7fffe4 [23] + (153) |11111111|11111110|11100 1fffdc [21] + (154) |11111111|11111111|011000 3fffd8 [22] + (155) |11111111|11111111|1100101 7fffe5 [23] + (156) |11111111|11111111|011001 3fffd9 [22] + (157) |11111111|11111111|1100110 7fffe6 [23] + (158) |11111111|11111111|1100111 7fffe7 [23] + (159) |11111111|11111111|11101111 ffffef [24] + (160) |11111111|11111111|011010 3fffda [22] + (161) |11111111|11111110|11101 1fffdd [21] + (162) |11111111|11111110|1001 fffe9 [20] + (163) |11111111|11111111|011011 3fffdb [22] + (164) |11111111|11111111|011100 3fffdc [22] + (165) |11111111|11111111|1101000 7fffe8 [23] + (166) |11111111|11111111|1101001 7fffe9 [23] + (167) |11111111|11111110|11110 1fffde [21] + (168) |11111111|11111111|1101010 7fffea [23] + (169) |11111111|11111111|011101 3fffdd [22] + (170) |11111111|11111111|011110 3fffde [22] + (171) |11111111|11111111|11110000 fffff0 [24] + (172) |11111111|11111110|11111 1fffdf [21] + (173) |11111111|11111111|011111 3fffdf [22] + (174) |11111111|11111111|1101011 7fffeb [23] + (175) |11111111|11111111|1101100 7fffec [23] + (176) |11111111|11111111|00000 1fffe0 [21] + (177) |11111111|11111111|00001 1fffe1 [21] + (178) |11111111|11111111|100000 3fffe0 [22] + (179) |11111111|11111111|00010 1fffe2 [21] + (180) |11111111|11111111|1101101 7fffed [23] + (181) |11111111|11111111|100001 3fffe1 [22] + (182) |11111111|11111111|1101110 7fffee [23] + (183) |11111111|11111111|1101111 7fffef [23] + (184) |11111111|11111110|1010 fffea [20] + (185) |11111111|11111111|100010 3fffe2 [22] + (186) |11111111|11111111|100011 3fffe3 [22] + (187) |11111111|11111111|100100 3fffe4 [22] + (188) |11111111|11111111|1110000 7ffff0 [23] + (189) |11111111|11111111|100101 3fffe5 [22] + (190) |11111111|11111111|100110 3fffe6 [22] + (191) |11111111|11111111|1110001 7ffff1 [23] + (192) |11111111|11111111|11111000|00 3ffffe0 [26] + (193) |11111111|11111111|11111000|01 3ffffe1 [26] + (194) |11111111|11111110|1011 fffeb [20] + (195) |11111111|11111110|001 7fff1 [19] + (196) |11111111|11111111|100111 3fffe7 [22] + (197) |11111111|11111111|1110010 7ffff2 [23] + (198) |11111111|11111111|101000 3fffe8 [22] + (199) |11111111|11111111|11110110|0 1ffffec [25] + (200) |11111111|11111111|11111000|10 3ffffe2 [26] + (201) |11111111|11111111|11111000|11 3ffffe3 [26] + (202) |11111111|11111111|11111001|00 3ffffe4 [26] + (203) |11111111|11111111|11111011|110 7ffffde [27] + (204) |11111111|11111111|11111011|111 7ffffdf [27] + (205) |11111111|11111111|11111001|01 3ffffe5 [26] + (206) |11111111|11111111|11110001 fffff1 [24] + (207) |11111111|11111111|11110110|1 1ffffed [25] + (208) |11111111|11111110|010 7fff2 [19] + (209) |11111111|11111111|00011 1fffe3 [21] + (210) |11111111|11111111|11111001|10 3ffffe6 [26] + (211) |11111111|11111111|11111100|000 7ffffe0 [27] + (212) |11111111|11111111|11111100|001 7ffffe1 [27] + (213) |11111111|11111111|11111001|11 3ffffe7 [26] + (214) |11111111|11111111|11111100|010 7ffffe2 [27] + (215) |11111111|11111111|11110010 fffff2 [24] + (216) |11111111|11111111|00100 1fffe4 [21] + (217) |11111111|11111111|00101 1fffe5 [21] + (218) |11111111|11111111|11111010|00 3ffffe8 [26] + (219) |11111111|11111111|11111010|01 3ffffe9 [26] + (220) |11111111|11111111|11111111|1101 ffffffd [28] + (221) |11111111|11111111|11111100|011 7ffffe3 [27] + (222) |11111111|11111111|11111100|100 7ffffe4 [27] + (223) |11111111|11111111|11111100|101 7ffffe5 [27] + (224) |11111111|11111110|1100 fffec [20] + (225) |11111111|11111111|11110011 fffff3 [24] + (226) |11111111|11111110|1101 fffed [20] + (227) |11111111|11111111|00110 1fffe6 [21] + (228) |11111111|11111111|101001 3fffe9 [22] + (229) |11111111|11111111|00111 1fffe7 [21] + (230) |11111111|11111111|01000 1fffe8 [21] + (231) |11111111|11111111|1110011 7ffff3 [23] + (232) |11111111|11111111|101010 3fffea [22] + (233) |11111111|11111111|101011 3fffeb [22] + (234) |11111111|11111111|11110111|0 1ffffee [25] + (235) |11111111|11111111|11110111|1 1ffffef [25] + (236) |11111111|11111111|11110100 fffff4 [24] + (237) |11111111|11111111|11110101 fffff5 [24] + (238) |11111111|11111111|11111010|10 3ffffea [26] + (239) |11111111|11111111|1110100 7ffff4 [23] + (240) |11111111|11111111|11111010|11 3ffffeb [26] + (241) |11111111|11111111|11111100|110 7ffffe6 [27] + (242) |11111111|11111111|11111011|00 3ffffec [26] + (243) |11111111|11111111|11111011|01 3ffffed [26] + (244) |11111111|11111111|11111100|111 7ffffe7 [27] + (245) |11111111|11111111|11111101|000 7ffffe8 [27] + (246) |11111111|11111111|11111101|001 7ffffe9 [27] + (247) |11111111|11111111|11111101|010 7ffffea [27] + (248) |11111111|11111111|11111101|011 7ffffeb [27] + (249) |11111111|11111111|11111111|1110 ffffffe [28] + (250) |11111111|11111111|11111101|100 7ffffec [27] + (251) |11111111|11111111|11111101|101 7ffffed [27] + (252) |11111111|11111111|11111101|110 7ffffee [27] + (253) |11111111|11111111|11111101|111 7ffffef [27] + (254) |11111111|11111111|11111110|000 7fffff0 [27] + (255) |11111111|11111111|11111011|10 3ffffee [26] +EOS (256) |11111111|11111111|11111111|111111 3fffffff [30] +""" class Node: + def __init__(self, term = None): self.term = term self.left = None @@ -20,21 +283,18 @@ class Node: self.id = None self.accept = False -def to_bin(s): - res = [] - for i in range(0, len(s), 8): - x = s[i:i+8] - x += '0'*(8 - len(x)) - a = 0 - for j in range(8): - a *= 2 - a += ord(x[j]) - ord('0') - res.append(a) #chr(a)) - return res +class Context: -nodes = [] + def __init__(self): + self.next_id_ = 0 + self.root = Node() -def insert(node, sym, bits): + def next_id(self): + id = self.next_id_ + self.next_id_ += 1 + return id + +def _add(node, sym, bits): if len(bits) == 0: node.term = sym return @@ -47,67 +307,71 @@ def insert(node, sym, bits): if node.right is None: node.right = Node() child = node.right - insert(child, sym, bits[1:]) + _add(child, sym, bits[1:]) -def traverse(node, bits, syms, start_node, root, depth): - if depth == 4: - if 256 in syms: - syms = [] +def huffman_tree_add(ctx, sym, bits): + _add(ctx.root, sym, bits) + +def _set_node_id(ctx, node, prefix): + if node.term is not None: + return + if len(prefix) <= 7 and [1] * len(prefix) == prefix: + node.accept = True + node.id = ctx.next_id() + _set_node_id(ctx, node.left, prefix + [0]) + _set_node_id(ctx, node.right, prefix + [1]) + +def huffman_tree_set_node_id(ctx): + _set_node_id(ctx, ctx.root, []) + +def _traverse(node, sym, start_node, root, left): + if left == 0: + if sym == 256: + sym = None node = None - start_node.trans.append((node, bits, syms)) + start_node.trans.append((node, sym)) return if node.term is not None: node = root - def go(node, bit): - nbits = list(bits) - nbits.append(bit) - nsyms = list(syms) + def go(node): if node.term is not None: - nsyms.append(node.term) - traverse(node, nbits, nsyms, start_node, root, depth + 1) + assert sym is None + nsym = node.term + else: + nsym = sym - go(node.left, 0) - go(node.right, 1) + _traverse(node, nsym, start_node, root, left - 1) -idseed = 0 + go(node.left) + go(node.right) -def dfs_setid(node, prefix): - if node.term is not None: - return - if len(prefix) <= 7 and [1] * len(prefix) == prefix: - node.accept = True - global idseed - node.id = idseed - idseed += 1 - dfs_setid(node.left, prefix + [0]) - dfs_setid(node.right, prefix + [1]) - -def dfs(node, root): +def _build_transition_table(ctx, node): if node is None: return - traverse(node, [], [], node, root, 0) - dfs(node.left, root) - dfs(node.right, root) + _traverse(node, None, node, ctx.root, 4) + _build_transition_table(ctx, node.left) + _build_transition_table(ctx, node.right) + +def huffman_tree_build_transition_table(ctx): + _build_transition_table(ctx, ctx.root) NGHTTP2_HUFF_ACCEPTED = 1 NGHTTP2_HUFF_SYM = 1 << 1 NGHTTP2_HUFF_FAIL = 1 << 2 -def dfs_print(node): +def _print_transition_table(node): if node.term is not None: return print '/* {} */'.format(node.id) print '{' - for nd, bits, syms in node.trans: - outlen = len(syms) + for nd, sym in node.trans: flags = 0 - if outlen == 0: + if sym is None: out = 0 else: - assert(outlen == 1) - out = syms[0] + out = sym flags |= NGHTTP2_HUFF_SYM if nd is None: id = 0 @@ -122,52 +386,50 @@ def dfs_print(node): flags |= NGHTTP2_HUFF_ACCEPTED print ' {{{}, 0x{:02x}, {}}},'.format(id, flags, out) print '},' - dfs_print(node.left) - dfs_print(node.right) + _print_transition_table(node.left) + _print_transition_table(node.right) -symbol_tbl = [(None, 0) for i in range(257)] -tables = {} +def huffman_tree_print_transition_table(ctx): + _print_transition_table(ctx.root) -root = Node() +if __name__ == '__main__': + ctx = Context() + symbol_tbl = [(None, 0) for i in range(257)] -for line in sys.stdin: - m = re.match(r'.*\(\s*(\d+)\)\s+([|01]+)\s+(\S+)\s+\[\s*(\d+)\].*', line) - if m: - #print m.group(1), m.group(2), m.group(4) - if len(m.group(3)) > 8: - raise Error('Code is more than 4 bytes long') - sym = int(m.group(1)) - bits = re.sub(r'\|', '', m.group(2)) - nbits = int(m.group(4)) - assert(len(bits) == nbits) - binpat = to_bin(bits) - assert(len(binpat) == (nbits+7)/8) - symbol_tbl[sym] = (binpat, nbits, m.group(3)) - #print "Inserting", sym - insert(root, sym, bits) + for line in StringIO.StringIO(HUFFMAN_CODE_TABLE): + m = re.match( + r'.*\(\s*(\d+)\)\s+([|01]+)\s+(\S+)\s+\[\s*(\d+)\].*', line) + if m: + sym = int(m.group(1)) + bits = re.sub(r'\|', '', m.group(2)) + code = m.group(3) + nbits = int(m.group(4)) + if len(code) > 8: + raise Error('Code is more than 4 bytes long') + assert(len(bits) == nbits) + symbol_tbl[sym] = (nbits, code) + huffman_tree_add(ctx, sym, bits) -dfs_setid(root, []) -dfs(root, root) + huffman_tree_set_node_id(ctx) + huffman_tree_build_transition_table(ctx) -print '''\ + print '''\ typedef struct { uint32_t nbits; uint32_t code; } nghttp2_huff_sym; ''' -print '''\ -const nghttp2_huff_sym huff_sym_table[] = {''' -for i in range(257): - pat = list(symbol_tbl[i][0]) - pat += [0]*(4 - len(pat)) print '''\ +const nghttp2_huff_sym huff_sym_table[] = {''' + for i in range(257): + print '''\ {{ {}, 0x{}u }}{}\ -'''.format(symbol_tbl[i][1], symbol_tbl[i][2], ',' if i < 256 else '') -print '};' -print '' +'''.format(symbol_tbl[i][0], symbol_tbl[i][1], ',' if i < 256 else '') + print '};' + print '' -print '''\ + print '''\ enum {{ NGHTTP2_HUFF_ACCEPTED = {}, NGHTTP2_HUFF_SYM = {}, @@ -175,7 +437,7 @@ enum {{ }} nghttp2_huff_decode_flag; '''.format(NGHTTP2_HUFF_ACCEPTED, NGHTTP2_HUFF_SYM, NGHTTP2_HUFF_FAIL) -print '''\ + print '''\ typedef struct { uint8_t state; uint8_t flags; @@ -183,7 +445,7 @@ typedef struct { } nghttp2_huff_decode; ''' -print '''\ + print '''\ const nghttp2_huff_decode huff_decode_table[][16] = {''' -dfs_print(root) -print '};' + huffman_tree_print_transition_table(ctx) + print '};' From 92a1ca5917896e8c4525ebe22855c07ade466110 Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Wed, 22 Apr 2015 17:51:28 +0800 Subject: [PATCH 02/13] Graceful shutdown and joinable server --- src/asio_io_service_pool.cc | 16 +++++++++------- src/asio_io_service_pool.h | 9 ++++++++- src/asio_server.cc | 14 ++++++++++++-- src/asio_server.h | 4 +++- src/asio_server_http2.cc | 20 ++++++++++++++++---- src/asio_server_http2_impl.cc | 15 ++++++++++++--- src/asio_server_http2_impl.h | 5 ++++- src/includes/nghttp2/asio_http2_server.h | 12 ++++++++++-- 8 files changed, 74 insertions(+), 21 deletions(-) diff --git a/src/asio_io_service_pool.cc b/src/asio_io_service_pool.cc index c6936d08..b6735df0 100644 --- a/src/asio_io_service_pool.cc +++ b/src/asio_io_service_pool.cc @@ -35,8 +35,6 @@ // #include "asio_io_service_pool.h" -#include - namespace nghttp2 { namespace asio_http2 { @@ -56,19 +54,23 @@ io_service_pool::io_service_pool(std::size_t pool_size) : next_io_service_(0) { } } -void io_service_pool::run() { +void io_service_pool::run(bool asynchronous) { // Create a pool of threads to run all of the io_services. - auto futs = std::vector>(); - for (std::size_t i = 0; i < io_services_.size(); ++i) { - futs.push_back(std::async(std::launch::async, + futures_.push_back(std::async(std::launch::async, (size_t (boost::asio::io_service::*)(void)) & boost::asio::io_service::run, io_services_[i])); } + if (!asynchronous) { + join(); + } +} + +void io_service_pool::join() { // Wait for all threads in the pool to exit. - for (auto &fut : futs) { + for (auto &fut : futures_) { fut.get(); } } diff --git a/src/asio_io_service_pool.h b/src/asio_io_service_pool.h index 7ebd883c..242cac8f 100644 --- a/src/asio_io_service_pool.h +++ b/src/asio_io_service_pool.h @@ -41,6 +41,7 @@ #include #include +#include #include #include @@ -58,11 +59,14 @@ public: explicit io_service_pool(std::size_t pool_size); /// Run all io_service objects in the pool. - void run(); + void run(bool asynchronous = false); /// Stop all io_service objects in the pool. void stop(); + /// Join on all io_service objects in the pool. + void join(); + /// Get an io_service to use. boost::asio::io_service &get_io_service(); @@ -75,6 +79,9 @@ private: /// The next io_service to use for a connection. std::size_t next_io_service_; + + /// Futures to all the io_service objects + std::vector> futures_; }; } // namespace asio_http2 diff --git a/src/asio_server.cc b/src/asio_server.cc index 323a8991..1f10cd5f 100644 --- a/src/asio_server.cc +++ b/src/asio_server.cc @@ -50,7 +50,7 @@ boost::system::error_code server::listen_and_serve(boost::system::error_code &ec, boost::asio::ssl::context *tls_context, const std::string &address, const std::string &port, - int backlog, serve_mux &mux) { + int backlog, serve_mux &mux, bool asynchronous) { ec.clear(); if (bind_and_listen(ec, address, port, backlog)) { @@ -65,7 +65,7 @@ server::listen_and_serve(boost::system::error_code &ec, } } - io_service_pool_.run(); + io_service_pool_.run(asynchronous); return ec; } @@ -156,6 +156,16 @@ void server::start_accept(tcp::acceptor &acceptor, serve_mux &mux) { }); } +void server::stop() +{ + io_service_pool_.stop(); +} + +void server::join() +{ + io_service_pool_.join(); +} + } // namespace server } // namespace asio_http2 } // namespace nghttp2 diff --git a/src/asio_server.h b/src/asio_server.h index bb4159e9..7f601201 100644 --- a/src/asio_server.h +++ b/src/asio_server.h @@ -69,7 +69,9 @@ public: listen_and_serve(boost::system::error_code &ec, boost::asio::ssl::context *tls_context, const std::string &address, const std::string &port, - int backlog, serve_mux &mux); + int backlog, serve_mux &mux, bool asynchronous = false); + void join(); + void stop(); private: /// Initiate an asynchronous accept operation. diff --git a/src/asio_server_http2.cc b/src/asio_server_http2.cc index 92bc8898..85e50221 100644 --- a/src/asio_server_http2.cc +++ b/src/asio_server_http2.cc @@ -54,15 +54,17 @@ http2 &http2::operator=(http2 &&other) noexcept { boost::system::error_code http2::listen_and_serve(boost::system::error_code &ec, const std::string &address, - const std::string &port) { - return impl_->listen_and_serve(ec, nullptr, address, port); + const std::string &port, + bool asynchronous) { + return impl_->listen_and_serve(ec, nullptr, address, port, asynchronous); } boost::system::error_code http2::listen_and_serve(boost::system::error_code &ec, boost::asio::ssl::context &tls_context, - const std::string &address, const std::string &port) { - return impl_->listen_and_serve(ec, &tls_context, address, port); + const std::string &address, const std::string &port, + bool asynchronous) { + return impl_->listen_and_serve(ec, &tls_context, address, port, asynchronous); } void http2::num_threads(size_t num_threads) { impl_->num_threads(num_threads); } @@ -73,6 +75,16 @@ bool http2::handle(std::string pattern, request_cb cb) { return impl_->handle(std::move(pattern), std::move(cb)); } +void http2::stop() +{ + impl_->stop(); +} + +void http2::join() +{ + return impl_->join(); +} + } // namespace server } // namespace asio_http2 diff --git a/src/asio_server_http2_impl.cc b/src/asio_server_http2_impl.cc index 96ca2432..142f418c 100644 --- a/src/asio_server_http2_impl.cc +++ b/src/asio_server_http2_impl.cc @@ -41,9 +41,9 @@ http2_impl::http2_impl() : num_threads_(1), backlog_(-1) {} boost::system::error_code http2_impl::listen_and_serve( boost::system::error_code &ec, boost::asio::ssl::context *tls_context, - const std::string &address, const std::string &port) { - return server(num_threads_) - .listen_and_serve(ec, tls_context, address, port, backlog_, mux_); + const std::string &address, const std::string &port, bool asynchronous) { + server_.reset(new server(num_threads_)); + return server_->listen_and_serve(ec, tls_context, address, port, backlog_, mux_, asynchronous); } void http2_impl::num_threads(size_t num_threads) { num_threads_ = num_threads; } @@ -54,6 +54,15 @@ bool http2_impl::handle(std::string pattern, request_cb cb) { return mux_.handle(std::move(pattern), std::move(cb)); } +void http2_impl::stop() { + return server_->stop(); +} + +void http2_impl::join() +{ + return server_->join(); +} + } // namespace server } // namespace asio_http2 diff --git a/src/asio_server_http2_impl.h b/src/asio_server_http2_impl.h index f097aea0..244486fa 100644 --- a/src/asio_server_http2_impl.h +++ b/src/asio_server_http2_impl.h @@ -45,10 +45,13 @@ public: boost::system::error_code listen_and_serve(boost::system::error_code &ec, boost::asio::ssl::context *tls_context, - const std::string &address, const std::string &port); + const std::string &address, const std::string &port, + bool asynchronous); void num_threads(size_t num_threads); void backlog(int backlog); bool handle(std::string pattern, request_cb cb); + void stop(); + void join(); private: std::unique_ptr server_; diff --git a/src/includes/nghttp2/asio_http2_server.h b/src/includes/nghttp2/asio_http2_server.h index bef17336..b3386425 100644 --- a/src/includes/nghttp2/asio_http2_server.h +++ b/src/includes/nghttp2/asio_http2_server.h @@ -139,14 +139,16 @@ public: // incoming requests in cleartext TCP connection. boost::system::error_code listen_and_serve(boost::system::error_code &ec, const std::string &address, - const std::string &port); + const std::string &port, + bool asynchronous = false); // Starts listening connection on given address and port and serves // incoming requests in SSL/TLS encrypted connection. boost::system::error_code listen_and_serve(boost::system::error_code &ec, boost::asio::ssl::context &tls_context, - const std::string &address, const std::string &port); + const std::string &address, const std::string &port, + bool asynchronous = false); // Registers request handler |cb| with path pattern |pattern|. This // function will fail and returns false if same pattern has been @@ -187,6 +189,12 @@ public: // connections. void backlog(int backlog); + // Gracefully stop http2 server + void stop(); + + // Join on http2 server and wait for it to fully stop + void join(); + private: std::unique_ptr impl_; }; From 492895921353c2fa5f4b031c2b73c6d496e245ff Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 22 Apr 2015 22:25:16 +0900 Subject: [PATCH 03/13] asio: Document asynchronous parameter for listen_and_serve --- src/includes/nghttp2/asio_http2_server.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/includes/nghttp2/asio_http2_server.h b/src/includes/nghttp2/asio_http2_server.h index b3386425..1c5a71fd 100644 --- a/src/includes/nghttp2/asio_http2_server.h +++ b/src/includes/nghttp2/asio_http2_server.h @@ -136,14 +136,20 @@ public: http2 &operator=(http2 &&other) noexcept; // Starts listening connection on given address and port and serves - // incoming requests in cleartext TCP connection. + // incoming requests in cleartext TCP connection. If |asynchronous| + // is false, this function blocks forever unless there is an error. + // If it is true, after server has started, this function returns + // immediately, and the caller should call join() to shutdown server + // gracefully. boost::system::error_code listen_and_serve(boost::system::error_code &ec, const std::string &address, const std::string &port, bool asynchronous = false); // Starts listening connection on given address and port and serves - // incoming requests in SSL/TLS encrypted connection. + // incoming requests in SSL/TLS encrypted connection. For + // |asynchronous| parameter, see cleartext version + // |listen_and_serve|. boost::system::error_code listen_and_serve(boost::system::error_code &ec, boost::asio::ssl::context &tls_context, From 77c556901cac2deb6dd94a7498edb89ff2543638 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 22 Apr 2015 22:26:04 +0900 Subject: [PATCH 04/13] nghttpx: Increase maximum header field set size --- src/shrpx_downstream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shrpx_downstream.h b/src/shrpx_downstream.h index 1f55cd40..718c8bd2 100644 --- a/src/shrpx_downstream.h +++ b/src/shrpx_downstream.h @@ -287,7 +287,7 @@ public: int change_priority(int32_t pri); // Maximum buffer size for header name/value pairs. - static const size_t MAX_HEADERS_SUM = 32768; + static constexpr size_t MAX_HEADERS_SUM = 128 * 1024; bool get_rst_stream_after_end_stream() const; void set_rst_stream_after_end_stream(bool f); From 5595ba643e126462e1fc6ff92a4dde850558983c Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 22 Apr 2015 23:03:29 +0900 Subject: [PATCH 05/13] travis: Use container-based infrastructure --- .travis.yml | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index 59d8c3a2..c13f8a25 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,28 +3,29 @@ compiler: - clang #Disable gcc build for the moment... # - gcc +sudo: false +addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-4.8 + - libstdc++-4.8-dev + - autoconf + - automake + - autotools-dev + - libtool + - pkg-config + - zlib1g-dev + - libcunit1-dev + - libssl-dev + - libxml2-dev + - libev-dev + - libevent-dev + - libjansson-dev + - libjemalloc-dev before_install: - $CC --version - - sudo add-apt-repository --yes ppa:ubuntu-toolchain-r/test - - sudo apt-get update -qq - #Install and use gcc-4.8 (don't build with gcc-4.6) - #libstdc++-4.8 is needed by Clang to build too - - sudo apt-get -qq install g++-4.8 libstdc++-4.8-dev - - > - sudo apt-get install --no-install-recommends -qq - autoconf - automake - autotools-dev - libtool - pkg-config - zlib1g-dev - libcunit1-dev - libssl-dev - libxml2-dev - libev-dev - libevent-dev - libjansson-dev - libjemalloc-dev - if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi - $CC --version before_script: From 4069aab4f79f71f3f9d630b964d36ee3664ad885 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 22 Apr 2015 23:17:21 +0900 Subject: [PATCH 06/13] travis: Attempt to enable gcc --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c13f8a25..678bb016 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: cpp compiler: - clang #Disable gcc build for the moment... -# - gcc + - gcc sudo: false addons: apt: From 58b7f4a096dccdefd8c5302aac267a1b2806f167 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 22 Apr 2015 23:25:54 +0900 Subject: [PATCH 07/13] Fix inline error with travis gcc-4.8 --- lib/nghttp2_hd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nghttp2_hd.c b/lib/nghttp2_hd.c index b26fbbc3..3a97ac71 100644 --- a/lib/nghttp2_hd.c +++ b/lib/nghttp2_hd.c @@ -114,7 +114,7 @@ static int memeq(const void *s1, const void *s2, size_t n) { * This function was generated by genlibtokenlookup.py. Inspired by * h2o header lookup. https://github.com/h2o/h2o */ -static inline int lookup_token(const uint8_t *name, size_t namelen) { +static int lookup_token(const uint8_t *name, size_t namelen) { switch (namelen) { case 2: switch (name[1]) { From 86ddda5c0ee084b2b236bff9d2645c3c80dcce83 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 22 Apr 2015 23:36:32 +0900 Subject: [PATCH 08/13] tiny-nghttpd: Fix compile error with travis gcc-4.8 --- examples/tiny-nghttpd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/tiny-nghttpd.c b/examples/tiny-nghttpd.c index b25293fe..a7677ee1 100644 --- a/examples/tiny-nghttpd.c +++ b/examples/tiny-nghttpd.c @@ -699,7 +699,8 @@ static ssize_t fd_read_callback(nghttp2_session *session _U_, nghttp2_data_source *source, void *user_data _U_) { stream *strm = source->ptr; - ssize_t nread = (int64_t)length < strm->fileleft ? length : strm->fileleft; + ssize_t nread = + (int64_t)length < strm->fileleft ? (int64_t)length : strm->fileleft; *data_flags |= NGHTTP2_DATA_FLAG_NO_COPY; From 6b8aa36c989f39b893ad059b7f50eaf7cc89d94e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 22 Apr 2015 23:47:13 +0900 Subject: [PATCH 09/13] travis: Use gcc-4.9 instead of gcc-4.8 --- .travis.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 678bb016..78e69d34 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: cpp compiler: - clang -#Disable gcc build for the moment... - gcc sudo: false addons: @@ -9,8 +8,8 @@ addons: sources: - ubuntu-toolchain-r-test packages: - - g++-4.8 - - libstdc++-4.8-dev + - g++-4.9 + - libstdc++-4.9-dev - autoconf - automake - autotools-dev @@ -26,7 +25,7 @@ addons: - libjemalloc-dev before_install: - $CC --version - - if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi + - if [ "$CXX" = "g++" ]; then export CXX="g++-4.9" CC="gcc-4.9"; fi - $CC --version before_script: - autoreconf -i From b9f602b9a2725abff21e5852b74fc3d4edcb5d29 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 23 Apr 2015 21:13:15 +0900 Subject: [PATCH 10/13] Update doc --- lib/includes/nghttp2/nghttp2.h | 59 ++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/lib/includes/nghttp2/nghttp2.h b/lib/includes/nghttp2/nghttp2.h index 6f601244..0c6122c2 100644 --- a/lib/includes/nghttp2/nghttp2.h +++ b/lib/includes/nghttp2/nghttp2.h @@ -241,8 +241,9 @@ typedef enum { */ NGHTTP2_ERR_UNSUPPORTED_VERSION = -503, /** - * Used as a return value from :type:`nghttp2_send_callback` and - * :type:`nghttp2_recv_callback` to indicate that the operation + * Used as a return value from :type:`nghttp2_send_callback`, + * :type:`nghttp2_recv_callback` and + * :type:`nghttp2_send_data_callback` to indicate that the operation * would block. */ NGHTTP2_ERR_WOULDBLOCK = -504, @@ -1275,10 +1276,10 @@ typedef ssize_t (*nghttp2_recv_callback)(nghttp2_session *session, uint8_t *buf, /** * @functypedef * - * Callback function invoked by `nghttp2_session_recv()` when a frame - * is received. The |user_data| pointer is the third argument passed - * in to the call to `nghttp2_session_client_new()` or - * `nghttp2_session_server_new()`. + * Callback function invoked by `nghttp2_session_recv()` and + * `nghttp2_session_mem_recv()` when a frame is received. The + * |user_data| pointer is the third argument passed in to the call to + * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`. * * If frame is HEADERS or PUSH_PROMISE, the ``nva`` and ``nvlen`` * member of their data structure are always ``NULL`` and 0 @@ -1313,14 +1314,14 @@ typedef int (*nghttp2_on_frame_recv_callback)(nghttp2_session *session, /** * @functypedef * - * Callback function invoked by `nghttp2_session_recv()` when an - * invalid non-DATA frame is received. The |error_code| indicates the - * error. It is usually one of the :enum:`nghttp2_error_code` but - * that is not guaranteed. When this callback function is invoked, - * the library automatically submits either RST_STREAM or GOAWAY - * frame. The |user_data| pointer is the third argument passed in to - * the call to `nghttp2_session_client_new()` or - * `nghttp2_session_server_new()`. + * Callback function invoked by `nghttp2_session_recv()` and + * `nghttp2_session_mem_recv()` when an invalid non-DATA frame is + * received. The |error_code| indicates the error. It is usually one + * of the :enum:`nghttp2_error_code` but that is not guaranteed. When + * this callback function is invoked, the library automatically + * submits either RST_STREAM or GOAWAY frame. The |user_data| pointer + * is the third argument passed in to the call to + * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`. * * If frame is HEADERS or PUSH_PROMISE, the ``nva`` and ``nvlen`` * member of their data structure are always ``NULL`` and 0 @@ -1328,7 +1329,7 @@ typedef int (*nghttp2_on_frame_recv_callback)(nghttp2_session *session, * * The implementation of this function must return 0 if it succeeds. * If nonzero is returned, it is treated as fatal error and - * `nghttp2_session_recv()` and `nghttp2_session_send()` functions + * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. * * To set this callback to :type:`nghttp2_session_callbacks`, use @@ -1361,7 +1362,7 @@ typedef int (*nghttp2_on_invalid_frame_recv_callback)( * region included in the input bytes. * * The implementation of this function must return 0 if it succeeds. - * If nonzero is returned, it is treated as fatal error and + * If nonzero is returned, it is treated as fatal error, and * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. * @@ -1384,7 +1385,7 @@ typedef int (*nghttp2_on_data_chunk_recv_callback)(nghttp2_session *session, * * The implementation of this function must return 0 if it succeeds. * If nonzero is returned, it is treated as fatal error and - * `nghttp2_session_recv()` and `nghttp2_session_send()` functions + * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. * * To set this callback to :type:`nghttp2_session_callbacks`, use @@ -1403,7 +1404,7 @@ typedef int (*nghttp2_before_frame_send_callback)(nghttp2_session *session, * * The implementation of this function must return 0 if it succeeds. * If nonzero is returned, it is treated as fatal error and - * `nghttp2_session_recv()` and `nghttp2_session_send()` functions + * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. * * To set this callback to :type:`nghttp2_session_callbacks`, use @@ -1425,7 +1426,7 @@ typedef int (*nghttp2_on_frame_send_callback)(nghttp2_session *session, * * The implementation of this function must return 0 if it succeeds. * If nonzero is returned, it is treated as fatal error and - * `nghttp2_session_recv()` and `nghttp2_session_send()` functions + * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. * * `nghttp2_session_get_stream_user_data()` can be used to get @@ -1455,8 +1456,9 @@ typedef int (*nghttp2_on_frame_not_send_callback)(nghttp2_session *session, * * The implementation of this function must return 0 if it succeeds. * If nonzero is returned, it is treated as fatal error and - * `nghttp2_session_recv()` and `nghttp2_session_send()` functions - * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. + * `nghttp2_session_recv()`, `nghttp2_session_mem_recv()`, + * `nghttp2_session_send()`, and `nghttp2_session_mem_send()` + * functions immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. * * To set this callback to :type:`nghttp2_session_callbacks`, use * `nghttp2_session_callbacks_set_on_stream_close_callback()`. @@ -1498,7 +1500,7 @@ typedef int (*nghttp2_on_stream_close_callback)(nghttp2_session *session, * frame with ``frame->headers.cat == NGHTTP2_HCAT_HEADERS`` * containing final response headers (non-1xx status code). The * trailer headers also has ``frame->headers.cat == - * NGHTTP2_HCAT_HEADERS`` which does not containg any status code. + * NGHTTP2_HCAT_HEADERS`` which does not contain any status code. * * The implementation of this function must return 0 if it succeeds or * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. If nonzero value other than @@ -1596,8 +1598,8 @@ typedef int (*nghttp2_on_header_callback)(nghttp2_session *session, * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. Returning * ``frame->hd.length`` means no padding is added. Returning * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` will make - * `nghttp2_session_send()` function immediately return - * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. + * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions + * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. * * To set this callback to :type:`nghttp2_session_callbacks`, use * `nghttp2_session_callbacks_set_select_padding_callback()`. @@ -1726,8 +1728,8 @@ NGHTTP2_EXTERN void nghttp2_session_callbacks_set_recv_callback( /** * @function * - * Sets callback function invoked by `nghttp2_session_recv()` when a - * frame is received. + * Sets callback function invoked by `nghttp2_session_recv()` and + * `nghttp2_session_mem_recv()` when a frame is received. */ NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_recv_callback( nghttp2_session_callbacks *cbs, @@ -1736,8 +1738,9 @@ NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_recv_callback( /** * @function * - * Sets callback function invoked by `nghttp2_session_recv()` when an - * invalid non-DATA frame is received. + * Sets callback function invoked by `nghttp2_session_recv()` and + * `nghttp2_session_mem_recv()` when an invalid non-DATA frame is + * received. */ NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_invalid_frame_recv_callback( From 514558afc01dd58ad2167224a903950cb4075504 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 23 Apr 2015 23:43:30 +0900 Subject: [PATCH 11/13] Allow NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE from nghttp2_on_begin_headers_callback Since application most likely allocates the stream object in nghttp2_on_begin_headers_callback, it is desirable to handle its failure as stream error. But previously it only signals success or fatal error. Submitting RST_STREAM does not prevent nghttp2_on_header_callback from being invoked. This commit improves this situation by allowing NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE from nghttp2_on_begin_headers_callback. If that value is returned, library submits RST_STREAM with error code INTERNAL_ERROR, and nghttp2_on_header_callback and nghttp2_on_frame_recv_callback for that frame are not invoked. Note that for PUSH_PROMISE frame, the stream to be reset is promised stream. --- lib/includes/nghttp2/nghttp2.h | 23 +++++++--- lib/nghttp2_session.c | 34 +++++++++++++++ tests/main.c | 2 + tests/nghttp2_session_test.c | 78 ++++++++++++++++++++++++++++++++++ tests/nghttp2_session_test.h | 1 + 5 files changed, 133 insertions(+), 5 deletions(-) diff --git a/lib/includes/nghttp2/nghttp2.h b/lib/includes/nghttp2/nghttp2.h index 0c6122c2..9caafe2e 100644 --- a/lib/includes/nghttp2/nghttp2.h +++ b/lib/includes/nghttp2/nghttp2.h @@ -1502,11 +1502,24 @@ typedef int (*nghttp2_on_stream_close_callback)(nghttp2_session *session, * trailer headers also has ``frame->headers.cat == * NGHTTP2_HCAT_HEADERS`` which does not contain any status code. * - * The implementation of this function must return 0 if it succeeds or - * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. If nonzero value other than - * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned, it is treated as - * if :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned. If - * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned, + * Returning :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will close + * the stream (promised stream if frame is PUSH_PROMISE) by issuing + * RST_STREAM with :enum:`NGHTTP2_INTERNAL_ERROR`. In this case, + * :type:`nghttp2_on_header_callback` and + * :type:`nghttp2_on_frame_recv_callback` will not be invoked. If a + * different error code is desirable, use + * `nghttp2_submit_rst_stream()` with a desired error code and then + * return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`. Again, use + * `frame->push_promise.promised_stream_id` as stream_id parameter in + * `nghttp2_submit_rst_stream()` if frame is PUSH_PROMISE. + * + * The implementation of this function must return 0 if it succeeds. + * It can return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` to + * reset the stream (promised stream if frame is PUSH_PROMISE). For + * critical errors, it must return + * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. If the other value is + * returned, it is treated as if :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` + * is returned. If :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned, * `nghttp2_session_mem_recv()` function will immediately return * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`. * diff --git a/lib/nghttp2_session.c b/lib/nghttp2_session.c index 108a30c1..c4f61355 100644 --- a/lib/nghttp2_session.c +++ b/lib/nghttp2_session.c @@ -3069,6 +3069,9 @@ static int session_call_on_begin_headers(nghttp2_session *session, if (session->callbacks.on_begin_headers_callback) { rv = session->callbacks.on_begin_headers_callback(session, frame, session->user_data); + if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) { + return rv; + } if (rv != 0) { return NGHTTP2_ERR_CALLBACK_FAILURE; } @@ -5085,6 +5088,16 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in, busy = 1; + if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) { + rv = nghttp2_session_add_rst_stream( + session, iframe->frame.hd.stream_id, NGHTTP2_INTERNAL_ERROR); + if (nghttp2_is_fatal(rv)) { + return rv; + } + iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK; + break; + } + if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) { iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK; break; @@ -5329,6 +5342,16 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in, busy = 1; + if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) { + rv = nghttp2_session_add_rst_stream( + session, iframe->frame.hd.stream_id, NGHTTP2_INTERNAL_ERROR); + if (nghttp2_is_fatal(rv)) { + return rv; + } + iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK; + break; + } + if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) { iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK; break; @@ -5393,6 +5416,17 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in, busy = 1; + if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) { + rv = nghttp2_session_add_rst_stream( + session, iframe->frame.push_promise.promised_stream_id, + NGHTTP2_INTERNAL_ERROR); + if (nghttp2_is_fatal(rv)) { + return rv; + } + iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK; + break; + } + if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) { iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK; break; diff --git a/tests/main.c b/tests/main.c index 09a92124..933da918 100644 --- a/tests/main.c +++ b/tests/main.c @@ -262,6 +262,8 @@ int main(int argc _U_, char *argv[] _U_) { test_nghttp2_session_reset_pending_headers) || !CU_add_test(pSuite, "session_send_data_callback", test_nghttp2_session_send_data_callback) || + !CU_add_test(pSuite, "session_on_begin_headers_temporal_failure", + test_nghttp2_session_on_begin_headers_temporal_failure) || !CU_add_test(pSuite, "http_mandatory_headers", test_nghttp2_http_mandatory_headers) || !CU_add_test(pSuite, "http_content_length", diff --git a/tests/nghttp2_session_test.c b/tests/nghttp2_session_test.c index a685918b..3e83d2b4 100644 --- a/tests/nghttp2_session_test.c +++ b/tests/nghttp2_session_test.c @@ -405,6 +405,12 @@ static int on_begin_headers_callback(nghttp2_session *session _U_, return 0; } +static int temporal_failure_on_begin_headers_callback( + nghttp2_session *session, const nghttp2_frame *frame, void *user_data) { + on_begin_headers_callback(session, frame, user_data); + return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; +} + static ssize_t defer_data_source_read_callback(nghttp2_session *session _U_, int32_t stream_id _U_, uint8_t *buf _U_, size_t len _U_, @@ -7033,6 +7039,78 @@ void test_nghttp2_session_send_data_callback(void) { nghttp2_session_del(session); } +void test_nghttp2_session_on_begin_headers_temporal_failure(void) { + nghttp2_session *session; + nghttp2_session_callbacks callbacks; + my_user_data ud; + nghttp2_bufs bufs; + nghttp2_mem *mem; + ssize_t rv; + nghttp2_hd_deflater deflater; + nghttp2_outbound_item *item; + + mem = nghttp2_mem_default(); + frame_pack_bufs_init(&bufs); + nghttp2_hd_deflate_init(&deflater, mem); + + memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); + callbacks.on_begin_headers_callback = + temporal_failure_on_begin_headers_callback; + callbacks.on_header_callback = on_header_callback; + callbacks.on_frame_recv_callback = on_frame_recv_callback; + callbacks.send_callback = null_send_callback; + nghttp2_session_server_new(&session, &callbacks, &ud); + + rv = pack_headers(&bufs, &deflater, 1, NGHTTP2_FLAG_END_HEADERS, reqnv, + ARRLEN(reqnv), mem); + CU_ASSERT(0 == rv); + + ud.header_cb_called = 0; + ud.frame_recv_cb_called = 0; + rv = nghttp2_session_mem_recv(session, bufs.head->buf.pos, + nghttp2_bufs_len(&bufs)); + CU_ASSERT(nghttp2_bufs_len(&bufs) == rv); + CU_ASSERT(0 == ud.header_cb_called); + CU_ASSERT(0 == ud.frame_recv_cb_called); + + item = nghttp2_session_get_next_ob_item(session); + CU_ASSERT(NGHTTP2_RST_STREAM == item->frame.hd.type); + CU_ASSERT(1 == item->frame.hd.stream_id); + CU_ASSERT(NGHTTP2_INTERNAL_ERROR == item->frame.rst_stream.error_code); + + nghttp2_session_del(session); + nghttp2_hd_deflate_free(&deflater); + + nghttp2_bufs_reset(&bufs); + /* check for PUSH_PROMISE */ + nghttp2_hd_deflate_init(&deflater, mem); + nghttp2_session_client_new(&session, &callbacks, &ud); + + nghttp2_session_open_stream(session, 1, NGHTTP2_STREAM_FLAG_NONE, + &pri_spec_default, NGHTTP2_STREAM_OPENING, NULL); + + rv = pack_push_promise(&bufs, &deflater, 1, NGHTTP2_FLAG_END_HEADERS, 2, + reqnv, ARRLEN(reqnv), mem); + CU_ASSERT(0 == rv); + + ud.header_cb_called = 0; + ud.frame_recv_cb_called = 0; + rv = nghttp2_session_mem_recv(session, bufs.head->buf.pos, + nghttp2_bufs_len(&bufs)); + CU_ASSERT(nghttp2_bufs_len(&bufs) == rv); + CU_ASSERT(0 == ud.header_cb_called); + CU_ASSERT(0 == ud.frame_recv_cb_called); + + item = nghttp2_session_get_next_ob_item(session); + CU_ASSERT(NGHTTP2_RST_STREAM == item->frame.hd.type); + CU_ASSERT(2 == item->frame.hd.stream_id); + CU_ASSERT(NGHTTP2_INTERNAL_ERROR == item->frame.rst_stream.error_code); + + nghttp2_session_del(session); + nghttp2_hd_deflate_free(&deflater); + nghttp2_bufs_free(&bufs); +} + static void check_nghttp2_http_recv_headers_fail( nghttp2_session *session, nghttp2_hd_deflater *deflater, int32_t stream_id, int stream_state, const nghttp2_nv *nva, size_t nvlen) { diff --git a/tests/nghttp2_session_test.h b/tests/nghttp2_session_test.h index b6fe967d..1b4d2f64 100644 --- a/tests/nghttp2_session_test.h +++ b/tests/nghttp2_session_test.h @@ -124,6 +124,7 @@ void test_nghttp2_session_open_idle_stream(void); void test_nghttp2_session_cancel_reserved_remote(void); void test_nghttp2_session_reset_pending_headers(void); void test_nghttp2_session_send_data_callback(void); +void test_nghttp2_session_on_begin_headers_temporal_failure(void); void test_nghttp2_http_mandatory_headers(void); void test_nghttp2_http_content_length(void); void test_nghttp2_http_content_length_mismatch(void); From c925c32233b3c71808b7c82b2d020a24fc00dfb5 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 23 Apr 2015 23:57:39 +0900 Subject: [PATCH 12/13] Fix bug that promised stream is not reset on temporal failure from on_header_callback --- lib/nghttp2_session.c | 6 +++++- tests/nghttp2_session_test.c | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/nghttp2_session.c b/lib/nghttp2_session.c index c4f61355..d0937c7c 100644 --- a/lib/nghttp2_session.c +++ b/lib/nghttp2_session.c @@ -5533,8 +5533,12 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in, in += hd_proclen; iframe->payloadleft -= hd_proclen; + /* Use promised stream ID for PUSH_PROMISE */ rv = nghttp2_session_add_rst_stream( - session, iframe->frame.hd.stream_id, NGHTTP2_INTERNAL_ERROR); + session, iframe->frame.hd.type == NGHTTP2_PUSH_PROMISE + ? iframe->frame.push_promise.promised_stream_id + : iframe->frame.hd.stream_id, + NGHTTP2_INTERNAL_ERROR); if (nghttp2_is_fatal(rv)) { return rv; } diff --git a/tests/nghttp2_session_test.c b/tests/nghttp2_session_test.c index 3e83d2b4..0a465d4f 100644 --- a/tests/nghttp2_session_test.c +++ b/tests/nghttp2_session_test.c @@ -6711,21 +6711,50 @@ void test_nghttp2_session_on_header_temporal_failure(void) { nghttp2_frame_pack_frame_hd(&buf->pos[hdpos], &hd); + ud.header_cb_called = 0; rv = nghttp2_session_mem_recv(session, buf->pos, nghttp2_bufs_len(&bufs)); CU_ASSERT(rv == nghttp2_bufs_len(&bufs)); + CU_ASSERT(1 == ud.header_cb_called); item = nghttp2_session_get_next_ob_item(session); CU_ASSERT(NGHTTP2_RST_STREAM == item->frame.hd.type); + CU_ASSERT(1 == item->frame.hd.stream_id); /* Make sure no header decompression error occurred */ CU_ASSERT(NGHTTP2_GOAWAY_NONE == session->goaway_flags); - nghttp2_bufs_free(&bufs); - nghttp2_hd_deflate_free(&deflater); nghttp2_session_del(session); + + nghttp2_bufs_reset(&bufs); + + /* Check for PUSH_PROMISE */ + nghttp2_hd_deflate_init(&deflater, mem); + nghttp2_session_client_new(&session, &callbacks, &ud); + + nghttp2_session_open_stream(session, 1, NGHTTP2_STREAM_FLAG_NONE, + &pri_spec_default, NGHTTP2_STREAM_OPENING, NULL); + + rv = pack_push_promise(&bufs, &deflater, 1, NGHTTP2_FLAG_END_HEADERS, 2, + reqnv, ARRLEN(reqnv), mem); + CU_ASSERT(0 == rv); + + ud.header_cb_called = 0; + rv = nghttp2_session_mem_recv(session, bufs.head->buf.pos, + nghttp2_bufs_len(&bufs)); + CU_ASSERT(nghttp2_bufs_len(&bufs) == rv); + CU_ASSERT(1 == ud.header_cb_called); + + item = nghttp2_session_get_next_ob_item(session); + CU_ASSERT(NGHTTP2_RST_STREAM == item->frame.hd.type); + CU_ASSERT(2 == item->frame.hd.stream_id); + CU_ASSERT(NGHTTP2_INTERNAL_ERROR == item->frame.rst_stream.error_code); + + nghttp2_session_del(session); + nghttp2_hd_deflate_free(&deflater); + nghttp2_bufs_free(&bufs); } void test_nghttp2_session_recv_client_preface(void) { From 6b0b8ea7d572c8e3cbb0382d60902ac91ab7d1cb Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 24 Apr 2015 00:05:10 +0900 Subject: [PATCH 13/13] Update doc --- lib/includes/nghttp2/nghttp2.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/includes/nghttp2/nghttp2.h b/lib/includes/nghttp2/nghttp2.h index 9caafe2e..9f456e50 100644 --- a/lib/includes/nghttp2/nghttp2.h +++ b/lib/includes/nghttp2/nghttp2.h @@ -1510,8 +1510,8 @@ typedef int (*nghttp2_on_stream_close_callback)(nghttp2_session *session, * different error code is desirable, use * `nghttp2_submit_rst_stream()` with a desired error code and then * return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`. Again, use - * `frame->push_promise.promised_stream_id` as stream_id parameter in - * `nghttp2_submit_rst_stream()` if frame is PUSH_PROMISE. + * ``frame->push_promise.promised_stream_id`` as stream_id parameter + * in `nghttp2_submit_rst_stream()` if frame is PUSH_PROMISE. * * The implementation of this function must return 0 if it succeeds. * It can return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` to @@ -1574,12 +1574,15 @@ typedef int (*nghttp2_on_begin_headers_callback)(nghttp2_session *session, * included in the input bytes. * * Returning :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will close - * the stream by issuing RST_STREAM with - * :enum:`NGHTTP2_INTERNAL_ERROR`. In this case, + * the stream (promised stream if frame is PUSH_PROMISE) by issuing + * RST_STREAM with :enum:`NGHTTP2_INTERNAL_ERROR`. In this case, + * :type:`nghttp2_on_header_callback` and * :type:`nghttp2_on_frame_recv_callback` will not be invoked. If a * different error code is desirable, use * `nghttp2_submit_rst_stream()` with a desired error code and then - * return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`. + * return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`. Again, use + * ``frame->push_promise.promised_stream_id`` as stream_id parameter + * in `nghttp2_submit_rst_stream()` if frame is PUSH_PROMISE. * * The implementation of this function must return 0 if it succeeds. * It may return :enum:`NGHTTP2_ERR_PAUSE` or