header.go 1.5 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
package common

import (

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

)

type Header struct {
	header http.Header
}

func (h *Header) Headers() http.Header {

	n := make(http.Header)
	for k, v := range h.header {
		n[k] = v
	}
	return n
}
func (h *Header) String()string  {

	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)
}
func (h *Header) SetHeader(key, value string) {
	h.header.Set(key, value)
}

func (h *Header) AddHeader(key, value string) {
	h.header.Add(key, value)
}

func (h *Header) DelHeader(key string) {
	h.header.Del(key)
}

func (h *Header) GetHeader(name string) string {
	return h.header.Get(name)
}
func NewHeader(header http.Header) *Header {
	if header == nil {
		header = make(http.Header)
	}
	return &Header{
		header: header,
	}
}

type PriorityHeader struct {
	*Header
	setHeader    *Header
	appendHeader *Header
}

func (h *PriorityHeader) Set() goku_plugin.Header {
	if h.setHeader == nil {
		h.setHeader = NewHeader(nil)
	}
	return h.setHeader
}

func (h *PriorityHeader) Append() goku_plugin.Header {
	if h.appendHeader == nil {
		h.appendHeader = NewHeader(nil)
	}
	return h.setHeader
}

func NewPriorityHeader() *PriorityHeader {
	return &PriorityHeader{
		Header:       NewHeader(nil),
		setHeader:    nil,
		appendHeader: nil,
	}
}