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,1026 @@
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
+
22
+ #include "uv.h"
23
+ #include "internal.h"
24
+
25
+ #include <stdio.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+ #include <assert.h>
29
+ #include <errno.h>
30
+
31
+ #include <sys/types.h>
32
+ #include <sys/socket.h>
33
+ #include <sys/uio.h>
34
+ #include <sys/un.h>
35
+ #include <unistd.h>
36
+
37
+
38
+ static void uv__stream_connect(uv_stream_t*);
39
+ static void uv__write(uv_stream_t* stream);
40
+ static void uv__read(uv_stream_t* stream);
41
+ static void uv__stream_io(uv_loop_t* loop, uv__io_t* w, int events);
42
+
43
+
44
+ static size_t uv__buf_count(uv_buf_t bufs[], int bufcnt) {
45
+ size_t total = 0;
46
+ int i;
47
+
48
+ for (i = 0; i < bufcnt; i++) {
49
+ total += bufs[i].len;
50
+ }
51
+
52
+ return total;
53
+ }
54
+
55
+
56
+ void uv__stream_init(uv_loop_t* loop,
57
+ uv_stream_t* stream,
58
+ uv_handle_type type) {
59
+ uv__handle_init(loop, (uv_handle_t*)stream, type);
60
+ loop->counters.stream_init++;
61
+
62
+ stream->alloc_cb = NULL;
63
+ stream->close_cb = NULL;
64
+ stream->connection_cb = NULL;
65
+ stream->connect_req = NULL;
66
+ stream->shutdown_req = NULL;
67
+ stream->accepted_fd = -1;
68
+ stream->fd = -1;
69
+ stream->delayed_error = 0;
70
+ ngx_queue_init(&stream->write_queue);
71
+ ngx_queue_init(&stream->write_completed_queue);
72
+ stream->write_queue_size = 0;
73
+
74
+ uv__io_init(&stream->read_watcher, uv__stream_io, -1, 0);
75
+ uv__io_init(&stream->write_watcher, uv__stream_io, -1, 0);
76
+ }
77
+
78
+
79
+ int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
80
+ socklen_t yes;
81
+
82
+ assert(fd >= 0);
83
+ stream->fd = fd;
84
+
85
+ stream->flags |= flags;
86
+
87
+ if (stream->type == UV_TCP) {
88
+ /* Reuse the port address if applicable. */
89
+ yes = 1;
90
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) == -1) {
91
+ uv__set_sys_error(stream->loop, errno);
92
+ return -1;
93
+ }
94
+
95
+ if ((stream->flags & UV_TCP_NODELAY) &&
96
+ uv__tcp_nodelay((uv_tcp_t*)stream, 1)) {
97
+ return -1;
98
+ }
99
+
100
+ /* TODO Use delay the user passed in. */
101
+ if ((stream->flags & UV_TCP_KEEPALIVE) &&
102
+ uv__tcp_keepalive((uv_tcp_t*)stream, 1, 60)) {
103
+ return -1;
104
+ }
105
+ }
106
+
107
+ /* Associate the fd with each watcher. */
108
+ uv__io_set(&stream->read_watcher, uv__stream_io, fd, UV__IO_READ);
109
+ uv__io_set(&stream->write_watcher, uv__stream_io, fd, UV__IO_WRITE);
110
+
111
+ return 0;
112
+ }
113
+
114
+
115
+ void uv__stream_destroy(uv_stream_t* stream) {
116
+ uv_write_t* req;
117
+ ngx_queue_t* q;
118
+
119
+ assert(stream->flags & UV_CLOSED);
120
+
121
+ if (stream->connect_req) {
122
+ uv__req_unregister(stream->loop, stream->connect_req);
123
+ uv__set_artificial_error(stream->loop, UV_EINTR);
124
+ stream->connect_req->cb(stream->connect_req, -1);
125
+ stream->connect_req = NULL;
126
+ }
127
+
128
+ while (!ngx_queue_empty(&stream->write_queue)) {
129
+ q = ngx_queue_head(&stream->write_queue);
130
+ ngx_queue_remove(q);
131
+
132
+ req = ngx_queue_data(q, uv_write_t, queue);
133
+ uv__req_unregister(stream->loop, req);
134
+
135
+ if (req->bufs != req->bufsml)
136
+ free(req->bufs);
137
+
138
+ if (req->cb) {
139
+ uv__set_artificial_error(req->handle->loop, UV_EINTR);
140
+ req->cb(req, -1);
141
+ }
142
+ }
143
+
144
+ while (!ngx_queue_empty(&stream->write_completed_queue)) {
145
+ q = ngx_queue_head(&stream->write_completed_queue);
146
+ ngx_queue_remove(q);
147
+
148
+ req = ngx_queue_data(q, uv_write_t, queue);
149
+ uv__req_unregister(stream->loop, req);
150
+
151
+ if (req->cb) {
152
+ uv__set_sys_error(stream->loop, req->error);
153
+ req->cb(req, req->error ? -1 : 0);
154
+ }
155
+ }
156
+
157
+ if (stream->shutdown_req) {
158
+ uv__req_unregister(stream->loop, stream->shutdown_req);
159
+ uv__set_artificial_error(stream->loop, UV_EINTR);
160
+ stream->shutdown_req->cb(stream->shutdown_req, -1);
161
+ stream->shutdown_req = NULL;
162
+ }
163
+ }
164
+
165
+
166
+ void uv__server_io(uv_loop_t* loop, uv__io_t* w, int events) {
167
+ int fd;
168
+ uv_stream_t* stream = container_of(w, uv_stream_t, read_watcher);
169
+
170
+ assert(events == UV__IO_READ);
171
+ assert(!(stream->flags & UV_CLOSING));
172
+
173
+ if (stream->accepted_fd >= 0) {
174
+ uv__io_stop(loop, &stream->read_watcher);
175
+ return;
176
+ }
177
+
178
+ /* connection_cb can close the server socket while we're
179
+ * in the loop so check it on each iteration.
180
+ */
181
+ while (stream->fd != -1) {
182
+ assert(stream->accepted_fd < 0);
183
+ fd = uv__accept(stream->fd);
184
+
185
+ if (fd < 0) {
186
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
187
+ /* No problem. */
188
+ return;
189
+ } else if (errno == EMFILE) {
190
+ /* TODO special trick. unlock reserved socket, accept, close. */
191
+ return;
192
+ } else if (errno == ECONNABORTED) {
193
+ /* ignore */
194
+ continue;
195
+ } else {
196
+ uv__set_sys_error(stream->loop, errno);
197
+ stream->connection_cb((uv_stream_t*)stream, -1);
198
+ }
199
+ } else {
200
+ stream->accepted_fd = fd;
201
+ stream->connection_cb((uv_stream_t*)stream, 0);
202
+ if (stream->accepted_fd >= 0) {
203
+ /* The user hasn't yet accepted called uv_accept() */
204
+ uv__io_stop(stream->loop, &stream->read_watcher);
205
+ return;
206
+ }
207
+ }
208
+ }
209
+ }
210
+
211
+
212
+ int uv_accept(uv_stream_t* server, uv_stream_t* client) {
213
+ uv_stream_t* streamServer;
214
+ uv_stream_t* streamClient;
215
+ int saved_errno;
216
+ int status;
217
+
218
+ /* TODO document this */
219
+ assert(server->loop == client->loop);
220
+
221
+ saved_errno = errno;
222
+ status = -1;
223
+
224
+ streamServer = (uv_stream_t*)server;
225
+ streamClient = (uv_stream_t*)client;
226
+
227
+ if (streamServer->accepted_fd < 0) {
228
+ uv__set_sys_error(server->loop, EAGAIN);
229
+ goto out;
230
+ }
231
+
232
+ if (uv__stream_open(streamClient, streamServer->accepted_fd,
233
+ UV_STREAM_READABLE | UV_STREAM_WRITABLE)) {
234
+ /* TODO handle error */
235
+ close(streamServer->accepted_fd);
236
+ streamServer->accepted_fd = -1;
237
+ goto out;
238
+ }
239
+
240
+ uv__io_start(streamServer->loop, &streamServer->read_watcher);
241
+ streamServer->accepted_fd = -1;
242
+ status = 0;
243
+
244
+ out:
245
+ errno = saved_errno;
246
+ return status;
247
+ }
248
+
249
+
250
+ int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) {
251
+ int r;
252
+
253
+ switch (stream->type) {
254
+ case UV_TCP:
255
+ r = uv_tcp_listen((uv_tcp_t*)stream, backlog, cb);
256
+ break;
257
+
258
+ case UV_NAMED_PIPE:
259
+ r = uv_pipe_listen((uv_pipe_t*)stream, backlog, cb);
260
+ break;
261
+
262
+ default:
263
+ assert(0);
264
+ return -1;
265
+ }
266
+
267
+ if (r == 0)
268
+ uv__handle_start(stream);
269
+
270
+ return r;
271
+ }
272
+
273
+
274
+ uv_write_t* uv_write_queue_head(uv_stream_t* stream) {
275
+ ngx_queue_t* q;
276
+ uv_write_t* req;
277
+
278
+ if (ngx_queue_empty(&stream->write_queue)) {
279
+ return NULL;
280
+ }
281
+
282
+ q = ngx_queue_head(&stream->write_queue);
283
+ if (!q) {
284
+ return NULL;
285
+ }
286
+
287
+ req = ngx_queue_data(q, struct uv_write_s, queue);
288
+ assert(req);
289
+
290
+ return req;
291
+ }
292
+
293
+
294
+ static void uv__drain(uv_stream_t* stream) {
295
+ uv_shutdown_t* req;
296
+
297
+ assert(!uv_write_queue_head(stream));
298
+ assert(stream->write_queue_size == 0);
299
+
300
+ uv__io_stop(stream->loop, &stream->write_watcher);
301
+
302
+ /* Shutdown? */
303
+ if ((stream->flags & UV_STREAM_SHUTTING) &&
304
+ !(stream->flags & UV_CLOSING) &&
305
+ !(stream->flags & UV_STREAM_SHUT)) {
306
+ assert(stream->shutdown_req);
307
+
308
+ req = stream->shutdown_req;
309
+ stream->shutdown_req = NULL;
310
+ uv__req_unregister(stream->loop, req);
311
+
312
+ if (shutdown(stream->fd, SHUT_WR)) {
313
+ /* Error. Report it. User should call uv_close(). */
314
+ uv__set_sys_error(stream->loop, errno);
315
+ if (req->cb) {
316
+ req->cb(req, -1);
317
+ }
318
+ } else {
319
+ uv__set_sys_error(stream->loop, 0);
320
+ ((uv_handle_t*) stream)->flags |= UV_STREAM_SHUT;
321
+ if (req->cb) {
322
+ req->cb(req, 0);
323
+ }
324
+ }
325
+ }
326
+ }
327
+
328
+
329
+ static size_t uv__write_req_size(uv_write_t* req) {
330
+ size_t size;
331
+
332
+ size = uv__buf_count(req->bufs + req->write_index,
333
+ req->bufcnt - req->write_index);
334
+ assert(req->handle->write_queue_size >= size);
335
+
336
+ return size;
337
+ }
338
+
339
+
340
+ static void uv__write_req_finish(uv_write_t* req) {
341
+ uv_stream_t* stream = req->handle;
342
+
343
+ /* Pop the req off tcp->write_queue. */
344
+ ngx_queue_remove(&req->queue);
345
+ if (req->bufs != req->bufsml) {
346
+ free(req->bufs);
347
+ }
348
+ req->bufs = NULL;
349
+
350
+ /* Add it to the write_completed_queue where it will have its
351
+ * callback called in the near future.
352
+ */
353
+ ngx_queue_insert_tail(&stream->write_completed_queue, &req->queue);
354
+ uv__io_feed(stream->loop, &stream->write_watcher, UV__IO_WRITE);
355
+ }
356
+
357
+
358
+ /* On success returns NULL. On error returns a pointer to the write request
359
+ * which had the error.
360
+ */
361
+ static void uv__write(uv_stream_t* stream) {
362
+ uv_write_t* req;
363
+ struct iovec* iov;
364
+ int iovcnt;
365
+ ssize_t n;
366
+
367
+ if (stream->flags & UV_CLOSING) {
368
+ /* Handle was closed this tick. We've received a stale
369
+ * 'is writable' callback from the event loop, ignore.
370
+ */
371
+ return;
372
+ }
373
+
374
+ start:
375
+
376
+ assert(stream->fd >= 0);
377
+
378
+ /* Get the request at the head of the queue. */
379
+ req = uv_write_queue_head(stream);
380
+ if (!req) {
381
+ assert(stream->write_queue_size == 0);
382
+ return;
383
+ }
384
+
385
+ assert(req->handle == stream);
386
+
387
+ /*
388
+ * Cast to iovec. We had to have our own uv_buf_t instead of iovec
389
+ * because Windows's WSABUF is not an iovec.
390
+ */
391
+ assert(sizeof(uv_buf_t) == sizeof(struct iovec));
392
+ iov = (struct iovec*) &(req->bufs[req->write_index]);
393
+ iovcnt = req->bufcnt - req->write_index;
394
+
395
+ /*
396
+ * Now do the actual writev. Note that we've been updating the pointers
397
+ * inside the iov each time we write. So there is no need to offset it.
398
+ */
399
+
400
+ if (req->send_handle) {
401
+ struct msghdr msg;
402
+ char scratch[64];
403
+ struct cmsghdr *cmsg;
404
+ int fd_to_send = req->send_handle->fd;
405
+
406
+ assert(fd_to_send >= 0);
407
+
408
+ msg.msg_name = NULL;
409
+ msg.msg_namelen = 0;
410
+ msg.msg_iov = iov;
411
+ msg.msg_iovlen = iovcnt;
412
+ msg.msg_flags = 0;
413
+
414
+ msg.msg_control = (void*) scratch;
415
+ msg.msg_controllen = CMSG_LEN(sizeof(fd_to_send));
416
+
417
+ cmsg = CMSG_FIRSTHDR(&msg);
418
+ cmsg->cmsg_level = SOL_SOCKET;
419
+ cmsg->cmsg_type = SCM_RIGHTS;
420
+ cmsg->cmsg_len = msg.msg_controllen;
421
+ *(int*) CMSG_DATA(cmsg) = fd_to_send;
422
+
423
+ do {
424
+ n = sendmsg(stream->fd, &msg, 0);
425
+ }
426
+ while (n == -1 && errno == EINTR);
427
+ } else {
428
+ do {
429
+ if (iovcnt == 1) {
430
+ n = write(stream->fd, iov[0].iov_base, iov[0].iov_len);
431
+ } else {
432
+ n = writev(stream->fd, iov, iovcnt);
433
+ }
434
+ }
435
+ while (n == -1 && errno == EINTR);
436
+ }
437
+
438
+ if (n < 0) {
439
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
440
+ /* Error */
441
+ req->error = errno;
442
+ stream->write_queue_size -= uv__write_req_size(req);
443
+ uv__write_req_finish(req);
444
+ return;
445
+ } else if (stream->flags & UV_STREAM_BLOCKING) {
446
+ /* If this is a blocking stream, try again. */
447
+ goto start;
448
+ }
449
+ } else {
450
+ /* Successful write */
451
+
452
+ /* Update the counters. */
453
+ while (n >= 0) {
454
+ uv_buf_t* buf = &(req->bufs[req->write_index]);
455
+ size_t len = buf->len;
456
+
457
+ assert(req->write_index < req->bufcnt);
458
+
459
+ if ((size_t)n < len) {
460
+ buf->base += n;
461
+ buf->len -= n;
462
+ stream->write_queue_size -= n;
463
+ n = 0;
464
+
465
+ /* There is more to write. */
466
+ if (stream->flags & UV_STREAM_BLOCKING) {
467
+ /*
468
+ * If we're blocking then we should not be enabling the write
469
+ * watcher - instead we need to try again.
470
+ */
471
+ goto start;
472
+ } else {
473
+ /* Break loop and ensure the watcher is pending. */
474
+ break;
475
+ }
476
+
477
+ } else {
478
+ /* Finished writing the buf at index req->write_index. */
479
+ req->write_index++;
480
+
481
+ assert((size_t)n >= len);
482
+ n -= len;
483
+
484
+ assert(stream->write_queue_size >= len);
485
+ stream->write_queue_size -= len;
486
+
487
+ if (req->write_index == req->bufcnt) {
488
+ /* Then we're done! */
489
+ assert(n == 0);
490
+ uv__write_req_finish(req);
491
+ /* TODO: start trying to write the next request. */
492
+ return;
493
+ }
494
+ }
495
+ }
496
+ }
497
+
498
+ /* Either we've counted n down to zero or we've got EAGAIN. */
499
+ assert(n == 0 || n == -1);
500
+
501
+ /* Only non-blocking streams should use the write_watcher. */
502
+ assert(!(stream->flags & UV_STREAM_BLOCKING));
503
+
504
+ /* We're not done. */
505
+ uv__io_start(stream->loop, &stream->write_watcher);
506
+ }
507
+
508
+
509
+ static void uv__write_callbacks(uv_stream_t* stream) {
510
+ uv_write_t* req;
511
+ ngx_queue_t* q;
512
+
513
+ while (!ngx_queue_empty(&stream->write_completed_queue)) {
514
+ /* Pop a req off write_completed_queue. */
515
+ q = ngx_queue_head(&stream->write_completed_queue);
516
+ req = ngx_queue_data(q, uv_write_t, queue);
517
+ ngx_queue_remove(q);
518
+ uv__req_unregister(stream->loop, req);
519
+
520
+ /* NOTE: call callback AFTER freeing the request data. */
521
+ if (req->cb) {
522
+ uv__set_sys_error(stream->loop, req->error);
523
+ req->cb(req, req->error ? -1 : 0);
524
+ }
525
+ }
526
+
527
+ assert(ngx_queue_empty(&stream->write_completed_queue));
528
+
529
+ /* Write queue drained. */
530
+ if (!uv_write_queue_head(stream)) {
531
+ uv__drain(stream);
532
+ }
533
+ }
534
+
535
+
536
+ static uv_handle_type uv__handle_type(int fd) {
537
+ struct sockaddr_storage ss;
538
+ socklen_t len;
539
+
540
+ memset(&ss, 0, sizeof(ss));
541
+ len = sizeof(ss);
542
+
543
+ if (getsockname(fd, (struct sockaddr*)&ss, &len))
544
+ return UV_UNKNOWN_HANDLE;
545
+
546
+ switch (ss.ss_family) {
547
+ case AF_UNIX:
548
+ return UV_NAMED_PIPE;
549
+ case AF_INET:
550
+ case AF_INET6:
551
+ return UV_TCP;
552
+ }
553
+
554
+ return UV_UNKNOWN_HANDLE;
555
+ }
556
+
557
+
558
+ static void uv__read(uv_stream_t* stream) {
559
+ uv_buf_t buf;
560
+ ssize_t nread;
561
+ struct msghdr msg;
562
+ struct cmsghdr* cmsg;
563
+ char cmsg_space[64];
564
+ int count;
565
+
566
+ /* Prevent loop starvation when the data comes in as fast as (or faster than)
567
+ * we can read it. XXX Need to rearm fd if we switch to edge-triggered I/O.
568
+ */
569
+ count = 32;
570
+
571
+ /* XXX: Maybe instead of having UV_STREAM_READING we just test if
572
+ * tcp->read_cb is NULL or not?
573
+ */
574
+ while ((stream->read_cb || stream->read2_cb)
575
+ && (stream->flags & UV_STREAM_READING)
576
+ && (count-- > 0)) {
577
+ assert(stream->alloc_cb);
578
+ buf = stream->alloc_cb((uv_handle_t*)stream, 64 * 1024);
579
+
580
+ assert(buf.len > 0);
581
+ assert(buf.base);
582
+ assert(stream->fd >= 0);
583
+
584
+ if (stream->read_cb) {
585
+ do {
586
+ nread = read(stream->fd, buf.base, buf.len);
587
+ }
588
+ while (nread < 0 && errno == EINTR);
589
+ } else {
590
+ assert(stream->read2_cb);
591
+ /* read2_cb uses recvmsg */
592
+ msg.msg_flags = 0;
593
+ msg.msg_iov = (struct iovec*) &buf;
594
+ msg.msg_iovlen = 1;
595
+ msg.msg_name = NULL;
596
+ msg.msg_namelen = 0;
597
+ /* Set up to receive a descriptor even if one isn't in the message */
598
+ msg.msg_controllen = 64;
599
+ msg.msg_control = (void *) cmsg_space;
600
+
601
+ do {
602
+ nread = recvmsg(stream->fd, &msg, 0);
603
+ }
604
+ while (nread < 0 && errno == EINTR);
605
+ }
606
+
607
+
608
+ if (nread < 0) {
609
+ /* Error */
610
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
611
+ /* Wait for the next one. */
612
+ if (stream->flags & UV_STREAM_READING) {
613
+ uv__io_start(stream->loop, &stream->read_watcher);
614
+ }
615
+ uv__set_sys_error(stream->loop, EAGAIN);
616
+
617
+ if (stream->read_cb) {
618
+ stream->read_cb(stream, 0, buf);
619
+ } else {
620
+ stream->read2_cb((uv_pipe_t*)stream, 0, buf, UV_UNKNOWN_HANDLE);
621
+ }
622
+
623
+ return;
624
+ } else {
625
+ /* Error. User should call uv_close(). */
626
+ uv__set_sys_error(stream->loop, errno);
627
+
628
+ if (stream->read_cb) {
629
+ stream->read_cb(stream, -1, buf);
630
+ } else {
631
+ stream->read2_cb((uv_pipe_t*)stream, -1, buf, UV_UNKNOWN_HANDLE);
632
+ }
633
+
634
+ assert(!uv__io_active(&stream->read_watcher));
635
+ return;
636
+ }
637
+
638
+ } else if (nread == 0) {
639
+ /* EOF */
640
+ uv__set_artificial_error(stream->loop, UV_EOF);
641
+ uv__io_stop(stream->loop, &stream->read_watcher);
642
+ if (!uv__io_active(&stream->write_watcher))
643
+ uv__handle_stop(stream);
644
+
645
+ if (stream->read_cb) {
646
+ stream->read_cb(stream, -1, buf);
647
+ } else {
648
+ stream->read2_cb((uv_pipe_t*)stream, -1, buf, UV_UNKNOWN_HANDLE);
649
+ }
650
+ return;
651
+ } else {
652
+ /* Successful read */
653
+ ssize_t buflen = buf.len;
654
+
655
+ if (stream->read_cb) {
656
+ stream->read_cb(stream, nread, buf);
657
+ } else {
658
+ assert(stream->read2_cb);
659
+
660
+ /*
661
+ * XXX: Some implementations can send multiple file descriptors in a
662
+ * single message. We should be using CMSG_NXTHDR() to walk the
663
+ * chain to get at them all. This would require changing the API to
664
+ * hand these back up the caller, is a pain.
665
+ */
666
+
667
+ for (cmsg = CMSG_FIRSTHDR(&msg);
668
+ msg.msg_controllen > 0 && cmsg != NULL;
669
+ cmsg = CMSG_NXTHDR(&msg, cmsg)) {
670
+
671
+ if (cmsg->cmsg_type == SCM_RIGHTS) {
672
+ if (stream->accepted_fd != -1) {
673
+ fprintf(stderr, "(libuv) ignoring extra FD received\n");
674
+ }
675
+
676
+ stream->accepted_fd = *(int *) CMSG_DATA(cmsg);
677
+
678
+ } else {
679
+ fprintf(stderr, "ignoring non-SCM_RIGHTS ancillary data: %d\n",
680
+ cmsg->cmsg_type);
681
+ }
682
+ }
683
+
684
+
685
+ if (stream->accepted_fd >= 0) {
686
+ stream->read2_cb((uv_pipe_t*)stream, nread, buf,
687
+ uv__handle_type(stream->accepted_fd));
688
+ } else {
689
+ stream->read2_cb((uv_pipe_t*)stream, nread, buf, UV_UNKNOWN_HANDLE);
690
+ }
691
+ }
692
+
693
+ /* Return if we didn't fill the buffer, there is no more data to read. */
694
+ if (nread < buflen) {
695
+ return;
696
+ }
697
+ }
698
+ }
699
+ }
700
+
701
+
702
+ int uv_shutdown(uv_shutdown_t* req, uv_stream_t* stream, uv_shutdown_cb cb) {
703
+ assert((stream->type == UV_TCP || stream->type == UV_NAMED_PIPE) &&
704
+ "uv_shutdown (unix) only supports uv_handle_t right now");
705
+ assert(stream->fd >= 0);
706
+
707
+ if (!(stream->flags & UV_STREAM_WRITABLE) ||
708
+ stream->flags & UV_STREAM_SHUT ||
709
+ stream->flags & UV_CLOSED ||
710
+ stream->flags & UV_CLOSING) {
711
+ uv__set_artificial_error(stream->loop, UV_ENOTCONN);
712
+ return -1;
713
+ }
714
+
715
+ /* Initialize request */
716
+ uv__req_init(stream->loop, req, UV_SHUTDOWN);
717
+ req->handle = stream;
718
+ req->cb = cb;
719
+ stream->shutdown_req = req;
720
+ stream->flags |= UV_STREAM_SHUTTING;
721
+
722
+ uv__io_start(stream->loop, &stream->write_watcher);
723
+
724
+ return 0;
725
+ }
726
+
727
+
728
+ static void uv__stream_io(uv_loop_t* loop, uv__io_t* w, int events) {
729
+ uv_stream_t* stream;
730
+
731
+ /* either UV__IO_READ or UV__IO_WRITE but not both */
732
+ assert(!!(events & UV__IO_READ) ^ !!(events & UV__IO_WRITE));
733
+
734
+ if (events & UV__IO_READ)
735
+ stream = container_of(w, uv_stream_t, read_watcher);
736
+ else
737
+ stream = container_of(w, uv_stream_t, write_watcher);
738
+
739
+ assert(stream->type == UV_TCP ||
740
+ stream->type == UV_NAMED_PIPE ||
741
+ stream->type == UV_TTY);
742
+ assert(!(stream->flags & UV_CLOSING));
743
+
744
+ if (stream->connect_req)
745
+ uv__stream_connect(stream);
746
+ else if (events & UV__IO_READ) {
747
+ assert(stream->fd >= 0);
748
+ uv__read(stream);
749
+ }
750
+ else {
751
+ assert(stream->fd >= 0);
752
+ uv__write(stream);
753
+ uv__write_callbacks(stream);
754
+ }
755
+ }
756
+
757
+
758
+ /**
759
+ * We get called here from directly following a call to connect(2).
760
+ * In order to determine if we've errored out or succeeded must call
761
+ * getsockopt.
762
+ */
763
+ static void uv__stream_connect(uv_stream_t* stream) {
764
+ int error;
765
+ uv_connect_t* req = stream->connect_req;
766
+ socklen_t errorsize = sizeof(int);
767
+
768
+ assert(stream->type == UV_TCP || stream->type == UV_NAMED_PIPE);
769
+ assert(req);
770
+
771
+ if (stream->delayed_error) {
772
+ /* To smooth over the differences between unixes errors that
773
+ * were reported synchronously on the first connect can be delayed
774
+ * until the next tick--which is now.
775
+ */
776
+ error = stream->delayed_error;
777
+ stream->delayed_error = 0;
778
+ } else {
779
+ /* Normal situation: we need to get the socket error from the kernel. */
780
+ assert(stream->fd >= 0);
781
+ getsockopt(stream->fd, SOL_SOCKET, SO_ERROR, &error, &errorsize);
782
+ }
783
+
784
+ if (error == EINPROGRESS)
785
+ return;
786
+
787
+ if (error == 0)
788
+ uv__io_start(stream->loop, &stream->read_watcher);
789
+
790
+ stream->connect_req = NULL;
791
+ uv__req_unregister(stream->loop, req);
792
+
793
+ if (req->cb) {
794
+ uv__set_sys_error(stream->loop, error);
795
+ req->cb(req, error ? -1 : 0);
796
+ }
797
+ }
798
+
799
+
800
+ int uv__connect(uv_connect_t* req, uv_stream_t* stream, struct sockaddr* addr,
801
+ socklen_t addrlen, uv_connect_cb cb) {
802
+ int sockfd;
803
+ int r;
804
+
805
+ if (stream->fd <= 0) {
806
+ if ((sockfd = uv__socket(addr->sa_family, SOCK_STREAM, 0)) == -1) {
807
+ uv__set_sys_error(stream->loop, errno);
808
+ return -1;
809
+ }
810
+
811
+ if (uv__stream_open(stream,
812
+ sockfd,
813
+ UV_STREAM_READABLE | UV_STREAM_WRITABLE)) {
814
+ close(sockfd);
815
+ return -2;
816
+ }
817
+ }
818
+
819
+ uv__req_init(stream->loop, req, UV_CONNECT);
820
+ req->cb = cb;
821
+ req->handle = stream;
822
+ ngx_queue_init(&req->queue);
823
+
824
+ if (stream->connect_req) {
825
+ uv__set_sys_error(stream->loop, EALREADY);
826
+ return -1;
827
+ }
828
+
829
+ if (stream->type != UV_TCP) {
830
+ uv__set_sys_error(stream->loop, ENOTSOCK);
831
+ return -1;
832
+ }
833
+
834
+ stream->connect_req = req;
835
+
836
+ do {
837
+ r = connect(stream->fd, addr, addrlen);
838
+ }
839
+ while (r == -1 && errno == EINTR);
840
+
841
+ stream->delayed_error = 0;
842
+
843
+ if (r != 0 && errno != EINPROGRESS) {
844
+ switch (errno) {
845
+ /* If we get a ECONNREFUSED wait until the next tick to report the
846
+ * error. Solaris wants to report immediately--other unixes want to
847
+ * wait.
848
+ *
849
+ * XXX: do the same for ECONNABORTED?
850
+ */
851
+ case ECONNREFUSED:
852
+ stream->delayed_error = errno;
853
+ break;
854
+
855
+ default:
856
+ uv__set_sys_error(stream->loop, errno);
857
+ return -1;
858
+ }
859
+ }
860
+
861
+ uv__io_start(stream->loop, &stream->write_watcher);
862
+
863
+ if (stream->delayed_error)
864
+ uv__io_feed(stream->loop, &stream->write_watcher, UV__IO_WRITE);
865
+
866
+ return 0;
867
+ }
868
+
869
+
870
+ int uv_write2(uv_write_t* req, uv_stream_t* stream, uv_buf_t bufs[], int bufcnt,
871
+ uv_stream_t* send_handle, uv_write_cb cb) {
872
+ int empty_queue;
873
+
874
+ assert((stream->type == UV_TCP || stream->type == UV_NAMED_PIPE ||
875
+ stream->type == UV_TTY) &&
876
+ "uv_write (unix) does not yet support other types of streams");
877
+
878
+ if (stream->fd < 0) {
879
+ uv__set_sys_error(stream->loop, EBADF);
880
+ return -1;
881
+ }
882
+
883
+ if (send_handle) {
884
+ if (stream->type != UV_NAMED_PIPE || !((uv_pipe_t*)stream)->ipc) {
885
+ uv__set_sys_error(stream->loop, EOPNOTSUPP);
886
+ return -1;
887
+ }
888
+ }
889
+
890
+ empty_queue = (stream->write_queue_size == 0);
891
+
892
+ /* Initialize the req */
893
+ uv__req_init(stream->loop, req, UV_WRITE);
894
+ req->cb = cb;
895
+ req->handle = stream;
896
+ req->error = 0;
897
+ req->send_handle = send_handle;
898
+ ngx_queue_init(&req->queue);
899
+
900
+ if (bufcnt <= UV_REQ_BUFSML_SIZE)
901
+ req->bufs = req->bufsml;
902
+ else
903
+ req->bufs = malloc(sizeof(uv_buf_t) * bufcnt);
904
+
905
+ memcpy(req->bufs, bufs, bufcnt * sizeof(uv_buf_t));
906
+ req->bufcnt = bufcnt;
907
+ req->write_index = 0;
908
+ stream->write_queue_size += uv__buf_count(bufs, bufcnt);
909
+
910
+ /* Append the request to write_queue. */
911
+ ngx_queue_insert_tail(&stream->write_queue, &req->queue);
912
+
913
+ /* If the queue was empty when this function began, we should attempt to
914
+ * do the write immediately. Otherwise start the write_watcher and wait
915
+ * for the fd to become writable.
916
+ */
917
+ if (stream->connect_req) {
918
+ /* Still connecting, do nothing. */
919
+ }
920
+ else if (empty_queue) {
921
+ uv__write(stream);
922
+ }
923
+ else {
924
+ /*
925
+ * blocking streams should never have anything in the queue.
926
+ * if this assert fires then somehow the blocking stream isn't being
927
+ * sufficiently flushed in uv__write.
928
+ */
929
+ assert(!(stream->flags & UV_STREAM_BLOCKING));
930
+ uv__io_start(stream->loop, &stream->write_watcher);
931
+ }
932
+
933
+ return 0;
934
+ }
935
+
936
+
937
+ /* The buffers to be written must remain valid until the callback is called.
938
+ * This is not required for the uv_buf_t array.
939
+ */
940
+ int uv_write(uv_write_t* req, uv_stream_t* stream, uv_buf_t bufs[], int bufcnt,
941
+ uv_write_cb cb) {
942
+ return uv_write2(req, stream, bufs, bufcnt, NULL, cb);
943
+ }
944
+
945
+
946
+ int uv__read_start_common(uv_stream_t* stream, uv_alloc_cb alloc_cb,
947
+ uv_read_cb read_cb, uv_read2_cb read2_cb) {
948
+ assert(stream->type == UV_TCP || stream->type == UV_NAMED_PIPE ||
949
+ stream->type == UV_TTY);
950
+
951
+ if (stream->flags & UV_CLOSING) {
952
+ uv__set_sys_error(stream->loop, EINVAL);
953
+ return -1;
954
+ }
955
+
956
+ /* The UV_STREAM_READING flag is irrelevant of the state of the tcp - it just
957
+ * expresses the desired state of the user.
958
+ */
959
+ stream->flags |= UV_STREAM_READING;
960
+
961
+ /* TODO: try to do the read inline? */
962
+ /* TODO: keep track of tcp state. If we've gotten a EOF then we should
963
+ * not start the IO watcher.
964
+ */
965
+ assert(stream->fd >= 0);
966
+ assert(alloc_cb);
967
+
968
+ stream->read_cb = read_cb;
969
+ stream->read2_cb = read2_cb;
970
+ stream->alloc_cb = alloc_cb;
971
+
972
+ uv__io_start(stream->loop, &stream->read_watcher);
973
+ uv__handle_start(stream);
974
+
975
+ return 0;
976
+ }
977
+
978
+
979
+ int uv_read_start(uv_stream_t* stream, uv_alloc_cb alloc_cb,
980
+ uv_read_cb read_cb) {
981
+ return uv__read_start_common(stream, alloc_cb, read_cb, NULL);
982
+ }
983
+
984
+
985
+ int uv_read2_start(uv_stream_t* stream, uv_alloc_cb alloc_cb,
986
+ uv_read2_cb read_cb) {
987
+ return uv__read_start_common(stream, alloc_cb, NULL, read_cb);
988
+ }
989
+
990
+
991
+ int uv_read_stop(uv_stream_t* stream) {
992
+ uv__io_stop(stream->loop, &stream->read_watcher);
993
+ uv__handle_stop(stream);
994
+ stream->flags &= ~UV_STREAM_READING;
995
+ stream->read_cb = NULL;
996
+ stream->read2_cb = NULL;
997
+ stream->alloc_cb = NULL;
998
+ return 0;
999
+ }
1000
+
1001
+
1002
+ int uv_is_readable(const uv_stream_t* stream) {
1003
+ return stream->flags & UV_STREAM_READABLE;
1004
+ }
1005
+
1006
+
1007
+ int uv_is_writable(const uv_stream_t* stream) {
1008
+ return stream->flags & UV_STREAM_WRITABLE;
1009
+ }
1010
+
1011
+
1012
+ void uv__stream_close(uv_stream_t* handle) {
1013
+ uv_read_stop(handle);
1014
+ uv__io_stop(handle->loop, &handle->write_watcher);
1015
+
1016
+ close(handle->fd);
1017
+ handle->fd = -1;
1018
+
1019
+ if (handle->accepted_fd >= 0) {
1020
+ close(handle->accepted_fd);
1021
+ handle->accepted_fd = -1;
1022
+ }
1023
+
1024
+ assert(!uv__io_active(&handle->read_watcher));
1025
+ assert(!uv__io_active(&handle->write_watcher));
1026
+ }