racer 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (336) hide show
  1. data/LICENSE +22 -0
  2. data/bin/racer +33 -0
  3. data/ext/libuv/AUTHORS +45 -0
  4. data/ext/libuv/LICENSE +42 -0
  5. data/ext/libuv/Makefile +119 -0
  6. data/ext/libuv/README.md +88 -0
  7. data/ext/libuv/build/gcc_version.py +20 -0
  8. data/ext/libuv/common.gypi +176 -0
  9. data/ext/libuv/config-mingw.mk +61 -0
  10. data/ext/libuv/config-unix.mk +171 -0
  11. data/ext/libuv/gyp_uv +64 -0
  12. data/ext/libuv/include/ares.h +591 -0
  13. data/ext/libuv/include/ares_version.h +24 -0
  14. data/ext/libuv/include/uv-private/eio.h +403 -0
  15. data/ext/libuv/include/uv-private/ev.h +838 -0
  16. data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
  17. data/ext/libuv/include/uv-private/tree.h +768 -0
  18. data/ext/libuv/include/uv-private/uv-unix.h +310 -0
  19. data/ext/libuv/include/uv-private/uv-win.h +510 -0
  20. data/ext/libuv/include/uv.h +1796 -0
  21. data/ext/libuv/src/ares/AUTHORS +37 -0
  22. data/ext/libuv/src/ares/CHANGES +1218 -0
  23. data/ext/libuv/src/ares/CMakeLists.txt +22 -0
  24. data/ext/libuv/src/ares/NEWS +21 -0
  25. data/ext/libuv/src/ares/README +60 -0
  26. data/ext/libuv/src/ares/README.cares +13 -0
  27. data/ext/libuv/src/ares/README.msvc +142 -0
  28. data/ext/libuv/src/ares/README.node +21 -0
  29. data/ext/libuv/src/ares/RELEASE-NOTES +26 -0
  30. data/ext/libuv/src/ares/TODO +23 -0
  31. data/ext/libuv/src/ares/ares__close_sockets.c +66 -0
  32. data/ext/libuv/src/ares/ares__get_hostent.c +263 -0
  33. data/ext/libuv/src/ares/ares__read_line.c +71 -0
  34. data/ext/libuv/src/ares/ares__timeval.c +111 -0
  35. data/ext/libuv/src/ares/ares_cancel.c +63 -0
  36. data/ext/libuv/src/ares/ares_data.c +190 -0
  37. data/ext/libuv/src/ares/ares_data.h +65 -0
  38. data/ext/libuv/src/ares/ares_destroy.c +105 -0
  39. data/ext/libuv/src/ares/ares_dns.h +90 -0
  40. data/ext/libuv/src/ares/ares_expand_name.c +200 -0
  41. data/ext/libuv/src/ares/ares_expand_string.c +75 -0
  42. data/ext/libuv/src/ares/ares_fds.c +63 -0
  43. data/ext/libuv/src/ares/ares_free_hostent.c +42 -0
  44. data/ext/libuv/src/ares/ares_free_string.c +25 -0
  45. data/ext/libuv/src/ares/ares_getenv.c +30 -0
  46. data/ext/libuv/src/ares/ares_getenv.h +26 -0
  47. data/ext/libuv/src/ares/ares_gethostbyaddr.c +301 -0
  48. data/ext/libuv/src/ares/ares_gethostbyname.c +523 -0
  49. data/ext/libuv/src/ares/ares_getnameinfo.c +427 -0
  50. data/ext/libuv/src/ares/ares_getopt.c +122 -0
  51. data/ext/libuv/src/ares/ares_getopt.h +53 -0
  52. data/ext/libuv/src/ares/ares_getsock.c +72 -0
  53. data/ext/libuv/src/ares/ares_init.c +1809 -0
  54. data/ext/libuv/src/ares/ares_iphlpapi.h +221 -0
  55. data/ext/libuv/src/ares/ares_ipv6.h +78 -0
  56. data/ext/libuv/src/ares/ares_library_init.c +142 -0
  57. data/ext/libuv/src/ares/ares_library_init.h +42 -0
  58. data/ext/libuv/src/ares/ares_llist.c +86 -0
  59. data/ext/libuv/src/ares/ares_llist.h +42 -0
  60. data/ext/libuv/src/ares/ares_mkquery.c +195 -0
  61. data/ext/libuv/src/ares/ares_nowarn.c +181 -0
  62. data/ext/libuv/src/ares/ares_nowarn.h +55 -0
  63. data/ext/libuv/src/ares/ares_options.c +248 -0
  64. data/ext/libuv/src/ares/ares_parse_a_reply.c +263 -0
  65. data/ext/libuv/src/ares/ares_parse_aaaa_reply.c +259 -0
  66. data/ext/libuv/src/ares/ares_parse_mx_reply.c +170 -0
  67. data/ext/libuv/src/ares/ares_parse_ns_reply.c +182 -0
  68. data/ext/libuv/src/ares/ares_parse_ptr_reply.c +217 -0
  69. data/ext/libuv/src/ares/ares_parse_srv_reply.c +179 -0
  70. data/ext/libuv/src/ares/ares_parse_txt_reply.c +201 -0
  71. data/ext/libuv/src/ares/ares_platform.c +11035 -0
  72. data/ext/libuv/src/ares/ares_platform.h +43 -0
  73. data/ext/libuv/src/ares/ares_private.h +355 -0
  74. data/ext/libuv/src/ares/ares_process.c +1295 -0
  75. data/ext/libuv/src/ares/ares_query.c +183 -0
  76. data/ext/libuv/src/ares/ares_rules.h +144 -0
  77. data/ext/libuv/src/ares/ares_search.c +321 -0
  78. data/ext/libuv/src/ares/ares_send.c +134 -0
  79. data/ext/libuv/src/ares/ares_setup.h +199 -0
  80. data/ext/libuv/src/ares/ares_strcasecmp.c +66 -0
  81. data/ext/libuv/src/ares/ares_strcasecmp.h +30 -0
  82. data/ext/libuv/src/ares/ares_strdup.c +42 -0
  83. data/ext/libuv/src/ares/ares_strdup.h +26 -0
  84. data/ext/libuv/src/ares/ares_strerror.c +56 -0
  85. data/ext/libuv/src/ares/ares_timeout.c +80 -0
  86. data/ext/libuv/src/ares/ares_version.c +11 -0
  87. data/ext/libuv/src/ares/ares_writev.c +79 -0
  88. data/ext/libuv/src/ares/ares_writev.h +36 -0
  89. data/ext/libuv/src/ares/bitncmp.c +59 -0
  90. data/ext/libuv/src/ares/bitncmp.h +26 -0
  91. data/ext/libuv/src/ares/config_cygwin/ares_config.h +512 -0
  92. data/ext/libuv/src/ares/config_darwin/ares_config.h +512 -0
  93. data/ext/libuv/src/ares/config_freebsd/ares_config.h +512 -0
  94. data/ext/libuv/src/ares/config_linux/ares_config.h +512 -0
  95. data/ext/libuv/src/ares/config_netbsd/ares_config.h +512 -0
  96. data/ext/libuv/src/ares/config_openbsd/ares_config.h +512 -0
  97. data/ext/libuv/src/ares/config_sunos/ares_config.h +512 -0
  98. data/ext/libuv/src/ares/config_win32/ares_config.h +369 -0
  99. data/ext/libuv/src/ares/get_ver.awk +35 -0
  100. data/ext/libuv/src/ares/inet_net_pton.c +451 -0
  101. data/ext/libuv/src/ares/inet_net_pton.h +31 -0
  102. data/ext/libuv/src/ares/inet_ntop.c +208 -0
  103. data/ext/libuv/src/ares/inet_ntop.h +26 -0
  104. data/ext/libuv/src/ares/nameser.h +203 -0
  105. data/ext/libuv/src/ares/setup_once.h +504 -0
  106. data/ext/libuv/src/ares/windows_port.c +22 -0
  107. data/ext/libuv/src/cares.c +225 -0
  108. data/ext/libuv/src/fs-poll.c +237 -0
  109. data/ext/libuv/src/unix/async.c +119 -0
  110. data/ext/libuv/src/unix/core.c +647 -0
  111. data/ext/libuv/src/unix/cygwin.c +84 -0
  112. data/ext/libuv/src/unix/darwin.c +341 -0
  113. data/ext/libuv/src/unix/dl.c +83 -0
  114. data/ext/libuv/src/unix/eio/Changes +63 -0
  115. data/ext/libuv/src/unix/eio/LICENSE +36 -0
  116. data/ext/libuv/src/unix/eio/Makefile.am +15 -0
  117. data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
  118. data/ext/libuv/src/unix/eio/autogen.sh +3 -0
  119. data/ext/libuv/src/unix/eio/config.h.in +86 -0
  120. data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
  121. data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
  122. data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
  123. data/ext/libuv/src/unix/eio/config_linux.h +94 -0
  124. data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
  125. data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
  126. data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
  127. data/ext/libuv/src/unix/eio/configure.ac +22 -0
  128. data/ext/libuv/src/unix/eio/demo.c +194 -0
  129. data/ext/libuv/src/unix/eio/ecb.h +370 -0
  130. data/ext/libuv/src/unix/eio/eio.3 +3428 -0
  131. data/ext/libuv/src/unix/eio/eio.c +2593 -0
  132. data/ext/libuv/src/unix/eio/eio.pod +969 -0
  133. data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
  134. data/ext/libuv/src/unix/eio/xthread.h +164 -0
  135. data/ext/libuv/src/unix/error.c +104 -0
  136. data/ext/libuv/src/unix/ev/Changes +388 -0
  137. data/ext/libuv/src/unix/ev/LICENSE +36 -0
  138. data/ext/libuv/src/unix/ev/Makefile.am +18 -0
  139. data/ext/libuv/src/unix/ev/Makefile.in +771 -0
  140. data/ext/libuv/src/unix/ev/README +58 -0
  141. data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
  142. data/ext/libuv/src/unix/ev/autogen.sh +6 -0
  143. data/ext/libuv/src/unix/ev/config.guess +1526 -0
  144. data/ext/libuv/src/unix/ev/config.h.in +125 -0
  145. data/ext/libuv/src/unix/ev/config.sub +1658 -0
  146. data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
  147. data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
  148. data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
  149. data/ext/libuv/src/unix/ev/config_linux.h +141 -0
  150. data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
  151. data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
  152. data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
  153. data/ext/libuv/src/unix/ev/configure +13037 -0
  154. data/ext/libuv/src/unix/ev/configure.ac +18 -0
  155. data/ext/libuv/src/unix/ev/depcomp +630 -0
  156. data/ext/libuv/src/unix/ev/ev++.h +816 -0
  157. data/ext/libuv/src/unix/ev/ev.3 +5311 -0
  158. data/ext/libuv/src/unix/ev/ev.c +3925 -0
  159. data/ext/libuv/src/unix/ev/ev.pod +5243 -0
  160. data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
  161. data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
  162. data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
  163. data/ext/libuv/src/unix/ev/ev_port.c +179 -0
  164. data/ext/libuv/src/unix/ev/ev_select.c +310 -0
  165. data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
  166. data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
  167. data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
  168. data/ext/libuv/src/unix/ev/event.c +402 -0
  169. data/ext/libuv/src/unix/ev/event.h +170 -0
  170. data/ext/libuv/src/unix/ev/install-sh +294 -0
  171. data/ext/libuv/src/unix/ev/libev.m4 +39 -0
  172. data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
  173. data/ext/libuv/src/unix/ev/missing +336 -0
  174. data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
  175. data/ext/libuv/src/unix/freebsd.c +312 -0
  176. data/ext/libuv/src/unix/fs.c +699 -0
  177. data/ext/libuv/src/unix/internal.h +180 -0
  178. data/ext/libuv/src/unix/kqueue.c +122 -0
  179. data/ext/libuv/src/unix/linux/core.c +474 -0
  180. data/ext/libuv/src/unix/linux/inotify.c +209 -0
  181. data/ext/libuv/src/unix/linux/syscalls.c +268 -0
  182. data/ext/libuv/src/unix/linux/syscalls.h +93 -0
  183. data/ext/libuv/src/unix/loop-watcher.c +63 -0
  184. data/ext/libuv/src/unix/loop.c +81 -0
  185. data/ext/libuv/src/unix/netbsd.c +108 -0
  186. data/ext/libuv/src/unix/openbsd.c +295 -0
  187. data/ext/libuv/src/unix/pipe.c +261 -0
  188. data/ext/libuv/src/unix/poll.c +116 -0
  189. data/ext/libuv/src/unix/process.c +447 -0
  190. data/ext/libuv/src/unix/stream.c +1026 -0
  191. data/ext/libuv/src/unix/sunos.c +483 -0
  192. data/ext/libuv/src/unix/tcp.c +328 -0
  193. data/ext/libuv/src/unix/thread.c +211 -0
  194. data/ext/libuv/src/unix/timer.c +138 -0
  195. data/ext/libuv/src/unix/tty.c +146 -0
  196. data/ext/libuv/src/unix/udp.c +661 -0
  197. data/ext/libuv/src/unix/uv-eio.c +107 -0
  198. data/ext/libuv/src/unix/uv-eio.h +13 -0
  199. data/ext/libuv/src/uv-common.c +384 -0
  200. data/ext/libuv/src/uv-common.h +171 -0
  201. data/ext/libuv/src/win/async.c +130 -0
  202. data/ext/libuv/src/win/core.c +283 -0
  203. data/ext/libuv/src/win/dl.c +86 -0
  204. data/ext/libuv/src/win/error.c +150 -0
  205. data/ext/libuv/src/win/fs-event.c +512 -0
  206. data/ext/libuv/src/win/fs.c +1856 -0
  207. data/ext/libuv/src/win/getaddrinfo.c +365 -0
  208. data/ext/libuv/src/win/handle-inl.h +145 -0
  209. data/ext/libuv/src/win/handle.c +150 -0
  210. data/ext/libuv/src/win/internal.h +337 -0
  211. data/ext/libuv/src/win/loop-watcher.c +123 -0
  212. data/ext/libuv/src/win/pipe.c +1675 -0
  213. data/ext/libuv/src/win/poll.c +618 -0
  214. data/ext/libuv/src/win/process-stdio.c +479 -0
  215. data/ext/libuv/src/win/process.c +1013 -0
  216. data/ext/libuv/src/win/req-inl.h +225 -0
  217. data/ext/libuv/src/win/req.c +25 -0
  218. data/ext/libuv/src/win/stream-inl.h +69 -0
  219. data/ext/libuv/src/win/stream.c +167 -0
  220. data/ext/libuv/src/win/tcp.c +1400 -0
  221. data/ext/libuv/src/win/thread.c +372 -0
  222. data/ext/libuv/src/win/threadpool.c +74 -0
  223. data/ext/libuv/src/win/timer.c +226 -0
  224. data/ext/libuv/src/win/tty.c +1801 -0
  225. data/ext/libuv/src/win/udp.c +718 -0
  226. data/ext/libuv/src/win/util.c +864 -0
  227. data/ext/libuv/src/win/winapi.c +132 -0
  228. data/ext/libuv/src/win/winapi.h +4443 -0
  229. data/ext/libuv/src/win/winsock.c +557 -0
  230. data/ext/libuv/src/win/winsock.h +171 -0
  231. data/ext/libuv/test/benchmark-ares.c +117 -0
  232. data/ext/libuv/test/benchmark-fs-stat.c +164 -0
  233. data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
  234. data/ext/libuv/test/benchmark-list.h +114 -0
  235. data/ext/libuv/test/benchmark-loop-count.c +88 -0
  236. data/ext/libuv/test/benchmark-million-timers.c +65 -0
  237. data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
  238. data/ext/libuv/test/benchmark-pound.c +324 -0
  239. data/ext/libuv/test/benchmark-pump.c +462 -0
  240. data/ext/libuv/test/benchmark-sizes.c +42 -0
  241. data/ext/libuv/test/benchmark-spawn.c +162 -0
  242. data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
  243. data/ext/libuv/test/benchmark-thread.c +64 -0
  244. data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
  245. data/ext/libuv/test/blackhole-server.c +118 -0
  246. data/ext/libuv/test/dns-server.c +321 -0
  247. data/ext/libuv/test/echo-server.c +378 -0
  248. data/ext/libuv/test/fixtures/empty_file +0 -0
  249. data/ext/libuv/test/fixtures/load_error.node +1 -0
  250. data/ext/libuv/test/run-benchmarks.c +64 -0
  251. data/ext/libuv/test/run-tests.c +138 -0
  252. data/ext/libuv/test/runner-unix.c +290 -0
  253. data/ext/libuv/test/runner-unix.h +36 -0
  254. data/ext/libuv/test/runner-win.c +285 -0
  255. data/ext/libuv/test/runner-win.h +42 -0
  256. data/ext/libuv/test/runner.c +326 -0
  257. data/ext/libuv/test/runner.h +159 -0
  258. data/ext/libuv/test/task.h +109 -0
  259. data/ext/libuv/test/test-async.c +118 -0
  260. data/ext/libuv/test/test-callback-order.c +76 -0
  261. data/ext/libuv/test/test-callback-stack.c +203 -0
  262. data/ext/libuv/test/test-connection-fail.c +148 -0
  263. data/ext/libuv/test/test-counters-init.c +215 -0
  264. data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
  265. data/ext/libuv/test/test-delayed-accept.c +197 -0
  266. data/ext/libuv/test/test-dlerror.c +58 -0
  267. data/ext/libuv/test/test-error.c +59 -0
  268. data/ext/libuv/test/test-fail-always.c +29 -0
  269. data/ext/libuv/test/test-fs-event.c +442 -0
  270. data/ext/libuv/test/test-fs-poll.c +146 -0
  271. data/ext/libuv/test/test-fs.c +1843 -0
  272. data/ext/libuv/test/test-get-currentexe.c +63 -0
  273. data/ext/libuv/test/test-get-loadavg.c +36 -0
  274. data/ext/libuv/test/test-get-memory.c +38 -0
  275. data/ext/libuv/test/test-getaddrinfo.c +122 -0
  276. data/ext/libuv/test/test-gethostbyname.c +189 -0
  277. data/ext/libuv/test/test-getsockname.c +342 -0
  278. data/ext/libuv/test/test-hrtime.c +54 -0
  279. data/ext/libuv/test/test-idle.c +81 -0
  280. data/ext/libuv/test/test-ipc-send-recv.c +209 -0
  281. data/ext/libuv/test/test-ipc.c +620 -0
  282. data/ext/libuv/test/test-list.h +421 -0
  283. data/ext/libuv/test/test-loop-handles.c +336 -0
  284. data/ext/libuv/test/test-multiple-listen.c +102 -0
  285. data/ext/libuv/test/test-mutexes.c +63 -0
  286. data/ext/libuv/test/test-pass-always.c +28 -0
  287. data/ext/libuv/test/test-ping-pong.c +253 -0
  288. data/ext/libuv/test/test-pipe-bind-error.c +140 -0
  289. data/ext/libuv/test/test-pipe-connect-error.c +96 -0
  290. data/ext/libuv/test/test-platform-output.c +87 -0
  291. data/ext/libuv/test/test-poll-close.c +77 -0
  292. data/ext/libuv/test/test-poll.c +573 -0
  293. data/ext/libuv/test/test-process-title.c +42 -0
  294. data/ext/libuv/test/test-ref.c +338 -0
  295. data/ext/libuv/test/test-run-once.c +48 -0
  296. data/ext/libuv/test/test-semaphore.c +111 -0
  297. data/ext/libuv/test/test-shutdown-close.c +103 -0
  298. data/ext/libuv/test/test-shutdown-eof.c +183 -0
  299. data/ext/libuv/test/test-spawn.c +847 -0
  300. data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
  301. data/ext/libuv/test/test-tcp-bind-error.c +191 -0
  302. data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
  303. data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
  304. data/ext/libuv/test/test-tcp-close.c +129 -0
  305. data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  306. data/ext/libuv/test/test-tcp-connect-error.c +70 -0
  307. data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
  308. data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
  309. data/ext/libuv/test/test-tcp-flags.c +51 -0
  310. data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  311. data/ext/libuv/test/test-tcp-write-error.c +168 -0
  312. data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
  313. data/ext/libuv/test/test-tcp-writealot.c +170 -0
  314. data/ext/libuv/test/test-thread.c +183 -0
  315. data/ext/libuv/test/test-threadpool.c +57 -0
  316. data/ext/libuv/test/test-timer-again.c +141 -0
  317. data/ext/libuv/test/test-timer.c +152 -0
  318. data/ext/libuv/test/test-tty.c +110 -0
  319. data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
  320. data/ext/libuv/test/test-udp-ipv6.c +156 -0
  321. data/ext/libuv/test/test-udp-multicast-join.c +139 -0
  322. data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
  323. data/ext/libuv/test/test-udp-options.c +86 -0
  324. data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
  325. data/ext/libuv/test/test-util.c +97 -0
  326. data/ext/libuv/test/test-walk-handles.c +77 -0
  327. data/ext/libuv/uv.gyp +452 -0
  328. data/ext/libuv/vcbuild.bat +105 -0
  329. data/ext/racer/extconf.rb +20 -0
  330. data/ext/racer/http_parser.c +2059 -0
  331. data/ext/racer/http_parser.h +318 -0
  332. data/ext/racer/mkmf.log +22 -0
  333. data/ext/racer/racer.c +551 -0
  334. data/lib/racer.rb +2 -0
  335. data/lib/racer/version.rb +3 -0
  336. metadata +382 -0
