mirror of
https://github.com/ehang-io/nps.git
synced 2025-07-03 04:53:50 +00:00
添加gzip压缩
This commit is contained in:
parent
ef9bfa3290
commit
f0abcd98e4
18
http.go
18
http.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -75,8 +76,13 @@ func EncodeResponse(r *http.Response) ([]byte, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
binary.Write(raw, binary.LittleEndian, int32(len(respBytes)))
|
var buf bytes.Buffer
|
||||||
if err := binary.Write(raw, binary.LittleEndian, respBytes); err != nil {
|
zw := gzip.NewWriter(&buf)
|
||||||
|
zw.Write(respBytes)
|
||||||
|
zw.Close()
|
||||||
|
binary.Write(raw, binary.LittleEndian, int32(len(buf.Bytes())))
|
||||||
|
if err := binary.Write(raw, binary.LittleEndian, buf.Bytes()); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return raw.Bytes(), nil
|
return raw.Bytes(), nil
|
||||||
@ -84,8 +90,12 @@ func EncodeResponse(r *http.Response) ([]byte, error) {
|
|||||||
|
|
||||||
//// 将字节转为response
|
//// 将字节转为response
|
||||||
func DecodeResponse(data []byte) (*http.Response, error) {
|
func DecodeResponse(data []byte) (*http.Response, error) {
|
||||||
|
zr, err := gzip.NewReader(bytes.NewReader(data))
|
||||||
resp, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(data)), nil)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer zr.Close()
|
||||||
|
resp, err := http.ReadResponse(bufio.NewReader(zr), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user