context.go 3.9 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
package common

import (
	"net/http"
	"strconv"

	goku_plugin "github.com/eolinker/goku-plugin"
)

var _ goku_plugin.ContextProxy = (*Context)(nil)

type Context struct {
	w http.ResponseWriter
	*CookiesHandler
	*PriorityHeader
	*StatusHandler
	*StoreHandler
	RequestOrg           *RequestReader
	ProxyRequest         *Request
	ProxyResponseHandler *ResponseReader
	Body                 []byte
	strategyId           string
	strategyName         string
	apiId                int
	requestId            string
	finalTargetServer    string
	retryTargetServers   string
}

func (ctx *Context) FinalTargetServer() string {
	return ctx.finalTargetServer
}
func (ctx *Context) SetFinalTargetServer(finalTargetServer string) {
	ctx.finalTargetServer = finalTargetServer
}
func (ctx *Context) RetryTargetServers() string {
	return ctx.retryTargetServers
}
func (ctx *Context) SetRetryTargetServers(retryTargetServers string) {
	ctx.retryTargetServers = retryTargetServers
}

Y
Your Name 已提交
43
func (ctx *Context) Finish() (n int, statusCode int) {
E
eoLinker API Management 已提交
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 91 92 93

	header := ctx.PriorityHeader.header

	statusCode = ctx.StatusHandler.code
	if statusCode == 0 {
		statusCode = 504
	}

	bodyAllowed := true
	switch {
	case statusCode >= 100 && statusCode <= 199:
		bodyAllowed = false
		break
	case statusCode == 204:
		bodyAllowed = false
		break
	case statusCode == 304:
		bodyAllowed = false
		break
	}

	if ctx.PriorityHeader.appendHeader != nil {
		for k, vs := range ctx.PriorityHeader.appendHeader.header {
			for _, v := range vs {
				header.Add(k, v)
			}
		}
	}

	if ctx.PriorityHeader.setHeader != nil {
		for k, vs := range ctx.PriorityHeader.setHeader.header {
			header.Del(k)
			for _, v := range vs {
				header.Add(k, v)
			}
		}
	}

	for k, vs := range ctx.PriorityHeader.header {
		if k == "Content-Length" && bodyAllowed {
			vs = []string{strconv.Itoa(len(string(ctx.Body)))}
		}
		for _, v := range vs {
			ctx.w.Header().Add(k, v)
		}
	}

	ctx.w.WriteHeader(statusCode)

	if !bodyAllowed {
Y
Your Name 已提交
94
		return 0, statusCode
E
eoLinker API Management 已提交
95 96
	}
	n, _ = ctx.w.Write(ctx.Body)
Y
Your Name 已提交
97
	return n, statusCode
E
eoLinker API Management 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
}
func (ctx *Context) RequestId() string {
	return ctx.requestId
}

func NewContext(r *http.Request, requestId string, w http.ResponseWriter) *Context {
	requestreader := NewRequestReader(r)
	return &Context{
		CookiesHandler:       newCookieHandle(r.Header),
		PriorityHeader:       NewPriorityHeader(),
		StatusHandler:        NewStatusHandler(),
		StoreHandler:         NewStoreHandler(),
		RequestOrg:           requestreader,
		ProxyRequest:         NewRequest(requestreader),
		ProxyResponseHandler: nil,
		requestId:            requestId,
		w:                    w,
	}
}
func (ctx *Context) SetProxyResponse(response *http.Response) {

	ctx.ProxyResponseHandler = newResponseReader(response)
	if ctx.ProxyResponseHandler != nil {
		ctx.Body = ctx.ProxyResponseHandler.body
		ctx.SetStatus(ctx.ProxyResponseHandler.StatusCode(), ctx.ProxyResponseHandler.Status())
		ctx.header = ctx.ProxyResponseHandler.header
	}

}
func (ctx *Context) Write(w http.ResponseWriter) {
	if ctx.StatusCode() == 0 {
		ctx.SetStatus(200, "200 ok")
	}
	if ctx.Body != nil {
		w.Write(ctx.Body)
	}

	w.WriteHeader(ctx.StatusCode())

}

func (ctx *Context) GetBody() []byte {
	return ctx.Body
}
func (ctx *Context) SetBody(data []byte) {
	ctx.Body = data
}

func (ctx *Context) ProxyResponse() goku_plugin.ResponseReader {
	return ctx.ProxyResponseHandler
}

func (ctx *Context) StrategyId() string {
	return ctx.strategyId
}
func (ctx *Context) SetStrategyId(strategyId string) {
	ctx.strategyId = strategyId
}
func (ctx *Context) StrategyName() string {
	return ctx.strategyName
}
func (ctx *Context) SetStrategyName(strategyName string) {
	ctx.strategyName = strategyName
}
func (ctx *Context) ApiID() int {
	return ctx.apiId
}
func (ctx *Context) SetApiID(apiId int) {
	ctx.apiId = apiId
}
func (ctx *Context) Request() goku_plugin.RequestReader {
	return ctx.RequestOrg
}

func (ctx *Context) Proxy() goku_plugin.Request {
	return ctx.ProxyRequest
}