Package httputil

import "net/http/httputil"
Overview
Index
Examples

Overview ▾

包httputil提供了HTTP实用程序功能,补充了net / http包中更常见的功能.

Index ▾

Examples

DumpRequest
DumpRequestOut
DumpResponse
ReverseProxy

Package files

dump.go httputil.go persist.go reverseproxy.go

Variables

var (
    // Deprecated: No longer used.
    ErrPersistEOF = &http.ProtocolError{ErrorString: "persistent connection closed"}

    // Deprecated: No longer used.
    ErrClosed = &http.ProtocolError{ErrorString: "connection closed by user"}

    // Deprecated: No longer used.
    ErrPipeline = &http.ProtocolError{ErrorString: "pipeline error"}
)

当读取行过长的格式错误的分块数据时,将返回ErrLineTooLong.

var ErrLineTooLong = internal.ErrLineTooLong

func DumpRequest

func DumpRequest(req *http.Request, body bool) ([]byte, error)

DumpRequest以其HTTP / 1.x线路表示形式返回给定请求. 服务器只能使用它调试客户端请求. 返回的表示形式只是一个近似值; 初始请求的一些详细信息在解析为http.Request时会丢失. 特别是,标题字段名称的顺序和大小写将丢失. 多值标头中的值顺序保持不变. HTTP / 2请求以HTTP / 1.x格式而不是原始二进制表示形式转储.

如果body为true,则DumpRequest也会返回body. 为此,它将消耗req.Body,然后将其替换为新的io.ReadCloser并产生相同的字节. 如果DumpRequest返回错误,则req的状态未定义.

http.Request.Write的文档详细说明了转储中包括哪些req字段.

Example

"POST / HTTP/1.1\r\nHost: www.example.org\r\nAccept-Encoding: gzip\r\nContent-Length: 75\r\nUser-Agent: Go-http-client/1.1\r\n\r\nGo is a general-purpose language designed with systems programming in mind."

func DumpRequestOut

func DumpRequestOut(req *http.Request, body bool) ([]byte, error)

DumpRequestOut类似于DumpRequest,但用于传出客户端请求. 它包括标准http.Transport添加的任何标头,例如User-Agent.

Example

"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."

func DumpResponse

func DumpResponse(resp *http.Response, body bool) ([]byte, error)

DumpResponse类似于DumpRequest,但转储响应.

Example

"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"

func NewChunkedReader

func NewChunkedReader(r io.Reader) io.Reader

NewChunkedReader返回一个新的chunkedReader,该块将从r读取的数据转换为HTTP"分块"格式,然后再返回. 当读取最后的0长度的块时,chunkedReader返回io.EOF.

普通应用程序不需要NewChunkedReader. 读取响应正文时,http程序包会​​自动解码分块.

func NewChunkedWriter

func NewChunkedWriter(w io.Writer) io.WriteCloser

NewChunkedWriter返回一个新的chunkedWriter,它将写入写入w之前将写入转换为HTTP"分块"格式. 关闭返回的chunkedWriter将发送最后的0长度的块,该块标记流的结尾,但不发送出现在预告片之后的最终CRLF; 预告片和最后的CRLF必须分开写.

普通应用程序不需要NewChunkedWriter. 如果处理程序未设置Content-Length标头,则http软件包会自动添加分块. 在处理程序中使用NewChunkedWriter会导致双重分块或具有Content-Length长度的分块,两者都是错误的.

type BufferPool 1.6

BufferPool是用于获取和返回由io.CopyBuffer使用的临时字节片的接口.

type BufferPool interface {
    Get() []byte
    Put([]byte)
}

type ClientConn

ClientConn is an artifact of Go's early HTTP implementation. It is low-level, old, and unused by Go's current HTTP stack. We should have deleted it before Go 1.

不推荐使用:而是在包net / http中使用"客户端"或"传输".

type ClientConn struct {
    // contains filtered or unexported fields
}

func NewClientConn

func NewClientConn(c net.Conn, r *bufio.Reader) *ClientConn

NewClientConn是Go早期HTTP实现的产物. 它是低级的,旧的,并且未被Go的当前HTTP堆栈使用. 我们应该在Go 1之前删除它.

不推荐使用:改用net / http包中的Client或Transport.

func NewProxyClientConn

func NewProxyClientConn(c net.Conn, r *bufio.Reader) *ClientConn

NewProxyClientConn是Go早期HTTP实现的产物. 它是低级的,旧的,并且未被Go的当前HTTP堆栈使用. 我们应该在Go 1之前删除它.

不推荐使用:改用net / http包中的Client或Transport.

func (*ClientConn) Close

func (cc *ClientConn) Close() error

Close调用Hijack,然后也关闭基础连接.

func (*ClientConn) Do

func (cc *ClientConn) Do(req *http.Request) (*http.Response, error)

Do是编写请求并读取响应的便捷方法.

func (*ClientConn) Hijack

func (cc *ClientConn) Hijack() (c net.Conn, r *bufio.Reader)

Hijack分离ClientConn并返回基础连接以及可能有一些剩余数据的读取侧缓冲区. 在用户或Read发出保持活动逻辑结束信号之前,可以调用Hijack. 在读或写过程中,用户不应致电Hijack.

func (*ClientConn) Pending

