Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

281 rader
9.0 KiB

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