You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

935 lines
33 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>httputil - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package httputil</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "net/http/httputil"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package httputil provides HTTP utility functions, complementing the
  69. more common ones in the net/http package.
  70. </p>
  71. </div>
  72. </div>
  73. <div id="pkg-index" class="toggleVisible">
  74. <div class="collapsed">
  75. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  76. </div>
  77. <div class="expanded">
  78. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  79. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  80. <div id="manual-nav">
  81. <dl>
  82. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  83. <dd><a href="index.html#DumpRequest">func DumpRequest(req *http.Request, body bool) (dump []byte, err error)</a></dd>
  84. <dd><a href="index.html#DumpRequestOut">func DumpRequestOut(req *http.Request, body bool) ([]byte, error)</a></dd>
  85. <dd><a href="index.html#DumpResponse">func DumpResponse(resp *http.Response, body bool) (dump []byte, err error)</a></dd>
  86. <dd><a href="index.html#NewChunkedReader">func NewChunkedReader(r io.Reader) io.Reader</a></dd>
  87. <dd><a href="index.html#NewChunkedWriter">func NewChunkedWriter(w io.Writer) io.WriteCloser</a></dd>
  88. <dd><a href="index.html#BufferPool">type BufferPool</a></dd>
  89. <dd><a href="index.html#ClientConn">type ClientConn</a></dd>
  90. <dd>&nbsp; &nbsp; <a href="index.html#NewClientConn">func NewClientConn(c net.Conn, r *bufio.Reader) *ClientConn</a></dd>
  91. <dd>&nbsp; &nbsp; <a href="index.html#NewProxyClientConn">func NewProxyClientConn(c net.Conn, r *bufio.Reader) *ClientConn</a></dd>
  92. <dd>&nbsp; &nbsp; <a href="index.html#ClientConn.Close">func (cc *ClientConn) Close() error</a></dd>
  93. <dd>&nbsp; &nbsp; <a href="index.html#ClientConn.Do">func (cc *ClientConn) Do(req *http.Request) (resp *http.Response, err error)</a></dd>
  94. <dd>&nbsp; &nbsp; <a href="index.html#ClientConn.Hijack">func (cc *ClientConn) Hijack() (c net.Conn, r *bufio.Reader)</a></dd>
  95. <dd>&nbsp; &nbsp; <a href="index.html#ClientConn.Pending">func (cc *ClientConn) Pending() int</a></dd>
  96. <dd>&nbsp; &nbsp; <a href="index.html#ClientConn.Read">func (cc *ClientConn) Read(req *http.Request) (resp *http.Response, err error)</a></dd>
  97. <dd>&nbsp; &nbsp; <a href="index.html#ClientConn.Write">func (cc *ClientConn) Write(req *http.Request) (err error)</a></dd>
  98. <dd><a href="index.html#ReverseProxy">type ReverseProxy</a></dd>
  99. <dd>&nbsp; &nbsp; <a href="index.html#NewSingleHostReverseProxy">func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy</a></dd>
  100. <dd>&nbsp; &nbsp; <a href="index.html#ReverseProxy.ServeHTTP">func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)</a></dd>
  101. <dd><a href="index.html#ServerConn">type ServerConn</a></dd>
  102. <dd>&nbsp; &nbsp; <a href="index.html#NewServerConn">func NewServerConn(c net.Conn, r *bufio.Reader) *ServerConn</a></dd>
  103. <dd>&nbsp; &nbsp; <a href="index.html#ServerConn.Close">func (sc *ServerConn) Close() error</a></dd>
  104. <dd>&nbsp; &nbsp; <a href="index.html#ServerConn.Hijack">func (sc *ServerConn) Hijack() (c net.Conn, r *bufio.Reader)</a></dd>
  105. <dd>&nbsp; &nbsp; <a href="index.html#ServerConn.Pending">func (sc *ServerConn) Pending() int</a></dd>
  106. <dd>&nbsp; &nbsp; <a href="index.html#ServerConn.Read">func (sc *ServerConn) Read() (req *http.Request, err error)</a></dd>
  107. <dd>&nbsp; &nbsp; <a href="index.html#ServerConn.Write">func (sc *ServerConn) Write(req *http.Request, resp *http.Response) error</a></dd>
  108. </dl>
  109. </div><!-- #manual-nav -->
  110. <div id="pkg-examples">
  111. <h4>Examples</h4>
  112. <dl>
  113. <dd><a class="exampleLink" href="index.html#example_DumpRequest">DumpRequest</a></dd>
  114. <dd><a class="exampleLink" href="index.html#example_DumpRequestOut">DumpRequestOut</a></dd>
  115. <dd><a class="exampleLink" href="index.html#example_DumpResponse">DumpResponse</a></dd>
  116. <dd><a class="exampleLink" href="index.html#example_ReverseProxy">ReverseProxy</a></dd>
  117. </dl>
  118. </div>
  119. <h4>Package files</h4>
  120. <p>
  121. <span style="font-size:90%">
  122. <a href="http://localhost:6060/src/net/http/httputil/dump.go">dump.go</a>
  123. <a href="http://localhost:6060/src/net/http/httputil/httputil.go">httputil.go</a>
  124. <a href="http://localhost:6060/src/net/http/httputil/persist.go">persist.go</a>
  125. <a href="http://localhost:6060/src/net/http/httputil/reverseproxy.go">reverseproxy.go</a>
  126. </span>
  127. </p>
  128. </div><!-- .expanded -->
  129. </div><!-- #pkg-index -->
  130. <div id="pkg-callgraph" class="toggle" style="display: none">
  131. <div class="collapsed">
  132. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  133. </div> <!-- .expanded -->
  134. <div class="expanded">
  135. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  136. <p>
  137. In the call graph viewer below, each node
  138. is a function belonging to this package
  139. and its children are the functions it
  140. calls&mdash;perhaps dynamically.
  141. </p>
  142. <p>
  143. The root nodes are the entry points of the
  144. package: functions that may be called from
  145. outside the package.
  146. There may be non-exported or anonymous
  147. functions among them if they are called
  148. dynamically from another package.
  149. </p>
  150. <p>
  151. Click a node to visit that function's source code.
  152. From there you can visit its callers by
  153. clicking its declaring <code>func</code>
  154. token.
  155. </p>
  156. <p>
  157. Functions may be omitted if they were
  158. determined to be unreachable in the
  159. particular programs or tests that were
  160. analyzed.
  161. </p>
  162. <!-- Zero means show all package entry points. -->
  163. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  164. </div>
  165. </div> <!-- #pkg-callgraph -->
  166. <h2 id="pkg-variables">Variables</h2>
  167. <pre>var (
  168. <span id="ErrPersistEOF">ErrPersistEOF</span> = &amp;<a href="../index.html">http</a>.<a href="../index.html#ProtocolError">ProtocolError</a>{<a href="index.html#ErrorString">ErrorString</a>: &#34;persistent connection closed&#34;}
  169. <span id="ErrClosed">ErrClosed</span> = &amp;<a href="../index.html">http</a>.<a href="../index.html#ProtocolError">ProtocolError</a>{<a href="index.html#ErrorString">ErrorString</a>: &#34;connection closed by user&#34;}
  170. <span id="ErrPipeline">ErrPipeline</span> = &amp;<a href="../index.html">http</a>.<a href="../index.html#ProtocolError">ProtocolError</a>{<a href="index.html#ErrorString">ErrorString</a>: &#34;pipeline error&#34;}
  171. )</pre>
  172. <pre>var <span id="ErrLineTooLong">ErrLineTooLong</span> = <a href="../internal/index.html">internal</a>.<a href="../internal/index.html#ErrLineTooLong">ErrLineTooLong</a></pre>
  173. <p>
  174. ErrLineTooLong is returned when reading malformed chunked data
  175. with lines that are too long.
  176. </p>
  177. <h2 id="DumpRequest">func <a href="http://localhost:6060/src/net/http/httputil/dump.go?s=5737:5808#L184">DumpRequest</a></h2>
  178. <pre>func DumpRequest(req *<a href="../index.html">http</a>.<a href="../index.html#Request">Request</a>, body <a href="../../../builtin/index.html#bool">bool</a>) (dump []<a href="../../../builtin/index.html#byte">byte</a>, err <a href="../../../builtin/index.html#error">error</a>)</pre>
  179. <p>
  180. DumpRequest returns the given request in its HTTP/1.x wire
  181. representation. It should only be used by servers to debug client
  182. requests. The returned representation is an approximation only;
  183. some details of the initial request are lost while parsing it into
  184. an http.Request. In particular, the order and case of header field
  185. names are lost. The order of values in multi-valued headers is kept
  186. intact. HTTP/2 requests are dumped in HTTP/1.x form, not in their
  187. original binary representations.
  188. </p>
  189. <p>
  190. If body is true, DumpRequest also returns the body. To do so, it
  191. consumes req.Body and then replaces it with a new io.ReadCloser
  192. that yields the same bytes. If DumpRequest returns an error,
  193. the state of req is undefined.
  194. </p>
  195. <p>
  196. The documentation for http.Request.Write details which fields
  197. of req are included in the dump.
  198. </p>
  199. <div id="example_DumpRequest" class="toggle">
  200. <div class="collapsed">
  201. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  202. </div>
  203. <div class="expanded">
  204. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  205. <p>Code:</p>
  206. <pre class="code">ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  207. dump, err := httputil.DumpRequest(r, true)
  208. if err != nil {
  209. http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
  210. return
  211. }
  212. fmt.Fprintf(w, &#34;%q&#34;, dump)
  213. }))
  214. defer ts.Close()
  215. const body = &#34;Go is a general-purpose language designed with systems programming in mind.&#34;
  216. req, err := http.NewRequest(&#34;POST&#34;, ts.URL, strings.NewReader(body))
  217. if err != nil {
  218. log.Fatal(err)
  219. }
  220. req.Host = &#34;www.example.org&#34;
  221. resp, err := http.DefaultClient.Do(req)
  222. if err != nil {
  223. log.Fatal(err)
  224. }
  225. defer resp.Body.Close()
  226. b, err := ioutil.ReadAll(resp.Body)
  227. if err != nil {
  228. log.Fatal(err)
  229. }
  230. fmt.Printf(&#34;%s&#34;, b)
  231. <span class="comment"></pre>
  232. <p>Output:</p>
  233. <pre class="output">&#34;POST / HTTP/1.1\r\nHost: www.example.org\r\nAccept-Encoding: gzip\r\nUser-Agent: Go-http-client/1.1\r\n\r\nGo is a general-purpose language designed with systems programming in mind.&#34;
  234. </pre>
  235. </div>
  236. </div>
  237. <h2 id="DumpRequestOut">func <a href="http://localhost:6060/src/net/http/httputil/dump.go?s=1687:1752#L51">DumpRequestOut</a></h2>
  238. <pre>func DumpRequestOut(req *<a href="../index.html">http</a>.<a href="../index.html#Request">Request</a>, body <a href="../../../builtin/index.html#bool">bool</a>) ([]<a href="../../../builtin/index.html#byte">byte</a>, <a href="../../../builtin/index.html#error">error</a>)</pre>
  239. <p>
  240. DumpRequestOut is like DumpRequest but for outgoing client requests. It
  241. includes any headers that the standard http.Transport adds, such as
  242. User-Agent.
  243. </p>
  244. <div id="example_DumpRequestOut" class="toggle">
  245. <div class="collapsed">
  246. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  247. </div>
  248. <div class="expanded">
  249. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  250. <p>Code:</p>
  251. <pre class="code">const body = &#34;Go is a general-purpose language designed with systems programming in mind.&#34;
  252. req, err := http.NewRequest(&#34;PUT&#34;, &#34;http://www.example.org&#34;, strings.NewReader(body))
  253. if err != nil {
  254. log.Fatal(err)
  255. }
  256. dump, err := httputil.DumpRequestOut(req, true)
  257. if err != nil {
  258. log.Fatal(err)
  259. }
  260. fmt.Printf(&#34;%q&#34;, dump)
  261. <span class="comment"></pre>
  262. <p>Output:</p>
  263. <pre class="output">&#34;PUT / HTTP/1.1\r\nHost: www.example.org\r\nUser-Agent: Go-http-client/1.1\r\nContent-Length: 75\r\nAccept-Encoding: gzip\r\n\r\nGo is a general-purpose language designed with systems programming in mind.&#34;
  264. </pre>
  265. </div>
  266. </div>
  267. <h2 id="DumpResponse">func <a href="http://localhost:6060/src/net/http/httputil/dump.go?s=8200:8274#L273">DumpResponse</a></h2>
  268. <pre>func DumpResponse(resp *<a href="../index.html">http</a>.<a href="../index.html#Response">Response</a>, body <a href="../../../builtin/index.html#bool">bool</a>) (dump []<a href="../../../builtin/index.html#byte">byte</a>, err <a href="../../../builtin/index.html#error">error</a>)</pre>
  269. <p>
  270. DumpResponse is like DumpRequest but dumps a response.
  271. </p>
  272. <div id="example_DumpResponse" class="toggle">
  273. <div class="collapsed">
  274. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  275. </div>
  276. <div class="expanded">
  277. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  278. <p>Code:</p>
  279. <pre class="code">const body = &#34;Go is a general-purpose language designed with systems programming in mind.&#34;
  280. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  281. w.Header().Set(&#34;Date&#34;, &#34;Wed, 19 Jul 1972 19:00:00 GMT&#34;)
  282. fmt.Fprintln(w, body)
  283. }))
  284. defer ts.Close()
  285. resp, err := http.Get(ts.URL)
  286. if err != nil {
  287. log.Fatal(err)
  288. }
  289. defer resp.Body.Close()
  290. dump, err := httputil.DumpResponse(resp, true)
  291. if err != nil {
  292. log.Fatal(err)
  293. }
  294. fmt.Printf(&#34;%q&#34;, dump)
  295. <span class="comment"></pre>
  296. <p>Output:</p>
  297. <pre class="output">&#34;HTTP/1.1 200 OK\r\nContent-Length: 76\r\nContent-Type: text/plain; charset=utf-8\r\nDate: Wed, 19 Jul 1972 19:00:00 GMT\r\n\r\nGo is a general-purpose language designed with systems programming in mind.\n&#34;
  298. </pre>
  299. </div>
  300. </div>
  301. <h2 id="NewChunkedReader">func <a href="http://localhost:6060/src/net/http/httputil/httputil.go?s=688:732#L10">NewChunkedReader</a></h2>
  302. <pre>func NewChunkedReader(r <a href="../../../io/index.html">io</a>.<a href="../../../io/index.html#Reader">Reader</a>) <a href="../../../io/index.html">io</a>.<a href="../../../io/index.html#Reader">Reader</a></pre>
  303. <p>
  304. NewChunkedReader returns a new chunkedReader that translates the data read from r
  305. out of HTTP &#34;chunked&#34; format before returning it.
  306. The chunkedReader returns io.EOF when the final 0-length chunk is read.
  307. </p>
  308. <p>
  309. NewChunkedReader is not needed by normal applications. The http package
  310. automatically decodes chunking when reading response bodies.
  311. </p>
  312. <h2 id="NewChunkedWriter">func <a href="http://localhost:6060/src/net/http/httputil/httputil.go?s=1309:1358#L23">NewChunkedWriter</a></h2>
  313. <pre>func NewChunkedWriter(w <a href="../../../io/index.html">io</a>.<a href="../../../io/index.html#Writer">Writer</a>) <a href="../../../io/index.html">io</a>.<a href="../../../io/index.html#WriteCloser">WriteCloser</a></pre>
  314. <p>
  315. NewChunkedWriter returns a new chunkedWriter that translates writes into HTTP
  316. &#34;chunked&#34; format before writing them to w. Closing the returned chunkedWriter
  317. sends the final 0-length chunk that marks the end of the stream.
  318. </p>
  319. <p>
  320. NewChunkedWriter is not needed by normal applications. The http
  321. package adds chunking automatically if handlers don&#39;t set a
  322. Content-Length header. Using NewChunkedWriter inside a handler
  323. would result in double chunking or chunking with a Content-Length
  324. length, both of which are wrong.
  325. </p>
  326. <h2 id="BufferPool">type <a href="http://localhost:6060/src/net/http/httputil/reverseproxy.go?s=1597:1653#L48">BufferPool</a></h2>
  327. <pre>type BufferPool interface {
  328. Get() []<a href="../../../builtin/index.html#byte">byte</a>
  329. Put([]<a href="../../../builtin/index.html#byte">byte</a>)
  330. }</pre>
  331. <p>
  332. A BufferPool is an interface for getting and returning temporary
  333. byte slices for use by io.CopyBuffer.
  334. </p>
  335. <h2 id="ClientConn">type <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=6260:6628#L219">ClientConn</a></h2>
  336. <pre>type ClientConn struct {
  337. <span class="comment">// contains filtered or unexported fields</span>
  338. }</pre>
  339. <p>
  340. A ClientConn sends request and receives headers over an underlying
  341. connection, while respecting the HTTP keepalive logic. ClientConn
  342. supports hijacking the connection calling Hijack to
  343. regain control of the underlying net.Conn and deal with it as desired.
  344. </p>
  345. <p>
  346. ClientConn is low-level and old. Applications should instead use
  347. Client or Transport in the net/http package.
  348. </p>
  349. <h3 id="NewClientConn">func <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=6867:6926#L237">NewClientConn</a></h3>
  350. <pre>func NewClientConn(c <a href="../../index.html">net</a>.<a href="../../index.html#Conn">Conn</a>, r *<a href="../../../bufio/index.html">bufio</a>.<a href="../../../bufio/index.html#Reader">Reader</a>) *<a href="index.html#ClientConn">ClientConn</a></pre>
  351. <p>
  352. NewClientConn returns a new ClientConn reading and writing c. If r is not
  353. nil, it is the buffer to use when reading c.
  354. </p>
  355. <p>
  356. ClientConn is low-level and old. Applications should use Client or
  357. Transport in the net/http package.
  358. </p>
  359. <h3 id="NewProxyClientConn">func <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=7321:7385#L254">NewProxyClientConn</a></h3>
  360. <pre>func NewProxyClientConn(c <a href="../../index.html">net</a>.<a href="../../index.html#Conn">Conn</a>, r *<a href="../../../bufio/index.html">bufio</a>.<a href="../../../bufio/index.html#Reader">Reader</a>) *<a href="index.html#ClientConn">ClientConn</a></pre>
  361. <p>
  362. NewProxyClientConn works like NewClientConn but writes Requests
  363. using Request&#39;s WriteProxy method.
  364. </p>
  365. <p>
  366. New code should not use NewProxyClientConn. See Client or
  367. Transport in the net/http package instead.
  368. </p>
  369. <h3 id="ClientConn.Close">func (*ClientConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=8002:8037#L275">Close</a></h3>
  370. <pre>func (cc *<a href="index.html#ClientConn">ClientConn</a>) Close() <a href="../../../builtin/index.html#error">error</a></pre>
  371. <p>
  372. Close calls Hijack and then also closes the underlying connection
  373. </p>
  374. <h3 id="ClientConn.Do">func (*ClientConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=11320:11396#L413">Do</a></h3>
  375. <pre>func (cc *<a href="index.html#ClientConn">ClientConn</a>) Do(req *<a href="../index.html">http</a>.<a href="../index.html#Request">Request</a>) (resp *<a href="../index.html">http</a>.<a href="../index.html#Response">Response</a>, err <a href="../../../builtin/index.html#error">error</a>)</pre>
  376. <p>
  377. Do is convenience method that writes a request and reads a response.
  378. </p>
  379. <h3 id="ClientConn.Hijack">func (*ClientConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=7779:7839#L264">Hijack</a></h3>
  380. <pre>func (cc *<a href="index.html#ClientConn">ClientConn</a>) Hijack() (c <a href="../../index.html">net</a>.<a href="../../index.html#Conn">Conn</a>, r *<a href="../../../bufio/index.html">bufio</a>.<a href="../../../bufio/index.html#Reader">Reader</a>)</pre>
  381. <p>
  382. Hijack detaches the ClientConn and returns the underlying connection as well
  383. as the read-side bufio which may have some left over data. Hijack may be
  384. called before the user or Read have signaled the end of the keep-alive
  385. logic. The user should not call Hijack while Read or Write is in progress.
  386. </p>
  387. <h3 id="ClientConn.Pending">func (*ClientConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=9593:9628#L341">Pending</a></h3>
  388. <pre>func (cc *<a href="index.html#ClientConn">ClientConn</a>) Pending() <a href="../../../builtin/index.html#int">int</a></pre>
  389. <p>
  390. Pending returns the number of unanswered requests
  391. that have been sent on the connection.
  392. </p>
  393. <h3 id="ClientConn.Read">func (*ClientConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=9973:10051#L351">Read</a></h3>
  394. <pre>func (cc *<a href="index.html#ClientConn">ClientConn</a>) Read(req *<a href="../index.html">http</a>.<a href="../index.html#Request">Request</a>) (resp *<a href="../index.html">http</a>.<a href="../index.html#Response">Response</a>, err <a href="../../../builtin/index.html#error">error</a>)</pre>
  395. <p>
  396. Read reads the next response from the wire. A valid response might be
  397. returned together with an ErrPersistEOF, which means that the remote
  398. requested that this be the last request serviced. Read can be called
  399. concurrently with Write, but not with another Read.
  400. </p>
  401. <h3 id="ClientConn.Write">func (*ClientConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=8502:8560#L288">Write</a></h3>
  402. <pre>func (cc *<a href="index.html#ClientConn">ClientConn</a>) Write(req *<a href="../index.html">http</a>.<a href="../index.html#Request">Request</a>) (err <a href="../../../builtin/index.html#error">error</a>)</pre>
  403. <p>
  404. Write writes a request. An ErrPersistEOF error is returned if the connection
  405. has been closed in an HTTP keepalive sense. If req.Close equals true, the
  406. keepalive connection is logically closed after this request and the opposing
  407. server is informed. An ErrUnexpectedEOF indicates the remote closed the
  408. underlying TCP connection, which is usually considered as graceful close.
  409. </p>
  410. <h2 id="ReverseProxy">type <a href="http://localhost:6060/src/net/http/httputil/reverseproxy.go?s=564:1486#L17">ReverseProxy</a></h2>
  411. <pre>type ReverseProxy struct {
  412. <span class="comment">// Director must be a function which modifies</span>
  413. <span class="comment">// the request into a new request to be sent</span>
  414. <span class="comment">// using Transport. Its response is then copied</span>
  415. <span class="comment">// back to the original client unmodified.</span>
  416. Director func(*<a href="../index.html">http</a>.<a href="../index.html#Request">Request</a>)
  417. <span class="comment">// The transport used to perform proxy requests.</span>
  418. <span class="comment">// If nil, http.DefaultTransport is used.</span>
  419. Transport <a href="../index.html">http</a>.<a href="../index.html#RoundTripper">RoundTripper</a>
  420. <span class="comment">// FlushInterval specifies the flush interval</span>
  421. <span class="comment">// to flush to the client while copying the</span>
  422. <span class="comment">// response body.</span>
  423. <span class="comment">// If zero, no periodic flushing is done.</span>
  424. FlushInterval <a href="../../../time/index.html">time</a>.<a href="../../../time/index.html#Duration">Duration</a>
  425. <span class="comment">// ErrorLog specifies an optional logger for errors</span>
  426. <span class="comment">// that occur when attempting to proxy the request.</span>
  427. <span class="comment">// If nil, logging goes to os.Stderr via the log package&#39;s</span>
  428. <span class="comment">// standard logger.</span>
  429. ErrorLog *<a href="../../../log/index.html">log</a>.<a href="../../../log/index.html#Logger">Logger</a>
  430. <span class="comment">// BufferPool optionally specifies a buffer pool to</span>
  431. <span class="comment">// get byte slices for use by io.CopyBuffer when</span>
  432. <span class="comment">// copying HTTP response bodies.</span>
  433. BufferPool <a href="index.html#BufferPool">BufferPool</a>
  434. }</pre>
  435. <p>
  436. ReverseProxy is an HTTP Handler that takes an incoming request and
  437. sends it to another server, proxying the response back to the
  438. client.
  439. </p>
  440. <div id="example_ReverseProxy" class="toggle">
  441. <div class="collapsed">
  442. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  443. </div>
  444. <div class="expanded">
  445. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  446. <p>Code:</p>
  447. <pre class="code">backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  448. fmt.Fprintln(w, &#34;this call was relayed by the reverse proxy&#34;)
  449. }))
  450. defer backendServer.Close()
  451. rpURL, err := url.Parse(backendServer.URL)
  452. if err != nil {
  453. log.Fatal(err)
  454. }
  455. frontendProxy := httptest.NewServer(httputil.NewSingleHostReverseProxy(rpURL))
  456. defer frontendProxy.Close()
  457. resp, err := http.Get(frontendProxy.URL)
  458. if err != nil {
  459. log.Fatal(err)
  460. }
  461. b, err := ioutil.ReadAll(resp.Body)
  462. if err != nil {
  463. log.Fatal(err)
  464. }
  465. fmt.Printf(&#34;%s&#34;, b)
  466. <span class="comment"></pre>
  467. <p>Output:</p>
  468. <pre class="output">this call was relayed by the reverse proxy
  469. </pre>
  470. </div>
  471. </div>
  472. <h3 id="NewSingleHostReverseProxy">func <a href="http://localhost:6060/src/net/http/httputil/reverseproxy.go?s=2298:2359#L72">NewSingleHostReverseProxy</a></h3>
  473. <pre>func NewSingleHostReverseProxy(target *<a href="../../url/index.html">url</a>.<a href="../../url/index.html#URL">URL</a>) *<a href="index.html#ReverseProxy">ReverseProxy</a></pre>
  474. <p>
  475. NewSingleHostReverseProxy returns a new ReverseProxy that routes
  476. URLs to the scheme, host, and base path provided in target. If the
  477. target&#39;s path is &#34;/base&#34; and the incoming request was for &#34;/dir&#34;,
  478. the target request will be for /base/dir.
  479. NewSingleHostReverseProxy does not rewrite the Host header.
  480. To rewrite Host headers, use ReverseProxy directly with a custom
  481. Director policy.
  482. </p>
  483. <h3 id="ReverseProxy.ServeHTTP">func (*ReverseProxy) <a href="http://localhost:6060/src/net/http/httputil/reverseproxy.go?s=3769:3844#L130">ServeHTTP</a></h3>
  484. <pre>func (p *<a href="index.html#ReverseProxy">ReverseProxy</a>) ServeHTTP(rw <a href="../index.html">http</a>.<a href="../index.html#ResponseWriter">ResponseWriter</a>, req *<a href="../index.html">http</a>.<a href="../index.html#Request">Request</a>)</pre>
  485. <h2 id="ServerConn">type <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=1189:1506#L26">ServerConn</a></h2>
  486. <pre>type ServerConn struct {
  487. <span class="comment">// contains filtered or unexported fields</span>
  488. }</pre>
  489. <p>
  490. A ServerConn reads requests and sends responses over an underlying
  491. connection, until the HTTP keepalive logic commands an end. ServerConn
  492. also allows hijacking the underlying connection by calling Hijack
  493. to regain control over the connection. ServerConn supports pipe-lining,
  494. i.e. requests can be read out of sync (but in the same order) while the
  495. respective responses are sent.
  496. </p>
  497. <p>
  498. ServerConn is low-level and old. Applications should instead use Server
  499. in the net/http package.
  500. </p>
  501. <h3 id="NewServerConn">func <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=1739:1798#L43">NewServerConn</a></h3>
  502. <pre>func NewServerConn(c <a href="../../index.html">net</a>.<a href="../../index.html#Conn">Conn</a>, r *<a href="../../../bufio/index.html">bufio</a>.<a href="../../../bufio/index.html#Reader">Reader</a>) *<a href="index.html#ServerConn">ServerConn</a></pre>
  503. <p>
  504. NewServerConn returns a new ServerConn reading and writing c. If r is not
  505. nil, it is the buffer to use when reading c.
  506. </p>
  507. <p>
  508. ServerConn is low-level and old. Applications should instead use Server
  509. in the net/http package.
  510. </p>
  511. <h3 id="ServerConn.Close">func (*ServerConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=2436:2471#L65">Close</a></h3>
  512. <pre>func (sc *<a href="index.html#ServerConn">ServerConn</a>) Close() <a href="../../../builtin/index.html#error">error</a></pre>
  513. <p>
  514. Close calls Hijack and then also closes the underlying connection
  515. </p>
  516. <h3 id="ServerConn.Hijack">func (*ServerConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=2213:2273#L54">Hijack</a></h3>
  517. <pre>func (sc *<a href="index.html#ServerConn">ServerConn</a>) Hijack() (c <a href="../../index.html">net</a>.<a href="../../index.html#Conn">Conn</a>, r *<a href="../../../bufio/index.html">bufio</a>.<a href="../../../bufio/index.html#Reader">Reader</a>)</pre>
  518. <p>
  519. Hijack detaches the ServerConn and returns the underlying connection as well
  520. as the read-side bufio which may have some left over data. Hijack may be
  521. called before Read has signaled the end of the keep-alive logic. The user
  522. should not call Hijack while Read or Write is in progress.
  523. </p>
  524. <h3 id="ServerConn.Pending">func (*ServerConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=4528:4563#L153">Pending</a></h3>
  525. <pre>func (sc *<a href="index.html#ServerConn">ServerConn</a>) Pending() <a href="../../../builtin/index.html#int">int</a></pre>
  526. <p>
  527. Pending returns the number of unanswered requests
  528. that have been received on the connection.
  529. </p>
  530. <h3 id="ServerConn.Read">func (*ServerConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=2806:2865#L77">Read</a></h3>
  531. <pre>func (sc *<a href="index.html#ServerConn">ServerConn</a>) Read() (req *<a href="../index.html">http</a>.<a href="../index.html#Request">Request</a>, err <a href="../../../builtin/index.html#error">error</a>)</pre>
  532. <p>
  533. Read returns the next request on the wire. An ErrPersistEOF is returned if
  534. it is gracefully determined that there are no more requests (e.g. after the
  535. first request on an HTTP/1.0 connection, or after a Connection:close on a
  536. HTTP/1.1 connection).
  537. </p>
  538. <h3 id="ServerConn.Write">func (*ServerConn) <a href="http://localhost:6060/src/net/http/httputil/persist.go?s=4875:4948#L162">Write</a></h3>
  539. <pre>func (sc *<a href="index.html#ServerConn">ServerConn</a>) Write(req *<a href="../index.html">http</a>.<a href="../index.html#Request">Request</a>, resp *<a href="../index.html">http</a>.<a href="../index.html#Response">Response</a>) <a href="../../../builtin/index.html#error">error</a></pre>
  540. <p>
  541. Write writes resp in response to req. To close the connection gracefully, set the
  542. Response.Close field to true. Write should be considered operational until
  543. it returns an error, regardless of any errors returned on the Read side.
  544. </p>
  545. <div id="footer">
  546. Build version go1.6.<br>
  547. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  548. the content of this page is licensed under the
  549. Creative Commons Attribution 3.0 License,
  550. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  551. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  552. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  553. </div>
  554. </div><!-- .container -->
  555. </div><!-- #page -->
  556. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  557. <script type="text/javascript" src="../../../../lib/godoc/jquery.js"></script>
  558. <script type="text/javascript" src="../../../../lib/godoc/jquery.treeview.js"></script>
  559. <script type="text/javascript" src="../../../../lib/godoc/jquery.treeview.edit.js"></script>
  560. <script type="text/javascript" src="../../../../lib/godoc/godocs.js"></script>
  561. </body>
  562. </html>