kmgHttp

package
v0.0.0-...-05317bf Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2015 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SessionCookieName = "kmgSession"
View Source
var SessionPsk = [32]byte{0xd8, 0x51, 0xea, 0x81, 0xb9, 0xe, 0xf, 0x2f, 0x8c, 0x85, 0x5f, 0xb6, 0x14, 0xb2}

Functions

func AddCommandList

func AddCommandList()

func AddParameterToUrl

func AddParameterToUrl(urls string, key string, value string) (urlout string, err error)

@deprecated

func AddUriProxyToDefaultServer

func AddUriProxyToDefaultServer(uri, targetUrl string)

url明文反向代理. uri表示,以此uri作为前缀可以进入这个地方,此处代理不会修改用户的uri 例如: 注册 AddUriProxyToDefaultServer("/data/upload/","http://b.com/") 用户访问 http://xxx.com/data/upload/745dfc0e7d24b9232a0226116e115967.jpg 此处会去访问 http://b.com/data/upload/745dfc0e7d24b9232a0226116e115967.jpg

func ClearHttpDefaultServer

func ClearHttpDefaultServer()

清空默认的Http服务器的路径表

func CompressHandler

func CompressHandler(fn http.Handler) http.Handler

func CompressHandlerFunc

func CompressHandlerFunc(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request)

func CompressWriteByte

func CompressWriteByte(r *http.Request, w http.ResponseWriter, b []byte)

如果使用了这个,golang的猜测返回类型的东西会挂掉.请设置输出的内容的类型 这个地方返回错误没有什么意义,(调用者无法处理)

func CompressWriteHtml

func CompressWriteHtml(req *http.Request, w http.ResponseWriter, html []byte)

func DumpResponseToBytes

func DumpResponseToBytes(resp *http.Response) (b []byte, err error)

sometimes it is hard to remember how to dump response to bytes

func FormatHttpsCertOrKey

func FormatHttpsCertOrKey(inS string) string

去掉cert和key,复制粘帖带来的各种格式错误

func GetDomainName

func GetDomainName(url string) (domainName string, protocol string)

@deprecated

func GetHttpsCertNotCheckClient

func GetHttpsCertNotCheckClient() *http.Client

func GoListenAndServeTLSWithCertContent

func GoListenAndServeTLSWithCertContent(addr string, certS string, keyS string, handler http.Handler) error

func HeaderToString

func HeaderToString(header http.Header) (s string)

func HttpHandleCompressFlateWrap

func HttpHandleCompressFlateWrap(fn http.Handler) http.Handler

a flate(DEFLATE) compress wrap around http request and response, !!not handle any http header!!

func HttpHandleCompressGzipWrap

func HttpHandleCompressGzipWrap(fn http.Handler) http.Handler

a flate(DEFLATE) compress wrap around http request and response, !!not handle any http header!!

func HttpProxyToWriter

func HttpProxyToWriter(w http.ResponseWriter, req *http.Request) (err error)

