header.go 1.8 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8
package common

import (
	goku_plugin "github.com/eolinker/goku-plugin"
	"net/http"
	"net/url"
)

Y
Your Name 已提交
9
//Header header
E
eoLinker API Management 已提交
10 11 12 13
type Header struct {
	header http.Header
}

Y
Your Name 已提交
14
//Headers 返回headers
E
eoLinker API Management 已提交
15 16 17 18 19 20 21 22
func (h *Header) Headers() http.Header {

	n := make(http.Header)
	for k, v := range h.header {
		n[k] = v
	}
	return n
}
Y
Your Name 已提交
23
func (h *Header) String() string {
E
eoLinker API Management 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

	return url.Values(h.header).Encode()
	//buf:=bytes.NewBuffer(nil)
	//for k,v:=range h.header{
	//	buf.WriteByte('&')
	//	buf.WriteString(k)
	//	buf.WriteString("=")
	//	buf.WriteString(strings.Join(v,","))
	//
	//}
	//data:=buf.Bytes()
	//if len(data)>1{
	//	data=data[1:]
	//}
	//return string(data)
}
Y
Your Name 已提交
40 41

//SetHeader 设置请求头
E
eoLinker API Management 已提交
42 43 44 45
func (h *Header) SetHeader(key, value string) {
	h.header.Set(key, value)
}

Y
Your Name 已提交
46
//AddHeader 新增请求头
E
eoLinker API Management 已提交
47 48 49 50
func (h *Header) AddHeader(key, value string) {
	h.header.Add(key, value)
}

Y
Your Name 已提交
51
//DelHeader 删除请求头
E
eoLinker API Management 已提交
52 53 54 55
func (h *Header) DelHeader(key string) {
	h.header.Del(key)
}

Y
Your Name 已提交
56
//GetHeader 通过名字获取请求头
E
eoLinker API Management 已提交
57 58 59
func (h *Header) GetHeader(name string) string {
	return h.header.Get(name)
}
Y
Your Name 已提交
60 61

//NewHeader 创建header请求头
E
eoLinker API Management 已提交
62 63 64 65 66 67 68 69 70
func NewHeader(header http.Header) *Header {
	if header == nil {
		header = make(http.Header)
	}
	return &Header{
		header: header,
	}
}

Y
Your Name 已提交
71
//PriorityHeader proorityHeader
E
eoLinker API Management 已提交
72 73 74 75 76 77
type PriorityHeader struct {
	*Header
	setHeader    *Header
	appendHeader *Header
}

Y
Your Name 已提交
78
//Set set
E
eoLinker API Management 已提交
79 80 81 82 83 84 85
func (h *PriorityHeader) Set() goku_plugin.Header {
	if h.setHeader == nil {
		h.setHeader = NewHeader(nil)
	}
	return h.setHeader
}

Y
Your Name 已提交
86
//Append append
E
eoLinker API Management 已提交
87 88 89 90 91 92 93
func (h *PriorityHeader) Append() goku_plugin.Header {
	if h.appendHeader == nil {
		h.appendHeader = NewHeader(nil)
	}
	return h.setHeader
}

Y
Your Name 已提交
94
//NewPriorityHeader 创建优先级header
E
eoLinker API Management 已提交
95 96 97 98 99 100 101
func NewPriorityHeader() *PriorityHeader {
	return &PriorityHeader{
		Header:       NewHeader(nil),
		setHeader:    nil,
		appendHeader: nil,
	}
}