亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄 搜尋
archive archive/tar archive/zip bufio bufio(緩存) builtin builtin(內(nèi)置包) bytes bytes(包字節(jié)) compress compress/bzip2(壓縮/bzip2) compress/flate(壓縮/flate) compress/gzip(壓縮/gzip) compress/lzw(壓縮/lzw) compress/zlib(壓縮/zlib) container container/heap(容器數(shù)據(jù)結(jié)構(gòu)heap) container/list(容器數(shù)據(jù)結(jié)構(gòu)list) container/ring(容器數(shù)據(jù)結(jié)構(gòu)ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(數(shù)據(jù)庫/sql) database/sql/driver(數(shù)據(jù)庫/sql/driver) debug debug/dwarf(調(diào)試/dwarf) debug/elf(調(diào)試/elf) debug/gosym(調(diào)試/gosym) debug/macho(調(diào)試/macho) debug/pe(調(diào)試/pe) debug/plan9obj(調(diào)試/plan9obj) encoding encoding(編碼) encoding/ascii85(編碼/ascii85) encoding/asn1(編碼/asn1) encoding/base32(編碼/base32) encoding/base64(編碼/base64) encoding/binary(編碼/binary) encoding/csv(編碼/csv) encoding/gob(編碼/gob) encoding/hex(編碼/hex) encoding/json(編碼/json) encoding/pem(編碼/pem) encoding/xml(編碼/xml) errors errors(錯誤) expvar expvar flag flag(命令行參數(shù)解析flag包) fmt fmt go go/ast(抽象語法樹) go/build go/constant(常量) go/doc(文檔) go/format(格式) go/importer go/parser go/printer go/scanner(掃描儀) go/token(令牌) go/types(類型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(圖像) image/color(顏色) image/color/palette(調(diào)色板) image/draw(繪圖) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系統(tǒng)) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路徑) plugin plugin(插件) reflect reflect(反射) regexp regexp(正則表達(dá)式) regexp/syntax runtime runtime(運行時) runtime/debug(調(diào)試) runtime/internal/sys runtime/pprof runtime/race(競爭) runtime/trace(執(zhí)行追蹤器) sort sort(排序算法) strconv strconv(轉(zhuǎn)換) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系統(tǒng)調(diào)用) testing testing(測試) testing/iotest testing/quick text text/scanner(掃描文本) text/tabwriter text/template(定義模板) text/template/parse time time(時間戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • import "net/http/httputil"

  • 概述

  • 索引

  • 示例

概述

軟件包httputil提供HTTP實用程序功能,補充了net/http軟件包中較常見的功能。

索引

  • 變量

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

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

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

  • func NewChunkedReader(r io.Reader) io.Reader

  • func NewChunkedWriter(w io.Writer) io.WriteCloser

  • type BufferPool

  • type ClientConn

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

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

  • func (cc *ClientConn) Close() error

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

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

  • func (cc *ClientConn) Pending() int

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

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

  • type ReverseProxy

  • func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy

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

  • type ServerConn

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

  • func (sc *ServerConn) Close() error

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

  • func (sc *ServerConn) Pending() int

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

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

示例

DumpRequest DumpRequestOut DumpResponse ReverseProxy

文件包

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

變量

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

使用太長的行讀取格式不正確的分塊數(shù)據(jù)時,會返回ErrLineTooLong。

var ErrLineTooLong = internal.ErrLineTooLong

func DumpRequest

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

DumpRequest以HTTP/1.x線表示形式返回給定的請求。它只能被服務(wù)器用來調(diào)試客戶端請求。返回的表示只是一個近似值; 解析為http.Request時,初始請求的某些細(xì)節(jié)會丟失。特別是頭字段名稱的順序和大小寫會丟失。多值頭中值的順序保持不變。HTTP/2請求以HTTP/1.x形式轉(zhuǎn)儲,而不是以其原始二進(jìn)制表示。

如果body正確,DumpRequest也返回正文。為此,它使用req.Body,然后用一個新的產(chǎn)生相同字節(jié)的io.ReadCloser來替換它。如果DumpRequest返回錯誤,則req的狀態(tài)是未定義的。

http.Request.Write的文檔詳細(xì)說明了哪些req字段包含在轉(zhuǎn)儲中。

示例

package mainimport ("fmt""io/ioutil""log""net/http""net/http/httptest""net/http/httputil""strings")func main() {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		dump, err := httputil.DumpRequest(r, true)if err != nil {
			http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)return}

		fmt.Fprintf(w, "%q", dump)}))
	defer ts.Close()const body = "Go is a general-purpose language designed with systems programming in mind."
	req, err := http.NewRequest("POST", ts.URL, strings.NewReader(body))if err != nil {
		log.Fatal(err)}
	req.Host = "www.example.org"
	resp, err := http.DefaultClient.Do(req)if err != nil {
		log.Fatal(err)}
	defer resp.Body.Close()

	b, err := ioutil.ReadAll(resp.Body)if err != nil {
		log.Fatal(err)}

	fmt.Printf("%s", b)}

