mod_http2.inl 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  1. /* Prototype implementation for HTTP2. Do not use in production.
  2. * There may be memory leaks, security vulnerabilities, ...
  3. */
  4. /***********************************************************************/
  5. /*** HPACK ***/
  6. /***********************************************************************/
  7. /* hpack predefined table. See:
  8. * https://tools.ietf.org/html/rfc7541#appendix-A
  9. */
  10. static struct mg_header hpack_predefined[62] = {{NULL, NULL},
  11. {":authority", NULL},
  12. {":method", "GET"},
  13. {":method", "POST"},
  14. {":path", "/"},
  15. {":path", "/index.html"},
  16. {":scheme", "http"},
  17. {":scheme", "https"},
  18. {":status", "200"},
  19. {":status", "204"},
  20. {":status", "206"},
  21. {":status", "304"},
  22. {":status", "400"},
  23. {":status", "404"},
  24. {":status", "500"},
  25. {"accept-charset", NULL},
  26. {"accept-encoding", NULL},
  27. {"accept-language", NULL},
  28. {"accept-ranges", NULL},
  29. {"accept", NULL},
  30. {"access-control-allow-origin",
  31. NULL},
  32. {"age", NULL},
  33. {"allow", NULL},
  34. {"authorization", NULL},
  35. {"cache-control", NULL},
  36. {"content-encoding", NULL},
  37. {"content-disposition", NULL},
  38. {"content-language", NULL},
  39. {"content-length", NULL},
  40. {"content-location", NULL},
  41. {"content-range", NULL},
  42. {"content-type", NULL},
  43. {"cookie", NULL},
  44. {"date", NULL},
  45. {"etag", NULL},
  46. {"expect", NULL},
  47. {"expires", NULL},
  48. {"from", NULL},
  49. {"host", NULL},
  50. {"if-match", NULL},
  51. {"if-modified-since", NULL},
  52. {"if-none-match", NULL},
  53. {"if-range", NULL},
  54. {"if-unmodified-since", NULL},
  55. {"last-modified", NULL},
  56. {"link", NULL},
  57. {"location", NULL},
  58. {"max-forwards", NULL},
  59. {"proxy-authenticate", NULL},
  60. {"proxy-authorization", NULL},
  61. {"range", NULL},
  62. {"referer", NULL},
  63. {"refresh", NULL},
  64. {"retry-after", NULL},
  65. {"server", NULL},
  66. {"set-cookie", NULL},
  67. {"strict-transport-security",
  68. NULL},
  69. {"transfer-encoding", NULL},
  70. {"user-agent", NULL},
  71. {"vary", NULL},
  72. {"via", NULL},
  73. {"www-authenticate", NULL}};
  74. /* Huffman decoding: https://tools.ietf.org/html/rfc7541#appendix-B
  75. '0' ( 48) |00000 0 [ 5]
  76. '1' ( 49) |00001 1 [ 5]
  77. '2' ( 50) |00010 2 [ 5]
  78. 'a' ( 97) |00011 3 [ 5]
  79. 'c' ( 99) |00100 4 [ 5]
  80. 'e' (101) |00101 5 [ 5]
  81. 'i' (105) |00110 6 [ 5]
  82. 'o' (111) |00111 7 [ 5]
  83. 's' (115) |01000 8 [ 5]
  84. 't' (116) |01001 9 [ 5]
  85. ' ' ( 32) |010100 14 [ 6]
  86. '%' ( 37) |010101 15 [ 6]
  87. '-' ( 45) |010110 16 [ 6]
  88. '.' ( 46) |010111 17 [ 6]
  89. '/' ( 47) |011000 18 [ 6]
  90. '3' ( 51) |011001 19 [ 6]
  91. '4' ( 52) |011010 1a [ 6]
  92. '5' ( 53) |011011 1b [ 6]
  93. '6' ( 54) |011100 1c [ 6]
  94. '7' ( 55) |011101 1d [ 6]
  95. '8' ( 56) |011110 1e [ 6]
  96. '9' ( 57) |011111 1f [ 6]
  97. '=' ( 61) |100000 20 [ 6]
  98. 'A' ( 65) |100001 21 [ 6]
  99. '_' ( 95) |100010 22 [ 6]
  100. 'b' ( 98) |100011 23 [ 6]
  101. 'd' (100) |100100 24 [ 6]
  102. 'f' (102) |100101 25 [ 6]
  103. 'g' (103) |100110 26 [ 6]
  104. 'h' (104) |100111 27 [ 6]
  105. 'l' (108) |101000 28 [ 6]
  106. 'm' (109) |101001 29 [ 6]
  107. 'n' (110) |101010 2a [ 6]
  108. 'p' (112) |101011 2b [ 6]
  109. 'r' (114) |101100 2c [ 6]
  110. 'u' (117) |101101 2d [ 6]
  111. ':' ( 58) |1011100 5c [ 7]
  112. 'B' ( 66) |1011101 5d [ 7]
  113. 'C' ( 67) |1011110 5e [ 7]
  114. 'D' ( 68) |1011111 5f [ 7]
  115. 'E' ( 69) |1100000 60 [ 7]
  116. 'F' ( 70) |1100001 61 [ 7]
  117. 'G' ( 71) |1100010 62 [ 7]
  118. 'H' ( 72) |1100011 63 [ 7]
  119. 'I' ( 73) |1100100 64 [ 7]
  120. 'J' ( 74) |1100101 65 [ 7]
  121. 'K' ( 75) |1100110 66 [ 7]
  122. 'L' ( 76) |1100111 67 [ 7]
  123. 'M' ( 77) |1101000 68 [ 7]
  124. 'N' ( 78) |1101001 69 [ 7]
  125. 'O' ( 79) |1101010 6a [ 7]
  126. 'P' ( 80) |1101011 6b [ 7]
  127. 'Q' ( 81) |1101100 6c [ 7]
  128. 'R' ( 82) |1101101 6d [ 7]
  129. 'S' ( 83) |1101110 6e [ 7]
  130. 'T' ( 84) |1101111 6f [ 7]
  131. 'U' ( 85) |1110000 70 [ 7]
  132. 'V' ( 86) |1110001 71 [ 7]
  133. 'W' ( 87) |1110010 72 [ 7]
  134. 'Y' ( 89) |1110011 73 [ 7]
  135. 'j' (106) |1110100 74 [ 7]
  136. 'k' (107) |1110101 75 [ 7]
  137. 'q' (113) |1110110 76 [ 7]
  138. 'v' (118) |1110111 77 [ 7]
  139. 'w' (119) |1111000 78 [ 7]
  140. 'x' (120) |1111001 79 [ 7]
  141. 'y' (121) |1111010 7a [ 7]
  142. 'z' (122) |1111011 7b [ 7]
  143. '&' ( 38) |11111000 f8 [ 8]
  144. '*' ( 42) |11111001 f9 [ 8]
  145. ',' ( 44) |11111010 fa [ 8]
  146. ';' ( 59) |11111011 fb [ 8]
  147. 'X' ( 88) |11111100 fc [ 8]
  148. 'Z' ( 90) |11111101 fd [ 8]
  149. '!' ( 33) |11111110|00 3f8 [10]
  150. '"' ( 34) |11111110|01 3f9 [10]
  151. '(' ( 40) |11111110|10 3fa [10]
  152. ')' ( 41) |11111110|11 3fb [10]
  153. '?' ( 63) |11111111|00 3fc [10]
  154. ''' ( 39) |11111111|010 7fa [11]
  155. '+' ( 43) |11111111|011 7fb [11]
  156. '|' (124) |11111111|100 7fc [11]
  157. '#' ( 35) |11111111|1010 ffa [12]
  158. '>' ( 62) |11111111|1011 ffb [12]
  159. ( 0) |11111111|11000 1ff8 [13]
  160. '$' ( 36) |11111111|11001 1ff9 [13]
  161. '@' ( 64) |11111111|11010 1ffa [13]
  162. '[' ( 91) |11111111|11011 1ffb [13]
  163. ']' ( 93) |11111111|11100 1ffc [13]
  164. '~' (126) |11111111|11101 1ffd [13]
  165. '^' ( 94) |11111111|111100 3ffc [14]
  166. '}' (125) |11111111|111101 3ffd [14]
  167. '<' ( 60) |11111111|1111100 7ffc [15]
  168. '`' ( 96) |11111111|1111101 7ffd [15]
  169. '{' (123) |11111111|1111110 7ffe [15]
  170. '\' ( 92) |11111111|11111110|000 7fff0 [19]
  171. (195) |11111111|11111110|001 7fff1 [19]
  172. (208) |11111111|11111110|010 7fff2 [19]
  173. (128) |11111111|11111110|0110 fffe6 [20]
  174. (130) |11111111|11111110|0111 fffe7 [20]
  175. (131) |11111111|11111110|1000 fffe8 [20]
  176. (162) |11111111|11111110|1001 fffe9 [20]
  177. (184) |11111111|11111110|1010 fffea [20]
  178. (194) |11111111|11111110|1011 fffeb [20]
  179. (224) |11111111|11111110|1100 fffec [20]
  180. (226) |11111111|11111110|1101 fffed [20]
  181. (153) |11111111|11111110|11100 1fffdc [21]
  182. (161) |11111111|11111110|11101 1fffdd [21]
  183. (167) |11111111|11111110|11110 1fffde [21]
  184. (172) |11111111|11111110|11111 1fffdf [21]
  185. (176) |11111111|11111111|00000 1fffe0 [21]
  186. (177) |11111111|11111111|00001 1fffe1 [21]
  187. (179) |11111111|11111111|00010 1fffe2 [21]
  188. (209) |11111111|11111111|00011 1fffe3 [21]
  189. (216) |11111111|11111111|00100 1fffe4 [21]
  190. (217) |11111111|11111111|00101 1fffe5 [21]
  191. (227) |11111111|11111111|00110 1fffe6 [21]
  192. (229) |11111111|11111111|00111 1fffe7 [21]
  193. (230) |11111111|11111111|01000 1fffe8 [21]
  194. (129) |11111111|11111111|010010 3fffd2 [22]
  195. (132) |11111111|11111111|010011 3fffd3 [22]
  196. (133) |11111111|11111111|010100 3fffd4 [22]
  197. (134) |11111111|11111111|010101 3fffd5 [22]
  198. (136) |11111111|11111111|010110 3fffd6 [22]
  199. (146) |11111111|11111111|010111 3fffd7 [22]
  200. (154) |11111111|11111111|011000 3fffd8 [22]
  201. (156) |11111111|11111111|011001 3fffd9 [22]
  202. (160) |11111111|11111111|011010 3fffda [22]
  203. (163) |11111111|11111111|011011 3fffdb [22]
  204. (164) |11111111|11111111|011100 3fffdc [22]
  205. (169) |11111111|11111111|011101 3fffdd [22]
  206. (170) |11111111|11111111|011110 3fffde [22]
  207. (173) |11111111|11111111|011111 3fffdf [22]
  208. (178) |11111111|11111111|100000 3fffe0 [22]
  209. (181) |11111111|11111111|100001 3fffe1 [22]
  210. (185) |11111111|11111111|100010 3fffe2 [22]
  211. (186) |11111111|11111111|100011 3fffe3 [22]
  212. (187) |11111111|11111111|100100 3fffe4 [22]
  213. (189) |11111111|11111111|100101 3fffe5 [22]
  214. (190) |11111111|11111111|100110 3fffe6 [22]
  215. (196) |11111111|11111111|100111 3fffe7 [22]
  216. (198) |11111111|11111111|101000 3fffe8 [22]
  217. (228) |11111111|11111111|101001 3fffe9 [22]
  218. (232) |11111111|11111111|101010 3fffea [22]
  219. (233) |11111111|11111111|101011 3fffeb [22]
  220. ( 1) |11111111|11111111|1011000 7fffd8 [23]
  221. (135) |11111111|11111111|1011001 7fffd9 [23]
  222. (137) |11111111|11111111|1011010 7fffda [23]
  223. (138) |11111111|11111111|1011011 7fffdb [23]
  224. (139) |11111111|11111111|1011100 7fffdc [23]
  225. (140) |11111111|11111111|1011101 7fffdd [23]
  226. (141) |11111111|11111111|1011110 7fffde [23]
  227. (143) |11111111|11111111|1011111 7fffdf [23]
  228. (147) |11111111|11111111|1100000 7fffe0 [23]
  229. (149) |11111111|11111111|1100001 7fffe1 [23]
  230. (150) |11111111|11111111|1100010 7fffe2 [23]
  231. (151) |11111111|11111111|1100011 7fffe3 [23]
  232. (152) |11111111|11111111|1100100 7fffe4 [23]
  233. (155) |11111111|11111111|1100101 7fffe5 [23]
  234. (157) |11111111|11111111|1100110 7fffe6 [23]
  235. (158) |11111111|11111111|1100111 7fffe7 [23]
  236. (165) |11111111|11111111|1101000 7fffe8 [23]
  237. (166) |11111111|11111111|1101001 7fffe9 [23]
  238. (168) |11111111|11111111|1101010 7fffea [23]
  239. (174) |11111111|11111111|1101011 7fffeb [23]
  240. (175) |11111111|11111111|1101100 7fffec [23]
  241. (180) |11111111|11111111|1101101 7fffed [23]
  242. (182) |11111111|11111111|1101110 7fffee [23]
  243. (183) |11111111|11111111|1101111 7fffef [23]
  244. (188) |11111111|11111111|1110000 7ffff0 [23]
  245. (191) |11111111|11111111|1110001 7ffff1 [23]
  246. (197) |11111111|11111111|1110010 7ffff2 [23]
  247. (231) |11111111|11111111|1110011 7ffff3 [23]
  248. (239) |11111111|11111111|1110100 7ffff4 [23]
  249. ( 9) |11111111|11111111|11101010 ffffea [24]
  250. (142) |11111111|11111111|11101011 ffffeb [24]
  251. (144) |11111111|11111111|11101100 ffffec [24]
  252. (145) |11111111|11111111|11101101 ffffed [24]
  253. (148) |11111111|11111111|11101110 ffffee [24]
  254. (159) |11111111|11111111|11101111 ffffef [24]
  255. (171) |11111111|11111111|11110000 fffff0 [24]
  256. (206) |11111111|11111111|11110001 fffff1 [24]
  257. (215) |11111111|11111111|11110010 fffff2 [24]
  258. (225) |11111111|11111111|11110011 fffff3 [24]
  259. (236) |11111111|11111111|11110100 fffff4 [24]
  260. (237) |11111111|11111111|11110101 fffff5 [24]
  261. (199) |11111111|11111111|11110110|0 1ffffec [25]
  262. (207) |11111111|11111111|11110110|1 1ffffed [25]
  263. (234) |11111111|11111111|11110111|0 1ffffee [25]
  264. (235) |11111111|11111111|11110111|1 1ffffef [25]
  265. (192) |11111111|11111111|11111000|00 3ffffe0 [26]
  266. (193) |11111111|11111111|11111000|01 3ffffe1 [26]
  267. (200) |11111111|11111111|11111000|10 3ffffe2 [26]
  268. (201) |11111111|11111111|11111000|11 3ffffe3 [26]
  269. (202) |11111111|11111111|11111001|00 3ffffe4 [26]
  270. (205) |11111111|11111111|11111001|01 3ffffe5 [26]
  271. (210) |11111111|11111111|11111001|10 3ffffe6 [26]
  272. (213) |11111111|11111111|11111001|11 3ffffe7 [26]
  273. (218) |11111111|11111111|11111010|00 3ffffe8 [26]
  274. (219) |11111111|11111111|11111010|01 3ffffe9 [26]
  275. (238) |11111111|11111111|11111010|10 3ffffea [26]
  276. (240) |11111111|11111111|11111010|11 3ffffeb [26]
  277. (242) |11111111|11111111|11111011|00 3ffffec [26]
  278. (243) |11111111|11111111|11111011|01 3ffffed [26]
  279. (255) |11111111|11111111|11111011|10 3ffffee [26]
  280. (203) |11111111|11111111|11111011|110 7ffffde [27]
  281. (204) |11111111|11111111|11111011|111 7ffffdf [27]
  282. (211) |11111111|11111111|11111100|000 7ffffe0 [27]
  283. (212) |11111111|11111111|11111100|001 7ffffe1 [27]
  284. (214) |11111111|11111111|11111100|010 7ffffe2 [27]
  285. (221) |11111111|11111111|11111100|011 7ffffe3 [27]
  286. (222) |11111111|11111111|11111100|100 7ffffe4 [27]
  287. (223) |11111111|11111111|11111100|101 7ffffe5 [27]
  288. (241) |11111111|11111111|11111100|110 7ffffe6 [27]
  289. (244) |11111111|11111111|11111100|111 7ffffe7 [27]
  290. (245) |11111111|11111111|11111101|000 7ffffe8 [27]
  291. (246) |11111111|11111111|11111101|001 7ffffe9 [27]
  292. (247) |11111111|11111111|11111101|010 7ffffea [27]
  293. (248) |11111111|11111111|11111101|011 7ffffeb [27]
  294. (250) |11111111|11111111|11111101|100 7ffffec [27]
  295. (251) |11111111|11111111|11111101|101 7ffffed [27]
  296. (252) |11111111|11111111|11111101|110 7ffffee [27]
  297. (253) |11111111|11111111|11111101|111 7ffffef [27]
  298. (254) |11111111|11111111|11111110|000 7fffff0 [27]
  299. ( 2) |11111111|11111111|11111110|0010 fffffe2 [28]
  300. ( 3) |11111111|11111111|11111110|0011 fffffe3 [28]
  301. ( 4) |11111111|11111111|11111110|0100 fffffe4 [28]
  302. ( 5) |11111111|11111111|11111110|0101 fffffe5 [28]
  303. ( 6) |11111111|11111111|11111110|0110 fffffe6 [28]
  304. ( 7) |11111111|11111111|11111110|0111 fffffe7 [28]
  305. ( 8) |11111111|11111111|11111110|1000 fffffe8 [28]
  306. ( 11) |11111111|11111111|11111110|1001 fffffe9 [28]
  307. ( 12) |11111111|11111111|11111110|1010 fffffea [28]
  308. ( 14) |11111111|11111111|11111110|1011 fffffeb [28]
  309. ( 15) |11111111|11111111|11111110|1100 fffffec [28]
  310. ( 16) |11111111|11111111|11111110|1101 fffffed [28]
  311. ( 17) |11111111|11111111|11111110|1110 fffffee [28]
  312. ( 18) |11111111|11111111|11111110|1111 fffffef [28]
  313. ( 19) |11111111|11111111|11111111|0000 ffffff0 [28]
  314. ( 20) |11111111|11111111|11111111|0001 ffffff1 [28]
  315. ( 21) |11111111|11111111|11111111|0010 ffffff2 [28]
  316. ( 23) |11111111|11111111|11111111|0011 ffffff3 [28]
  317. ( 24) |11111111|11111111|11111111|0100 ffffff4 [28]
  318. ( 25) |11111111|11111111|11111111|0101 ffffff5 [28]
  319. ( 26) |11111111|11111111|11111111|0110 ffffff6 [28]
  320. ( 27) |11111111|11111111|11111111|0111 ffffff7 [28]
  321. ( 28) |11111111|11111111|11111111|1000 ffffff8 [28]
  322. ( 29) |11111111|11111111|11111111|1001 ffffff9 [28]
  323. ( 30) |11111111|11111111|11111111|1010 ffffffa [28]
  324. ( 31) |11111111|11111111|11111111|1011 ffffffb [28]
  325. (127) |11111111|11111111|11111111|1100 ffffffc [28]
  326. (220) |11111111|11111111|11111111|1101 ffffffd [28]
  327. (249) |11111111|11111111|11111111|1110 ffffffe [28]
  328. ( 10) |11111111|11111111|11111111|111100 3ffffffc [30]
  329. ( 13) |11111111|11111111|11111111|111101 3ffffffd [30]
  330. ( 22) |11111111|11111111|11111111|111110 3ffffffe [30]
  331. (256) |11111111|11111111|11111111|111111 3fffffff [30]
  332. */
  333. struct {
  334. uint8_t decoded;
  335. uint8_t bitcount;
  336. uint32_t encoded;
  337. } hpack_huff_dec[] = {
  338. {48, 5, 0x0},
  339. {49, 5, 0x1},
  340. {50, 5, 0x2},
  341. {97, 5, 0x3},
  342. {99, 5, 0x4},
  343. {101, 5, 0x5},
  344. {105, 5, 0x6},
  345. {111, 5, 0x7},
  346. {115, 5, 0x8},
  347. {116, 5, 0x9},
  348. {32, 6, 0x14},
  349. {37, 6, 0x15},
  350. {45, 6, 0x16},
  351. {46, 6, 0x17},
  352. {47, 6, 0x18},
  353. {51, 6, 0x19},
  354. {52, 6, 0x1a},
  355. {53, 6, 0x1b},
  356. {54, 6, 0x1c},
  357. {55, 6, 0x1d},
  358. {56, 6, 0x1e},
  359. {57, 6, 0x1f},
  360. {61, 6, 0x20},
  361. {65, 6, 0x21},
  362. {95, 6, 0x22},
  363. {98, 6, 0x23},
  364. {100, 6, 0x24},
  365. {102, 6, 0x25},
  366. {103, 6, 0x26},
  367. {104, 6, 0x27},
  368. {108, 6, 0x28},
  369. {109, 6, 0x29},
  370. {110, 6, 0x2a},
  371. {112, 6, 0x2b},
  372. {114, 6, 0x2c},
  373. {117, 6, 0x2d},
  374. {58, 7, 0x5c},
  375. {66, 7, 0x5d},
  376. {67, 7, 0x5e},
  377. {68, 7, 0x5f},
  378. {69, 7, 0x60},
  379. {70, 7, 0x61},
  380. {71, 7, 0x62},
  381. {72, 7, 0x63},
  382. {73, 7, 0x64},
  383. {74, 7, 0x65},
  384. {75, 7, 0x66},
  385. {76, 7, 0x67},
  386. {77, 7, 0x68},
  387. {78, 7, 0x69},
  388. {79, 7, 0x6a},
  389. {80, 7, 0x6b},
  390. {81, 7, 0x6c},
  391. {82, 7, 0x6d},
  392. {83, 7, 0x6e},
  393. {84, 7, 0x6f},
  394. {85, 7, 0x70},
  395. {86, 7, 0x71},
  396. {87, 7, 0x72},
  397. {89, 7, 0x73},
  398. {106, 7, 0x74},
  399. {107, 7, 0x75},
  400. {113, 7, 0x76},
  401. {118, 7, 0x77},
  402. {119, 7, 0x78},
  403. {120, 7, 0x79},
  404. {121, 7, 0x7a},
  405. {122, 7, 0x7b},
  406. {38, 8, 0xf8},
  407. {42, 8, 0xf9},
  408. {44, 8, 0xfa},
  409. {59, 8, 0xfb},
  410. {88, 8, 0xfc},
  411. {90, 8, 0xfd},
  412. {33, 10, 0x3f8},
  413. {34, 10, 0x3f9},
  414. {40, 10, 0x3fa},
  415. {41, 10, 0x3fb},
  416. {63, 10, 0x3fc},
  417. {39, 11, 0x7fa},
  418. {43, 11, 0x7fb},
  419. {124, 11, 0x7fc},
  420. {35, 12, 0xffa},
  421. {62, 12, 0xffb},
  422. {0, 13, 0x1ff8},
  423. {36, 13, 0x1ff9},
  424. {64, 13, 0x1ffa},
  425. {91, 13, 0x1ffb},
  426. {93, 13, 0x1ffc},
  427. {126, 13, 0x1ffd},
  428. {94, 14, 0x3ffc},
  429. {125, 14, 0x3ffd},
  430. {60, 15, 0x7ffc},
  431. {96, 15, 0x7ffd},
  432. {123, 15, 0x7ffe},
  433. {92, 19, 0x7fff0},
  434. {195, 19, 0x7fff1},
  435. {208, 19, 0x7fff2},
  436. {128, 20, 0xfffe6},
  437. {130, 20, 0xfffe7},
  438. {131, 20, 0xfffe8},
  439. {162, 20, 0xfffe9},
  440. {184, 20, 0xfffea},
  441. {194, 20, 0xfffeb},
  442. {224, 20, 0xfffec},
  443. {226, 20, 0xfffed},
  444. {153, 21, 0x1fffdc},
  445. {161, 21, 0x1fffdd},
  446. {167, 21, 0x1fffde},
  447. {172, 21, 0x1fffdf},
  448. {176, 21, 0x1fffe0},
  449. {177, 21, 0x1fffe1},
  450. {179, 21, 0x1fffe2},
  451. {209, 21, 0x1fffe3},
  452. {216, 21, 0x1fffe4},
  453. {217, 21, 0x1fffe5},
  454. {227, 21, 0x1fffe6},
  455. {229, 21, 0x1fffe7},
  456. {230, 21, 0x1fffe8},
  457. {129, 22, 0x3fffd2},
  458. {132, 22, 0x3fffd3},
  459. {133, 22, 0x3fffd4},
  460. {134, 22, 0x3fffd5},
  461. {136, 22, 0x3fffd6},
  462. {146, 22, 0x3fffd7},
  463. {154, 22, 0x3fffd8},
  464. {156, 22, 0x3fffd9},
  465. {160, 22, 0x3fffda},
  466. {163, 22, 0x3fffdb},
  467. {164, 22, 0x3fffdc},
  468. {169, 22, 0x3fffdd},
  469. {170, 22, 0x3fffde},
  470. {173, 22, 0x3fffdf},
  471. {178, 22, 0x3fffe0},
  472. {181, 22, 0x3fffe1},
  473. {185, 22, 0x3fffe2},
  474. {186, 22, 0x3fffe3},
  475. {187, 22, 0x3fffe4},
  476. {189, 22, 0x3fffe5},
  477. {190, 22, 0x3fffe6},
  478. {196, 22, 0x3fffe7},
  479. {198, 22, 0x3fffe8},
  480. {228, 22, 0x3fffe9},
  481. {232, 22, 0x3fffea},
  482. {233, 22, 0x3fffeb},
  483. {1, 23, 0x7fffd8},
  484. {135, 23, 0x7fffd9},
  485. {137, 23, 0x7fffda},
  486. {138, 23, 0x7fffdb},
  487. {139, 23, 0x7fffdc},
  488. {140, 23, 0x7fffdd},
  489. {141, 23, 0x7fffde},
  490. {143, 23, 0x7fffdf},
  491. {147, 23, 0x7fffe0},
  492. {149, 23, 0x7fffe1},
  493. {150, 23, 0x7fffe2},
  494. {151, 23, 0x7fffe3},
  495. {152, 23, 0x7fffe4},
  496. {155, 23, 0x7fffe5},
  497. {157, 23, 0x7fffe6},
  498. {158, 23, 0x7fffe7},
  499. {165, 23, 0x7fffe8},
  500. {166, 23, 0x7fffe9},
  501. {168, 23, 0x7fffea},
  502. {174, 23, 0x7fffeb},
  503. {175, 23, 0x7fffec},
  504. {180, 23, 0x7fffed},
  505. {182, 23, 0x7fffee},
  506. {183, 23, 0x7fffef},
  507. {188, 23, 0x7ffff0},
  508. {191, 23, 0x7ffff1},
  509. {197, 23, 0x7ffff2},
  510. {231, 23, 0x7ffff3},
  511. {239, 23, 0x7ffff4},
  512. {9, 24, 0xffffea},
  513. {142, 24, 0xffffeb},
  514. {144, 24, 0xffffec},
  515. {145, 24, 0xffffed},
  516. {148, 24, 0xffffee},
  517. {159, 24, 0xffffef},
  518. {171, 24, 0xfffff0},
  519. {206, 24, 0xfffff1},
  520. {215, 24, 0xfffff2},
  521. {225, 24, 0xfffff3},
  522. {236, 24, 0xfffff4},
  523. {237, 24, 0xfffff5},
  524. {199, 25, 0x1ffffec},
  525. {207, 25, 0x1ffffed},
  526. {234, 25, 0x1ffffee},
  527. {235, 25, 0x1ffffef},
  528. {192, 26, 0x3ffffe0},
  529. {193, 26, 0x3ffffe1},
  530. {200, 26, 0x3ffffe2},
  531. {201, 26, 0x3ffffe3},
  532. {202, 26, 0x3ffffe4},
  533. {205, 26, 0x3ffffe5},
  534. {210, 26, 0x3ffffe6},
  535. {213, 26, 0x3ffffe7},
  536. {218, 26, 0x3ffffe8},
  537. {219, 26, 0x3ffffe9},
  538. {238, 26, 0x3ffffea},
  539. {240, 26, 0x3ffffeb},
  540. {242, 26, 0x3ffffec},
  541. {243, 26, 0x3ffffed},
  542. {255, 26, 0x3ffffee},
  543. {203, 27, 0x7ffffde},
  544. {204, 27, 0x7ffffdf},
  545. {211, 27, 0x7ffffe0},
  546. {212, 27, 0x7ffffe1},
  547. {214, 27, 0x7ffffe2},
  548. {221, 27, 0x7ffffe3},
  549. {222, 27, 0x7ffffe4},
  550. {223, 27, 0x7ffffe5},
  551. {241, 27, 0x7ffffe6},
  552. {244, 27, 0x7ffffe7},
  553. {245, 27, 0x7ffffe8},
  554. {246, 27, 0x7ffffe9},
  555. {247, 27, 0x7ffffea},
  556. {248, 27, 0x7ffffeb},
  557. {250, 27, 0x7ffffec},
  558. {251, 27, 0x7ffffed},
  559. {252, 27, 0x7ffffee},
  560. {253, 27, 0x7ffffef},
  561. {254, 27, 0x7fffff0},
  562. {2, 28, 0xfffffe2},
  563. {3, 28, 0xfffffe3},
  564. {4, 28, 0xfffffe4},
  565. {5, 28, 0xfffffe5},
  566. {6, 28, 0xfffffe6},
  567. {7, 28, 0xfffffe7},
  568. {8, 28, 0xfffffe8},
  569. {11, 28, 0xfffffe9},
  570. {12, 28, 0xfffffea},
  571. {14, 28, 0xfffffeb},
  572. {15, 28, 0xfffffec},
  573. {16, 28, 0xfffffed},
  574. {17, 28, 0xfffffee},
  575. {18, 28, 0xfffffef},
  576. {19, 28, 0xffffff0},
  577. {20, 28, 0xffffff1},
  578. {21, 28, 0xffffff2},
  579. {23, 28, 0xffffff3},
  580. {24, 28, 0xffffff4},
  581. {25, 28, 0xffffff5},
  582. {26, 28, 0xffffff6},
  583. {27, 28, 0xffffff7},
  584. {28, 28, 0xffffff8},
  585. {29, 28, 0xffffff9},
  586. {30, 28, 0xffffffa},
  587. {31, 28, 0xffffffb},
  588. {127, 28, 0xffffffc},
  589. {220, 28, 0xffffffd},
  590. {249, 28, 0xffffffe},
  591. {10, 30, 0x3ffffffc},
  592. {13, 30, 0x3ffffffd},
  593. {22, 30, 0x3ffffffe},
  594. {(uint8_t)256, 30, 0x3fffffff} /* filling/termination */
  595. };
  596. /* highest value with 5, 6, 7, ... 28, 29, 30 and all (32) bits */
  597. uint32_t hpack_huff_end_code[] = {0x9, 0x2d, 0x7b, 0xfd,
  598. 0, 0x3fc, 0x7fc, 0xffb,
  599. 0x1ffd, 0x3ffd, 0x7ffe, 0,
  600. 0, 0, 0x7fff2, 0xfffed,
  601. 0x1fffe8, 0x3fffeb, 0x7ffff4, 0xfffff5,
  602. 0x1ffffef, 0x3ffffee, 0x7fffff0, 0xffffffe,
  603. 0, 0x3ffffffe, 0xFFFFFFFFu};
  604. /* lowest index with 5, 6, 7, ... 28, 29, 30 and all (32) bits */
  605. uint8_t hpack_huff_start_index[] = {0, 10, 36, 68, 0, 74, 79, 82, 84,
  606. 90, 92, 0, 0, 0, 95, 98, 106, 119,
  607. 145, 174, 186, 190, 205, 224, 0, 253, 0};
  608. /* Function to decode an integer from a HPACK encoded block */
  609. /* Integers have a variable size encoding, according to the RFC.
  610. * The integer starts at index *i, idx_mask masks the available bits in
  611. * the first byte. The index *i is advanced until the end of the
  612. * encoded integer.
  613. */
  614. static uint64_t
  615. hpack_getnum(const uint8_t *buf,
  616. int *i,
  617. uint8_t idx_mask,
  618. struct mg_context *ctx)
  619. {
  620. uint64_t num = (buf[*i] & idx_mask);
  621. (void)ctx;
  622. if (num == idx_mask) {
  623. /* Algorithm from https://tools.ietf.org/html/rfc7541#section-5.1 */
  624. uint32_t M = 0;
  625. do {
  626. (*i)++;
  627. num = num + ((buf[*i] & 0x7F) << M);
  628. M += 7;
  629. } while ((buf[*i] & 0x80) == 0x80);
  630. }
  631. (*i)++;
  632. return num;
  633. }
  634. /* Function to decode a string from a HPACK encoded block */
  635. /* Strings have a variable size and can be either encoded directly (8 bits
  636. * per char), or using huffman encoding (variable bits per char).
  637. * The string starts at index *i. This index is advanced until the end of
  638. * the encoded string.
  639. */
  640. static char *
  641. hpack_decode(const uint8_t *buf, int *i, struct mg_context *ctx)
  642. {
  643. uint64_t byte_len64;
  644. int byte_len;
  645. int bit_len;
  646. uint8_t is_huff = ((buf[*i] & 0x80) == 0x80);
  647. /* Get length of string in bytes */
  648. byte_len64 = hpack_getnum(buf, i, 0x7f, ctx);
  649. if (byte_len64 > 1024) {
  650. /* TODO */
  651. return NULL;
  652. }
  653. byte_len = (int)byte_len64;
  654. bit_len = byte_len * 8;
  655. /* Now read the string */
  656. if (!is_huff) {
  657. /* Not huffman encoded: Copy directly */
  658. char *result = mg_malloc_ctx(byte_len + 1, ctx);
  659. if (result) {
  660. memcpy(result, buf + (*i), byte_len);
  661. result[byte_len] = 0;
  662. }
  663. (*i) += byte_len;
  664. return result;
  665. } else {
  666. /* Huffman encoded: need to decode bitwise */
  667. const uint8_t *pData =
  668. buf + (*i); /* begin pointer of bit input string */
  669. int bitRead = 0; /* number of encoded bits read */
  670. uint32_t bytesStored = 0; /* number of decoded bytes stored */
  671. uint8_t str[2048]; /* storage buffer for decoded string */
  672. for (;;) {
  673. uint32_t accu = 0; /* accu register: collect bits */
  674. uint8_t bc = 0; /* number of bits collected */
  675. int n;
  676. /* Collect bits in this loop, until we have a valid huff code in
  677. * accu */
  678. do {
  679. accu <<= 1;
  680. accu |= (pData[bitRead / 8] >> (7 - (bitRead & 7))) & 1;
  681. bitRead++;
  682. bc++;
  683. if (bitRead > bit_len) {
  684. /* We used all bits. Return the decoded string. */
  685. str[bytesStored] = 0; /* Terminate string */
  686. (*i) += byte_len; /* Advance parsing index */
  687. return mg_strdup_ctx((char *)str,
  688. ctx); /* Return a string copy */
  689. }
  690. } while ((bc < 5) || (accu > hpack_huff_end_code[bc - 5]));
  691. /* Find matching code in huffman encoding table */
  692. for (n = hpack_huff_start_index[bc - 5]; n < 256; n++) {
  693. if (accu == hpack_huff_dec[n].encoded) {
  694. str[bytesStored] = hpack_huff_dec[n].decoded;
  695. bytesStored++;
  696. break;
  697. }
  698. }
  699. }
  700. }
  701. }
  702. static void
  703. append_bits(uint8_t *target,
  704. uint32_t offset,
  705. uint32_t value,
  706. uint8_t value_bits)
  707. {
  708. uint32_t offset_bytes = offset / 8;
  709. uint32_t offset_bits = offset % 8;
  710. uint32_t remaining_bits, ac;
  711. value &= ~(0xFFFFFFFF << value_bits);
  712. remaining_bits = 8 - offset_bits;
  713. if (value_bits <= remaining_bits) {
  714. ac = value << (remaining_bits - value_bits);
  715. target[offset_bytes] |= ac;
  716. return;
  717. }
  718. ac = value >> (value_bits - remaining_bits);
  719. target[offset_bytes] |= ac;
  720. append_bits(target,
  721. offset + remaining_bits,
  722. value,
  723. value_bits - remaining_bits);
  724. }
  725. static int
  726. hpack_encode(uint8_t *store, const char *load, int lower)
  727. {
  728. uint32_t nohuff_len = strlen(load);
  729. uint32_t len_bits = 0;
  730. uint32_t len_bytes;
  731. uint32_t spare_bits;
  732. uint32_t i;
  733. memset(store, 0, nohuff_len + 1);
  734. for (i = 0; i < nohuff_len; i++) {
  735. uint8_t b = (uint8_t)((char)(lower ? tolower(load[i]) : load[i]));
  736. int idx;
  737. for (idx = 0; idx <= 255; idx++) {
  738. if (hpack_huff_dec[idx].decoded == b) {
  739. append_bits((uint8_t *)store + 1,
  740. len_bits,
  741. hpack_huff_dec[idx].encoded,
  742. hpack_huff_dec[idx].bitcount);
  743. len_bits += hpack_huff_dec[idx].bitcount;
  744. break;
  745. }
  746. }
  747. }
  748. len_bytes = (len_bits + 7) / 8;
  749. spare_bits = len_bytes * 8 - len_bits;
  750. if (spare_bits) {
  751. append_bits((uint8_t *)store + 1, len_bits, 0xFFFFFFFF, spare_bits);
  752. }
  753. if (len_bytes >= 127) {
  754. // TODO: Shift string and encode len in more bytes
  755. return 0;
  756. }
  757. *store = 0x80 + (uint8_t)len_bytes;
  758. if ((len_bytes >= nohuff_len) && (0)) {
  759. *store = (uint8_t)nohuff_len;
  760. if (lower) {
  761. for (i = 1; i <= nohuff_len; i++) {
  762. store[i] = tolower(load[i]);
  763. }
  764. } else {
  765. memcpy(store + 1, load, nohuff_len);
  766. }
  767. return nohuff_len + 1;
  768. } else {
  769. /*
  770. int i = 0;
  771. char *test = hpack_decode(store, &i, NULL);
  772. i = i; // breakpoint for debugging / testing
  773. */
  774. }
  775. return len_bytes + 1;
  776. }
  777. /***********************************************************************/
  778. /*** HTTP 2 ***/
  779. /***********************************************************************/
  780. static const char http2_pri[] = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
  781. static unsigned char http2_pri_len = 24; /* = strlen(http2_pri) */
  782. /* Read and check the HTTP/2 primer/preface:
  783. * See https://tools.ietf.org/html/rfc7540#section-3.5 */
  784. static int
  785. is_valid_http2_primer(struct mg_connection *conn)
  786. {
  787. size_t pri_len = http2_pri_len;
  788. char buf[32];
  789. if (pri_len > sizeof(buf)) {
  790. /* Should never be reached - the RFC primer has 24 bytes */
  791. return 0;
  792. }
  793. int read_pri_len = mg_read(conn, buf, pri_len);
  794. if ((read_pri_len != (int)pri_len)
  795. || (0 != memcmp(buf, http2_pri, pri_len))) {
  796. return 0;
  797. }
  798. return 1;
  799. }
  800. #define mg_xwrite(conn, data, len) \
  801. push_all((conn)->phys_ctx, \
  802. NULL, \
  803. (conn)->client.sock, \
  804. (conn)->ssl, \
  805. (const char *)(data), \
  806. (int)(len));
  807. static void
  808. http2_settings_acknowledge(struct mg_connection *conn)
  809. {
  810. unsigned char http2_set_ackn_frame[9] = {0, 0, 0, 4, 1, 0, 0, 0, 0};
  811. DEBUG_TRACE("%s", "Sending settings frame");
  812. mg_xwrite(conn, http2_set_ackn_frame, 9);
  813. }
  814. struct http2_settings {
  815. uint32_t settings_header_table_size;
  816. uint32_t settings_enable_push;
  817. uint32_t settings_max_concurrent_streams;
  818. uint32_t settings_initial_window_size;
  819. uint32_t settings_max_frame_size;
  820. uint32_t settings_max_header_list_size;
  821. };
  822. const struct http2_settings http2_default_settings =
  823. {4096, 1, UINT32_MAX, 65535, 16384, UINT32_MAX};
  824. const struct http2_settings http2_civetweb_server_settings =
  825. {4096, 0, 100, 65535, 16384, 65535};
  826. enum {
  827. HTTP2_ERR_NO_ERROR = 0,
  828. HTTP2_ERR_PROTOCOL_ERROR,
  829. HTTP2_ERR_INTERNAL_ERROR,
  830. HTTP2_ERR_FLOW_CONTROL_ERROR,
  831. HTTP2_ERR_SETTINGS_TIMEOUT,
  832. HTTP2_ERR_STREAM_CLOSED,
  833. HTTP2_ERR_FRAME_SIZE_ERROR,
  834. HTTP2_ERR_REFUSED_STREAM,
  835. HTTP2_ERR_CANCEL,
  836. HTTP2_ERR_COMPRESSION_ERROR,
  837. HTTP2_ERR_CONNECT_ERROR,
  838. HTTP2_ERR_ENHANCE_YOUR_CALM,
  839. HTTP2_ERR_INADEQUATE_SECURITY,
  840. HTTP2_ERR_HTTP_1_1_REQUIRED
  841. };
  842. static void
  843. http2_send_settings(struct mg_connection *conn,
  844. const struct http2_settings *set)
  845. {
  846. uint16_t id;
  847. uint32_t data;
  848. uint8_t http2_settings_frame[9] = {0, 0, 36, 4, 0, 0, 0, 0, 0};
  849. mg_xwrite(conn, http2_settings_frame, 9);
  850. id = htons(1);
  851. data = htonl(set->settings_header_table_size);
  852. mg_xwrite(conn, &id, 2);
  853. mg_xwrite(conn, &data, 4);
  854. id = htons(1);
  855. data = htonl(set->settings_enable_push);
  856. mg_xwrite(conn, &id, 2);
  857. mg_xwrite(conn, &data, 4);
  858. id = htons(1);
  859. data = htonl(set->settings_max_concurrent_streams);
  860. mg_xwrite(conn, &id, 2);
  861. mg_xwrite(conn, &data, 4);
  862. id = htons(1);
  863. data = htonl(set->settings_initial_window_size);
  864. mg_xwrite(conn, &id, 2);
  865. mg_xwrite(conn, &data, 4);
  866. id = htons(1);
  867. data = htonl(set->settings_max_frame_size);
  868. mg_xwrite(conn, &id, 2);
  869. mg_xwrite(conn, &data, 4);
  870. id = htons(1);
  871. data = htonl(set->settings_max_header_list_size);
  872. mg_xwrite(conn, &id, 2);
  873. mg_xwrite(conn, &data, 4);
  874. DEBUG_TRACE("%s", "HTTP2 settings sent");
  875. }
  876. static int
  877. http2_send_response_headers(struct mg_connection *conn)
  878. {
  879. unsigned char http2_header_frame[9] = {0, 0, 0, 1, 4, 0, 0, 0, 0};
  880. uint8_t header_bin[1024];
  881. uint16_t header_len = 0;
  882. int has_date = 0;
  883. int has_connection = 0;
  884. int i;
  885. if ((conn->status_code < 100) || (conn->status_code > 999)) {
  886. /* Invalid status: Set status to "Internal Server Error" */
  887. conn->status_code = 500;
  888. }
  889. switch (conn->status_code) {
  890. case 200:
  891. header_bin[header_len++] = 0x88;
  892. break;
  893. case 204:
  894. header_bin[header_len++] = 0x89;
  895. break;
  896. case 206:
  897. header_bin[header_len++] = 0x8A;
  898. break;
  899. case 304:
  900. header_bin[header_len++] = 0x8B;
  901. break;
  902. case 400:
  903. header_bin[header_len++] = 0x8C;
  904. break;
  905. case 404:
  906. header_bin[header_len++] = 0x8D;
  907. break;
  908. case 500:
  909. header_bin[header_len++] = 0x8E;
  910. break;
  911. default:
  912. header_bin[header_len++] = 0x48;
  913. header_bin[header_len++] = 0x03;
  914. header_bin[header_len++] = 0x30 + (conn->status_code / 100);
  915. header_bin[header_len++] = 0x30 + ((conn->status_code / 10) % 10);
  916. header_bin[header_len++] = 0x30 + (conn->status_code % 10);
  917. break;
  918. }
  919. /* Add all headers */
  920. for (i = 0; i < conn->response_info.num_headers; i++) {
  921. uint16_t predef = 0;
  922. uint16_t j;
  923. /* Filter headers not valid in HTTP/2 */
  924. if (!mg_strcasecmp("Connection",
  925. conn->response_info.http_headers[i].name)) {
  926. has_connection = 1;
  927. continue; /* do not send */
  928. }
  929. /* Check if this header is known in HPACK (static table index 15 to 61)
  930. * see https://tools.ietf.org/html/rfc7541#appendix-A */
  931. for (j = 15; j <= 61; j++) {
  932. if (!mg_strcasecmp(hpack_predefined[j].name,
  933. conn->response_info.http_headers[i].name)) {
  934. predef = j;
  935. break;
  936. }
  937. }
  938. if (predef) {
  939. /* Predefined header found */
  940. header_bin[header_len++] = 0x40 + predef;
  941. } else {
  942. /* Rare header, do not index */
  943. header_bin[header_len++] = 0x10;
  944. j = hpack_encode(header_bin + header_len,
  945. conn->response_info.http_headers[i].name,
  946. 1);
  947. header_len += j;
  948. }
  949. j = hpack_encode(header_bin + header_len,
  950. conn->response_info.http_headers[i].value,
  951. 0);
  952. header_len += j;
  953. /* Mark required headers as sent */
  954. if (!mg_strcasecmp("Date", conn->response_info.http_headers[i].name)) {
  955. has_date = 1;
  956. }
  957. }
  958. /* Add required headers, if they have not been sent yet */
  959. if (!has_date) {
  960. /* Create header frame */
  961. char date[64];
  962. uint8_t date_len;
  963. time_t curtime = time(NULL);
  964. gmt_time_string(date, sizeof(date), &curtime);
  965. date_len = (uint8_t)strlen(date);
  966. header_bin[header_len++] =
  967. 0x61; /* "Date" predefined HPACK index 33 (0x21) + 0x40 */
  968. header_bin[header_len++] = date_len;
  969. memcpy(header_bin + header_len, date, date_len);
  970. header_len += date_len;
  971. }
  972. http2_header_frame[1] = (header_len & 0xFF00) >> 8;
  973. http2_header_frame[2] = (header_len & 0xFF);
  974. http2_header_frame[5] = (conn->http2.stream_id & 0xFF000000u) >> 24;
  975. http2_header_frame[6] = (conn->http2.stream_id & 0xFF0000u) >> 16;
  976. http2_header_frame[7] = (conn->http2.stream_id & 0xFF00u) >> 8;
  977. http2_header_frame[8] = (conn->http2.stream_id & 0xFFu);
  978. /* Send header frame */
  979. mg_xwrite(conn, http2_header_frame, 9);
  980. mg_xwrite(conn, header_bin, header_len);
  981. DEBUG_TRACE("HTTP2 response header sent: stream %u", conn->http2.stream_id);
  982. return 42; /* TODO */
  983. }
  984. static void
  985. http2_data_frame_head(struct mg_connection *conn,
  986. uint32_t frame_size,
  987. int is_final)
  988. {
  989. unsigned char http2_data_frame[9];
  990. uint32_t stream_id = conn->http2.stream_id;
  991. http2_data_frame[0] = (frame_size & 0xFF0000) >> 16;
  992. http2_data_frame[1] = (frame_size & 0xFF00) >> 8;
  993. http2_data_frame[2] = (frame_size & 0xFF);
  994. http2_data_frame[3] = 0; /* frame type "DATA" */
  995. http2_data_frame[4] = (is_final ? 1 : 0);
  996. http2_data_frame[5] = (stream_id & 0xFF000000u) >> 24;
  997. http2_data_frame[6] = (stream_id & 0xFF0000u) >> 16;
  998. http2_data_frame[7] = (stream_id & 0xFF00u) >> 8;
  999. http2_data_frame[8] = (stream_id & 0xFFu);
  1000. DEBUG_TRACE("HTTP2 begin data frame: stream %u, frame_size %u (final: %i)",
  1001. stream_id,
  1002. frame_size,
  1003. is_final);
  1004. mg_xwrite(conn, http2_data_frame, 9);
  1005. }
  1006. static void
  1007. http2_send_window(struct mg_connection *conn,
  1008. uint32_t stream_id,
  1009. uint32_t window_size)
  1010. {
  1011. unsigned char http2_window_frame[9] = {0, 0, 4, 8, 0, 0, 0, 0, 0};
  1012. uint32_t data = htonl(window_size);
  1013. DEBUG_TRACE("HTTP2 send window_size: stream %u, error %u",
  1014. stream_id,
  1015. window_size);
  1016. http2_window_frame[5] = (stream_id & 0xFF000000u) >> 24;
  1017. http2_window_frame[6] = (stream_id & 0xFF0000u) >> 16;
  1018. http2_window_frame[7] = (stream_id & 0xFF00u) >> 8;
  1019. http2_window_frame[8] = (stream_id & 0xFFu);
  1020. mg_xwrite(conn, http2_window_frame, 9);
  1021. mg_xwrite(conn, &data, 4);
  1022. }
  1023. static void
  1024. http2_reset_stream(struct mg_connection *conn,
  1025. uint32_t stream_id,
  1026. uint32_t error_id)
  1027. {
  1028. unsigned char http2_reset_frame[9] = {0, 0, 4, 3, 0, 0, 0, 0, 0};
  1029. uint32_t val = htonl(error_id);
  1030. DEBUG_TRACE("HTTP2 send reset: stream %u, error %u", stream_id, error_id);
  1031. http2_reset_frame[5] = (stream_id & 0xFF000000u) >> 24;
  1032. http2_reset_frame[6] = (stream_id & 0xFF0000u) >> 16;
  1033. http2_reset_frame[7] = (stream_id & 0xFF00u) >> 8;
  1034. http2_reset_frame[8] = (stream_id & 0xFFu);
  1035. mg_xwrite(conn, http2_reset_frame, 9);
  1036. mg_xwrite(conn, &val, 4);
  1037. }
  1038. static void
  1039. http2_must_use_http1(struct mg_connection *conn)
  1040. {
  1041. DEBUG_TRACE("HTTP2 not available for this URL (%s)", conn->path_info);
  1042. http2_reset_stream(conn, conn->http2.stream_id, 0xd);
  1043. }
  1044. /* The HTTP2 implementation collects request headers as array of dynamically
  1045. * allocated string values. This array must be freed once the request is
  1046. * handled.
  1047. * This is different to the HTTP/1.x implementation: For HTTP/1.x, the header
  1048. * list is implemented as pointers into an existing buffer, so free must not
  1049. * be called for HTTP/1.x.
  1050. * Thus free_buffered_request_header_list is in mod_http2.inl.
  1051. */
  1052. #if defined(DEBUG)
  1053. static int mem_h_count = 0;
  1054. static int mem_d_count = 0;
  1055. #define CHECK_LEAK_HDR_ALLOC(ptr) \
  1056. DEBUG_TRACE("H NEW %08x (%i): %s", \
  1057. (uint32_t)ptr, \
  1058. ++mem_h_count, \
  1059. (const char *)ptr)
  1060. #define CHECK_LEAK_HDR_FREE(ptr) \
  1061. DEBUG_TRACE("H DEL %08x (%i): %s", \
  1062. (uint32_t)ptr, \
  1063. --mem_h_count, \
  1064. (const char *)ptr)
  1065. #define CHECK_LEAK_DYN_ALLOC(ptr) \
  1066. DEBUG_TRACE("D NEW %08x (%i): %s", \
  1067. (uint32_t)ptr, \
  1068. ++mem_d_count, \
  1069. (const char *)ptr)
  1070. #define CHECK_LEAK_DYN_FREE(ptr) \
  1071. DEBUG_TRACE("D DEL %08x (%i): %s", \
  1072. (uint32_t)ptr, \
  1073. --mem_d_count, \
  1074. (const char *)ptr)
  1075. #else
  1076. #define CHECK_LEAK_HDR_ALLOC(ptr)
  1077. #define CHECK_LEAK_HDR_FREE(ptr)
  1078. #define CHECK_LEAK_DYN_ALLOC(ptr)
  1079. #define CHECK_LEAK_DYN_FREE(ptr)
  1080. #endif
  1081. /* The dynamic header table may be resized on a HTTP2 client request.
  1082. * A tablesize=0 will free all memory.
  1083. */
  1084. static void
  1085. purge_dynamic_header_table(struct mg_connection *conn, uint32_t tableSize)
  1086. {
  1087. DEBUG_TRACE("HTTP2 dynamic header table set to %u", tableSize);
  1088. while (conn->http2.dyn_table_size > tableSize) {
  1089. conn->http2.dyn_table_size--;
  1090. CHECK_LEAK_DYN_FREE(
  1091. conn->http2.dyn_table[conn->http2.dyn_table_size].name);
  1092. CHECK_LEAK_DYN_FREE(
  1093. conn->http2.dyn_table[conn->http2.dyn_table_size].value);
  1094. mg_free((void *)conn->http2.dyn_table[conn->http2.dyn_table_size].name);
  1095. conn->http2.dyn_table[conn->http2.dyn_table_size].name = 0;
  1096. mg_free(
  1097. (void *)conn->http2.dyn_table[conn->http2.dyn_table_size].value);
  1098. conn->http2.dyn_table[conn->http2.dyn_table_size].value = 0;
  1099. }
  1100. }
  1101. /* Internal function to free request header list.
  1102. * Not to be confused with the response header list.
  1103. */
  1104. static void
  1105. free_buffered_request_header_list(struct mg_connection *conn)
  1106. {
  1107. while (conn->request_info.num_headers > 0) {
  1108. conn->request_info.num_headers--;
  1109. CHECK_LEAK_HDR_FREE(
  1110. conn->request_info.http_headers[conn->request_info.num_headers]
  1111. .name);
  1112. CHECK_LEAK_HDR_FREE(
  1113. conn->request_info.http_headers[conn->request_info.num_headers]
  1114. .value);
  1115. mg_free((void *)conn->request_info
  1116. .http_headers[conn->request_info.num_headers]
  1117. .name);
  1118. conn->request_info.http_headers[conn->request_info.num_headers].name =
  1119. 0;
  1120. mg_free((void *)conn->request_info
  1121. .http_headers[conn->request_info.num_headers]
  1122. .value);
  1123. conn->request_info.http_headers[conn->request_info.num_headers].value =
  1124. 0;
  1125. }
  1126. }
  1127. /* HTTP2 requires a different handling loop */
  1128. static void
  1129. handle_http2(struct mg_connection *conn)
  1130. {
  1131. unsigned char http2_frame_head[9];
  1132. uint32_t http2_frame_size;
  1133. uint8_t http2_frame_type;
  1134. uint8_t http2_frame_flags;
  1135. uint32_t http2_frame_stream_id;
  1136. uint32_t http_window_length = 0;
  1137. int bytes_read;
  1138. uint8_t *buf;
  1139. int my_settings_accepted = 0;
  1140. int my_settings_sent;
  1141. const char *my_hpack_headers[128];
  1142. struct http2_settings client_settings = http2_default_settings;
  1143. struct http2_settings server_settings = http2_default_settings;
  1144. /* Send own settings */
  1145. http2_send_settings(conn, &http2_civetweb_server_settings);
  1146. my_settings_sent = 1;
  1147. // http2_send_window(conn, 0, /* 0x3fff0001 */ 1024*1024);
  1148. /* initialize hpack header table with predefined header fields */
  1149. memset((void *)my_hpack_headers, 0, sizeof(my_hpack_headers));
  1150. memcpy((void *)my_hpack_headers,
  1151. hpack_predefined,
  1152. sizeof(hpack_predefined));
  1153. buf = (uint8_t *)mg_malloc_ctx(server_settings.settings_max_frame_size,
  1154. conn->phys_ctx);
  1155. if (!buf) {
  1156. /* Out of memory */
  1157. DEBUG_TRACE("%s", "Out of memory for HTTP2 frame");
  1158. return;
  1159. }
  1160. for (;;) {
  1161. /* HTTP/2 is handled frame by frame */
  1162. int frame_is_end_stream = 0;
  1163. int frame_is_end_headers = 0;
  1164. int frame_is_padded = 0;
  1165. int frame_is_priority = 0;
  1166. bytes_read = mg_read(conn, http2_frame_head, sizeof(http2_frame_head));
  1167. if (bytes_read != sizeof(http2_frame_head)) {
  1168. /* TODO: errormsg */
  1169. goto clean_http2;
  1170. }
  1171. /* Extract data from frame header */
  1172. http2_frame_size = ((uint32_t)http2_frame_head[0] * 0x10000u)
  1173. + ((uint32_t)http2_frame_head[1] * 0x100u)
  1174. + ((uint32_t)http2_frame_head[2]);
  1175. http2_frame_type = http2_frame_head[3];
  1176. http2_frame_flags = http2_frame_head[4];
  1177. http2_frame_stream_id = ((uint32_t)http2_frame_head[5] * 0x1000000u)
  1178. + ((uint32_t)http2_frame_head[6] * 0x10000u)
  1179. + ((uint32_t)http2_frame_head[7] * 0x100u)
  1180. + ((uint32_t)http2_frame_head[8]);
  1181. frame_is_end_stream = (0 != (http2_frame_flags & 0x01));
  1182. frame_is_end_headers = (0 != (http2_frame_flags & 0x04));
  1183. frame_is_padded = (0 != (http2_frame_flags & 0x08));
  1184. frame_is_priority = (0 != (http2_frame_flags & 0x20));
  1185. if (http2_frame_size > server_settings.settings_max_frame_size) {
  1186. /* TODO: Error Message */
  1187. DEBUG_TRACE("HTTP2 frame too large (%lu)",
  1188. (unsigned long)http2_frame_size);
  1189. goto clean_http2;
  1190. }
  1191. bytes_read = mg_read(conn, buf, http2_frame_size);
  1192. if (bytes_read != (int)http2_frame_size) {
  1193. /* TODO: Error Message - or read again? */
  1194. DEBUG_TRACE("HTTP2 read error (%li != %li)",
  1195. (signed long int)bytes_read,
  1196. (signed long int)http2_frame_size);
  1197. goto clean_http2;
  1198. }
  1199. DEBUG_TRACE("HTTP2 frame type %u, size %u, stream %u, flags %02x",
  1200. http2_frame_type,
  1201. http2_frame_size,
  1202. http2_frame_stream_id,
  1203. http2_frame_flags);
  1204. /* Further processing according to frame type. See definition: */
  1205. /* https://tools.ietf.org/html/rfc7540#section-6 */
  1206. switch (http2_frame_type) {
  1207. case 0: /* DATA */
  1208. {
  1209. int i = 0; /* TODO */
  1210. DEBUG_TRACE("%s", "HTTP2 DATA frame?");
  1211. } break;
  1212. case 1: /* HEADERS */
  1213. {
  1214. int i = 0;
  1215. uint8_t padding = 0;
  1216. uint32_t dependency = 0;
  1217. uint8_t weight = 0;
  1218. uint8_t exclusive = 0;
  1219. if (frame_is_padded) {
  1220. padding = buf[i];
  1221. i++;
  1222. DEBUG_TRACE("HTTP2 frame padded by %u bytes", padding);
  1223. }
  1224. if (frame_is_priority) {
  1225. uint32_t val = ((uint32_t)buf[0 + i] * 0x1000000u)
  1226. + ((uint32_t)buf[1 + i] * 0x10000u)
  1227. + ((uint32_t)buf[2 + i] * 0x100u)
  1228. + ((uint32_t)buf[3 + i]);
  1229. dependency = (val & 0x7FFFFFFFu);
  1230. exclusive = ((val & 0x80000000u) != 0);
  1231. weight = buf[4 + i];
  1232. i += 5;
  1233. DEBUG_TRACE(
  1234. "HTTP2 frame weight %u, dependency %u (exclusive: %i)",
  1235. weight,
  1236. dependency,
  1237. exclusive);
  1238. }
  1239. conn->request_info.num_headers = 0;
  1240. while (i < (int)http2_frame_size - (int)padding) {
  1241. const char *key = 0;
  1242. const char *val = 0;
  1243. uint8_t idx_mask = 0;
  1244. uint8_t value_known = 0;
  1245. uint8_t indexing = 0;
  1246. uint64_t idx = 0;
  1247. /* Classify next entry by checking the bit mask */
  1248. if ((buf[i] & 0x80u) == 0x80u) {
  1249. /* Indexed Header Field Representation:
  1250. * https://tools.ietf.org/html/rfc7541#section-6.1 */
  1251. idx_mask = 0x7fu;
  1252. value_known = 1;
  1253. } else if ((buf[i] & 0xC0u) == 0x40u) {
  1254. /* Literal Header Field with Incremental Indexing:
  1255. * https://tools.ietf.org/html/rfc7541#section-6.2.1 */
  1256. idx_mask = 0x3fu;
  1257. indexing = 1;
  1258. } else if ((buf[i] & 0xF0u) == 0x00u) {
  1259. /* Literal Header Field without Indexing:
  1260. * https://tools.ietf.org/html/rfc7541#section-6.2.2 */
  1261. idx_mask = 0x0fu;
  1262. } else if ((buf[i] & 0xF0u) == 0x10u) {
  1263. /* Literal Header Field Never Indexed:
  1264. * https://tools.ietf.org/html/rfc7541#section-6.2.3 */
  1265. idx_mask = 0x0fu;
  1266. } else if ((buf[i] & 0xE0u) == 0x20u) {
  1267. uint64_t tableSize;
  1268. /* Dynamic Table Size Update:
  1269. * https://tools.ietf.org/html/rfc7541#section-6.3 */
  1270. idx_mask = 0x1fu;
  1271. tableSize = hpack_getnum(buf, &i, idx_mask, conn->phys_ctx);
  1272. /* TODO: check if tablesize > allowed table size */
  1273. /* Purge additional table entries */
  1274. purge_dynamic_header_table(conn, (uint32_t)tableSize);
  1275. /* Process next frame */
  1276. continue;
  1277. } else {
  1278. DEBUG_TRACE("HTTP2 unknown start pattern %02x", buf[i]);
  1279. goto clean_http2;
  1280. }
  1281. /* Get the header name table index */
  1282. idx = hpack_getnum(buf, &i, idx_mask, conn->phys_ctx);
  1283. /* Get Header name "key" */
  1284. if (idx == 0) {
  1285. /* Index 0: Header name encoded in following bytes */
  1286. key = hpack_decode(buf, &i, conn->phys_ctx);
  1287. CHECK_LEAK_HDR_ALLOC(key);
  1288. } else if (/*(idx >= 15) &&*/ (idx <= 61)) {
  1289. /* Take key name from predefined header table */
  1290. key = mg_strdup_ctx(hpack_predefined[idx].name,
  1291. conn->phys_ctx); /* leak? */
  1292. CHECK_LEAK_HDR_ALLOC(key);
  1293. } else if ((idx >= 62)
  1294. && ((idx - 61) <= conn->http2.dyn_table_size)) {
  1295. /* Take from dynamic header table */
  1296. uint32_t local_table_idx = (uint32_t)idx - 62;
  1297. key = mg_strdup_ctx(
  1298. conn->http2.dyn_table[local_table_idx].name,
  1299. conn->phys_ctx);
  1300. CHECK_LEAK_HDR_ALLOC(key);
  1301. } else {
  1302. /* protocol violation */
  1303. DEBUG_TRACE("HTTP2 invalid index %lu", (unsigned long)idx);
  1304. goto clean_http2;
  1305. }
  1306. /* key is allocated now and must be freed later */
  1307. /* Get header value */
  1308. if (value_known) {
  1309. /* Server must already know the value */
  1310. if (idx <= 61) {
  1311. if (hpack_predefined[idx].value) {
  1312. val = mg_strdup_ctx(hpack_predefined[idx].value,
  1313. conn->phys_ctx); /* leak? */
  1314. CHECK_LEAK_HDR_ALLOC(val);
  1315. } else {
  1316. /* protocol violation */
  1317. DEBUG_TRACE("HTTP2 indexed header %lu has no value "
  1318. "(key: %s)",
  1319. (unsigned long)idx,
  1320. key);
  1321. CHECK_LEAK_HDR_FREE(key);
  1322. mg_free((void *)key);
  1323. goto clean_http2;
  1324. }
  1325. } else if ((idx >= 62)
  1326. && ((idx - 61) <= conn->http2.dyn_table_size)) {
  1327. uint32_t local_table_idx = (uint32_t)idx - 62;
  1328. val = mg_strdup_ctx(
  1329. conn->http2.dyn_table[local_table_idx].value,
  1330. conn->phys_ctx);
  1331. CHECK_LEAK_HDR_ALLOC(val);
  1332. } else {
  1333. /* protocol violation */
  1334. DEBUG_TRACE(
  1335. "HTTP2 indexed header %lu out of range (key: %s)",
  1336. (unsigned long)idx,
  1337. key);
  1338. CHECK_LEAK_HDR_FREE(key);
  1339. mg_free((void *)key);
  1340. goto clean_http2;
  1341. }
  1342. } else {
  1343. /* Read value from HTTP2 stream */
  1344. val = hpack_decode(buf, &i, conn->phys_ctx); /* leak? */
  1345. CHECK_LEAK_HDR_ALLOC(val);
  1346. if (indexing) {
  1347. /* Add to index */
  1348. if (conn->http2.dyn_table_size
  1349. >= HTTP2_DYN_TABLE_SIZE) {
  1350. /* Too many elements */
  1351. DEBUG_TRACE("HTTP2 index table is full (key: %s, "
  1352. "value: %s)",
  1353. key,
  1354. val);
  1355. CHECK_LEAK_HDR_FREE(key);
  1356. CHECK_LEAK_HDR_FREE(val);
  1357. mg_free((void *)key);
  1358. mg_free((void *)val);
  1359. goto clean_http2;
  1360. }
  1361. /* Add to table of dynamic headers */
  1362. conn->http2.dyn_table[conn->http2.dyn_table_size].name =
  1363. mg_strdup_ctx(key, conn->phys_ctx); /* leak */
  1364. conn->http2.dyn_table[conn->http2.dyn_table_size]
  1365. .value =
  1366. mg_strdup_ctx(val, conn->phys_ctx); /* leak */
  1367. CHECK_LEAK_DYN_ALLOC(
  1368. conn->http2.dyn_table[conn->http2.dyn_table_size]
  1369. .name);
  1370. CHECK_LEAK_DYN_ALLOC(
  1371. conn->http2.dyn_table[conn->http2.dyn_table_size]
  1372. .value);
  1373. conn->http2.dyn_table_size++;
  1374. DEBUG_TRACE("HTTP2 new dynamic header table entry %i "
  1375. "(key: %s, value: %s)",
  1376. (int)conn->http2.dyn_table_size,
  1377. key,
  1378. val);
  1379. }
  1380. }
  1381. /* val and key are allocated now and must be freed later */
  1382. /* Store these pointers in conn->request_info[].http_headers,
  1383. * free_buffered_header_list(conn) will clean up later. */
  1384. /* Add header for this request */
  1385. if ((key != NULL) && (val != NULL)
  1386. && (conn->request_info.num_headers < MG_MAX_HEADERS)) {
  1387. conn->request_info
  1388. .http_headers[conn->request_info.num_headers]
  1389. .name = key;
  1390. conn->request_info
  1391. .http_headers[conn->request_info.num_headers]
  1392. .value = val;
  1393. conn->request_info.num_headers++;
  1394. /* Some headers need to be stored in the request structure
  1395. */
  1396. if (!strcmp(":method", key)) {
  1397. conn->request_info.request_method = val;
  1398. } else if (!strcmp(":path", key)) {
  1399. conn->request_info.local_uri = val;
  1400. conn->request_info.request_uri = val;
  1401. } else if (!strcmp(":status", key)) {
  1402. conn->status_code = atoi(val);
  1403. }
  1404. DEBUG_TRACE("HTTP2 request header (key: %s, value: %s)",
  1405. key,
  1406. val);
  1407. } else {
  1408. /* - either key or value are NULL (out of memory)
  1409. * - or the max. number of headers is reached
  1410. * in both cases free all memory
  1411. */
  1412. DEBUG_TRACE("%s", "HTTP2 cannot add header");
  1413. CHECK_LEAK_HDR_FREE(key);
  1414. CHECK_LEAK_HDR_FREE(val);
  1415. mg_free((void *)key);
  1416. key = NULL;
  1417. mg_free((void *)val);
  1418. val = NULL;
  1419. }
  1420. }
  1421. /* stream id */
  1422. conn->http2.stream_id = http2_frame_stream_id;
  1423. /* header parsed */
  1424. DEBUG_TRACE("HTTP2 handle_request (stream %u)",
  1425. http2_frame_stream_id);
  1426. handle_request(conn);
  1427. /* Send "final" frame */
  1428. DEBUG_TRACE("HTTP2 handle_request done (stream %u)",
  1429. http2_frame_stream_id);
  1430. http2_data_frame_head(conn, 0, 1);
  1431. free_buffered_response_header_list(conn);
  1432. free_buffered_request_header_list(conn);
  1433. } break;
  1434. case 2: /* PRIORITY */
  1435. {
  1436. uint32_t dependStream =
  1437. ((uint32_t)buf[0] * 0x1000000u) + ((uint32_t)buf[1] * 0x10000u)
  1438. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1439. uint8_t weight = buf[4];
  1440. DEBUG_TRACE("HTTP2 priority %u dependent stream %u",
  1441. weight,
  1442. dependStream);
  1443. } break;
  1444. case 3: /* RST_STREAM */
  1445. {
  1446. uint32_t errorId =
  1447. ((uint32_t)buf[0] * 0x1000000u) + ((uint32_t)buf[1] * 0x10000u)
  1448. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1449. DEBUG_TRACE("HTTP2 reset with error %u", errorId);
  1450. } break;
  1451. case 4: /* SETTINGS */
  1452. if (http2_frame_stream_id != 0) {
  1453. /* Send protocol error */
  1454. http2_reset_stream(conn,
  1455. http2_frame_stream_id,
  1456. HTTP2_ERR_PROTOCOL_ERROR);
  1457. DEBUG_TRACE("%s", "HTTP2 received invalid settings frame");
  1458. } else if (http2_frame_flags) {
  1459. /* ACK frame. Do not reply. */
  1460. my_settings_accepted++;
  1461. DEBUG_TRACE("%s", "CivetWeb settings confirmed by peer");
  1462. } else {
  1463. int i;
  1464. for (i = 0; i < (int)http2_frame_size; i += 6) {
  1465. uint16_t id =
  1466. ((uint16_t)buf[i] * 0x100u) + ((uint16_t)buf[i + 1]);
  1467. uint32_t val = ((uint32_t)buf[i + 2] * 0x1000000u)
  1468. + ((uint32_t)buf[i + 3] * 0x10000u)
  1469. + ((uint32_t)buf[i + 4] * 0x100u)
  1470. + ((uint32_t)buf[i + 5]);
  1471. switch (id) {
  1472. case 1:
  1473. client_settings.settings_header_table_size = val;
  1474. DEBUG_TRACE("Received settings header_table_size: %u",
  1475. val);
  1476. break;
  1477. case 2:
  1478. client_settings.settings_enable_push = (val != 0);
  1479. DEBUG_TRACE("Received settings enable_push: %u", val);
  1480. break;
  1481. case 3:
  1482. client_settings.settings_max_concurrent_streams = val;
  1483. DEBUG_TRACE(
  1484. "Received settings max_concurrent_streams: %u",
  1485. val);
  1486. break;
  1487. case 4:
  1488. client_settings.settings_initial_window_size = val;
  1489. DEBUG_TRACE("Received settings initial_window_size: %u",
  1490. val);
  1491. break;
  1492. case 5:
  1493. client_settings.settings_max_frame_size = val;
  1494. DEBUG_TRACE("Received settings max_frame_size: %u",
  1495. val);
  1496. break;
  1497. case 6:
  1498. client_settings.settings_max_header_list_size = val;
  1499. DEBUG_TRACE(
  1500. "Received settings max_header_list_size: %u", val);
  1501. break;
  1502. default:
  1503. /* Unknown setting. Ignore it. */
  1504. DEBUG_TRACE("Received unknown settings id=%u: %u",
  1505. id,
  1506. val);
  1507. break;
  1508. }
  1509. }
  1510. /* Every settings frame must be acknowledged */
  1511. http2_settings_acknowledge(conn);
  1512. }
  1513. break;
  1514. case 5: /* PUSH_PROMISE */
  1515. DEBUG_TRACE("%s", "Push promise not supported");
  1516. break;
  1517. case 6: /* PING */
  1518. if (http2_frame_flags == 0) {
  1519. /* Set "reply" flag, and send same data back */
  1520. DEBUG_TRACE("%s", "Replying to ping");
  1521. http2_frame_head[4] = 1;
  1522. mg_xwrite(conn, http2_frame_head, sizeof(http2_frame_head));
  1523. mg_xwrite(conn, buf, http2_frame_size);
  1524. }
  1525. break;
  1526. case 7: /* GOAWAY */
  1527. {
  1528. uint32_t lastStream =
  1529. ((uint32_t)buf[0] * 0x1000000u) + ((uint32_t)buf[1] * 0x10000u)
  1530. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1531. uint32_t errorId =
  1532. ((uint32_t)buf[4] * 0x1000000u) + ((uint32_t)buf[5] * 0x10000u)
  1533. + ((uint32_t)buf[6] * 0x100u) + ((uint32_t)buf[7]);
  1534. ; /* followed by debug data */
  1535. uint32_t debugDataLen = http2_frame_size - 8;
  1536. char *debugData = (char *)buf + 8;
  1537. DEBUG_TRACE("HTTP2 goaway stream %u, error %u (%.*s)",
  1538. lastStream,
  1539. errorId,
  1540. debugDataLen,
  1541. debugData);
  1542. } break;
  1543. case 8: /* WINDOW_UPDATE */
  1544. {
  1545. uint32_t val = ((uint32_t)buf[0] * 0x1000000u)
  1546. + ((uint32_t)buf[1] * 0x10000u)
  1547. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1548. http_window_length = (val & 0x7FFFFFFFu);
  1549. DEBUG_TRACE("HTTP2 window update stream %u, length %u",
  1550. http2_frame_stream_id,
  1551. http_window_length);
  1552. } break;
  1553. case 9: /* CONTINUATION */
  1554. DEBUG_TRACE("%s", "HTTP2 Continue");
  1555. break;
  1556. default:
  1557. /* TODO: Error Message */
  1558. DEBUG_TRACE("%s", "Unknown frame type");
  1559. goto clean_http2;
  1560. }
  1561. }
  1562. clean_http2:
  1563. DEBUG_TRACE("%s", "HTTP2 free buffer, connection handler finished");
  1564. mg_free(buf);
  1565. }
  1566. #if 0
  1567. static void
  1568. HPACK_TEST()
  1569. {
  1570. uint64_t test;
  1571. for (test = 0;; test++) {
  1572. char in[32] = {0};
  1573. uint8_t out[32] = {0};
  1574. char *check;
  1575. int i;
  1576. int l;
  1577. memcpy(in, &test, sizeof(test));
  1578. l = hpack_encode(out, in, 0);
  1579. i = 0;
  1580. check = hpack_decode(out, &i, NULL);
  1581. if (strcmp(in, check)) {
  1582. printf("Error\n");
  1583. }
  1584. mg_free(check);
  1585. }
  1586. }
  1587. static void
  1588. HPACK_TABLE_TEST()
  1589. {
  1590. int i;
  1591. uint32_t hpack_huff_end_code_expected[32] = { 0 };
  1592. uint8_t hpack_huff_start_index_expected[32] = { 0 };
  1593. int reverse_map[256] = { 0 };
  1594. for (i = 0; i < 256; i++) {
  1595. reverse_map[i] = -1;
  1596. }
  1597. for (i = 0; i < 256; i++) {
  1598. uint8_t bits = hpack_huff_dec[i].bitcount;
  1599. uint8_t dec = hpack_huff_dec[i].decoded;
  1600. if (bits > hpack_huff_dec[i + 1].bitcount) {
  1601. ck_abort_msg("hpack_huff_dec disorder at index %i", i);
  1602. }
  1603. if (hpack_huff_dec[i].encoded & (0xFFFFFFFFul << bits)) {
  1604. ck_abort_msg("hpack_huff_dec bits inconsistent at index %i", i);
  1605. }
  1606. if ((bits < 5) || (bits > 30)) {
  1607. ck_abort_msg("hpack_huff_dec bits out of range at index %i", i);
  1608. }
  1609. if (reverse_map[dec] != -1) {
  1610. ck_abort_msg("hpack_huff_dec duplicate: %i", hpack_huff_dec[i].decoded);
  1611. }
  1612. reverse_map[dec] = i;
  1613. hpack_huff_end_code_expected[bits - 5] = hpack_huff_dec[i].encoded;
  1614. }
  1615. for (i = 255; i >= 0; i--) {
  1616. uint8_t bits = hpack_huff_dec[i].bitcount;
  1617. hpack_huff_start_index_expected[bits - 5] = i;
  1618. }
  1619. for (i = 0; i < 256; i++) {
  1620. if (reverse_map[i] == -1) {
  1621. ck_abort_msg("reverse map at %i mising", i);
  1622. }
  1623. }
  1624. i = sizeof(hpack_huff_start_index) / sizeof(hpack_huff_start_index[0]);
  1625. if (i != 27) {
  1626. ck_abort_msg("hpack_huff_start_index size error: ", i);
  1627. }
  1628. i = sizeof(hpack_huff_end_code) / sizeof(hpack_huff_end_code[0]);
  1629. if (i != 27) {
  1630. ck_abort_msg("hpack_huff_end_code size error: ", i);
  1631. }
  1632. for (i = 0; i < 27; i++) {
  1633. if (hpack_huff_start_index_expected[i] != hpack_huff_start_index[i]) {
  1634. ck_abort_msg("hpack_huff_start_index error at %i", i);
  1635. }
  1636. if (hpack_huff_end_code_expected[i] != hpack_huff_end_code[i]) {
  1637. ck_abort_msg("hpack_huff_end_code error at %i", i);
  1638. }
  1639. }
  1640. }
  1641. #endif
  1642. static void
  1643. process_new_http2_connection(struct mg_connection *conn)
  1644. {
  1645. if (!is_valid_http2_primer(conn)) {
  1646. /* Primer does not match expectation from RFC.
  1647. * See https://tools.ietf.org/html/rfc7540#section-3.5 */
  1648. DEBUG_TRACE("%s", "No valid HTTP2 primer");
  1649. mg_send_http_error(conn, 400, "%s", "Invalid HTTP/2 primer");
  1650. } else {
  1651. /* Valid HTTP/2 primer received */
  1652. DEBUG_TRACE("%s", "Start handling HTTP2");
  1653. handle_http2(conn);
  1654. /* Free memory allocated for headers, if not done yet */
  1655. DEBUG_TRACE("%s", "Free remaining HTTP2 header memory");
  1656. free_buffered_response_header_list(conn);
  1657. free_buffered_request_header_list(conn);
  1658. purge_dynamic_header_table(conn, 0);
  1659. }
  1660. }