@@ -0,0 +1,318 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to
5
+ * deal in the Software without restriction, including without limitation the
6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ * sell copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ * IN THE SOFTWARE.
20
+ */
21
+ #ifndef http_parser_h
22
+ #define http_parser_h
23
+ #ifdef __cplusplus
24
+ extern "C" {
25
+ #endif
26
+
27
+ #define HTTP_PARSER_VERSION_MAJOR 1
28
+ #define HTTP_PARSER_VERSION_MINOR 0
29
+
30
+ #include <sys/types.h>
31
+ #if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
32
+ typedef __int8 int8_t;
33
+ typedef unsigned __int8 uint8_t;
34
+ typedef __int16 int16_t;
35
+ typedef unsigned __int16 uint16_t;
36
+ typedef __int32 int32_t;
37
+ typedef unsigned __int32 uint32_t;
38
+ typedef __int64 int64_t;
39
+ typedef unsigned __int64 uint64_t;
40
+
41
+ typedef unsigned int size_t;
42
+ typedef int ssize_t;
43
+ #else
44
+ #include <stdint.h>
45
+ #endif
46
+
47
+ /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
48
+ * faster
49
+ */
50
+ #ifndef HTTP_PARSER_STRICT
51
+ # define HTTP_PARSER_STRICT 1
52
+ #endif
53
+
54
+ /* Compile with -DHTTP_PARSER_DEBUG=1 to add extra debugging information to
55
+ * the error reporting facility.
56
+ */
57
+ #ifndef HTTP_PARSER_DEBUG
58
+ # define HTTP_PARSER_DEBUG 0
59
+ #endif
60
+
61
+
62
+ /* Maximium header size allowed */
63
+ #define HTTP_MAX_HEADER_SIZE (80*1024)
64
+
65
+
66
+ typedef struct http_parser http_parser;
67
+ typedef struct http_parser_settings http_parser_settings;
68
+
69
+
70
+ /* Callbacks should return non-zero to indicate an error. The parser will
71
+ * then halt execution.
72
+ *
73
+ * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
74
+ * returning '1' from on_headers_complete will tell the parser that it
75
+ * should not expect a body. This is used when receiving a response to a
76
+ * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
77
+ * chunked' headers that indicate the presence of a body.
78
+ *
79
+ * http_data_cb does not return data chunks. It will be call arbitrarally
80
+ * many times for each string. E.G. you might get 10 callbacks for "on_path"
81
+ * each providing just a few characters more data.
82
+ */
83
+ typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
84
+ typedef int (*http_cb) (http_parser*);
85
+
86
+
87
+ /* Request Methods */
88
+ #define HTTP_METHOD_MAP(XX) \
89
+ XX(0, DELETE, DELETE) \
90
+ XX(1, GET, GET) \
91
+ XX(2, HEAD, HEAD) \
92
+ XX(3, POST, POST) \
93
+ XX(4, PUT, PUT) \
94
+ /* pathological */ \
95
+ XX(5, CONNECT, CONNECT) \
96
+ XX(6, OPTIONS, OPTIONS) \
97
+ XX(7, TRACE, TRACE) \
98
+ /* webdav */ \
99
+ XX(8, COPY, COPY) \
100
+ XX(9, LOCK, LOCK) \
101
+ XX(10, MKCOL, MKCOL) \
102
+ XX(11, MOVE, MOVE) \
103
+ XX(12, PROPFIND, PROPFIND) \
104
+ XX(13, PROPPATCH, PROPPATCH) \
105
+ XX(14, SEARCH, SEARCH) \
106
+ XX(15, UNLOCK, UNLOCK) \
107
+ /* subversion */ \
108
+ XX(16, REPORT, REPORT) \
109
+ XX(17, MKACTIVITY, MKACTIVITY) \
110
+ XX(18, CHECKOUT, CHECKOUT) \
111
+ XX(19, MERGE, MERGE) \
112
+ /* upnp */ \
113
+ XX(20, MSEARCH, M-SEARCH) \
114
+ XX(21, NOTIFY, NOTIFY) \
115
+ XX(22, SUBSCRIBE, SUBSCRIBE) \
116
+ XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
117
+ /* RFC-5789 */ \
118
+ XX(24, PATCH, PATCH) \
119
+ XX(25, PURGE, PURGE) \
120
+
121
+ enum http_method
122
+ {
123
+ #define XX(num, name, string) HTTP_##name = num,
124
+ HTTP_METHOD_MAP(XX)
125
+ #undef XX
126
+ };
127
+
128
+
129
+ enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
130
+
131
+
132
+ /* Flag values for http_parser.flags field */
133
+ enum flags
134
+ { F_CHUNKED = 1 << 0
135
+ , F_CONNECTION_KEEP_ALIVE = 1 << 1
136
+ , F_CONNECTION_CLOSE = 1 << 2
137
+ , F_TRAILING = 1 << 3
138
+ , F_UPGRADE = 1 << 4
139
+ , F_SKIPBODY = 1 << 5
140
+ };
141
+
142
+
143
+ /* Map for errno-related constants
144
+ *
145
+ * The provided argument should be a macro that takes 2 arguments.
146
+ */
147
+ #define HTTP_ERRNO_MAP(XX) \
148
+ /* No error */ \
149
+ XX(OK, "success") \
150
+ \
151
+ /* Callback-related errors */ \
152
+ XX(CB_message_begin, "the on_message_begin callback failed") \
153
+ XX(CB_url, "the on_url callback failed") \
154
+ XX(CB_header_field, "the on_header_field callback failed") \
155
+ XX(CB_header_value, "the on_header_value callback failed") \
156
+ XX(CB_headers_complete, "the on_headers_complete callback failed") \
157
+ XX(CB_body, "the on_body callback failed") \
158
+ XX(CB_message_complete, "the on_message_complete callback failed") \
159
+ \
160
+ /* Parsing-related errors */ \
161
+ XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
162
+ XX(HEADER_OVERFLOW, \
163
+ "too many header bytes seen; overflow detected") \
164
+ XX(CLOSED_CONNECTION, \
165
+ "data received after completed connection: close message") \
166
+ XX(INVALID_VERSION, "invalid HTTP version") \
167
+ XX(INVALID_STATUS, "invalid HTTP status code") \
168
+ XX(INVALID_METHOD, "invalid HTTP method") \
169
+ XX(INVALID_URL, "invalid URL") \
170
+ XX(INVALID_HOST, "invalid host") \
171
+ XX(INVALID_PORT, "invalid port") \
172
+ XX(INVALID_PATH, "invalid path") \
173
+ XX(INVALID_QUERY_STRING, "invalid query string") \
174
+ XX(INVALID_FRAGMENT, "invalid fragment") \
175
+ XX(LF_EXPECTED, "LF character expected") \
176
+ XX(INVALID_HEADER_TOKEN, "invalid character in header") \
177
+ XX(INVALID_CONTENT_LENGTH, \
178
+ "invalid character in content-length header") \
179
+ XX(INVALID_CHUNK_SIZE, \
180
+ "invalid character in chunk size header") \
181
+ XX(INVALID_CONSTANT, "invalid constant string") \
182
+ XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
183
+ XX(STRICT, "strict mode assertion failed") \
184
+ XX(PAUSED, "parser is paused") \
185
+ XX(UNKNOWN, "an unknown error occurred")
186
+
187
+
188
+ /* Define HPE_* values for each errno value above */
189
+ #define HTTP_ERRNO_GEN(n, s) HPE_##n,
190
+ enum http_errno {
191
+ HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
192
+ };
193
+ #undef HTTP_ERRNO_GEN
194
+
195
+
196
+ /* Get an http_errno value from an http_parser */
197
+ #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
198
+
199
+ /* Get the line number that generated the current error */
200
+ #if HTTP_PARSER_DEBUG
201
+ #define HTTP_PARSER_ERRNO_LINE(p) ((p)->error_lineno)
202
+ #else
203
+ #define HTTP_PARSER_ERRNO_LINE(p) 0
204
+ #endif
205
+
206
+
207
+ struct http_parser {
208
+ /** PRIVATE **/
209
+ unsigned char type : 2; /* enum http_parser_type */
210
+ unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
211
+ unsigned char state; /* enum state from http_parser.c */
212
+ unsigned char header_state; /* enum header_state from http_parser.c */
213
+ unsigned char index; /* index into current matcher */
214
+
215
+ uint32_t nread; /* # bytes read in various scenarios */
216
+ uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
217
+
218
+ /** READ-ONLY **/
219
+ unsigned short http_major;
220
+ unsigned short http_minor;
221
+ unsigned short status_code; /* responses only */
222
+ unsigned char method; /* requests only */
223
+ unsigned char http_errno : 7;
224
+
225
+ /* 1 = Upgrade header was present and the parser has exited because of that.
226
+ * 0 = No upgrade header present.
227
+ * Should be checked when http_parser_execute() returns in addition to
228
+ * error checking.
229
+ */
230
+ unsigned char upgrade : 1;
231
+
232
+ #if HTTP_PARSER_DEBUG
233
+ uint32_t error_lineno;
234
+ #endif
235
+
236
+ /** PUBLIC **/
237
+ void *data; /* A pointer to get hook to the "connection" or "socket" object */
238
+ };
239
+
240
+
241
+ struct http_parser_settings {
242
+ http_cb on_message_begin;
243
+ http_data_cb on_url;
244
+ http_data_cb on_header_field;
245
+ http_data_cb on_header_value;
246
+ http_cb on_headers_complete;
247
+ http_data_cb on_body;
248
+ http_cb on_message_complete;
249
+ };
250
+
251
+
252
+ enum http_parser_url_fields
253
+ { UF_SCHEMA = 0
254
+ , UF_HOST = 1
255
+ , UF_PORT = 2
256
+ , UF_PATH = 3
257
+ , UF_QUERY = 4
258
+ , UF_FRAGMENT = 5
259
+ , UF_MAX = 6
260
+ };
261
+
262
+
263
+ /* Result structure for http_parser_parse_url().
264
+ *
265
+ * Callers should index into field_data[] with UF_* values iff field_set
266
+ * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
267
+ * because we probably have padding left over), we convert any port to
268
+ * a uint16_t.
269
+ */
270
+ struct http_parser_url {
271
+ uint16_t field_set; /* Bitmask of (1 << UF_*) values */
272
+ uint16_t port; /* Converted UF_PORT string */
273
+
274
+ struct {
275
+ uint16_t off; /* Offset into buffer in which field starts */
276
+ uint16_t len; /* Length of run in buffer */
277
+ } field_data[UF_MAX];
278
+ };
279
+
280
+
281
+ void http_parser_init(http_parser *parser, enum http_parser_type type);
282
+
283
+
284
+ size_t http_parser_execute(http_parser *parser,
285
+ const http_parser_settings *settings,
286
+ const char *data,
287
+ size_t len);
288
+
289
+
290
+ /* If http_should_keep_alive() in the on_headers_complete or
291
+ * on_message_complete callback returns true, then this will be should be
292
+ * the last message on the connection.
293
+ * If you are the server, respond with the "Connection: close" header.
294
+ * If you are the client, close the connection.
295
+ */
296
+ int http_should_keep_alive(http_parser *parser);
297
+
298
+ /* Returns a string version of the HTTP method. */
299
+ const char *http_method_str(enum http_method m);
300
+
301
+ /* Return a string name of the given error */
302
+ const char *http_errno_name(enum http_errno err);
303
+
304
+ /* Return a string description of the given error */
305
+ const char *http_errno_description(enum http_errno err);
306
+
307
+ /* Parse a URL; return nonzero on failure */
308
+ int http_parser_parse_url(const char *buf, size_t buflen,
309
+ int is_connect,
310
+ struct http_parser_url *u);
311
+
312
+ /* Pause or un-pause the parser; a nonzero value pauses */
313
+ void http_parser_pause(http_parser *parser, int paused);
314
+
315
+ #ifdef __cplusplus
316
+ }
317
+ #endif
318
+ #endif
@@ -0,0 +1,22 @@
1
+ have_library: checking for main() in -luv... -------------------- yes
2
+
3
+ "clang -o conftest -I/Users/charlie/.rvm/rubies/ruby-1.9.3-p194/include/ruby-1.9.1/x86_64-darwin11.4.0 -I/Users/charlie/.rvm/rubies/ruby-1.9.3-p194/include/ruby-1.9.1/ruby/backward -I/Users/charlie/.rvm/rubies/ruby-1.9.3-p194/include/ruby-1.9.1 -I. -I/Users/charlie/code/ricer/ext/libuv/include -I/Users/charlie/.rvm/usr/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -O3 -g3 -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration -fno-common -pipe -std=c99 -fPIC conftest.c -L. -L/Users/charlie/.rvm/rubies/ruby-1.9.3-p194/lib -L/Users/charlie/code/ricer/ext/ricer -L/Users/charlie/.rvm/usr/lib -L. -lruby.1.9.1 -lpthread -ldl -lobjc "
4
+ checked program was:
5
+ /* begin */
6
+ 1: #include "ruby.h"
7
+ 2:
8
+ 3: int main() {return 0;}
9
+ /* end */
10
+
11
+ "clang -o conftest -I/Users/charlie/.rvm/rubies/ruby-1.9.3-p194/include/ruby-1.9.1/x86_64-darwin11.4.0 -I/Users/charlie/.rvm/rubies/ruby-1.9.3-p194/include/ruby-1.9.1/ruby/backward -I/Users/charlie/.rvm/rubies/ruby-1.9.3-p194/include/ruby-1.9.1 -I. -I/Users/charlie/code/ricer/ext/libuv/include -I/Users/charlie/.rvm/usr/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -O3 -g3 -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration -fno-common -pipe -std=c99 -fPIC conftest.c -L. -L/Users/charlie/.rvm/rubies/ruby-1.9.3-p194/lib -L/Users/charlie/code/ricer/ext/ricer -L/Users/charlie/.rvm/usr/lib -L. -lruby.1.9.1 -luv -lpthread -ldl -lobjc "
12
+ checked program was:
13
+ /* begin */
14
+ 1: #include "ruby.h"
15
+ 2:
16
+ 3: /*top*/
17
+ 4: int main() {return 0;}
18
+ 5: int t() { void ((*volatile p)()); p = (void ((*)()))main; return 0; }
19
+ /* end */
20
+
21
+ --------------------
22
+
@@ -0,0 +1,551 @@
1
+ #include <ruby.h>
2
+ #include <stdlib.h>
3
+ #include <stdint.h>
4
+ #include <string.h>
5
+ #include <stdio.h>
6
+ #include <ctype.h>
7
+ #include <errno.h>
8
+ #include <stdbool.h>
9
+ #include <uv.h>
10
+ #include <arpa/inet.h>
11
+ #include "http_parser.h"
12
+
13
+ #define SERVER_SOFTWARE "Racer"
14
+ #define MAX_HEADER_SIZE 4096
15
+
16
+ typedef enum last_callback {
17
+ CB_URL,
18
+ CB_HEADER_FIELD,
19
+ CB_HEADER_VALUE,
20
+ CB_BODY,
21
+ CB_COMPLETE
22
+ } last_callback_t;
23
+
24
+ typedef struct client {
25
+ uv_stream_t socket;
26
+ http_parser_settings parser_settings;
27
+ http_parser parser;
28
+ last_callback_t last_callback;
29
+ size_t total_header_size;
30
+ char* url;
31
+ size_t url_len;
32
+ char* header_field;
33
+ size_t header_field_len;
34
+ char* header_value;
35
+ size_t header_value_len;
36
+ VALUE body;
37
+ bool shutdown;
38
+ bool sent_server_header;
39
+ VALUE env;
40
+ } client_t;
41
+
42
+ static VALUE globals;
43
+
44
+ static VALUE Racer;
45
+ static VALUE RacerStats;
46
+ static ID i_new;
47
+ static ID i_call;
48
+ static ID i_to_i;
49
+ static ID i_each;
50
+ static ID i_close;
51
+ static uint16_t port;
52
+ static VALUE app = Qnil;
53
+ static uv_loop_t* loop;
54
+ static size_t total_requests;
55
+
56
+ static VALUE StringIO;
57
+
58
+ static VALUE sREQUEST_METHOD;
59
+ static VALUE sREQUEST_PATH;
60
+ static VALUE sREQUEST_URI;
61
+ static VALUE sSCRIPT_NAME;
62
+ static VALUE sPATH_INFO;
63
+ static VALUE sQUERY_STRING;
64
+ static VALUE sSERVER_NAME;
65
+ static VALUE sSERVER_SOFTWARE;
66
+ static VALUE sSERVER_PORT;
67
+ static VALUE sREQUEST_PATH;
68
+ static VALUE sRacer;
69
+ static VALUE s_empty;
70
+ static VALUE s_crlf;
71
+ static VALUE s_colon_space;
72
+
73
+ static VALUE sRackInput;
74
+ static VALUE sRackErrors;
75
+ static VALUE sRackVersion;
76
+ static VALUE RACK_VERSION;
77
+ static VALUE sRackMultithread;
78
+ static VALUE sRackMultiprocess;
79
+ static VALUE sRackRunOnce;
80
+ static VALUE sRackUrlScheme;
81
+ static VALUE HTTP_URL_SCHEME;
82
+
83
+ static uv_buf_t uv_racer_alloc(uv_handle_t* handle, size_t suggested_size)
84
+ {
85
+ return uv_buf_init((char*)malloc(suggested_size), (uint32_t)suggested_size);
86
+ }
87
+
88
+ static void uv_racer_free(uv_buf_t buff)
89
+ {
90
+ free(buff.base);
91
+ }
92
+
93
+ static void on_close(uv_handle_t* stream)
94
+ {
95
+ client_t* client = (client_t*)stream->data;
96
+ if(client->url) {
97
+ free(client->url);
98
+ }
99
+ if(client->header_field) {
100
+ free(client->header_field);
101
+ }
102
+ if(client->header_value) {
103
+ free(client->header_value);
104
+ }
105
+ rb_gc_unregister_address(&client->body);
106
+ rb_gc_unregister_address(&client->env);
107
+ free(client);
108
+ }
109
+
110
+ static void on_shutdown(uv_shutdown_t* req, int status)
111
+ {
112
+ free(req);
113
+ uv_close((uv_handle_t*)req->handle, on_close);
114
+ }
115
+
116
+ static int on_http_url(http_parser* parser, const char* buff, size_t length)
117
+ {
118
+ client_t* client = (client_t*)parser->data;
119
+ if((client->total_header_size += length) > MAX_HEADER_SIZE) {
120
+ // headers too long
121
+ return 1;
122
+ }
123
+ client->url = realloc(client->url, client->url_len + length);
124
+ memcpy(client->url + client->url_len, buff, length);
125
+ client->url_len += length;
126
+ client->last_callback = CB_URL;
127
+ return 0;
128
+ }
129
+
130
+ static void save_last_header_value(client_t* client)
131
+ {
132
+ char cgi_header_name[5 /* HTTP_ */ + client->header_field_len + 1];
133
+ memcpy(cgi_header_name, "HTTP_", 5);
134
+ for(size_t i = 0; i < client->header_field_len; i++) {
135
+ if(client->header_field[i] == '-') {
136
+ cgi_header_name[i + 5] = '_';
137
+ } else {
138
+ cgi_header_name[i + 5] = toupper(client->header_field[i]);
139
+ }
140
+ }
141
+ cgi_header_name[client->header_field_len + 5] = 0;
142
+ VALUE header_name = rb_obj_freeze(rb_str_new(cgi_header_name, 5 + client->header_field_len));
143
+ VALUE header_value = rb_obj_freeze(rb_str_new(client->header_value, client->header_value_len));
144
+ rb_hash_aset(client->env, header_name, header_value);
145
+ free(client->header_field);
146
+ client->header_field = NULL;
147
+ client->header_field_len = 0;
148
+ free(client->header_value);
149
+ client->header_value = NULL;
150
+ client->header_value_len = 0;
151
+ }
152
+
153
+ static int on_http_header_field(http_parser* parser, const char* buff, size_t length)
154
+ {
155
+ client_t* client = (client_t*)parser->data;
156
+ if(client->last_callback == CB_HEADER_VALUE) {
157
+ // we need to save the last header/value pair
158
+ save_last_header_value(client);
159
+ }
160
+ if((client->total_header_size += length) > MAX_HEADER_SIZE) {
161
+ // headers too long
162
+ return 1;
163
+ }
164
+
165
+ client->header_field = realloc(client->header_field, client->header_field_len + length);
166
+ memcpy(client->header_field + client->header_field_len, buff, length);
167
+ client->header_field_len += length;
168
+
169
+ client->last_callback = CB_HEADER_FIELD;
170
+
171
+ return 0;
172
+ }
173
+
174
+ static int on_http_body(http_parser* parser, const char* buff, size_t length)
175
+ {
176
+ client_t* client = (client_t*)parser->data;
177
+ if((client->total_header_size += length) > MAX_HEADER_SIZE) {
178
+ // headers too long
179
+ return 1;
180
+ }
181
+ rb_str_cat(client->body, buff, length);
182
+ client->last_callback = CB_BODY;
183
+ return 0;
184
+ }
185
+
186
+ static int on_http_header_value(http_parser* parser, const char* buff, size_t length)
187
+ {
188
+ client_t* client = (client_t*)parser->data;
189
+ if((client->total_header_size += length) > MAX_HEADER_SIZE) {
190
+ // headers too long
191
+ return 1;
192
+ }
193
+
194
+ client->header_value = realloc(client->header_value, client->header_value_len + length);
195
+ memcpy(client->header_value + client->header_value_len, buff, length);
196
+ client->header_value_len += length;
197
+
198
+ client->last_callback = CB_HEADER_VALUE;
199
+
200
+ return 0;
201
+ }
202
+
203
+ static int on_http_headers_complete(http_parser* parser)
204
+ {
205
+ client_t* client = (client_t*)parser->data;
206
+ if(client->last_callback == CB_HEADER_VALUE) {
207
+ // we need to save the last header/value pair
208
+ save_last_header_value(client);
209
+ }
210
+ return 0;
211
+ }
212
+
213
+ static VALUE collect_headers(VALUE i, VALUE str, int argc, VALUE* argv)
214
+ {
215
+ if(argc < 1 || TYPE(argv[0]) != T_ARRAY) {
216
+ return Qnil;
217
+ }
218
+ VALUE* nv = RARRAY_PTR(argv[0]);
219
+ if(RARRAY_LEN(argv[0]) < 2) {
220
+ return Qnil;
221
+ }
222
+
223
+ char* name = rb_string_value_cstr(&nv[0]);
224
+ if(strcmp(name, "Content-Length") == 0 || strcmp(name, "Connection") == 0) {
225
+ return Qnil;
226
+ }
227
+ size_t name_len = strlen(name);
228
+ char* value = rb_string_value_cstr(&nv[1]);
229
+
230
+ while(value) {
231
+ rb_str_cat(str, name, name_len);
232
+ rb_str_concat(str, s_colon_space);
233
+ char* next = strchr(value, '\n');
234
+ if(next) {
235
+ rb_str_cat(str, value, (int)(next - value));
236
+ value = next + 1;
237
+ } else {
238
+ rb_str_cat(str, value, strlen(value));
239
+ value = NULL;
240
+ }
241
+ rb_str_concat(str, s_crlf);
242
+ }
243
+ return Qnil;
244
+ }
245
+
246
+ static VALUE collect_body(VALUE i, VALUE str, int argc, VALUE* argv)
247
+ {
248
+ if(argc < 1) {
249
+ return Qnil;
250
+ }
251
+ rb_str_concat(str, argv[0]);
252
+ return Qnil;
253
+ }
254
+
255
+ static void on_write(uv_write_t* req, int status)
256
+ {
257
+ client_t* client = req->handle->data;
258
+ free(((uv_buf_t*)req->data)->base);
259
+ free(req->data);
260
+ free(req);
261
+ }
262
+
263
+ static void http_bad_request(client_t* client)
264
+ {
265
+ uv_buf_t* b = malloc(sizeof(uv_buf_t));
266
+ const char* response = "HTTP/1.1 400 Bad Request\r\nServer: " SERVER_SOFTWARE "\r\n\r\n<h1>Bad Request</h1>";
267
+ b->len = strlen(response);
268
+ b->base = malloc(b->len);
269
+ memcpy(b->base, response, b->len);
270
+ uv_write_t* req = malloc(sizeof(uv_write_t));
271
+ req->data = b;
272
+ uv_write(req, &client->socket, b, 1, on_write);
273
+ client->shutdown = true;
274
+ uv_read_stop(&client->socket);
275
+ uv_shutdown_t* shutdown = malloc(sizeof(uv_shutdown_t));
276
+ uv_shutdown(shutdown, &client->socket, on_shutdown);
277
+ }
278
+
279
+ static int on_http_message_complete(http_parser* parser)
280
+ {
281
+ client_t* client = (client_t*)parser->data;
282
+
283
+ // set url, path name, etc:
284
+ rb_hash_aset(client->env, sSCRIPT_NAME, s_empty);
285
+ rb_hash_aset(client->env, sREQUEST_URI, rb_obj_freeze(rb_str_new(client->url, client->url_len)));
286
+ char* query_string = memchr(client->url, '?', client->url_len);
287
+ if(query_string) {
288
+ VALUE path = rb_obj_freeze(rb_str_new(client->url, (size_t)(query_string - client->url)));
289
+ rb_hash_aset(client->env, sPATH_INFO, path);
290
+ rb_hash_aset(client->env, sREQUEST_PATH, path);
291
+ rb_hash_aset(client->env, sQUERY_STRING, rb_obj_freeze(rb_str_new(query_string + 1, client->url_len - (size_t)(query_string - client->url) - 1)));
292
+ } else {
293
+ VALUE path = rb_obj_freeze(rb_str_new(client->url, client->url_len));
294
+ rb_hash_aset(client->env, sPATH_INFO, path);
295
+ rb_hash_aset(client->env, sREQUEST_PATH, path);
296
+ rb_hash_aset(client->env, sQUERY_STRING, s_empty);
297
+ }
298
+
299
+ // set request method:
300
+ rb_hash_aset(client->env, sREQUEST_METHOD, rb_obj_freeze(rb_str_new_cstr(http_method_str(parser->method))));
301
+
302
+ // set IO streams:
303
+ rb_hash_aset(client->env, sRackInput, rb_funcall(StringIO, i_new, 1, client->body));
304
+ rb_hash_aset(client->env, sRackErrors, rb_stderr);
305
+
306
+ // call into app
307
+ VALUE response = rb_funcall(app, i_call, 1, client->env);
308
+ if(TYPE(response) != T_ARRAY || RARRAY_LEN(response) < 3) {
309
+ http_bad_request(client);
310
+ return 1;
311
+ }
312
+
313
+ VALUE* response_ary = RARRAY_PTR(response);
314
+ VALUE v_status = response_ary[0];
315
+ VALUE v_headers = response_ary[1];
316
+ VALUE v_body = response_ary[2];
317
+
318
+ int status = 0;
319
+ if(TYPE(v_status) != T_FIXNUM) {
320
+ v_status = rb_funcall(v_status, i_to_i, 0);
321
+ if(TYPE(v_status) != T_FIXNUM) {
322
+ http_bad_request(client);
323
+ return 1;
324
+ }
325
+ }
326
+ status = FIX2INT(v_status);
327
+
328
+ VALUE v_body_str = rb_str_new_cstr("");
329
+ rb_block_call(v_body, i_each, 0, NULL, collect_body, v_body_str);
330
+ if(rb_respond_to(v_body, i_close)) {
331
+ rb_funcall(v_body, i_close, 0);
332
+ }
333
+
334
+ char buff[256];
335
+ sprintf(buff, "HTTP/1.1 %d\r\nContent-Length: %ld\r\nConnection: close\r\n", status, RSTRING_LEN(v_body_str));
336
+ VALUE v_response_str = rb_str_new(buff, strlen(buff));
337
+ rb_block_call(v_headers, i_each, 0, NULL, collect_headers, v_response_str);
338
+ rb_str_concat(v_response_str, s_crlf);
339
+ rb_str_concat(v_response_str, v_body_str);
340
+
341
+ uv_buf_t* b = malloc(sizeof(uv_buf_t));
342
+ b->len = RSTRING_LEN(v_response_str);
343
+ b->base = malloc(b->len);
344
+ memcpy(b->base, RSTRING_PTR(v_response_str), b->len);
345
+ uv_write_t* req = malloc(sizeof(uv_write_t));
346
+ req->data = b;
347
+ uv_write(req, &client->socket, b, 1, on_write);
348
+
349
+ client->last_callback = CB_COMPLETE;
350
+
351
+ // done
352
+ return 0;
353
+ }
354
+
355
+ static int on_http_message_begin(http_parser* parser)
356
+ {
357
+ client_t* client = (client_t*)parser->data;
358
+
359
+ if(client->url) {
360
+ free(client->url);
361
+ client->url = NULL;
362
+ client->url_len = 0;
363
+ }
364
+
365
+ total_requests++;
366
+
367
+ // initialize environment for new request:
368
+ client->body = rb_str_new("", 0);
369
+ client->env = rb_hash_new();
370
+ rb_hash_aset(client->env, sSERVER_SOFTWARE, sRacer);
371
+ rb_hash_aset(client->env, sSERVER_PORT, INT2FIX(port));
372
+ rb_hash_aset(client->env, sRackVersion, RACK_VERSION);
373
+ rb_hash_aset(client->env, sRackMultithread, Qfalse);
374
+ rb_hash_aset(client->env, sRackMultiprocess, Qfalse /* or Qtrue? i have no clue ... */);
375
+ rb_hash_aset(client->env, sRackRunOnce, Qfalse);
376
+ rb_hash_aset(client->env, sRackUrlScheme, HTTP_URL_SCHEME /* TODO return https if applicable */);
377
+
378
+ return 0;
379
+ }
380
+
381
+ static void on_read(uv_stream_t* stream, ssize_t nread, uv_buf_t buff)
382
+ {
383
+ client_t* client = (client_t*)stream->data;
384
+ if(nread < 0) {
385
+ client->shutdown = 1;
386
+ uv_read_stop(stream);
387
+ uv_shutdown_t* shutdown = malloc(sizeof(uv_shutdown_t));
388
+ uv_shutdown(shutdown, stream, on_shutdown);
389
+ } else if(nread == 0) {
390
+ if(http_parser_execute(&client->parser, &client->parser_settings, buff.base, nread) != (size_t)nread) {
391
+ http_bad_request(client);
392
+ }
393
+ } else {
394
+ if(http_parser_execute(&client->parser, &client->parser_settings, buff.base, nread) != (size_t)nread) {
395
+ http_bad_request(client);
396
+ } else {
397
+ if(client->last_callback == CB_COMPLETE && !http_should_keep_alive(&client->parser)) {
398
+ client->shutdown = 1;
399
+ uv_read_stop(stream);
400
+ uv_shutdown_t* shutdown = malloc(sizeof(uv_shutdown_t));
401
+ uv_shutdown(shutdown, stream, on_shutdown);
402
+ }
403
+ }
404
+ }
405
+ uv_racer_free(buff);
406
+ }
407
+
408
+ static void on_connection(uv_stream_t* server, int status)
409
+ {
410
+ client_t* client = malloc(sizeof(client_t));
411
+ memset(client, 0, sizeof(client_t));
412
+ uv_tcp_init(loop, (uv_tcp_t*)&client->socket);
413
+ uv_accept(server, &client->socket);
414
+
415
+ http_parser_init(&client->parser, HTTP_REQUEST);
416
+ client->parser.data = client;
417
+ client->socket.data = client;
418
+ client->parser_settings.on_message_begin = on_http_message_begin;
419
+ client->parser_settings.on_url = on_http_url;
420
+ client->parser_settings.on_header_field = on_http_header_field;
421
+ client->parser_settings.on_header_value = on_http_header_value;
422
+ client->parser_settings.on_headers_complete = on_http_headers_complete;
423
+ client->parser_settings.on_body = on_http_body;
424
+ client->parser_settings.on_message_complete = on_http_message_complete;
425
+
426
+ client->env = Qnil;
427
+ rb_gc_register_address(&client->env);
428
+ client->body = Qnil;
429
+ rb_gc_register_address(&client->body);
430
+
431
+ uv_read_start((uv_stream_t*)&client->socket, uv_racer_alloc, on_read);
432
+ }
433
+
434
+ static void run(struct sockaddr_in addr, VALUE _app)
435
+ {
436
+ const char* err = NULL;
437
+ const char* msg = NULL;
438
+ int errcode = 0;
439
+
440
+ app = _app;
441
+ loop = uv_loop_new();
442
+
443
+ uv_tcp_t tcp;
444
+ uv_tcp_init(loop, &tcp);
445
+ if((errcode = uv_tcp_bind(&tcp, addr)) != 0) {
446
+ err = "uv_tcp_bind failed";
447
+ msg = uv_strerror(uv_last_error(loop));
448
+ goto cleanup;
449
+ }
450
+ if((errcode = uv_listen((uv_stream_t*)&tcp, 16, on_connection)) != 0) {
451
+ err = "uv_tcp_listen failed";
452
+ msg = uv_strerror(uv_last_error(loop));
453
+ goto cleanup;
454
+ }
455
+
456
+ uv_run(loop);
457
+
458
+ cleanup:
459
+ uv_loop_delete(loop);
460
+ app = Qnil;
461
+ loop = NULL;
462
+ if(err) {
463
+ rb_raise(rb_eRuntimeError, "%s: %s (%d)", err, msg, errcode);
464
+ }
465
+ }
466
+
467
+ static VALUE Racer_run(VALUE self, VALUE v_address, VALUE v_port, VALUE app)
468
+ {
469
+ if(loop) {
470
+ rb_raise(rb_eRuntimeError, "Racer instance already running");
471
+ }
472
+ long _port = FIX2INT(v_port);
473
+ if(_port <= 0 || _port >= 65536) {
474
+ // out of range
475
+ rb_raise(rb_eArgError, "port number outside valid range");
476
+ }
477
+ char* addr_str = rb_string_value_cstr(&v_address);
478
+ port = (uint16_t)_port;
479
+ struct sockaddr_in addr = uv_ip4_addr(addr_str, port);
480
+ run(addr, app);
481
+ return Qnil;
482
+ }
483
+
484
+ static VALUE RacerStats_total_requests(VALUE self)
485
+ {
486
+ return INT2FIX(total_requests);
487
+ }
488
+
489
+ static void on_sigint(int signal)
490
+ {
491
+ rb_interrupt();
492
+ }
493
+
494
+ void Init_racer()
495
+ {
496
+ rb_gc_register_address(&app);
497
+
498
+ globals = rb_ary_new();
499
+ rb_gc_register_address(&globals);
500
+
501
+ i_new = rb_intern("new");
502
+ i_call = rb_intern("call");
503
+ i_to_i = rb_intern("to_i");
504
+ i_each = rb_intern("each");
505
+ i_close = rb_intern("close");
506
+
507
+ Racer = rb_define_module("Racer");
508
+ rb_ary_push(globals, Racer);
509
+ rb_define_singleton_method(Racer, "run", Racer_run, 3);
510
+
511
+ RacerStats = rb_define_module_under(Racer, "Stats");
512
+ rb_ary_push(globals, RacerStats);
513
+
514
+ rb_define_singleton_method(RacerStats, "total_requests", RacerStats_total_requests, 0);
515
+
516
+ #define GLOBAL_STR(var, str) var = rb_obj_freeze(rb_str_new_cstr(str)); rb_ary_push(globals, var);
517
+
518
+ GLOBAL_STR(s_empty, "");
519
+ GLOBAL_STR(s_crlf, "\r\n");
520
+ GLOBAL_STR(s_colon_space, ": ");
521
+ GLOBAL_STR(sREQUEST_METHOD, "REQUEST_METHOD");
522
+ GLOBAL_STR(sREQUEST_PATH, "REQUEST_PATH");
523
+ GLOBAL_STR(sREQUEST_URI, "REQUEST_URI");
524
+ GLOBAL_STR(sSCRIPT_NAME, "SCRIPT_NAME");
525
+ GLOBAL_STR(sPATH_INFO, "PATH_INFO");
526
+ GLOBAL_STR(sQUERY_STRING, "QUERY_STRING");
527
+ GLOBAL_STR(sSERVER_NAME, "SERVER_NAME");
528
+ GLOBAL_STR(sSERVER_SOFTWARE, "SERVER_SOFTWARE");
529
+ GLOBAL_STR(sSERVER_PORT, "SERVER_PORT");
530
+ GLOBAL_STR(sRacer, SERVER_SOFTWARE);
531
+
532
+ GLOBAL_STR(sRackInput, "rack.input");
533
+ GLOBAL_STR(sRackErrors, "rack.errors");
534
+ GLOBAL_STR(sRackVersion, "rack.version");
535
+ GLOBAL_STR(sRackMultithread, "rack.multithread");
536
+ GLOBAL_STR(sRackMultiprocess, "rack.multiprocess");
537
+ GLOBAL_STR(sRackRunOnce, "rack.run_once");
538
+ GLOBAL_STR(sRackUrlScheme, "rack.url_scheme");
539
+ GLOBAL_STR(HTTP_URL_SCHEME, "http");
540
+
541
+ RACK_VERSION = rb_obj_freeze(rb_ary_new3(2, INT2FIX(1), INT2FIX(1)));
542
+ rb_ary_push(globals, RACK_VERSION);
543
+
544
+ rb_require("stringio");
545
+ StringIO = rb_const_get(rb_cObject, rb_intern("StringIO"));
546
+ rb_ary_push(globals, StringIO);
547
+
548
+ signal(SIGINT, on_sigint);
549
+
550
+ #undef GLOBAL_STR
551
+ }