http2.inl 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  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, int max_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. /* check size */
  656. if ((*i) + byte_len > max_i) {
  657. return NULL;
  658. }
  659. /* Now read the string */
  660. if (!is_huff) {
  661. /* Not huffman encoded: Copy directly */
  662. char *result = (char *)mg_malloc_ctx(byte_len + 1, ctx);
  663. if (result) {
  664. memcpy(result, buf + (*i), byte_len);
  665. result[byte_len] = 0;
  666. }
  667. (*i) += byte_len;
  668. return result;
  669. } else {
  670. /* Huffman encoded: need to decode bitwise */
  671. const uint8_t *pData =
  672. buf + (*i); /* begin pointer of bit input string */
  673. int bitRead = 0; /* number of encoded bits read */
  674. uint32_t bytesStored = 0; /* number of decoded bytes stored */
  675. uint8_t str[2048]; /* storage buffer for decoded string */
  676. for (;;) {
  677. uint32_t accu = 0; /* accu register: collect bits */
  678. uint8_t bc = 0; /* number of bits collected */
  679. int n;
  680. /* Collect bits in this loop, until we have a valid huff code in
  681. * accu */
  682. do {
  683. accu <<= 1;
  684. accu |= (pData[bitRead / 8] >> (7 - (bitRead & 7))) & 1;
  685. bitRead++;
  686. bc++;
  687. if (bitRead > bit_len) {
  688. /* We used all bits. Return the decoded string. */
  689. str[bytesStored] = 0; /* Terminate string */
  690. (*i) += byte_len; /* Advance parsing index */
  691. return mg_strdup_ctx((char *)str,
  692. ctx); /* Return a string copy */
  693. }
  694. } while ((bc < 5) || (accu > hpack_huff_end_code[bc - 5]));
  695. /* Find matching code in huffman encoding table */
  696. for (n = hpack_huff_start_index[bc - 5]; n < 256; n++) {
  697. if (accu == hpack_huff_dec[n].encoded) {
  698. str[bytesStored] = hpack_huff_dec[n].decoded;
  699. bytesStored++;
  700. break;
  701. }
  702. }
  703. if (bytesStored == sizeof(str)) {
  704. /* too long */
  705. return 0;
  706. }
  707. }
  708. }
  709. }
  710. static void
  711. append_bits(uint8_t *target,
  712. uint32_t offset,
  713. uint32_t value,
  714. uint8_t value_bits)
  715. {
  716. uint32_t offset_bytes = offset / 8;
  717. uint32_t offset_bits = offset % 8;
  718. uint32_t remaining_bits, ac;
  719. value &= ~(0xFFFFFFFF << value_bits);
  720. remaining_bits = 8 - offset_bits;
  721. if (value_bits <= remaining_bits) {
  722. ac = value << (remaining_bits - value_bits);
  723. target[offset_bytes] |= ac;
  724. return;
  725. }
  726. ac = value >> (value_bits - remaining_bits);
  727. target[offset_bytes] |= ac;
  728. append_bits(target,
  729. offset + remaining_bits,
  730. value,
  731. value_bits - remaining_bits);
  732. }
  733. static int
  734. hpack_encode(uint8_t *store, const char *load, int lower)
  735. {
  736. uint32_t nohuff_len = strlen(load);
  737. uint32_t len_bits = 0;
  738. uint32_t len_bytes;
  739. uint32_t spare_bits;
  740. uint32_t i;
  741. memset(store, 0, nohuff_len + 1);
  742. for (i = 0; i < nohuff_len; i++) {
  743. uint8_t b = (uint8_t)((char)(lower ? tolower(load[i]) : load[i]));
  744. int idx;
  745. for (idx = 0; idx <= 255; idx++) {
  746. if (hpack_huff_dec[idx].decoded == b) {
  747. append_bits((uint8_t *)store + 1,
  748. len_bits,
  749. hpack_huff_dec[idx].encoded,
  750. hpack_huff_dec[idx].bitcount);
  751. len_bits += hpack_huff_dec[idx].bitcount;
  752. break;
  753. }
  754. }
  755. }
  756. len_bytes = (len_bits + 7) / 8;
  757. spare_bits = len_bytes * 8 - len_bits;
  758. if (spare_bits) {
  759. append_bits((uint8_t *)store + 1, len_bits, 0xFFFFFFFF, spare_bits);
  760. }
  761. if (len_bytes >= 127) {
  762. // TODO: Shift string and encode len in more bytes
  763. return 0;
  764. }
  765. *store = 0x80 + (uint8_t)len_bytes;
  766. if ((len_bytes >= nohuff_len) && (0)) {
  767. *store = (uint8_t)nohuff_len;
  768. if (lower) {
  769. for (i = 1; i <= nohuff_len; i++) {
  770. store[i] = tolower(load[i]);
  771. }
  772. } else {
  773. memcpy(store + 1, load, nohuff_len);
  774. }
  775. return nohuff_len + 1;
  776. } else {
  777. /*
  778. int i = 0;
  779. char *test = hpack_decode(store, &i, NULL);
  780. i = i; // breakpoint for debugging / testing
  781. */
  782. }
  783. return len_bytes + 1;
  784. }
  785. /***********************************************************************/
  786. /*** HTTP 2 ***/
  787. /***********************************************************************/
  788. static const char http2_pri[] = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
  789. static const unsigned char http2_pri_len = 24; /* = strlen(http2_pri) */
  790. /* Read and check the HTTP/2 primer/preface:
  791. * See https://tools.ietf.org/html/rfc7540#section-3.5 */
  792. static int
  793. is_valid_http2_primer(struct mg_connection *conn)
  794. {
  795. size_t pri_len = http2_pri_len;
  796. char buf[32]; /* Buffer must hold 24 bytes primer */
  797. int read_pri_len = mg_read(conn, buf, pri_len);
  798. if (read_pri_len != (int)pri_len) {
  799. /* Size does not match.
  800. * This includes cases where mg_read returns error codes */
  801. return 0;
  802. }
  803. if (0 != memcmp(buf, http2_pri, pri_len)) {
  804. /* Primer does not match */
  805. return 0;
  806. }
  807. /* Primer does match */
  808. return 1;
  809. }
  810. #define mg_xwrite(conn, data, len) \
  811. push_all((conn)->phys_ctx, \
  812. NULL, \
  813. (conn)->client.sock, \
  814. (conn)->ssl, \
  815. (const char *)(data), \
  816. (int)(len))
  817. static void
  818. http2_settings_acknowledge(struct mg_connection *conn)
  819. {
  820. unsigned char http2_set_ackn_frame[9] = {0, 0, 0, 4, 1, 0, 0, 0, 0};
  821. DEBUG_TRACE("%s", "Sending settings frame");
  822. mg_xwrite(conn, http2_set_ackn_frame, 9);
  823. }
  824. struct http2_settings {
  825. uint32_t settings_header_table_size;
  826. uint32_t settings_enable_push;
  827. uint32_t settings_max_concurrent_streams;
  828. uint32_t settings_initial_window_size;
  829. uint32_t settings_max_frame_size;
  830. uint32_t settings_max_header_list_size;
  831. };
  832. const struct http2_settings http2_default_settings =
  833. {4096, 1, UINT32_MAX, 65535, 16384, UINT32_MAX};
  834. const struct http2_settings http2_civetweb_server_settings =
  835. {4096, 0, 100, 65535, 16384, 65535};
  836. enum {
  837. HTTP2_ERR_NO_ERROR = 0,
  838. HTTP2_ERR_PROTOCOL_ERROR,
  839. HTTP2_ERR_INTERNAL_ERROR,
  840. HTTP2_ERR_FLOW_CONTROL_ERROR,
  841. HTTP2_ERR_SETTINGS_TIMEOUT,
  842. HTTP2_ERR_STREAM_CLOSED,
  843. HTTP2_ERR_FRAME_SIZE_ERROR,
  844. HTTP2_ERR_REFUSED_STREAM,
  845. HTTP2_ERR_CANCEL,
  846. HTTP2_ERR_COMPRESSION_ERROR,
  847. HTTP2_ERR_CONNECT_ERROR,
  848. HTTP2_ERR_ENHANCE_YOUR_CALM,
  849. HTTP2_ERR_INADEQUATE_SECURITY,
  850. HTTP2_ERR_HTTP_1_1_REQUIRED
  851. };
  852. static void
  853. http2_send_settings(struct mg_connection *conn,
  854. const struct http2_settings *set)
  855. {
  856. uint16_t id;
  857. uint32_t data;
  858. uint8_t http2_settings_frame[9] = {0, 0, 36, 4, 0, 0, 0, 0, 0};
  859. mg_xwrite(conn, http2_settings_frame, 9);
  860. id = htons(1);
  861. data = htonl(set->settings_header_table_size);
  862. mg_xwrite(conn, &id, 2);
  863. mg_xwrite(conn, &data, 4);
  864. id = htons(1);
  865. data = htonl(set->settings_enable_push);
  866. mg_xwrite(conn, &id, 2);
  867. mg_xwrite(conn, &data, 4);
  868. id = htons(1);
  869. data = htonl(set->settings_max_concurrent_streams);
  870. mg_xwrite(conn, &id, 2);
  871. mg_xwrite(conn, &data, 4);
  872. id = htons(1);
  873. data = htonl(set->settings_initial_window_size);
  874. mg_xwrite(conn, &id, 2);
  875. mg_xwrite(conn, &data, 4);
  876. id = htons(1);
  877. data = htonl(set->settings_max_frame_size);
  878. mg_xwrite(conn, &id, 2);
  879. mg_xwrite(conn, &data, 4);
  880. id = htons(1);
  881. data = htonl(set->settings_max_header_list_size);
  882. mg_xwrite(conn, &id, 2);
  883. mg_xwrite(conn, &data, 4);
  884. DEBUG_TRACE("%s", "HTTP2 settings sent");
  885. }
  886. static int
  887. http2_send_response_headers(struct mg_connection *conn)
  888. {
  889. unsigned char http2_header_frame[9] = {0, 0, 0, 1, 4, 0, 0, 0, 0};
  890. uint8_t header_bin[1024];
  891. uint16_t header_len = 0;
  892. int has_date = 0;
  893. int has_connection_header = 0;
  894. int i, ok;
  895. if ((conn->status_code < 100) || (conn->status_code > 999)) {
  896. /* Invalid status: Set status to "Internal Server Error" */
  897. conn->status_code = 500;
  898. }
  899. switch (conn->status_code) {
  900. case 200:
  901. header_bin[header_len++] = 0x88;
  902. break;
  903. case 204:
  904. header_bin[header_len++] = 0x89;
  905. break;
  906. case 206:
  907. header_bin[header_len++] = 0x8A;
  908. break;
  909. case 304:
  910. header_bin[header_len++] = 0x8B;
  911. break;
  912. case 400:
  913. header_bin[header_len++] = 0x8C;
  914. break;
  915. case 404:
  916. header_bin[header_len++] = 0x8D;
  917. break;
  918. case 500:
  919. header_bin[header_len++] = 0x8E;
  920. break;
  921. default:
  922. header_bin[header_len++] = 0x48;
  923. header_bin[header_len++] = 0x03;
  924. header_bin[header_len++] = 0x30 + (conn->status_code / 100);
  925. header_bin[header_len++] = 0x30 + ((conn->status_code / 10) % 10);
  926. header_bin[header_len++] = 0x30 + (conn->status_code % 10);
  927. break;
  928. }
  929. /* Add all headers */
  930. for (i = 0; i < conn->response_info.num_headers; i++) {
  931. uint16_t predef = 0;
  932. uint16_t j;
  933. /* Filter headers not valid in HTTP/2 */
  934. if (!mg_strcasecmp("Connection",
  935. conn->response_info.http_headers[i].name)) {
  936. has_connection_header = 1;
  937. continue; /* do not send */
  938. }
  939. /* Check if this header is known in HPACK (static table index 15 to 61)
  940. * see https://tools.ietf.org/html/rfc7541#appendix-A */
  941. for (j = 15; j <= 61; j++) {
  942. if (!mg_strcasecmp(hpack_predefined[j].name,
  943. conn->response_info.http_headers[i].name)) {
  944. predef = j;
  945. break;
  946. }
  947. }
  948. if (predef) {
  949. /* Predefined header found */
  950. header_bin[header_len++] = 0x40 + predef;
  951. } else {
  952. /* Rare header, do not index */
  953. header_bin[header_len++] = 0x10;
  954. j = hpack_encode(header_bin + header_len,
  955. conn->response_info.http_headers[i].name,
  956. 1);
  957. header_len += j;
  958. }
  959. j = hpack_encode(header_bin + header_len,
  960. conn->response_info.http_headers[i].value,
  961. 0);
  962. header_len += j;
  963. /* Mark required headers as sent */
  964. if (!mg_strcasecmp("Date", conn->response_info.http_headers[i].name)) {
  965. has_date = 1;
  966. }
  967. }
  968. /* Add required headers, if they have not been sent yet */
  969. if (!has_date) {
  970. /* Create header frame */
  971. char date[64];
  972. uint8_t date_len;
  973. time_t curtime = time(NULL);
  974. gmt_time_string(date, sizeof(date), &curtime);
  975. date_len = (uint8_t)strlen(date);
  976. header_bin[header_len++] =
  977. 0x61; /* "Date" predefined HPACK index 33 (0x21) + 0x40 */
  978. header_bin[header_len++] = date_len;
  979. memcpy(header_bin + header_len, date, date_len);
  980. header_len += date_len;
  981. }
  982. http2_header_frame[1] = (header_len & 0xFF00) >> 8;
  983. http2_header_frame[2] = (header_len & 0xFF);
  984. http2_header_frame[5] = (conn->http2.stream_id & 0xFF000000u) >> 24;
  985. http2_header_frame[6] = (conn->http2.stream_id & 0xFF0000u) >> 16;
  986. http2_header_frame[7] = (conn->http2.stream_id & 0xFF00u) >> 8;
  987. http2_header_frame[8] = (conn->http2.stream_id & 0xFFu);
  988. /* Send header frame */
  989. ok = 1;
  990. if (mg_xwrite(conn, http2_header_frame, 9) != 9) {
  991. ok = 0;
  992. } else if (mg_xwrite(conn, header_bin, header_len) != header_len) {
  993. ok = 0;
  994. }
  995. if (ok) {
  996. DEBUG_TRACE("HTTP2 response header sent: stream %u",
  997. conn->http2.stream_id);
  998. } else {
  999. DEBUG_TRACE("HTTP2 response header sending error: stream %u",
  1000. conn->http2.stream_id);
  1001. }
  1002. (void)has_connection_header; /* ignore for the moment */
  1003. return ok;
  1004. }
  1005. static void
  1006. http2_data_frame_head(struct mg_connection *conn,
  1007. uint32_t frame_size,
  1008. int is_final)
  1009. {
  1010. unsigned char http2_data_frame[9];
  1011. uint32_t stream_id = conn->http2.stream_id;
  1012. http2_data_frame[0] = (frame_size & 0xFF0000) >> 16;
  1013. http2_data_frame[1] = (frame_size & 0xFF00) >> 8;
  1014. http2_data_frame[2] = (frame_size & 0xFF);
  1015. http2_data_frame[3] = 0; /* frame type "DATA" */
  1016. http2_data_frame[4] = (is_final ? 1 : 0);
  1017. http2_data_frame[5] = (stream_id & 0xFF000000u) >> 24;
  1018. http2_data_frame[6] = (stream_id & 0xFF0000u) >> 16;
  1019. http2_data_frame[7] = (stream_id & 0xFF00u) >> 8;
  1020. http2_data_frame[8] = (stream_id & 0xFFu);
  1021. DEBUG_TRACE("HTTP2 begin data frame: stream %u, frame_size %u (final: %i)",
  1022. stream_id,
  1023. frame_size,
  1024. is_final);
  1025. mg_xwrite(conn, http2_data_frame, 9);
  1026. }
  1027. static void
  1028. http2_send_window(struct mg_connection *conn,
  1029. uint32_t stream_id,
  1030. uint32_t window_size)
  1031. {
  1032. unsigned char http2_window_frame[9] = {0, 0, 4, 8, 0, 0, 0, 0, 0};
  1033. uint32_t data = htonl(window_size);
  1034. DEBUG_TRACE("HTTP2 send window_size: stream %u, error %u",
  1035. stream_id,
  1036. window_size);
  1037. http2_window_frame[5] = (stream_id & 0xFF000000u) >> 24;
  1038. http2_window_frame[6] = (stream_id & 0xFF0000u) >> 16;
  1039. http2_window_frame[7] = (stream_id & 0xFF00u) >> 8;
  1040. http2_window_frame[8] = (stream_id & 0xFFu);
  1041. mg_xwrite(conn, http2_window_frame, 9);
  1042. mg_xwrite(conn, &data, 4);
  1043. }
  1044. static void
  1045. http2_reset_stream(struct mg_connection *conn,
  1046. uint32_t stream_id,
  1047. uint32_t error_id)
  1048. {
  1049. unsigned char http2_reset_frame[9] = {0, 0, 4, 3, 0, 0, 0, 0, 0};
  1050. uint32_t val = htonl(error_id);
  1051. DEBUG_TRACE("HTTP2 send reset: stream %u, error %u", stream_id, error_id);
  1052. http2_reset_frame[5] = (stream_id & 0xFF000000u) >> 24;
  1053. http2_reset_frame[6] = (stream_id & 0xFF0000u) >> 16;
  1054. http2_reset_frame[7] = (stream_id & 0xFF00u) >> 8;
  1055. http2_reset_frame[8] = (stream_id & 0xFFu);
  1056. mg_xwrite(conn, http2_reset_frame, 9);
  1057. mg_xwrite(conn, &val, 4);
  1058. }
  1059. static void
  1060. http2_must_use_http1(struct mg_connection *conn)
  1061. {
  1062. DEBUG_TRACE("HTTP2 not available for this URL (%s)", conn->path_info);
  1063. http2_reset_stream(conn, conn->http2.stream_id, 0xd);
  1064. }
  1065. /* The HTTP2 implementation collects request headers as array of dynamically
  1066. * allocated string values. This array must be freed once the request is
  1067. * handled.
  1068. * This is different to the HTTP/1.x implementation: For HTTP/1.x, the header
  1069. * list is implemented as pointers into an existing buffer, so free must not
  1070. * be called for HTTP/1.x.
  1071. * Thus free_buffered_request_header_list is in mod_http2.inl.
  1072. */
  1073. #if defined(DEBUG)
  1074. static int mem_h_count = 0;
  1075. static int mem_d_count = 0;
  1076. #define CHECK_LEAK_HDR_ALLOC(ptr) \
  1077. DEBUG_TRACE("H NEW %p (%i): %s", ptr, ++mem_h_count, (const char *)ptr)
  1078. #define CHECK_LEAK_HDR_FREE(ptr) \
  1079. DEBUG_TRACE("H DEL %p (%i): %s", ptr, --mem_h_count, (const char *)ptr)
  1080. #define CHECK_LEAK_DYN_ALLOC(ptr) \
  1081. DEBUG_TRACE("D NEW %p (%i): %s", ptr, ++mem_d_count, (const char *)ptr)
  1082. #define CHECK_LEAK_DYN_FREE(ptr) \
  1083. DEBUG_TRACE("D DEL %p (%i): %s", ptr, --mem_d_count, (const char *)ptr)
  1084. #else
  1085. #define CHECK_LEAK_HDR_ALLOC(ptr)
  1086. #define CHECK_LEAK_HDR_FREE(ptr)
  1087. #define CHECK_LEAK_DYN_ALLOC(ptr)
  1088. #define CHECK_LEAK_DYN_FREE(ptr)
  1089. #endif
  1090. /* The dynamic header table may be resized on a HTTP2 client request.
  1091. * A tablesize=0 will free all memory.
  1092. */
  1093. static void
  1094. purge_dynamic_header_table(struct mg_connection *conn, uint32_t tableSize)
  1095. {
  1096. DEBUG_TRACE("HTTP2 dynamic header table set to %u", tableSize);
  1097. while (conn->http2.dyn_table_size > tableSize) {
  1098. conn->http2.dyn_table_size--;
  1099. CHECK_LEAK_DYN_FREE(
  1100. conn->http2.dyn_table[conn->http2.dyn_table_size].name);
  1101. CHECK_LEAK_DYN_FREE(
  1102. conn->http2.dyn_table[conn->http2.dyn_table_size].value);
  1103. mg_free((void *)conn->http2.dyn_table[conn->http2.dyn_table_size].name);
  1104. conn->http2.dyn_table[conn->http2.dyn_table_size].name = 0;
  1105. mg_free(
  1106. (void *)conn->http2.dyn_table[conn->http2.dyn_table_size].value);
  1107. conn->http2.dyn_table[conn->http2.dyn_table_size].value = 0;
  1108. }
  1109. }
  1110. /* Internal function to free request header list.
  1111. * Not to be confused with the response header list.
  1112. */
  1113. static void
  1114. free_buffered_request_header_list(struct mg_connection *conn)
  1115. {
  1116. while (conn->request_info.num_headers > 0) {
  1117. conn->request_info.num_headers--;
  1118. CHECK_LEAK_HDR_FREE(
  1119. conn->request_info.http_headers[conn->request_info.num_headers]
  1120. .name);
  1121. CHECK_LEAK_HDR_FREE(
  1122. conn->request_info.http_headers[conn->request_info.num_headers]
  1123. .value);
  1124. mg_free((void *)conn->request_info
  1125. .http_headers[conn->request_info.num_headers]
  1126. .name);
  1127. conn->request_info.http_headers[conn->request_info.num_headers].name =
  1128. 0;
  1129. mg_free((void *)conn->request_info
  1130. .http_headers[conn->request_info.num_headers]
  1131. .value);
  1132. conn->request_info.http_headers[conn->request_info.num_headers].value =
  1133. 0;
  1134. }
  1135. }
  1136. /* HTTP2 requires a different handling loop */
  1137. static void
  1138. handle_http2(struct mg_connection *conn)
  1139. {
  1140. unsigned char http2_frame_head[9];
  1141. uint32_t http2_frame_size;
  1142. uint8_t http2_frame_type;
  1143. uint8_t http2_frame_flags;
  1144. uint32_t http2_frame_stream_id;
  1145. uint32_t http_window_length = 0;
  1146. int bytes_read;
  1147. uint8_t *buf;
  1148. int my_settings_accepted = 0;
  1149. const char *my_hpack_headers[128];
  1150. struct http2_settings client_settings = http2_default_settings;
  1151. struct http2_settings server_settings = http2_default_settings;
  1152. /* Send own settings */
  1153. http2_send_settings(conn, &http2_civetweb_server_settings);
  1154. // http2_send_window(conn, 0, /* 0x3fff0001 */ 1024*1024);
  1155. /* initialize hpack header table with predefined header fields */
  1156. memset((void *)my_hpack_headers, 0, sizeof(my_hpack_headers));
  1157. memcpy((void *)my_hpack_headers,
  1158. hpack_predefined,
  1159. sizeof(hpack_predefined));
  1160. buf = (uint8_t *)mg_malloc_ctx(server_settings.settings_max_frame_size,
  1161. conn->phys_ctx);
  1162. if (!buf) {
  1163. /* Out of memory */
  1164. DEBUG_TRACE("%s", "Out of memory for HTTP2 frame");
  1165. return;
  1166. }
  1167. for (;;) {
  1168. /* HTTP/2 is handled frame by frame */
  1169. int frame_is_end_stream = 0;
  1170. int frame_is_end_headers = 0;
  1171. int frame_is_padded = 0;
  1172. int frame_is_priority = 0;
  1173. #if defined(USE_SERVER_STATS)
  1174. conn->conn_state = 3; /* HTTP/2 ready */
  1175. #endif
  1176. bytes_read = mg_read(conn, http2_frame_head, sizeof(http2_frame_head));
  1177. if (bytes_read != sizeof(http2_frame_head)) {
  1178. /* TODO: errormsg */
  1179. goto clean_http2;
  1180. }
  1181. /* Extract data from frame header */
  1182. http2_frame_size = ((uint32_t)http2_frame_head[0] * 0x10000u)
  1183. + ((uint32_t)http2_frame_head[1] * 0x100u)
  1184. + ((uint32_t)http2_frame_head[2]);
  1185. http2_frame_type = http2_frame_head[3];
  1186. http2_frame_flags = http2_frame_head[4];
  1187. http2_frame_stream_id = ((uint32_t)http2_frame_head[5] * 0x1000000u)
  1188. + ((uint32_t)http2_frame_head[6] * 0x10000u)
  1189. + ((uint32_t)http2_frame_head[7] * 0x100u)
  1190. + ((uint32_t)http2_frame_head[8]);
  1191. frame_is_end_stream = (0 != (http2_frame_flags & 0x01));
  1192. frame_is_end_headers = (0 != (http2_frame_flags & 0x04));
  1193. frame_is_padded = (0 != (http2_frame_flags & 0x08));
  1194. frame_is_priority = (0 != (http2_frame_flags & 0x20));
  1195. if (http2_frame_size > server_settings.settings_max_frame_size) {
  1196. /* TODO: Error Message */
  1197. DEBUG_TRACE("HTTP2 frame too large (%lu)",
  1198. (unsigned long)http2_frame_size);
  1199. goto clean_http2;
  1200. }
  1201. bytes_read = mg_read(conn, buf, http2_frame_size);
  1202. if (bytes_read != (int)http2_frame_size) {
  1203. /* TODO: Error Message - or read again? */
  1204. DEBUG_TRACE("HTTP2 read error (%li != %li)",
  1205. (signed long int)bytes_read,
  1206. (signed long int)http2_frame_size);
  1207. goto clean_http2;
  1208. }
  1209. DEBUG_TRACE("HTTP2 frame type %u, size %u, stream %u, flags %02x",
  1210. http2_frame_type,
  1211. http2_frame_size,
  1212. http2_frame_stream_id,
  1213. http2_frame_flags);
  1214. /* Further processing according to frame type. See definition: */
  1215. /* https://tools.ietf.org/html/rfc7540#section-6 */
  1216. switch (http2_frame_type) {
  1217. case 0: /* DATA */
  1218. {
  1219. /* TODO */
  1220. DEBUG_TRACE("%s", "HTTP2 DATA frame?");
  1221. } break;
  1222. case 1: /* HEADERS */
  1223. {
  1224. int i = 0;
  1225. uint8_t padding = 0;
  1226. uint32_t dependency = 0;
  1227. uint8_t weight = 0;
  1228. uint8_t exclusive = 0;
  1229. /* Request start time */
  1230. clock_gettime(CLOCK_MONOTONIC, &(conn->req_time));
  1231. if (frame_is_padded) {
  1232. padding = buf[i];
  1233. i++;
  1234. DEBUG_TRACE("HTTP2 frame padded by %u bytes", padding);
  1235. }
  1236. if (frame_is_priority) {
  1237. uint32_t val = ((uint32_t)buf[0 + i] * 0x1000000u)
  1238. + ((uint32_t)buf[1 + i] * 0x10000u)
  1239. + ((uint32_t)buf[2 + i] * 0x100u)
  1240. + ((uint32_t)buf[3 + i]);
  1241. dependency = (val & 0x7FFFFFFFu);
  1242. exclusive = ((val & 0x80000000u) != 0);
  1243. weight = buf[4 + i];
  1244. i += 5;
  1245. DEBUG_TRACE(
  1246. "HTTP2 frame weight %u, dependency %u (exclusive: %i)",
  1247. weight,
  1248. dependency,
  1249. exclusive);
  1250. }
  1251. conn->request_info.num_headers = 0;
  1252. while (i < (int)http2_frame_size - (int)padding) {
  1253. const char *key = 0;
  1254. const char *val = 0;
  1255. uint8_t idx_mask = 0;
  1256. uint8_t value_known = 0;
  1257. uint8_t indexing = 0;
  1258. uint64_t idx = 0;
  1259. /* Classify next entry by checking the bit mask */
  1260. if ((buf[i] & 0x80u) == 0x80u) {
  1261. /* Indexed Header Field Representation:
  1262. * https://tools.ietf.org/html/rfc7541#section-6.1 */
  1263. idx_mask = 0x7fu;
  1264. value_known = 1;
  1265. } else if ((buf[i] & 0xC0u) == 0x40u) {
  1266. /* Literal Header Field with Incremental Indexing:
  1267. * https://tools.ietf.org/html/rfc7541#section-6.2.1 */
  1268. idx_mask = 0x3fu;
  1269. indexing = 1;
  1270. } else if ((buf[i] & 0xF0u) == 0x00u) {
  1271. /* Literal Header Field without Indexing:
  1272. * https://tools.ietf.org/html/rfc7541#section-6.2.2 */
  1273. idx_mask = 0x0fu;
  1274. } else if ((buf[i] & 0xF0u) == 0x10u) {
  1275. /* Literal Header Field Never Indexed:
  1276. * https://tools.ietf.org/html/rfc7541#section-6.2.3 */
  1277. idx_mask = 0x0fu;
  1278. } else if ((buf[i] & 0xE0u) == 0x20u) {
  1279. uint64_t tableSize;
  1280. /* Dynamic Table Size Update:
  1281. * https://tools.ietf.org/html/rfc7541#section-6.3 */
  1282. idx_mask = 0x1fu;
  1283. tableSize = hpack_getnum(buf, &i, idx_mask, conn->phys_ctx);
  1284. /* TODO: check if tablesize > allowed table size */
  1285. /* Purge additional table entries */
  1286. purge_dynamic_header_table(conn, (uint32_t)tableSize);
  1287. /* Process next frame */
  1288. continue;
  1289. } else {
  1290. DEBUG_TRACE("HTTP2 unknown start pattern %02x", buf[i]);
  1291. goto clean_http2;
  1292. }
  1293. /* Get the header name table index */
  1294. idx = hpack_getnum(buf, &i, idx_mask, conn->phys_ctx);
  1295. /* Get Header name "key" */
  1296. if (idx == 0) {
  1297. /* Index 0: Header name encoded in following bytes */
  1298. key =
  1299. hpack_decode(buf, &i, (int)bytes_read, conn->phys_ctx);
  1300. CHECK_LEAK_HDR_ALLOC(key);
  1301. if (!key) {
  1302. DEBUG_TRACE("%s", "HTTP2 key decoding error");
  1303. goto clean_http2;
  1304. }
  1305. } else if (/*(idx >= 15) &&*/ (idx <= 61)) {
  1306. /* Take key name from predefined header table */
  1307. key = mg_strdup_ctx(hpack_predefined[idx].name,
  1308. conn->phys_ctx); /* leak? */
  1309. CHECK_LEAK_HDR_ALLOC(key);
  1310. } else if ((idx >= 62)
  1311. && ((idx - 61) <= conn->http2.dyn_table_size)) {
  1312. /* Take from dynamic header table */
  1313. uint32_t local_table_idx = (uint32_t)idx - 62;
  1314. key = mg_strdup_ctx(
  1315. conn->http2.dyn_table[local_table_idx].name,
  1316. conn->phys_ctx);
  1317. CHECK_LEAK_HDR_ALLOC(key);
  1318. } else {
  1319. /* protocol violation */
  1320. DEBUG_TRACE("HTTP2 invalid index %lu", (unsigned long)idx);
  1321. goto clean_http2;
  1322. }
  1323. /* key is allocated now and must be freed later */
  1324. /* Get header value */
  1325. if (value_known) {
  1326. /* Server must already know the value */
  1327. if (idx <= 61) {
  1328. if (hpack_predefined[idx].value) {
  1329. val = mg_strdup_ctx(hpack_predefined[idx].value,
  1330. conn->phys_ctx); /* leak? */
  1331. CHECK_LEAK_HDR_ALLOC(val);
  1332. } else {
  1333. /* protocol violation */
  1334. DEBUG_TRACE("HTTP2 indexed header %lu has no value "
  1335. "(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 if ((idx >= 62)
  1343. && ((idx - 61) <= conn->http2.dyn_table_size)) {
  1344. uint32_t local_table_idx = (uint32_t)idx - 62;
  1345. val = mg_strdup_ctx(
  1346. conn->http2.dyn_table[local_table_idx].value,
  1347. conn->phys_ctx);
  1348. CHECK_LEAK_HDR_ALLOC(val);
  1349. } else {
  1350. /* protocol violation */
  1351. DEBUG_TRACE(
  1352. "HTTP2 indexed header %lu out of range (key: %s)",
  1353. (unsigned long)idx,
  1354. key);
  1355. CHECK_LEAK_HDR_FREE(key);
  1356. mg_free((void *)key);
  1357. goto clean_http2;
  1358. }
  1359. } else {
  1360. /* Read value from HTTP2 stream */
  1361. val = hpack_decode(buf,
  1362. &i,
  1363. (int)bytes_read,
  1364. conn->phys_ctx); /* leak? */
  1365. CHECK_LEAK_HDR_ALLOC(val);
  1366. if (!val) {
  1367. DEBUG_TRACE("%s", "HTTP2 value decoding error");
  1368. mg_free((void *)key);
  1369. goto clean_http2;
  1370. }
  1371. if (indexing) {
  1372. /* Add to index */
  1373. if (conn->http2.dyn_table_size
  1374. >= HTTP2_DYN_TABLE_SIZE) {
  1375. /* Too many elements */
  1376. DEBUG_TRACE("HTTP2 index table is full (key: %s, "
  1377. "value: %s)",
  1378. key,
  1379. val);
  1380. CHECK_LEAK_HDR_FREE(key);
  1381. CHECK_LEAK_HDR_FREE(val);
  1382. mg_free((void *)key);
  1383. mg_free((void *)val);
  1384. goto clean_http2;
  1385. }
  1386. /* Add to table of dynamic headers */
  1387. conn->http2.dyn_table[conn->http2.dyn_table_size].name =
  1388. mg_strdup_ctx(key, conn->phys_ctx); /* leak */
  1389. conn->http2.dyn_table[conn->http2.dyn_table_size]
  1390. .value =
  1391. mg_strdup_ctx(val, conn->phys_ctx); /* leak */
  1392. CHECK_LEAK_DYN_ALLOC(
  1393. conn->http2.dyn_table[conn->http2.dyn_table_size]
  1394. .name);
  1395. CHECK_LEAK_DYN_ALLOC(
  1396. conn->http2.dyn_table[conn->http2.dyn_table_size]
  1397. .value);
  1398. conn->http2.dyn_table_size++;
  1399. DEBUG_TRACE("HTTP2 new dynamic header table entry %i "
  1400. "(key: %s, value: %s)",
  1401. (int)conn->http2.dyn_table_size,
  1402. key,
  1403. val);
  1404. }
  1405. }
  1406. /* val and key are allocated now and must be freed later */
  1407. /* Store these pointers in conn->request_info[].http_headers,
  1408. * free_buffered_header_list(conn) will clean up later. */
  1409. /* Add header for this request */
  1410. if ((key != NULL) && (val != NULL)
  1411. && (conn->request_info.num_headers < MG_MAX_HEADERS)) {
  1412. conn->request_info
  1413. .http_headers[conn->request_info.num_headers]
  1414. .name = key;
  1415. conn->request_info
  1416. .http_headers[conn->request_info.num_headers]
  1417. .value = val;
  1418. conn->request_info.num_headers++;
  1419. /* Some headers need to be stored in the request structure
  1420. */
  1421. if (!strcmp(":method", key)) {
  1422. conn->request_info.request_method = val;
  1423. } else if (!strcmp(":path", key)) {
  1424. conn->request_info.local_uri = val;
  1425. conn->request_info.request_uri = val;
  1426. conn->request_info.local_uri_raw = val;
  1427. } else if (!strcmp(":status", key)) {
  1428. conn->status_code = atoi(val);
  1429. }
  1430. DEBUG_TRACE("HTTP2 request header (key: %s, value: %s)",
  1431. key,
  1432. val);
  1433. } else {
  1434. /* - either key or value are NULL (out of memory)
  1435. * - or the max. number of headers is reached
  1436. * in both cases free all memory
  1437. */
  1438. DEBUG_TRACE("%s", "HTTP2 cannot add header");
  1439. CHECK_LEAK_HDR_FREE(key);
  1440. CHECK_LEAK_HDR_FREE(val);
  1441. mg_free((void *)key);
  1442. key = NULL;
  1443. mg_free((void *)val);
  1444. val = NULL;
  1445. }
  1446. }
  1447. /* stream id */
  1448. conn->http2.stream_id = http2_frame_stream_id;
  1449. /* header parsed */
  1450. DEBUG_TRACE("HTTP2 handle_request (stream %u)",
  1451. http2_frame_stream_id);
  1452. handle_request_stat_log(conn);
  1453. /* Send "final" frame */
  1454. DEBUG_TRACE("HTTP2 handle_request done (stream %u)",
  1455. http2_frame_stream_id);
  1456. http2_data_frame_head(conn, 0, 1);
  1457. free_buffered_response_header_list(conn);
  1458. free_buffered_request_header_list(conn);
  1459. } break;
  1460. case 2: /* PRIORITY */
  1461. {
  1462. uint32_t dependStream =
  1463. ((uint32_t)buf[0] * 0x1000000u) + ((uint32_t)buf[1] * 0x10000u)
  1464. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1465. uint8_t weight = buf[4];
  1466. DEBUG_TRACE("HTTP2 priority %u dependent stream %u",
  1467. weight,
  1468. dependStream);
  1469. } break;
  1470. case 3: /* RST_STREAM */
  1471. {
  1472. uint32_t errorId =
  1473. ((uint32_t)buf[0] * 0x1000000u) + ((uint32_t)buf[1] * 0x10000u)
  1474. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1475. DEBUG_TRACE("HTTP2 reset with error %u", errorId);
  1476. } break;
  1477. case 4: /* SETTINGS */
  1478. if (http2_frame_stream_id != 0) {
  1479. /* Send protocol error */
  1480. http2_reset_stream(conn,
  1481. http2_frame_stream_id,
  1482. HTTP2_ERR_PROTOCOL_ERROR);
  1483. DEBUG_TRACE("%s", "HTTP2 received invalid settings frame");
  1484. } else if (http2_frame_flags) {
  1485. /* ACK frame. Do not reply. */
  1486. my_settings_accepted++;
  1487. DEBUG_TRACE("%s", "CivetWeb settings confirmed by peer");
  1488. } else {
  1489. int i;
  1490. for (i = 0; i < (int)http2_frame_size; i += 6) {
  1491. uint16_t id =
  1492. ((uint16_t)buf[i] * 0x100u) + ((uint16_t)buf[i + 1]);
  1493. uint32_t val = ((uint32_t)buf[i + 2] * 0x1000000u)
  1494. + ((uint32_t)buf[i + 3] * 0x10000u)
  1495. + ((uint32_t)buf[i + 4] * 0x100u)
  1496. + ((uint32_t)buf[i + 5]);
  1497. switch (id) {
  1498. case 1:
  1499. client_settings.settings_header_table_size = val;
  1500. DEBUG_TRACE("Received settings header_table_size: %u",
  1501. val);
  1502. break;
  1503. case 2:
  1504. client_settings.settings_enable_push = (val != 0);
  1505. DEBUG_TRACE("Received settings enable_push: %u", val);
  1506. break;
  1507. case 3:
  1508. client_settings.settings_max_concurrent_streams = val;
  1509. DEBUG_TRACE(
  1510. "Received settings max_concurrent_streams: %u",
  1511. val);
  1512. break;
  1513. case 4:
  1514. client_settings.settings_initial_window_size = val;
  1515. DEBUG_TRACE("Received settings initial_window_size: %u",
  1516. val);
  1517. break;
  1518. case 5:
  1519. client_settings.settings_max_frame_size = val;
  1520. DEBUG_TRACE("Received settings max_frame_size: %u",
  1521. val);
  1522. break;
  1523. case 6:
  1524. client_settings.settings_max_header_list_size = val;
  1525. DEBUG_TRACE(
  1526. "Received settings max_header_list_size: %u", val);
  1527. break;
  1528. default:
  1529. /* Unknown setting. Ignore it. */
  1530. DEBUG_TRACE("Received unknown settings id=%u: %u",
  1531. id,
  1532. val);
  1533. break;
  1534. }
  1535. }
  1536. /* Every settings frame must be acknowledged */
  1537. http2_settings_acknowledge(conn);
  1538. }
  1539. break;
  1540. case 5: /* PUSH_PROMISE */
  1541. DEBUG_TRACE("%s", "Push promise not supported");
  1542. break;
  1543. case 6: /* PING */
  1544. if (http2_frame_flags == 0) {
  1545. /* Set "reply" flag, and send same data back */
  1546. DEBUG_TRACE("%s", "Replying to ping");
  1547. http2_frame_head[4] = 1;
  1548. mg_xwrite(conn, http2_frame_head, sizeof(http2_frame_head));
  1549. mg_xwrite(conn, buf, http2_frame_size);
  1550. }
  1551. break;
  1552. case 7: /* GOAWAY */
  1553. {
  1554. uint32_t lastStream =
  1555. ((uint32_t)buf[0] * 0x1000000u) + ((uint32_t)buf[1] * 0x10000u)
  1556. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1557. uint32_t errorId =
  1558. ((uint32_t)buf[4] * 0x1000000u) + ((uint32_t)buf[5] * 0x10000u)
  1559. + ((uint32_t)buf[6] * 0x100u) + ((uint32_t)buf[7]);
  1560. ; /* followed by debug data */
  1561. uint32_t debugDataLen = http2_frame_size - 8;
  1562. char *debugData = (char *)buf + 8;
  1563. DEBUG_TRACE("HTTP2 goaway stream %u, error %u (%.*s)",
  1564. lastStream,
  1565. errorId,
  1566. debugDataLen,
  1567. debugData);
  1568. } break;
  1569. case 8: /* WINDOW_UPDATE */
  1570. {
  1571. uint32_t val = ((uint32_t)buf[0] * 0x1000000u)
  1572. + ((uint32_t)buf[1] * 0x10000u)
  1573. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1574. http_window_length = (val & 0x7FFFFFFFu);
  1575. DEBUG_TRACE("HTTP2 window update stream %u, length %u",
  1576. http2_frame_stream_id,
  1577. http_window_length);
  1578. } break;
  1579. case 9: /* CONTINUATION */
  1580. DEBUG_TRACE("%s", "HTTP2 Continue");
  1581. break;
  1582. default:
  1583. /* TODO: Error Message */
  1584. DEBUG_TRACE("%s", "Unknown frame type");
  1585. goto clean_http2;
  1586. }
  1587. /* not used in the moment */
  1588. (void)frame_is_end_stream;
  1589. (void)frame_is_end_headers;
  1590. (void)client_settings;
  1591. }
  1592. clean_http2:
  1593. DEBUG_TRACE("%s", "HTTP2 free buffer, connection handler finished");
  1594. mg_free(buf);
  1595. }
  1596. #if 0
  1597. static void
  1598. HPACK_TEST()
  1599. {
  1600. uint64_t test;
  1601. for (test = 0;; test++) {
  1602. char in[32] = {0};
  1603. uint8_t out[32] = {0};
  1604. char *check;
  1605. int i;
  1606. int l;
  1607. memcpy(in, &test, sizeof(test));
  1608. l = hpack_encode(out, in, 0);
  1609. i = 0;
  1610. check = hpack_decode(out, &i, NULL);
  1611. if (strcmp(in, check)) {
  1612. printf("Error\n");
  1613. }
  1614. mg_free(check);
  1615. }
  1616. }
  1617. static void
  1618. HPACK_TABLE_TEST()
  1619. {
  1620. int i;
  1621. uint32_t hpack_huff_end_code_expected[32] = { 0 };
  1622. uint8_t hpack_huff_start_index_expected[32] = { 0 };
  1623. int reverse_map[256] = { 0 };
  1624. for (i = 0; i < 256; i++) {
  1625. reverse_map[i] = -1;
  1626. }
  1627. for (i = 0; i < 256; i++) {
  1628. uint8_t bits = hpack_huff_dec[i].bitcount;
  1629. uint8_t dec = hpack_huff_dec[i].decoded;
  1630. if (bits > hpack_huff_dec[i + 1].bitcount) {
  1631. ck_abort_msg("hpack_huff_dec disorder at index %i", i);
  1632. }
  1633. if (hpack_huff_dec[i].encoded & (0xFFFFFFFFul << bits)) {
  1634. ck_abort_msg("hpack_huff_dec bits inconsistent at index %i", i);
  1635. }
  1636. if ((bits < 5) || (bits > 30)) {
  1637. ck_abort_msg("hpack_huff_dec bits out of range at index %i", i);
  1638. }
  1639. if (reverse_map[dec] != -1) {
  1640. ck_abort_msg("hpack_huff_dec duplicate: %i", hpack_huff_dec[i].decoded);
  1641. }
  1642. reverse_map[dec] = i;
  1643. hpack_huff_end_code_expected[bits - 5] = hpack_huff_dec[i].encoded;
  1644. }
  1645. for (i = 255; i >= 0; i--) {
  1646. uint8_t bits = hpack_huff_dec[i].bitcount;
  1647. hpack_huff_start_index_expected[bits - 5] = i;
  1648. }
  1649. for (i = 0; i < 256; i++) {
  1650. if (reverse_map[i] == -1) {
  1651. ck_abort_msg("reverse map at %i missing", i);
  1652. }
  1653. }
  1654. i = sizeof(hpack_huff_start_index) / sizeof(hpack_huff_start_index[0]);
  1655. if (i != 27) {
  1656. ck_abort_msg("hpack_huff_start_index size error: ", i);
  1657. }
  1658. i = sizeof(hpack_huff_end_code) / sizeof(hpack_huff_end_code[0]);
  1659. if (i != 27) {
  1660. ck_abort_msg("hpack_huff_end_code size error: ", i);
  1661. }
  1662. for (i = 0; i < 27; i++) {
  1663. if (hpack_huff_start_index_expected[i] != hpack_huff_start_index[i]) {
  1664. ck_abort_msg("hpack_huff_start_index error at %i", i);
  1665. }
  1666. if (hpack_huff_end_code_expected[i] != hpack_huff_end_code[i]) {
  1667. ck_abort_msg("hpack_huff_end_code error at %i", i);
  1668. }
  1669. }
  1670. }
  1671. #endif
  1672. static void
  1673. process_new_http2_connection(struct mg_connection *conn)
  1674. {
  1675. if (!is_valid_http2_primer(conn)) {
  1676. /* Primer does not match expectation from RFC.
  1677. * See https://tools.ietf.org/html/rfc7540#section-3.5 */
  1678. DEBUG_TRACE("%s", "No valid HTTP2 primer");
  1679. mg_send_http_error(conn, 400, "%s", "Invalid HTTP/2 primer");
  1680. } else {
  1681. /* Valid HTTP/2 primer received */
  1682. DEBUG_TRACE("%s", "Start handling HTTP2");
  1683. handle_http2(conn);
  1684. /* Free memory allocated for headers, if not done yet */
  1685. DEBUG_TRACE("%s", "Free remaining HTTP2 header memory");
  1686. free_buffered_response_header_list(conn);
  1687. free_buffered_request_header_list(conn);
  1688. purge_dynamic_header_table(conn, 0);
  1689. }
  1690. }