func (cc *ClientConn) Pending() int

Pending返回在连接上已发送的未答复的请求数.

func (*ClientConn) Read

func (cc *ClientConn) Read(req *http.Request) (resp *http.Response, err error)

读取从导线读取下一个响应. 可能会与ErrPersistEOF一起返回有效的响应,这意味着远程服务器请求这是最后一个服务的请求. 读可以与写同时调用,但不能与另一个读同时调用.

func (*ClientConn) Write

func (cc *ClientConn) Write(req *http.Request) error

写写一个请求. 如果从HTTP保持活动状态关闭了连接,则返回ErrPersistEOF错误. 如果req.Close等于true,则在请求后逻辑上关闭保持活动连接,并通知对端服务器. ErrUnexpectedEOF指示远程已关闭基础TCP连接,通常将其视为正常关闭.

type ReverseProxy

ReverseProxy是一个HTTP处理程序,它接收传入的请求并将其发送到另一台服务器,并将响应代理回客户端.

type ReverseProxy struct {
    // Director must be a function which modifies
    // the request into a new request to be sent
    // using Transport. Its response is then copied
    // back to the original client unmodified.
    // Director must not access the provided Request
    // after returning.
    Director func(*http.Request)

    // The transport used to perform proxy requests.
    // If nil, http.DefaultTransport is used.
    Transport http.RoundTripper

    // FlushInterval specifies the flush interval
    // to flush to the client while copying the
    // response body.
    // If zero, no periodic flushing is done.
    // A negative value means to flush immediately
    // after each write to the client.
    // The FlushInterval is ignored when ReverseProxy
    // recognizes a response as a streaming response;
    // for such responses, writes are flushed to the client
    // immediately.
    FlushInterval time.Duration

    // ErrorLog specifies an optional logger for errors
    // that occur when attempting to proxy the request.
    // If nil, logging is done via the log package's standard logger.
    ErrorLog *log.Logger // Go 1.4

    // BufferPool optionally specifies a buffer pool to
    // get byte slices for use by io.CopyBuffer when
    // copying HTTP response bodies.
    BufferPool BufferPool // Go 1.6

    // ModifyResponse is an optional function that modifies the
    // Response from the backend. It is called if the backend
    // returns a response at all, with any HTTP status code.
    // If the backend is unreachable, the optional ErrorHandler is
    // called without any call to ModifyResponse.
    //
    // If ModifyResponse returns an error, ErrorHandler is called
    // with its error value. If ErrorHandler is nil, its default
    // implementation is used.
    ModifyResponse func(*http.Response) error // Go 1.8

    // ErrorHandler is an optional function that handles errors
    // reaching the backend or errors from ModifyResponse.
    //
    // If nil, the default is to log the provided error and return
    // a 502 Status Bad Gateway response.
    ErrorHandler func(http.ResponseWriter, *http.Request, error) // Go 1.11
}

Example

this call was relayed by the reverse proxy

func NewSingleHostReverseProxy

func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy

NewSingleHostReverseProxy返回一个新的ReverseProxy,该URL将URL路由到目标中提供的方案,主机和基本路径. 如果目标的路径是" / base",而传入的请求是" / dir",则目标请求将是/ base / dir. NewSingleHostReverseProxy不会重写Host标头. 要重写主机头,请直接将ReverseProxy与自定义Director策略一起使用.

func (*ReverseProxy) ServeHTTP

func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type ServerConn

ServerConn是Go早期HTTP实现的产物. 它是低级的,旧的,并且未被Go的当前HTTP堆栈使用. 我们应该在Go 1之前删除它.

不推荐使用:改为使用net / http软件包中的Server.

type ServerConn struct {
    // contains filtered or unexported fields
}

func NewServerConn

func NewServerConn(c net.Conn, r *bufio.Reader) *ServerConn

NewServerConn是Go早期HTTP实现的产物. 它是低级的,旧的,并且未被Go的当前HTTP堆栈使用. 我们应该在Go 1之前删除它.

不推荐使用:改为使用net / http软件包中的Server.

func (*ServerConn) Close

func (sc *ServerConn) Close() error

Close调用Hijack,然后也关闭基础连接.

func (*ServerConn) Hijack

func (sc *ServerConn) Hijack() (net.Conn, *bufio.Reader)

Hijack分离ServerConn并返回基础连接以及可能有一些剩余数据的读取侧bufio. 在Read发出保持活动逻辑结束信号之前,可以调用Hijack. 在读或写过程中,用户不应致电Hijack.

func (*ServerConn) Pending

func (sc *ServerConn) Pending() int

待处理返回连接上已收到的未答复的请求数.

func (*ServerConn) Read

func (sc *ServerConn) Read() (*http.Request, error)

读取返回线路上的下一个请求. 如果正常确定不再有其他请求(例如,在HTTP / 1.0连接上的第一个请求之后,或在HTTP / 1.1连接上的Connection:close之后),则返回ErrPersistEOF.

func (*ServerConn) Write

func (sc *ServerConn) Write(req *http.Request, resp *http.Response) error

写入resp以响应req. 要正常关闭连接,请将Response.Close字段设置为true. 写入操作应被视为可操作的,直到返回错误为止,而不管读取端返回的任何错误如何.

by  ICOPY.SITE