選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

297 行
9.5 KiB

9年前
9年前
  1. # ibrowse [![Build Status](https://secure.travis-ci.org/cmullaparthi/ibrowse.svg?branch=master)](http://travis-ci.org/cmullaparthi/ibrowse)
  2. ibrowse is a HTTP client written in erlang.
  3. **License:** ibrowse is available under two different licenses.
  4. LGPL or the BSD license.
  5. **Comments to:** chandrashekhar.mullaparthi@gmail.com
  6. **Current Version:** 4.4.2
  7. **Latest Version:** git://github.com/cmullaparthi/ibrowse.git
  8. ## Features
  9. * [RFC2616](http://www.ietf.org/rfc/rfc2616.txt) compliant (AFAIK)
  10. * supports GET, POST, OPTIONS, HEAD, PUT, DELETE, TRACE,
  11. MKCOL, PROPFIND, PROPPATCH, LOCK, UNLOCK, MOVE and COPY
  12. * Understands HTTP/0.9, HTTP/1.0 and HTTP/1.1
  13. * Understands chunked encoding
  14. * Can generate requests using [Chunked Transfer-Encoding](http://en.wikipedia.org/wiki/Chunked_transfer_encoding)
  15. * Pools of connections to each webserver
  16. * Pipelining support
  17. * Download to file
  18. * Asynchronous requests. Responses are streamed to a process
  19. * Basic authentication
  20. * Supports proxy authentication
  21. * Supports SOCKS5
  22. * Authentication methods 0 (No authentication) and 2(Username/password) supported
  23. * Can talk to secure webservers using SSL
  24. * *Any other features in the code not listed here :)*
  25. ## Usage Examples
  26. Remember to start ibrowse first:
  27. ```erlang
  28. 5> ibrowse:start().
  29. {ok,<0.94.0>}
  30. ```
  31. ### Synchronous Requests
  32. A simple `GET` request:
  33. ```erlang
  34. 6> ibrowse:send_req("http://intranet/messenger/", [], get).
  35. {ok,"200",
  36. [{"Server","Microsoft-IIS/5.0"},
  37. {"Content-Location","http://intranet/messenger/index.html"},
  38. {"Date","Fri, 17 Dec 2004 15:16:19 GMT"},
  39. {"Content-Type","text/html"},
  40. {"Accept-Ranges","bytes"},
  41. {"Last-Modified","Fri, 17 Dec 2004 08:38:21 GMT"},
  42. {"Etag","\"aa7c9dc313e4c41:d77\""},
  43. {"Content-Length","953"}],
  44. "<html>...</html>"}
  45. ```
  46. A `GET` using a proxy:
  47. ```erlang
  48. 7> ibrowse:send_req("http://www.google.com/", [], get, [],
  49. [{proxy_user, "XXXXX"},
  50. {proxy_password, "XXXXX"},
  51. {proxy_host, "proxy"},
  52. {proxy_port, 8080}], 1000).
  53. {ok,"302",
  54. [{"Date","Fri, 17 Dec 2004 15:22:56 GMT"},
  55. {"Content-Length","217"},
  56. {"Content-Type","text/html"},
  57. {"Set-Cookie",
  58. "PREF=ID=f58155c797f9..."},
  59. {"Server","GWS/2.1"},
  60. {"Location",
  61. "http://www.google.co.uk/cxfer?c=PREF%3D:TM%3D110329..."},
  62. {"Via","1.1 netapp01 (NetCache NetApp/5.5R2)"}],
  63. "<HTML>...</HTML>\r\n"}
  64. ```
  65. A `GET` response saved to file. A temporary file is created and the
  66. filename returned. The response will only be saved to file if the
  67. status code is in the `200` range. The directory to download to can
  68. be set using the application env var `download_dir` - the default
  69. is the current working directory:
  70. ```erlang
  71. 8> ibrowse:send_req("http://www.erlang.se/", [], get, [],
  72. [{proxy_user, "XXXXX"},
  73. {proxy_password, "XXXXX"},
  74. {proxy_host, "proxy"},
  75. {proxy_port, 8080},
  76. {save_response_to_file, true}], 1000).
  77. {error,req_timedout}
  78. 9> ibrowse:send_req("http://www.erlang.se/", [], get, [],
  79. [{proxy_user, "XXXXX"},
  80. {proxy_password, "XXXXX"},
  81. {proxy_host, "proxy"},
  82. {proxy_port, 8080},
  83. {save_response_to_file, true}], 5000).
  84. {ok,"200",
  85. [{"Transfer-Encoding","chunked"},
  86. {"Date","Fri, 17 Dec 2004 15:24:36 GMT"},
  87. {"Content-Type","text/html"},
  88. {"Server","Apache/1.3.9 (Unix)"},
  89. {"Via","1.1 netapp01 (NetCache NetApp/5.5R2)"}],
  90. {file,"/Users/chandru/code/ibrowse/src/ibrowse_tmp_file_1103297041125854"}}
  91. ```
  92. Setting the size of the connection pool and pipeline. This sets the
  93. number of maximum connections to the specified server to `10` and the pipeline
  94. size to `1`. Connections are assumed to be already setup.
  95. ```erlang
  96. 11> ibrowse:set_dest("www.hotmail.com", 80, [{max_sessions, 10},
  97. {max_pipeline_size, 1}]).
  98. ok
  99. ```
  100. Example using the `HEAD` method:
  101. ```erlang
  102. 56> ibrowse:send_req("http://www.erlang.org", [], head).
  103. {ok,"200",
  104. [{"Date","Mon, 28 Feb 2005 04:40:53 GMT"},
  105. {"Server","Apache/1.3.9 (Unix)"},
  106. {"Last-Modified","Thu, 10 Feb 2005 09:31:23 GMT"},
  107. {"Etag","\"8d71d-1efa-420b29eb\""},
  108. {"Accept-ranges","bytes"},
  109. {"Content-Length","7930"},
  110. {"Content-Type","text/html"}],
  111. []}
  112. ```
  113. Example using the `OPTIONS` method:
  114. ```erlang
  115. 62> ibrowse:send_req("http://www.sun.com", [], options).
  116. {ok,"200",
  117. [{"Server","Sun Java System Web Server 6.1"},
  118. {"Date","Mon, 28 Feb 2005 04:44:39 GMT"},
  119. {"Content-Length","0"},
  120. {"P3p",
  121. "policyref=\"http://www.sun.com/p3p/Sun_P3P_Policy.xml\", CP=\"CAO DSP COR CUR ADMa DEVa TAIa PSAa PSDa CONi TELi OUR SAMi PUBi IND PHY ONL PUR COM NAV INT DEM CNT STA POL PRE GOV\""},
  122. {"Set-Cookie",
  123. "SUN_ID=X.X.X.X:169191109565879; EXPIRES=Wednesday, 31-Dec-2025 23:59:59 GMT; DOMAIN=.sun.com; PATH=/"},
  124. {"Allow",
  125. "HEAD, GET, PUT, POST, DELETE, TRACE, OPTIONS, MOVE, INDEX, MKDIR, RMDIR"}],
  126. []}
  127. ```
  128. ### Asynchronous Requests
  129. Example of an asynchronous `GET` request:
  130. ```erlang
  131. 18> ibrowse:send_req("http://www.google.com", [], get, [],
  132. [{proxy_user, "XXXXX"},
  133. {proxy_password, "XXXXX"},
  134. {proxy_host, "proxy"},
  135. {proxy_port, 8080},
  136. {stream_to, self()}]).
  137. {ibrowse_req_id,{1115,327256,389608}}
  138. 19> flush().
  139. Shell got {ibrowse_async_headers,{1115,327256,389608},
  140. "302",
  141. [{"Date","Thu, 05 May 2005 21:06:41 GMT"},
  142. {"Content-Length","217"},
  143. {"Content-Type","text/html"},
  144. {"Set-Cookie",
  145. "PREF=ID=b601f16bfa32f071:CR=1:TM=1115327201:LM=1115327201:S=OX5hSB525AMjUUu7; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com"},
  146. {"Server","GWS/2.1"},
  147. {"Location",
  148. "http://www.google.co.uk/cxfer?c=PREF%3D:TM%3D1115327201:S%3DDS9pDJ4IHcAuZ_AS&prev=/"},
  149. {"Via",
  150. "1.1 hatproxy01 (NetCache NetApp/5.6.2)"}]}
  151. Shell got {ibrowse_async_response,{1115,327256,389608},
  152. "<HTML>...</HTML>\r\n"}
  153. Shell got {ibrowse_async_response_end,{1115,327256,389608}}
  154. ok
  155. ```
  156. Another asynchronous `GET` request:
  157. ```erlang
  158. 24> ibrowse:send_req("http://yaws.hyber.org/simple_ex2.yaws", [], get, [],
  159. [{proxy_user, "XXXXX"},
  160. {proxy_password, "XXXXX"},
  161. {proxy_host, "proxy"},
  162. {proxy_port, 8080},
  163. {stream_to, self()}]).
  164. {ibrowse_req_id,{1115,327430,512314}}
  165. 25> flush().
  166. Shell got {ibrowse_async_headers,{1115,327430,512314},
  167. "200",
  168. [{"Date","Thu, 05 May 2005 20:58:08 GMT"},
  169. {"Content-Length","64"},
  170. {"Content-Type","text/html;charset="},
  171. {"Server",
  172. "Yaws/1.54 Yet Another Web Server"},
  173. {"Via",
  174. "1.1 hatproxy01 (NetCache NetApp/5.6.2)"}]}
  175. Shell got {ibrowse_async_response,{1115,327430,512314},
  176. "<html>...</html>\n"}
  177. Shell got {ibrowse_async_response_end,{1115,327430,512314}}
  178. ```
  179. Example of request which fails when using the async option. Here
  180. the `{ibrowse_req_id, ReqId}` is not returned. Instead the error code is
  181. returned.
  182. ```erlang
  183. 68> ibrowse:send_req("http://www.earlyriser.org", [], get, [], [{stream_to, self()}]).
  184. {error,conn_failed}
  185. ```
  186. ### Other Examples
  187. Example of request using both Proxy-Authorization and authorization
  188. by the final webserver:
  189. ```erlang
  190. 17> ibrowse:send_req("http://www.erlang.se/lic_area/protected/patches/erl_756_otp_beam.README",
  191. [], get, [],
  192. [{proxy_user, "XXXXX"},
  193. {proxy_password, "XXXXX"},
  194. {proxy_host, "proxy"},
  195. {proxy_port, 8080},
  196. {basic_auth, {"XXXXX", "XXXXXX"}}]).
  197. {ok,"200",
  198. [{"Accept-Ranges","bytes"},
  199. {"Date","Thu, 05 May 2005 21:02:09 GMT"},
  200. {"Content-Length","2088"},
  201. {"Content-Type","text/plain"},
  202. {"Server","Apache/1.3.9 (Unix)"},
  203. {"Last-Modified","Tue, 03 May 2005 15:08:18 GMT"},
  204. {"ETag","\"1384c8-828-427793e2\""},
  205. {"Via","1.1 hatproxy01 (NetCache NetApp/5.6.2)"}],
  206. "Patch Id:\t\terl_756_otp_beam\n..."}
  207. ```
  208. Example of a `TRACE` request. Very interesting! yaws.hyber.org didn't
  209. support this. Nor did www.google.com. But good old BBC supports this:
  210. ```erlang
  211. 37> ibrowse:send_req("http://www.bbc.co.uk/", [], trace, [],
  212. [{proxy_user, "XXXXX"},
  213. {proxy_password, "XXXXX"},
  214. {proxy_host, "proxy"},
  215. {proxy_port, 8080}]).
  216. {ok,"200",
  217. [{"Transfer-Encoding","chunked"},
  218. {"Date","Thu, 05 May 2005 21:40:27 GMT"},
  219. {"Content-Type","message/http"},
  220. {"Server","Apache/2.0.51 (Unix)"},
  221. {"Set-Cookie",
  222. "BBC-UID=7452e72a..."},
  223. {"Set-Cookie",
  224. "BBC-UID=7452e72a..."},
  225. {"Via","1.1 hatproxy01 (NetCache NetApp/5.6.2)"}],
  226. "TRACE / HTTP/1.1\r\nHost: www.bbc.co.uk\r\nConnection: keep-alive\r\nX-Forwarded-For: 172.24.28.29\r\nVia: 1.1 hatproxy01 (NetCache NetApp/5.6.2)\r\nCookie: BBC-UID=7452e...\r\n\r\n"}
  227. ```
  228. A `GET` using a socks5:
  229. ```erlang
  230. ibrowse:send_req("http://google.com", [], get, [],
  231. [{socks5_host, "127.0.0.1"},
  232. {socks5_port, 5335}]).
  233. ibrowse:send_req("http://google.com", [], get, [],
  234. [{socks5_host, "127.0.0.1"},
  235. {socks5_port, 5335},
  236. {socks5_user, "user4321"},
  237. {socks5_password, "pass7654"}]).
  238. ```