Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

296 righe
9.4 KiB

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