httpUtils

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const (
	HeaderFieldContentLength = "Content-Length"
	HeaderFieldContentType   = "application/json"
)
View Source
const (
	DefaultRtmpPort  = 1935
	DefaultHttpPort  = 80
	DefaultHttpsPort = 443
	DefaultRtspPort  = 554
	DefaultRtmpsPort = 443
	DefaultRtspsPort = 322
)

Variables

View Source
var (
	ErrHttpHeader   = errors.New("read http header failed")
	ErrFirstLine    = errors.New("parse first line failed")
	ErrParamMissing = errors.New("param missing")
)
View Source
var ErrInvalidUrl = errors.New("err: invalid url")

Functions

func ParseHttpRequest added in v0.0.7

func ParseHttpRequest(req *http.Request) string

ParseHttpRequest

@return 完整url

func ParseHttpRequestLine

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

ParseHttpRequestLine Request-Line = Method SP URI SP Version CRLF *****************************************************

  • @Description: 解析请求行
  • @param line 请求行 Method SP URI SP Version CRLF
  • @return method
  • @return uri
  • @return version
  • @return err *****************************************************

func ParseHttpStatusLine

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

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

  • @Description: 解析响应行
  • @param line 响应行 Version SP Status-Code SP Reason CRLF
  • @return version
  • @return statusCode
  • @return reason
  • @return err *****************************************************

func ReadHttpHeader

func ReadHttpHeader(r LineReader) (firstLine string, headers http.Header, headerRaw string, err error)

ReadHttpHeader *****************************************************

  • @Description: 读取请求头
  • @param r
  • @return firstLine request的request line或response的status line
  • @return headers request header fields的键值对
  • @return headerRaw request header字符串
  • @return err *****************************************************

Types

type HttpMsgCtx

type HttpMsgCtx struct {
	ReqMethodOrRespVersion string
	ReqUriOrRespStatusCode string
	ReqVersionOrRespReason string
	Headers                http.Header
	Body                   []byte
	HeaderRaw              string
}

func ReadHttpMessage

func ReadHttpMessage(r HttpReader) (ctx HttpMsgCtx, err error)

ReadHttpMessage *****************************************************

  • @Description: 读取请求头 如果HTTP Header中不包含`Content-Length`,则不会读取HTTP Body,并且err返回值为nil
  • @param r
  • @return ctx
  • @return err *****************************************************

type HttpReader

type HttpReader interface {
	LineReader
	io.Reader
}

type HttpReqMsgCtx

type HttpReqMsgCtx struct {
	Method    string
	Uri       string
	Version   string
	Headers   http.Header
	Body      []byte
	HeaderRaw string
}

func ReadHttpRequestMessage

func ReadHttpRequestMessage(r HttpReader) (ctx HttpReqMsgCtx, err error)

ReadHttpRequestMessage *****************************************************

  • @Description: 读取请求
  • @param r
  • @return ctx
  • @return err *****************************************************

type HttpRespMsgCtx

type HttpRespMsgCtx struct {
	Version    string
	StatusCode string
	Reason     string
	Headers    http.Header
	Body       []byte
	HeaderRaw  string
}

func ReadHttpResponseMessage

func ReadHttpResponseMessage(r HttpReader) (ctx HttpRespMsgCtx, err error)

ReadHttpResponseMessage *****************************************************

  • @Description: 读取响应
  • @param r
  • @return ctx
  • @return err *****************************************************

type LineReader

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

type UrlContext added in v0.0.7

type UrlContext struct {
	Url string

	Scheme       string
	Username     string
	Password     string
	StdHost      string // host or host:port
	HostWithPort string // 当原始url中不包含port时,填充scheme对应的默认port
	Host         string // 不包含port
	Port         int    // 当原始url中不包含port时,填充scheme对应的默认port

	//UrlPathContext
	PathWithRawQuery    string // 注意,有前面的'/'
	Path                string // 注意,有前面的'/'
	PathWithoutLastItem string // 注意,没有前面的'/',也没有后面的'/'
	LastItemOfPath      string // 注意,没有前面的'/'
	RawQuery            string // 参数,注意,没有前面的'?'

	RawUrlWithoutUserInfo string
	// contains filtered or unexported fields
}

func ParseHttpflvUrl added in v0.0.7

func ParseHttpflvUrl(rawUrl string) (ctx UrlContext, err error)

func ParseRtmpUrl added in v0.0.7

func ParseRtmpUrl(rawUrl string) (ctx UrlContext, err error)

func ParseRtspUrl added in v0.0.7

func ParseRtspUrl(rawUrl string) (ctx UrlContext, err error)

func ParseUrl added in v0.0.7

func ParseUrl(rawUrl string, defaultPort int) (ctx UrlContext, err error)

ParseUrl *****************************************************

  • @Description:
  • @param rawUrl
  • @param defaultPort 如果rawUrl中显示指定了端口,则该参数不生效。 如果设置为-1,内部依然会对常见协议(http, https, rtmp, rtsp)设置官方默认端口。
  • @return ctx
  • @return err *****************************************************

func (*UrlContext) GetFileType added in v0.0.7

func (u *UrlContext) GetFileType() string

func (*UrlContext) GetFilenameWithoutType added in v0.0.7

func (u *UrlContext) GetFilenameWithoutType() string

func (*UrlContext) GetQuery added in v0.0.7

func (u *UrlContext) GetQuery(key string) string

type UrlPathContext added in v0.0.7

type UrlPathContext struct {
	PathWithRawQuery    string
	Path                string
	PathWithoutLastItem string // 注意,没有前面的'/',也没有后面的'/'
	LastItemOfPath      string // 注意,没有前面的'/'
	RawQuery            string
}

Jump to

Keyboard shortcuts

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