func DumpRequestOut

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

DumpRequestOut就像DumpRequest,但是用于傳出客戶端請求。它包括標(biāo)準(zhǔn)http.Transport添加的任何標(biāo)題,例如User-Agent。

示例

package mainimport ("fmt""log""net/http""net/http/httputil""strings")func main() {const body = "Go is a general-purpose language designed with systems programming in mind."
	req, err := http.NewRequest("PUT", "http://www.example.org", strings.NewReader(body))if err != nil {
		log.Fatal(err)}

	dump, err := httputil.DumpRequestOut(req, true)if err != nil {
		log.Fatal(err)}

	fmt.Printf("%q", dump)}

func DumpResponse

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

DumpResponse與DumpRequest類似,但會轉(zhuǎn)儲響應(yīng)。

示例

package mainimport ("fmt""log""net/http""net/http/httptest""net/http/httputil")func main() {const body = "Go is a general-purpose language designed with systems programming in mind."
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Date", "Wed, 19 Jul 1972 19:00:00 GMT")
		fmt.Fprintln(w, body)}))
	defer ts.Close()

	resp, err := http.Get(ts.URL)if err != nil {
		log.Fatal(err)}
	defer resp.Body.Close()

	dump, err := httputil.DumpResponse(resp, true)if err != nil {
		log.Fatal(err)}

	fmt.Printf("%q", dump)}

func NewChunkedReader

func NewChunkedReader(r io.Reader) io.Reader

NewChunkedReader返回一個新的chunkedReader,它在返回之前將從r讀出的數(shù)據(jù)轉(zhuǎn)換成HTTP“chunked”格式。chunkedReader在讀取最后的0長度塊時返回io.EOF。

普通應(yīng)用程序不需要NewChunkedReader。在閱讀響應(yīng)主體時,http包會自動解碼分塊。

func NewChunkedWriter

func NewChunkedWriter(w io.Writer) io.WriteCloser

NewChunkedWriter返回一個新的chunkedWriter,在寫入w之前將寫入轉(zhuǎn)換為HTTP“分塊”格式。關(guān)閉返回的chunkedWriter將發(fā)送標(biāo)記流結(jié)束的最終0長度塊。

普通應(yīng)用程序不需要NewChunkedWriter。如果處理程序未設(shè)置Content-Length標(biāo)頭,則http包會自動添加分塊。在處理程序中使用NewChunkedWriter會導(dǎo)致雙重組塊或使用Content-Length長度分塊,這兩者都是錯誤的。

type BufferPool

BufferPool是一個獲取和返回io.CopyBuffer使用的臨時字節(jié)片的接口。

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

type ClientConn

ClientConn是Go早期HTTP實現(xiàn)的產(chǎn)物。它是Go的當(dāng)前HTTP堆棧的低級,舊的和未使用的。我們應(yīng)該在Go 1之前刪除它。

Deprecated:在net/http包中使用客戶端或傳輸。

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

func NewClientConn

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

NewClientConn是Go早期HTTP實現(xiàn)的一個工件。它是Go的當(dāng)前HTTP堆棧的低級,舊的和未使用的。我們應(yīng)該在Go 1之前刪除它。

Deprecated:使用客戶端或傳輸net/http包代替。

func NewProxyClientConn

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

NewProxyClientConn是Go早期HTTP實現(xiàn)的工件。它是Go的當(dāng)前HTTP堆棧的低級,舊的和未使用的。我們應(yīng)該在Go 1之前刪除它。

Deprecated:使用客戶端或傳輸net/http包代替。

func (*ClientConn) Close

func (cc *ClientConn) Close() error

Close調(diào)用Hijack,然后關(guān)閉底層連接。

func (*ClientConn) Do

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

Do是寫請求并讀取響應(yīng)的便利方法。

func (*ClientConn) Hijack

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

劫持分離ClientConn并返回底層連接以及可能留下數(shù)據(jù)的讀取端bufio??梢栽谟脩艋騌ead之前調(diào)用劫持標(biāo)志保持活動邏輯的結(jié)束。在讀取或?qū)懭脒^程中,用戶不應(yīng)該調(diào)用劫持。

func (*ClientConn) Pending

func (cc *ClientConn) Pending() int

Pending返回已經(jīng)在連接上發(fā)送的未應(yīng)答請求的數(shù)量。

func (*ClientConn) Read

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

Read從電線讀取下一個響應(yīng)。有效的響應(yīng)可能會與ErrPersistEOF一起返回,這意味著遠(yuǎn)程請求這是最后一次請求服務(wù)。Read可以與Write同時調(diào)用,但不能與另一個Read一起調(diào)用。

func (*ClientConn) Write

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

Write寫入請求。如果連接已經(jīng)以HTTP Keepalive意義關(guān)閉,則返回ErrPersistEOF錯誤。如果req.Close等于true,則在此請求和對端服務(wù)器被通知后,保持連接在邏輯上關(guān)閉。ErrUnexpectedEOF指示遠(yuǎn)程關(guān)閉了底層TCP連接,通常認(rèn)為該連接處于優(yōu)雅關(guān)閉狀態(tài)。

type ReverseProxy

ReverseProxy是一個HTTP處理程序,它接收傳入的請求并將其發(fā)送到另一個服務(wù)器,將響應(yīng)代理回客戶端。

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.
        FlushInterval time.Duration        // ErrorLog specifies an optional logger for errors        // that occur when attempting to proxy the request.        // If nil, logging goes to os.Stderr via the log package's        // standard logger.
        ErrorLog *log.Logger        // BufferPool optionally specifies a buffer pool to        // get byte slices for use by io.CopyBuffer when        // copying HTTP response bodies.
        BufferPool BufferPool        // ModifyResponse is an optional function that        // modifies the Response from the backend.        // If it returns an error, the proxy returns a StatusBadGateway error.
        ModifyResponse func(*http.Response) error}

示例

package mainimport ("fmt""io/ioutil""log""net/http""net/http/httptest""net/http/httputil""net/url")func main() {
	backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "this call was relayed by the reverse proxy")}))
	defer backendServer.Close()

	rpURL, err := url.Parse(backendServer.URL)if err != nil {
		log.Fatal(err)}
	frontendProxy := httptest.NewServer(httputil.NewSingleHostReverseProxy(rpURL))
	defer frontendProxy.Close()

	resp, err := http.Get(frontendProxy.URL)if err != nil {
		log.Fatal(err)}

	b, err := ioutil.ReadAll(resp.Body)if err != nil {
		log.Fatal(err)}

	fmt.Printf("%s", b)}

func NewSingleHostReverseProxy

func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy

NewSingleHostReverseProxy返回一個新的ReverseProxy,它將URL路由到目標(biāo)中提供的方案,主機和基本路徑。如果目標(biāo)路徑為“/base”,并且傳入請求為“/dir”,則目標(biāo)請求將為/base/dir。NewSingleHostReverseProxy不重寫Host頭。要重寫Host頭文件,請使用ReverseProxy直接使用自定義Director策略。

func (*ReverseProxy) ServeHTTP

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

type ServerConn

ServerConn是Go早期HTTP實現(xiàn)的產(chǎn)物。它是Go的當(dāng)前HTTP堆棧的低級,舊的和未使用的。我們應(yīng)該在Go 1之前刪除它。

Deprecated:改為在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實現(xiàn)的產(chǎn)物。它是Go的當(dāng)前HTTP堆棧的低級,舊的和未使用的。我們應(yīng)該在Go 1之前刪除它。

Deprecated:改為在net/http包中使用Server。

func (*ServerConn) Close

func (sc *ServerConn) Close() error

Close調(diào)用劫持,然后關(guān)閉底層連接。

func (*ServerConn) Hijack

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

劫持分離ServerConn并返回底層連接以及可能有一些遺留數(shù)據(jù)的讀取端bufio。在Read已經(jīng)發(fā)出keep-alive邏輯的結(jié)束信號之前,可能會調(diào)用劫持。在讀取或?qū)懭脒^程中,用戶不應(yīng)該調(diào)用劫持。

func (*ServerConn) Pending

func (sc *ServerConn) Pending() int

掛起返回在連接上收到的未應(yīng)答請求的數(shù)量。

func (*ServerConn) Read

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

讀取將返回線路上的下一個請求。如果優(yōu)雅地確定沒有更多請求(例如,在HTTP / 1.0連接上的第一個請求之后,或在HTTP/1.1連接上的Connection:close之后),則返回ErrPersistEOF。

func (*ServerConn) Write

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

Write寫入resp響應(yīng)請求。要正常關(guān)閉連接,請將Response.Close字段設(shè)置為true。寫操作應(yīng)該被認(rèn)為是可操作的,直到它返回一個錯誤,而不管在讀方面返回的任何錯誤。

上一篇: 下一篇: