nazahttp

package
v0.17.1-0...-36a5d2c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderFieldContentLength = "Content-Length"
	HeaderFieldContentType   = "application/json"
)

Variables

View Source
var (
	ErrHTTPHeader   = errors.New("nazahttp: fxxk")
	ErrParamMissing = errors.New("nazahttp: param missing")
)

Functions

func DownloadHTTPFile

func DownloadHTTPFile(url string, saveTo string, timeoutMSec int) (int64, error)

获取http文件保存至本地

func GetHTTPFile

func GetHTTPFile(url string, timeoutMSec int) ([]byte, error)

获取http文件保存至字节切片

func ParseHTTPRequestLine

func ParseHTTPRequestLine(line string) (method string, uri string, version string, err error)

Request-Line = Method SP URI SP Version CRLF

func ParseHTTPStatusLine

func ParseHTTPStatusLine(line string) (version string, statusCode string, reason string, err error)

Status-Line = Version SP Status-Code SP Reason CRLF

func PostJson

func PostJson(url string, info interface{}, client *http.Client) (*http.Response, error)

@param url 地址 @param info 需要序列化的结构体 @param client 注意,如果为nil,则使用http.DefaultClient

func ReadHTTPHeader

func ReadHTTPHeader(r LineReader) (firstLine string, headers map[string][]string, err error)

@return firstLine: request的request line或response的status line @return headers: request header fileds的键值对

func UnmarshalRequestJsonBody

func UnmarshalRequestJsonBody(r *http.Request, info interface{}, keyFieldList ...string) error

@brief 从http请求中解析body中的json字符串,并反序列化至结构体中

@param r http请求对象 @param info 输出参数,用于接收反序列化之后的数据 @param keyFieldList 可选参数,可指定一个或多个json中必须存在的字段

Types

type HTTPMsgCtx

type HTTPMsgCtx struct {
	ReqMethodOrRespVersion string
	ReqURIOrRespStatusCode string
	ReqVersionOrRespReason string
	Headers                map[string][]string
	Body                   []byte
}

func ReadHTTPMessage

func ReadHTTPMessage(r HTTPReader) (ctx HTTPMsgCtx, err error)

注意,如果HTTP Header中不包含`Content-Length`,则不会读取HTTP Body,并且err返回值为nil

func (*HTTPMsgCtx) GetHeader

func (r *HTTPMsgCtx) GetHeader(v string) string

func (*HTTPMsgCtx) GetHeaderArr

func (r *HTTPMsgCtx) GetHeaderArr(v string) []string

type HTTPReader

type HTTPReader interface {
	LineReader
	io.Reader
}

e.g. bufio.Reader

type HTTPReqMsgCtx

type HTTPReqMsgCtx struct {
	Method  string
	URI     string
	Version string
	Headers map[string][]string
	Body    []byte
}

func ReadHTTPRequestMessage

func ReadHTTPRequestMessage(r HTTPReader) (ctx HTTPReqMsgCtx, err error)

func (*HTTPReqMsgCtx) GetHeader

func (r *HTTPReqMsgCtx) GetHeader(v string) string

func (*HTTPReqMsgCtx) GetHeaderArr

func (r *HTTPReqMsgCtx) GetHeaderArr(v string) []string

type HTTPRespMsgCtx

type HTTPRespMsgCtx struct {
	Version    string
	StatusCode string
	Reason     string
	Headers    map[string][]string
	Body       []byte
}

func ReadHTTPResponseMessage

func ReadHTTPResponseMessage(r HTTPReader) (ctx HTTPRespMsgCtx, err error)

func (*HTTPRespMsgCtx) GetHeader

func (r *HTTPRespMsgCtx) GetHeader(v string) string

func (*HTTPRespMsgCtx) GetHeaderArr

func (r *HTTPRespMsgCtx) GetHeaderArr(v string) []string

type LineReader

type LineReader interface {
	ReadLine() (line []byte, isPrefix bool, err error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL