mod_http2.inl 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  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 = (char *)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_header = 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_header = 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. (void)has_connection_header; /* ignore for the moment */
  983. return 42; /* TODO */
  984. }
  985. static void
  986. http2_data_frame_head(struct mg_connection *conn,
  987. uint32_t frame_size,
  988. int is_final)
  989. {
  990. unsigned char http2_data_frame[9];
  991. uint32_t stream_id = conn->http2.stream_id;
  992. http2_data_frame[0] = (frame_size & 0xFF0000) >> 16;
  993. http2_data_frame[1] = (frame_size & 0xFF00) >> 8;
  994. http2_data_frame[2] = (frame_size & 0xFF);
  995. http2_data_frame[3] = 0; /* frame type "DATA" */
  996. http2_data_frame[4] = (is_final ? 1 : 0);
  997. http2_data_frame[5] = (stream_id & 0xFF000000u) >> 24;
  998. http2_data_frame[6] = (stream_id & 0xFF0000u) >> 16;
  999. http2_data_frame[7] = (stream_id & 0xFF00u) >> 8;
  1000. http2_data_frame[8] = (stream_id & 0xFFu);
  1001. DEBUG_TRACE("HTTP2 begin data frame: stream %u, frame_size %u (final: %i)",
  1002. stream_id,
  1003. frame_size,
  1004. is_final);
  1005. mg_xwrite(conn, http2_data_frame, 9);
  1006. }
  1007. static void
  1008. http2_send_window(struct mg_connection *conn,
  1009. uint32_t stream_id,
  1010. uint32_t window_size)
  1011. {
  1012. unsigned char http2_window_frame[9] = {0, 0, 4, 8, 0, 0, 0, 0, 0};
  1013. uint32_t data = htonl(window_size);
  1014. DEBUG_TRACE("HTTP2 send window_size: stream %u, error %u",
  1015. stream_id,
  1016. window_size);
  1017. http2_window_frame[5] = (stream_id & 0xFF000000u) >> 24;
  1018. http2_window_frame[6] = (stream_id & 0xFF0000u) >> 16;
  1019. http2_window_frame[7] = (stream_id & 0xFF00u) >> 8;
  1020. http2_window_frame[8] = (stream_id & 0xFFu);
  1021. mg_xwrite(conn, http2_window_frame, 9);
  1022. mg_xwrite(conn, &data, 4);
  1023. }
  1024. static void
  1025. http2_reset_stream(struct mg_connection *conn,
  1026. uint32_t stream_id,
  1027. uint32_t error_id)
  1028. {
  1029. unsigned char http2_reset_frame[9] = {0, 0, 4, 3, 0, 0, 0, 0, 0};
  1030. uint32_t val = htonl(error_id);
  1031. DEBUG_TRACE("HTTP2 send reset: stream %u, error %u", stream_id, error_id);
  1032. http2_reset_frame[5] = (stream_id & 0xFF000000u) >> 24;
  1033. http2_reset_frame[6] = (stream_id & 0xFF0000u) >> 16;
  1034. http2_reset_frame[7] = (stream_id & 0xFF00u) >> 8;
  1035. http2_reset_frame[8] = (stream_id & 0xFFu);
  1036. mg_xwrite(conn, http2_reset_frame, 9);
  1037. mg_xwrite(conn, &val, 4);
  1038. }
  1039. static void
  1040. http2_must_use_http1(struct mg_connection *conn)
  1041. {
  1042. DEBUG_TRACE("HTTP2 not available for this URL (%s)", conn->path_info);
  1043. http2_reset_stream(conn, conn->http2.stream_id, 0xd);
  1044. }
  1045. /* The HTTP2 implementation collects request headers as array of dynamically
  1046. * allocated string values. This array must be freed once the request is
  1047. * handled.
  1048. * This is different to the HTTP/1.x implementation: For HTTP/1.x, the header
  1049. * list is implemented as pointers into an existing buffer, so free must not
  1050. * be called for HTTP/1.x.
  1051. * Thus free_buffered_request_header_list is in mod_http2.inl.
  1052. */
  1053. #if defined(DEBUG)
  1054. static int mem_h_count = 0;
  1055. static int mem_d_count = 0;
  1056. #define CHECK_LEAK_HDR_ALLOC(ptr) \
  1057. DEBUG_TRACE("H NEW %p (%i): %s", ptr, ++mem_h_count, (const char *)ptr)
  1058. #define CHECK_LEAK_HDR_FREE(ptr) \
  1059. DEBUG_TRACE("H DEL %p (%i): %s", ptr, --mem_h_count, (const char *)ptr)
  1060. #define CHECK_LEAK_DYN_ALLOC(ptr) \
  1061. DEBUG_TRACE("D NEW %p (%i): %s", ptr, ++mem_d_count, (const char *)ptr)
  1062. #define CHECK_LEAK_DYN_FREE(ptr) \
  1063. DEBUG_TRACE("D DEL %p (%i): %s", ptr, --mem_d_count, (const char *)ptr)
  1064. #else
  1065. #define CHECK_LEAK_HDR_ALLOC(ptr)
  1066. #define CHECK_LEAK_HDR_FREE(ptr)
  1067. #define CHECK_LEAK_DYN_ALLOC(ptr)
  1068. #define CHECK_LEAK_DYN_FREE(ptr)
  1069. #endif
  1070. /* The dynamic header table may be resized on a HTTP2 client request.
  1071. * A tablesize=0 will free all memory.
  1072. */
  1073. static void
  1074. purge_dynamic_header_table(struct mg_connection *conn, uint32_t tableSize)
  1075. {
  1076. DEBUG_TRACE("HTTP2 dynamic header table set to %u", tableSize);
  1077. while (conn->http2.dyn_table_size > tableSize) {
  1078. conn->http2.dyn_table_size--;
  1079. CHECK_LEAK_DYN_FREE(
  1080. conn->http2.dyn_table[conn->http2.dyn_table_size].name);
  1081. CHECK_LEAK_DYN_FREE(
  1082. conn->http2.dyn_table[conn->http2.dyn_table_size].value);
  1083. mg_free((void *)conn->http2.dyn_table[conn->http2.dyn_table_size].name);
  1084. conn->http2.dyn_table[conn->http2.dyn_table_size].name = 0;
  1085. mg_free(
  1086. (void *)conn->http2.dyn_table[conn->http2.dyn_table_size].value);
  1087. conn->http2.dyn_table[conn->http2.dyn_table_size].value = 0;
  1088. }
  1089. }
  1090. /* Internal function to free request header list.
  1091. * Not to be confused with the response header list.
  1092. */
  1093. static void
  1094. free_buffered_request_header_list(struct mg_connection *conn)
  1095. {
  1096. while (conn->request_info.num_headers > 0) {
  1097. conn->request_info.num_headers--;
  1098. CHECK_LEAK_HDR_FREE(
  1099. conn->request_info.http_headers[conn->request_info.num_headers]
  1100. .name);
  1101. CHECK_LEAK_HDR_FREE(
  1102. conn->request_info.http_headers[conn->request_info.num_headers]
  1103. .value);
  1104. mg_free((void *)conn->request_info
  1105. .http_headers[conn->request_info.num_headers]
  1106. .name);
  1107. conn->request_info.http_headers[conn->request_info.num_headers].name =
  1108. 0;
  1109. mg_free((void *)conn->request_info
  1110. .http_headers[conn->request_info.num_headers]
  1111. .value);
  1112. conn->request_info.http_headers[conn->request_info.num_headers].value =
  1113. 0;
  1114. }
  1115. }
  1116. /* HTTP2 requires a different handling loop */
  1117. static void
  1118. handle_http2(struct mg_connection *conn)
  1119. {
  1120. unsigned char http2_frame_head[9];
  1121. uint32_t http2_frame_size;
  1122. uint8_t http2_frame_type;
  1123. uint8_t http2_frame_flags;
  1124. uint32_t http2_frame_stream_id;
  1125. uint32_t http_window_length = 0;
  1126. int bytes_read;
  1127. uint8_t *buf;
  1128. int my_settings_accepted = 0;
  1129. const char *my_hpack_headers[128];
  1130. struct http2_settings client_settings = http2_default_settings;
  1131. struct http2_settings server_settings = http2_default_settings;
  1132. /* Send own settings */
  1133. http2_send_settings(conn, &http2_civetweb_server_settings);
  1134. // http2_send_window(conn, 0, /* 0x3fff0001 */ 1024*1024);
  1135. /* initialize hpack header table with predefined header fields */
  1136. memset((void *)my_hpack_headers, 0, sizeof(my_hpack_headers));
  1137. memcpy((void *)my_hpack_headers,
  1138. hpack_predefined,
  1139. sizeof(hpack_predefined));
  1140. buf = (uint8_t *)mg_malloc_ctx(server_settings.settings_max_frame_size,
  1141. conn->phys_ctx);
  1142. if (!buf) {
  1143. /* Out of memory */
  1144. DEBUG_TRACE("%s", "Out of memory for HTTP2 frame");
  1145. return;
  1146. }
  1147. for (;;) {
  1148. /* HTTP/2 is handled frame by frame */
  1149. int frame_is_end_stream = 0;
  1150. int frame_is_end_headers = 0;
  1151. int frame_is_padded = 0;
  1152. int frame_is_priority = 0;
  1153. #if defined(USE_SERVER_STATS)
  1154. conn->conn_state = 3; /* HTTP/2 ready */
  1155. #endif
  1156. bytes_read = mg_read(conn, http2_frame_head, sizeof(http2_frame_head));
  1157. if (bytes_read != sizeof(http2_frame_head)) {
  1158. /* TODO: errormsg */
  1159. goto clean_http2;
  1160. }
  1161. /* Extract data from frame header */
  1162. http2_frame_size = ((uint32_t)http2_frame_head[0] * 0x10000u)
  1163. + ((uint32_t)http2_frame_head[1] * 0x100u)
  1164. + ((uint32_t)http2_frame_head[2]);
  1165. http2_frame_type = http2_frame_head[3];
  1166. http2_frame_flags = http2_frame_head[4];
  1167. http2_frame_stream_id = ((uint32_t)http2_frame_head[5] * 0x1000000u)
  1168. + ((uint32_t)http2_frame_head[6] * 0x10000u)
  1169. + ((uint32_t)http2_frame_head[7] * 0x100u)
  1170. + ((uint32_t)http2_frame_head[8]);
  1171. frame_is_end_stream = (0 != (http2_frame_flags & 0x01));
  1172. frame_is_end_headers = (0 != (http2_frame_flags & 0x04));
  1173. frame_is_padded = (0 != (http2_frame_flags & 0x08));
  1174. frame_is_priority = (0 != (http2_frame_flags & 0x20));
  1175. if (http2_frame_size > server_settings.settings_max_frame_size) {
  1176. /* TODO: Error Message */
  1177. DEBUG_TRACE("HTTP2 frame too large (%lu)",
  1178. (unsigned long)http2_frame_size);
  1179. goto clean_http2;
  1180. }
  1181. bytes_read = mg_read(conn, buf, http2_frame_size);
  1182. if (bytes_read != (int)http2_frame_size) {
  1183. /* TODO: Error Message - or read again? */
  1184. DEBUG_TRACE("HTTP2 read error (%li != %li)",
  1185. (signed long int)bytes_read,
  1186. (signed long int)http2_frame_size);
  1187. goto clean_http2;
  1188. }
  1189. DEBUG_TRACE("HTTP2 frame type %u, size %u, stream %u, flags %02x",
  1190. http2_frame_type,
  1191. http2_frame_size,
  1192. http2_frame_stream_id,
  1193. http2_frame_flags);
  1194. /* Further processing according to frame type. See definition: */
  1195. /* https://tools.ietf.org/html/rfc7540#section-6 */
  1196. switch (http2_frame_type) {
  1197. case 0: /* DATA */
  1198. {
  1199. /* TODO */
  1200. DEBUG_TRACE("%s", "HTTP2 DATA frame?");
  1201. } break;
  1202. case 1: /* HEADERS */
  1203. {
  1204. int i = 0;
  1205. uint8_t padding = 0;
  1206. uint32_t dependency = 0;
  1207. uint8_t weight = 0;
  1208. uint8_t exclusive = 0;
  1209. /* Request start time */
  1210. clock_gettime(CLOCK_MONOTONIC, &(conn->req_time));
  1211. if (frame_is_padded) {
  1212. padding = buf[i];
  1213. i++;
  1214. DEBUG_TRACE("HTTP2 frame padded by %u bytes", padding);
  1215. }
  1216. if (frame_is_priority) {
  1217. uint32_t val = ((uint32_t)buf[0 + i] * 0x1000000u)
  1218. + ((uint32_t)buf[1 + i] * 0x10000u)
  1219. + ((uint32_t)buf[2 + i] * 0x100u)
  1220. + ((uint32_t)buf[3 + i]);
  1221. dependency = (val & 0x7FFFFFFFu);
  1222. exclusive = ((val & 0x80000000u) != 0);
  1223. weight = buf[4 + i];
  1224. i += 5;
  1225. DEBUG_TRACE(
  1226. "HTTP2 frame weight %u, dependency %u (exclusive: %i)",
  1227. weight,
  1228. dependency,
  1229. exclusive);
  1230. }
  1231. conn->request_info.num_headers = 0;
  1232. while (i < (int)http2_frame_size - (int)padding) {
  1233. const char *key = 0;
  1234. const char *val = 0;
  1235. uint8_t idx_mask = 0;
  1236. uint8_t value_known = 0;
  1237. uint8_t indexing = 0;
  1238. uint64_t idx = 0;
  1239. /* Classify next entry by checking the bit mask */
  1240. if ((buf[i] & 0x80u) == 0x80u) {
  1241. /* Indexed Header Field Representation:
  1242. * https://tools.ietf.org/html/rfc7541#section-6.1 */
  1243. idx_mask = 0x7fu;
  1244. value_known = 1;
  1245. } else if ((buf[i] & 0xC0u) == 0x40u) {
  1246. /* Literal Header Field with Incremental Indexing:
  1247. * https://tools.ietf.org/html/rfc7541#section-6.2.1 */
  1248. idx_mask = 0x3fu;
  1249. indexing = 1;
  1250. } else if ((buf[i] & 0xF0u) == 0x00u) {
  1251. /* Literal Header Field without Indexing:
  1252. * https://tools.ietf.org/html/rfc7541#section-6.2.2 */
  1253. idx_mask = 0x0fu;
  1254. } else if ((buf[i] & 0xF0u) == 0x10u) {
  1255. /* Literal Header Field Never Indexed:
  1256. * https://tools.ietf.org/html/rfc7541#section-6.2.3 */
  1257. idx_mask = 0x0fu;
  1258. } else if ((buf[i] & 0xE0u) == 0x20u) {
  1259. uint64_t tableSize;
  1260. /* Dynamic Table Size Update:
  1261. * https://tools.ietf.org/html/rfc7541#section-6.3 */
  1262. idx_mask = 0x1fu;
  1263. tableSize = hpack_getnum(buf, &i, idx_mask, conn->phys_ctx);
  1264. /* TODO: check if tablesize > allowed table size */
  1265. /* Purge additional table entries */
  1266. purge_dynamic_header_table(conn, (uint32_t)tableSize);
  1267. /* Process next frame */
  1268. continue;
  1269. } else {
  1270. DEBUG_TRACE("HTTP2 unknown start pattern %02x", buf[i]);
  1271. goto clean_http2;
  1272. }
  1273. /* Get the header name table index */
  1274. idx = hpack_getnum(buf, &i, idx_mask, conn->phys_ctx);
  1275. /* Get Header name "key" */
  1276. if (idx == 0) {
  1277. /* Index 0: Header name encoded in following bytes */
  1278. key = hpack_decode(buf, &i, conn->phys_ctx);
  1279. CHECK_LEAK_HDR_ALLOC(key);
  1280. } else if (/*(idx >= 15) &&*/ (idx <= 61)) {
  1281. /* Take key name from predefined header table */
  1282. key = mg_strdup_ctx(hpack_predefined[idx].name,
  1283. conn->phys_ctx); /* leak? */
  1284. CHECK_LEAK_HDR_ALLOC(key);
  1285. } else if ((idx >= 62)
  1286. && ((idx - 61) <= conn->http2.dyn_table_size)) {
  1287. /* Take from dynamic header table */
  1288. uint32_t local_table_idx = (uint32_t)idx - 62;
  1289. key = mg_strdup_ctx(
  1290. conn->http2.dyn_table[local_table_idx].name,
  1291. conn->phys_ctx);
  1292. CHECK_LEAK_HDR_ALLOC(key);
  1293. } else {
  1294. /* protocol violation */
  1295. DEBUG_TRACE("HTTP2 invalid index %lu", (unsigned long)idx);
  1296. goto clean_http2;
  1297. }
  1298. /* key is allocated now and must be freed later */
  1299. /* Get header value */
  1300. if (value_known) {
  1301. /* Server must already know the value */
  1302. if (idx <= 61) {
  1303. if (hpack_predefined[idx].value) {
  1304. val = mg_strdup_ctx(hpack_predefined[idx].value,
  1305. conn->phys_ctx); /* leak? */
  1306. CHECK_LEAK_HDR_ALLOC(val);
  1307. } else {
  1308. /* protocol violation */
  1309. DEBUG_TRACE("HTTP2 indexed header %lu has no value "
  1310. "(key: %s)",
  1311. (unsigned long)idx,
  1312. key);
  1313. CHECK_LEAK_HDR_FREE(key);
  1314. mg_free((void *)key);
  1315. goto clean_http2;
  1316. }
  1317. } else if ((idx >= 62)
  1318. && ((idx - 61) <= conn->http2.dyn_table_size)) {
  1319. uint32_t local_table_idx = (uint32_t)idx - 62;
  1320. val = mg_strdup_ctx(
  1321. conn->http2.dyn_table[local_table_idx].value,
  1322. conn->phys_ctx);
  1323. CHECK_LEAK_HDR_ALLOC(val);
  1324. } else {
  1325. /* protocol violation */
  1326. DEBUG_TRACE(
  1327. "HTTP2 indexed header %lu out of range (key: %s)",
  1328. (unsigned long)idx,
  1329. key);
  1330. CHECK_LEAK_HDR_FREE(key);
  1331. mg_free((void *)key);
  1332. goto clean_http2;
  1333. }
  1334. } else {
  1335. /* Read value from HTTP2 stream */
  1336. val = hpack_decode(buf, &i, conn->phys_ctx); /* leak? */
  1337. CHECK_LEAK_HDR_ALLOC(val);
  1338. if (indexing) {
  1339. /* Add to index */
  1340. if (conn->http2.dyn_table_size
  1341. >= HTTP2_DYN_TABLE_SIZE) {
  1342. /* Too many elements */
  1343. DEBUG_TRACE("HTTP2 index table is full (key: %s, "
  1344. "value: %s)",
  1345. key,
  1346. val);
  1347. CHECK_LEAK_HDR_FREE(key);
  1348. CHECK_LEAK_HDR_FREE(val);
  1349. mg_free((void *)key);
  1350. mg_free((void *)val);
  1351. goto clean_http2;
  1352. }
  1353. /* Add to table of dynamic headers */
  1354. conn->http2.dyn_table[conn->http2.dyn_table_size].name =
  1355. mg_strdup_ctx(key, conn->phys_ctx); /* leak */
  1356. conn->http2.dyn_table[conn->http2.dyn_table_size]
  1357. .value =
  1358. mg_strdup_ctx(val, conn->phys_ctx); /* leak */
  1359. CHECK_LEAK_DYN_ALLOC(
  1360. conn->http2.dyn_table[conn->http2.dyn_table_size]
  1361. .name);
  1362. CHECK_LEAK_DYN_ALLOC(
  1363. conn->http2.dyn_table[conn->http2.dyn_table_size]
  1364. .value);
  1365. conn->http2.dyn_table_size++;
  1366. DEBUG_TRACE("HTTP2 new dynamic header table entry %i "
  1367. "(key: %s, value: %s)",
  1368. (int)conn->http2.dyn_table_size,
  1369. key,
  1370. val);
  1371. }
  1372. }
  1373. /* val and key are allocated now and must be freed later */
  1374. /* Store these pointers in conn->request_info[].http_headers,
  1375. * free_buffered_header_list(conn) will clean up later. */
  1376. /* Add header for this request */
  1377. if ((key != NULL) && (val != NULL)
  1378. && (conn->request_info.num_headers < MG_MAX_HEADERS)) {
  1379. conn->request_info
  1380. .http_headers[conn->request_info.num_headers]
  1381. .name = key;
  1382. conn->request_info
  1383. .http_headers[conn->request_info.num_headers]
  1384. .value = val;
  1385. conn->request_info.num_headers++;
  1386. /* Some headers need to be stored in the request structure
  1387. */
  1388. if (!strcmp(":method", key)) {
  1389. conn->request_info.request_method = val;
  1390. } else if (!strcmp(":path", key)) {
  1391. conn->request_info.local_uri = val;
  1392. conn->request_info.request_uri = val;
  1393. } else if (!strcmp(":status", key)) {
  1394. conn->status_code = atoi(val);
  1395. }
  1396. DEBUG_TRACE("HTTP2 request header (key: %s, value: %s)",
  1397. key,
  1398. val);
  1399. } else {
  1400. /* - either key or value are NULL (out of memory)
  1401. * - or the max. number of headers is reached
  1402. * in both cases free all memory
  1403. */
  1404. DEBUG_TRACE("%s", "HTTP2 cannot add header");
  1405. CHECK_LEAK_HDR_FREE(key);
  1406. CHECK_LEAK_HDR_FREE(val);
  1407. mg_free((void *)key);
  1408. key = NULL;
  1409. mg_free((void *)val);
  1410. val = NULL;
  1411. }
  1412. }
  1413. /* stream id */
  1414. conn->http2.stream_id = http2_frame_stream_id;
  1415. /* header parsed */
  1416. DEBUG_TRACE("HTTP2 handle_request (stream %u)",
  1417. http2_frame_stream_id);
  1418. handle_request_stat_log(conn);
  1419. /* Send "final" frame */
  1420. DEBUG_TRACE("HTTP2 handle_request done (stream %u)",
  1421. http2_frame_stream_id);
  1422. http2_data_frame_head(conn, 0, 1);
  1423. free_buffered_response_header_list(conn);
  1424. free_buffered_request_header_list(conn);
  1425. } break;
  1426. case 2: /* PRIORITY */
  1427. {
  1428. uint32_t dependStream =
  1429. ((uint32_t)buf[0] * 0x1000000u) + ((uint32_t)buf[1] * 0x10000u)
  1430. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1431. uint8_t weight = buf[4];
  1432. DEBUG_TRACE("HTTP2 priority %u dependent stream %u",
  1433. weight,
  1434. dependStream);
  1435. } break;
  1436. case 3: /* RST_STREAM */
  1437. {
  1438. uint32_t errorId =
  1439. ((uint32_t)buf[0] * 0x1000000u) + ((uint32_t)buf[1] * 0x10000u)
  1440. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1441. DEBUG_TRACE("HTTP2 reset with error %u", errorId);
  1442. } break;
  1443. case 4: /* SETTINGS */
  1444. if (http2_frame_stream_id != 0) {
  1445. /* Send protocol error */
  1446. http2_reset_stream(conn,
  1447. http2_frame_stream_id,
  1448. HTTP2_ERR_PROTOCOL_ERROR);
  1449. DEBUG_TRACE("%s", "HTTP2 received invalid settings frame");
  1450. } else if (http2_frame_flags) {
  1451. /* ACK frame. Do not reply. */
  1452. my_settings_accepted++;
  1453. DEBUG_TRACE("%s", "CivetWeb settings confirmed by peer");
  1454. } else {
  1455. int i;
  1456. for (i = 0; i < (int)http2_frame_size; i += 6) {
  1457. uint16_t id =
  1458. ((uint16_t)buf[i] * 0x100u) + ((uint16_t)buf[i + 1]);
  1459. uint32_t val = ((uint32_t)buf[i + 2] * 0x1000000u)
  1460. + ((uint32_t)buf[i + 3] * 0x10000u)
  1461. + ((uint32_t)buf[i + 4] * 0x100u)
  1462. + ((uint32_t)buf[i + 5]);
  1463. switch (id) {
  1464. case 1:
  1465. client_settings.settings_header_table_size = val;
  1466. DEBUG_TRACE("Received settings header_table_size: %u",
  1467. val);
  1468. break;
  1469. case 2:
  1470. client_settings.settings_enable_push = (val != 0);
  1471. DEBUG_TRACE("Received settings enable_push: %u", val);
  1472. break;
  1473. case 3:
  1474. client_settings.settings_max_concurrent_streams = val;
  1475. DEBUG_TRACE(
  1476. "Received settings max_concurrent_streams: %u",
  1477. val);
  1478. break;
  1479. case 4:
  1480. client_settings.settings_initial_window_size = val;
  1481. DEBUG_TRACE("Received settings initial_window_size: %u",
  1482. val);
  1483. break;
  1484. case 5:
  1485. client_settings.settings_max_frame_size = val;
  1486. DEBUG_TRACE("Received settings max_frame_size: %u",
  1487. val);
  1488. break;
  1489. case 6:
  1490. client_settings.settings_max_header_list_size = val;
  1491. DEBUG_TRACE(
  1492. "Received settings max_header_list_size: %u", val);
  1493. break;
  1494. default:
  1495. /* Unknown setting. Ignore it. */
  1496. DEBUG_TRACE("Received unknown settings id=%u: %u",
  1497. id,
  1498. val);
  1499. break;
  1500. }
  1501. }
  1502. /* Every settings frame must be acknowledged */
  1503. http2_settings_acknowledge(conn);
  1504. }
  1505. break;
  1506. case 5: /* PUSH_PROMISE */
  1507. DEBUG_TRACE("%s", "Push promise not supported");
  1508. break;
  1509. case 6: /* PING */
  1510. if (http2_frame_flags == 0) {
  1511. /* Set "reply" flag, and send same data back */
  1512. DEBUG_TRACE("%s", "Replying to ping");
  1513. http2_frame_head[4] = 1;
  1514. mg_xwrite(conn, http2_frame_head, sizeof(http2_frame_head));
  1515. mg_xwrite(conn, buf, http2_frame_size);
  1516. }
  1517. break;
  1518. case 7: /* GOAWAY */
  1519. {
  1520. uint32_t lastStream =
  1521. ((uint32_t)buf[0] * 0x1000000u) + ((uint32_t)buf[1] * 0x10000u)
  1522. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1523. uint32_t errorId =
  1524. ((uint32_t)buf[4] * 0x1000000u) + ((uint32_t)buf[5] * 0x10000u)
  1525. + ((uint32_t)buf[6] * 0x100u) + ((uint32_t)buf[7]);
  1526. ; /* followed by debug data */
  1527. uint32_t debugDataLen = http2_frame_size - 8;
  1528. char *debugData = (char *)buf + 8;
  1529. DEBUG_TRACE("HTTP2 goaway stream %u, error %u (%.*s)",
  1530. lastStream,
  1531. errorId,
  1532. debugDataLen,
  1533. debugData);
  1534. } break;
  1535. case 8: /* WINDOW_UPDATE */
  1536. {
  1537. uint32_t val = ((uint32_t)buf[0] * 0x1000000u)
  1538. + ((uint32_t)buf[1] * 0x10000u)
  1539. + ((uint32_t)buf[2] * 0x100u) + ((uint32_t)buf[3]);
  1540. http_window_length = (val & 0x7FFFFFFFu);
  1541. DEBUG_TRACE("HTTP2 window update stream %u, length %u",
  1542. http2_frame_stream_id,
  1543. http_window_length);
  1544. } break;
  1545. case 9: /* CONTINUATION */
  1546. DEBUG_TRACE("%s", "HTTP2 Continue");
  1547. break;
  1548. default:
  1549. /* TODO: Error Message */
  1550. DEBUG_TRACE("%s", "Unknown frame type");
  1551. goto clean_http2;
  1552. }
  1553. /* not used in the moment */
  1554. (void)frame_is_end_stream;
  1555. (void)frame_is_end_headers;
  1556. (void)client_settings;
  1557. }
  1558. clean_http2:
  1559. DEBUG_TRACE("%s", "HTTP2 free buffer, connection handler finished");
  1560. mg_free(buf);
  1561. }
  1562. #if 0
  1563. static void
  1564. HPACK_TEST()
  1565. {
  1566. uint64_t test;
  1567. for (test = 0;; test++) {
  1568. char in[32] = {0};
  1569. uint8_t out[32] = {0};
  1570. char *check;
  1571. int i;
  1572. int l;
  1573. memcpy(in, &test, sizeof(test));
  1574. l = hpack_encode(out, in, 0);
  1575. i = 0;
  1576. check = hpack_decode(out, &i, NULL);
  1577. if (strcmp(in, check)) {
  1578. printf("Error\n");
  1579. }
  1580. mg_free(check);
  1581. }
  1582. }
  1583. static void
  1584. HPACK_TABLE_TEST()
  1585. {
  1586. int i;
  1587. uint32_t hpack_huff_end_code_expected[32] = { 0 };
  1588. uint8_t hpack_huff_start_index_expected[32] = { 0 };
  1589. int reverse_map[256] = { 0 };
  1590. for (i = 0; i < 256; i++) {
  1591. reverse_map[i] = -1;
  1592. }
  1593. for (i = 0; i < 256; i++) {
  1594. uint8_t bits = hpack_huff_dec[i].bitcount;
  1595. uint8_t dec = hpack_huff_dec[i].decoded;
  1596. if (bits > hpack_huff_dec[i + 1].bitcount) {
  1597. ck_abort_msg("hpack_huff_dec disorder at index %i", i);
  1598. }
  1599. if (hpack_huff_dec[i].encoded & (0xFFFFFFFFul << bits)) {
  1600. ck_abort_msg("hpack_huff_dec bits inconsistent at index %i", i);
  1601. }
  1602. if ((bits < 5) || (bits > 30)) {
  1603. ck_abort_msg("hpack_huff_dec bits out of range at index %i", i);
  1604. }
  1605. if (reverse_map[dec] != -1) {
  1606. ck_abort_msg("hpack_huff_dec duplicate: %i", hpack_huff_dec[i].decoded);
  1607. }
  1608. reverse_map[dec] = i;
  1609. hpack_huff_end_code_expected[bits - 5] = hpack_huff_dec[i].encoded;
  1610. }
  1611. for (i = 255; i >= 0; i--) {
  1612. uint8_t bits = hpack_huff_dec[i].bitcount;
  1613. hpack_huff_start_index_expected[bits - 5] = i;
  1614. }
  1615. for (i = 0; i < 256; i++) {
  1616. if (reverse_map[i] == -1) {
  1617. ck_abort_msg("reverse map at %i mising", i);
  1618. }
  1619. }
  1620. i = sizeof(hpack_huff_start_index) / sizeof(hpack_huff_start_index[0]);
  1621. if (i != 27) {
  1622. ck_abort_msg("hpack_huff_start_index size error: ", i);
  1623. }
  1624. i = sizeof(hpack_huff_end_code) / sizeof(hpack_huff_end_code[0]);
  1625. if (i != 27) {
  1626. ck_abort_msg("hpack_huff_end_code size error: ", i);
  1627. }
  1628. for (i = 0; i < 27; i++) {
  1629. if (hpack_huff_start_index_expected[i] != hpack_huff_start_index[i]) {
  1630. ck_abort_msg("hpack_huff_start_index error at %i", i);
  1631. }
  1632. if (hpack_huff_end_code_expected[i] != hpack_huff_end_code[i]) {
  1633. ck_abort_msg("hpack_huff_end_code error at %i", i);
  1634. }
  1635. }
  1636. }
  1637. #endif
  1638. static void
  1639. process_new_http2_connection(struct mg_connection *conn)
  1640. {
  1641. if (!is_valid_http2_primer(conn)) {
  1642. /* Primer does not match expectation from RFC.
  1643. * See https://tools.ietf.org/html/rfc7540#section-3.5 */
  1644. DEBUG_TRACE("%s", "No valid HTTP2 primer");
  1645. mg_send_http_error(conn, 400, "%s", "Invalid HTTP/2 primer");
  1646. } else {
  1647. /* Valid HTTP/2 primer received */
  1648. DEBUG_TRACE("%s", "Start handling HTTP2");
  1649. handle_http2(conn);
  1650. /* Free memory allocated for headers, if not done yet */
  1651. DEBUG_TRACE("%s", "Free remaining HTTP2 header memory");
  1652. free_buffered_response_header_list(conn);
  1653. free_buffered_request_header_list(conn);
  1654. purge_dynamic_header_table(conn, 0);
  1655. }
  1656. }