进行http代理,并且代理到writer上面去 调用时,请修改req的参数,避免自己调用自己 如果出现错误,(对方服务器连不上之类的,不会修改w,会返回一个error 不跟踪redirect(跟踪redirect会导致redirect的请求的内容被返回)

func HttpRequestClone

func HttpRequestClone(in *http.Request) (out *http.Request, err error)

会把request里面的东西全部都读出来(body)

func HttpResponseToWrite

func HttpResponseToWrite(resp *http.Response, w http.ResponseWriter)

func HttpRoundTrip

func HttpRoundTrip(req *http.Request) (resp *http.Response, err error)

func MustAddContentToDefaultServer

func MustAddContentToDefaultServer(uri string, content []byte)

func MustAddFileToHttpPathToDefaultServer

func MustAddFileToHttpPathToDefaultServer(httpPath string, localFilePath string)

向http默认服务器加入一个本地文件或目录

func MustAddFileToHttpPathToServeMux

func MustAddFileToHttpPathToServeMux(mux *http.ServeMux, httpPath string, localFilePath string)

func MustAddUriProxyRefToUriToDefaultServer

func MustAddUriProxyRefToUriToDefaultServer(uri, targetUrl string)

url明文反向代理. uri 是否以/开头和结束无所谓, targetUrl 是否以/结束无所谓. uri表示,以此path作为前缀可以进入这个地方,此处代理可能会修改请求的path 例如: 注册 MustAddUriProxyRefToUriToDefaultServer("/data/upload/","http://b.com/") 用户访问 http://xxx.com/data/upload/745dfc0e7d24b9232a0226116e115967.jpg 此处会去访问 http://b.com/745dfc0e7d24b9232a0226116e115967.jpg

func MustGoHttpAsyncListenAndServeWithCloser

func MustGoHttpAsyncListenAndServeWithCloser(addr string, handler http.Handler) (closer func() error)

异步开启一个http服务器,这个服务器可以使用返回的closer关闭

func MustGoHttpsAsyncListenAndServeWithCloser

func MustGoHttpsAsyncListenAndServeWithCloser(addr string, tlsConfig *tls.Config, handler http.Handler) (closer func() error)

func MustHttpRequestClone

func MustHttpRequestClone(in *http.Request) *http.Request

func MustNewHttpNetServerV2

func MustNewHttpNetServerV2(Addr string, handler http.Handler) func() error

一个可以关闭的http服务器

func MustNewTestClientWithHttpProxy

func MustNewTestClientWithHttpProxy(httpProxy string) *http.Client

一个不验证证书,并且使用http代理的http客户端 httpProxy look like http://127.0.0.1:9876

func MustRequestFromString

func MustRequestFromString(reqString string) (req *http.Request)

func MustRequestToStringCanRead

func MustRequestToStringCanRead(req *http.Request) (s string)

把request转换成[]byte,并且使body可以被再次读取

func MustResponseReadAllBody

func MustResponseReadAllBody(resp *http.Response) (b []byte)

func MustSetParameterMapToUrl

func MustSetParameterMapToUrl(urls string, row map[string]string) (urlout string)

func MustSetParameterToUrl

func MustSetParameterToUrl(urls string, key string, value string) (urlout string)

func MustUrlGetContent

func MustUrlGetContent(url string) (b []byte)

func MustUrlGetContentProcess

func MustUrlGetContentProcess(url string) (b []byte)

func NewHttpNetServer

func NewHttpNetServer(Addr string, handler http.Handler) kmgNet.Server

一个http的满足 kmgNet.Server接口的服务器

func NewHttpsCertNotCheckClient

func NewHttpsCertNotCheckClient() *http.Client

func NewRequestFromByte

func NewRequestFromByte(r []byte) (req *http.Request, err error)

func NewResponseFromBytes

func NewResponseFromBytes(r []byte) (resp *http.Response, err error)

sometimes it is hard to remember how to get response from bytes ...

func NewTimeoutHttpClient

func NewTimeoutHttpClient(dur time.Duration) *http.Client

func NewTimeoutNoKeepAliveHttpClient

func NewTimeoutNoKeepAliveHttpClient(dur time.Duration) *http.Client

func Redirect301ToNewHost

func Redirect301ToNewHost(w http.ResponseWriter, req *http.Request, scheme string, host string)

func RequestReadAllBody

func RequestReadAllBody(req *http.Request) (b []byte, err error)

func ResponseReadAllBody

func ResponseReadAllBody(resp *http.Response) (b []byte, err error)

func SetParameterMapToUrl

func SetParameterMapToUrl(urls string, row map[string]string) (urlout string, err error)

func SetParameterToUrl

func SetParameterToUrl(urls string, key string, value string) (urlout string, err error)

func SortUrlQuery

func SortUrlQuery(vurl *url.URL)

sort url query to unique it 这个东西是做什么用的? @deprecated

func UrlGetContent

func UrlGetContent(url string) (b []byte, err error)

Types

type Context

type Context struct {
	DataMap map[string]string //上下文里面可以携带一些信息
	// contains filtered or unexported fields
}

该对象上的方法不应该被并发调用.

func NewContextFromHttp

func NewContextFromHttp(w http.ResponseWriter, req *http.Request) *Context

func NewTestContext

func NewTestContext() *Context

返回一个新的测试上下文,这个上下文的所有参数都是空的

func (*Context) DeleteInMap

func (c *Context) DeleteInMap(key string)

func (*Context) Error

func (c *Context) Error(err error)

func (*Context) GetClientIpStringIgnoreError

func (c *Context) GetClientIpStringIgnoreError() string

func (*Context) GetDataStr

func (c *Context) GetDataStr(key string) string

func (*Context) GetInMap

func (c *Context) GetInMap() map[string]string

func (*Context) GetRedirectUrl

func (c *Context) GetRedirectUrl() string

func (*Context) GetRequest

func (c *Context) GetRequest() *http.Request

func (*Context) GetRequestUrl

func (c *Context) GetRequestUrl() string

func (*Context) GetResponseByteList

func (c *Context) GetResponseByteList() []byte

func (*Context) GetResponseCode

func (c *Context) GetResponseCode() int

func (*Context) GetResponseHeader

func (c *Context) GetResponseHeader(key string) string

func (*Context) GetResponseString

func (c *Context) GetResponseString() string

func (*Context) GetResponseWriter

func (c *Context) GetResponseWriter() io.Writer

func (*Context) InHas

func (c *Context) InHas(key string) bool

func (*Context) InNum

func (c *Context) InNum(key string) int

根据key返回输入参数,包括post和url的query的数据,如果没有,或者不是整数返回0 返回类型为int

func (*Context) InStr

func (c *Context) InStr(key string) string

根据key返回输入参数,包括post和url的query的数据,如果没有返回"" 类型为string

func (*Context) InStrDefault

func (c *Context) InStrDefault(key string, def string) string

func (*Context) IsGet

func (c *Context) IsGet() bool

func (*Context) IsPost

func (c *Context) IsPost() bool

func (*Context) Log

func (c *Context) Log() *ContextLog

func (*Context) MustFirstInFile

func (c *Context) MustFirstInFile() *multipart.FileHeader

func (*Context) MustGetClientIp

func (c *Context) MustGetClientIp() net.IP

func (*Context) MustInFile

func (c *Context) MustInFile(key string) *multipart.FileHeader

func (*Context) MustInJson

func (c *Context) MustInJson(key string, obj interface{})

func (*Context) MustInNum

func (c *Context) MustInNum(key string) int

func (*Context) MustInStr

func (c *Context) MustInStr(key string) string

func (*Context) MustPost

func (c *Context) MustPost()

func (*Context) NewTestContextWithSession

func (c *Context) NewTestContextWithSession() *Context

仅把Session传递过去的上下文,其他东西都恢复默认值

func (*Context) NotFound

func (c *Context) NotFound(msg string)

func (*Context) Redirect

func (c *Context) Redirect(url string)

func (*Context) SessionClear

func (c *Context) SessionClear() *Context

清除Session里面的内容. 更换Session的Id.

func (*Context) SessionGetJson

func (c *Context) SessionGetJson(key string, obj interface{}) (err error)

func (*Context) SessionGetStr

func (c *Context) SessionGetStr(key string) string

从Session里面获取一个字符串

func (*Context) SessionSetJson

func (c *Context) SessionSetJson(key string, value interface{}) *Context

func (*Context) SessionSetStr

func (c *Context) SessionSetStr(key string, value string) *Context

向Session里面设置一个字符串

func (*Context) SetDataStr

func (c *Context) SetDataStr(key string, value string)

func (*Context) SetInMap

func (c *Context) SetInMap(data map[string]string) *Context

func (*Context) SetInStr

func (c *Context) SetInStr(key string, value string) *Context

func (*Context) SetPost

func (c *Context) SetPost() *Context

func (*Context) SetRequestUrl

func (c *Context) SetRequestUrl(url string) *Context

func (*Context) SetResponseCode

func (c *Context) SetResponseCode(code int)

func (*Context) SetResponseHeader

func (c *Context) SetResponseHeader(key string, value string)

func (*Context) WriteAttachmentFile

func (c *Context) WriteAttachmentFile(b []byte, fileName string)

func (*Context) WriteByte

func (c *Context) WriteByte(s []byte)

func (*Context) WriteJson

func (c *Context) WriteJson(obj interface{})

func (*Context) WriteString

func (c *Context) WriteString(s string)

func (*Context) WriteToResponseWriter

func (c *Context) WriteToResponseWriter(w http.ResponseWriter, req *http.Request)

type ContextLog

type ContextLog struct {
	Method          string            `json:",omitempty"`
	ResponseCode    int               `json:",omitempty"`
	Url             string            `json:",omitempty"`
	RemoteAddr      string            `json:",omitempty"`
	UA              string            `json:",omitempty"`
	Refer           string            `json:",omitempty"`
	RedirectUrl     string            `json:",omitempty"`
	InMap           map[string]string `json:",omitempty"`
	ProcessTime     string            `json:",omitempty"`
	RequestSize     int               `json:",omitempty"`
	ResponseSize    int               `json:",omitempty"`
	ResponseContent string            `json:",omitempty"`
}

type LogStruct

type LogStruct struct {
	Method     string
	Url        string
	RemoteAddr string
	UA         string `json:",omitempty"`
	Refer      string `json:",omitempty"`
	Host       string `json:",omitempty"`
}

func NewLogStruct

func NewLogStruct(req *http.Request) *LogStruct

type ResponseWriterWraper

type ResponseWriterWraper struct {
	io.Writer
	http.ResponseWriter
}

func (ResponseWriterWraper) Write

func (w ResponseWriterWraper) Write(b []byte) (int, error)

TODO 这个地方缺少猜类型功能,导致不能直接wrapper到任意Handler上去.(会导致 html Type 错误.) TODO 有时候压缩会有负效果,此处应该可以自动判断出来.

Jump to

Keyboard shortcuts

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