context.go 4.5 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11
package common

import (
	"net/http"
	"strconv"

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

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

Y
Your Name 已提交
12
//Context context
E
eoLinker API Management 已提交
13 14 15 16 17 18 19 20 21 22
type Context struct {
	w http.ResponseWriter
	*CookiesHandler
	*PriorityHeader
	*StatusHandler
	*StoreHandler
	RequestOrg           *RequestReader
	ProxyRequest         *Request
	ProxyResponseHandler *ResponseReader
	Body                 []byte
Y
Your Name 已提交
23
	strategyID           string
E
eoLinker API Management 已提交
24
	strategyName         string
Y
Your Name 已提交
25 26
	apiID                int
	requestID            string
E
eoLinker API Management 已提交
27 28 29 30
	finalTargetServer    string
	retryTargetServers   string
}

Y
Your Name 已提交
31
//FinalTargetServer 获取最终目标转发服务器
E
eoLinker API Management 已提交
32 33 34
func (ctx *Context) FinalTargetServer() string {
	return ctx.finalTargetServer
}
Y
Your Name 已提交
35 36

//SetFinalTargetServer 设置最终目标服务器
E
eoLinker API Management 已提交
37 38 39
func (ctx *Context) SetFinalTargetServer(finalTargetServer string) {
	ctx.finalTargetServer = finalTargetServer
}
Y
Your Name 已提交
40 41

//RetryTargetServers 重试目标服务器
E
eoLinker API Management 已提交
42 43 44
func (ctx *Context) RetryTargetServers() string {
	return ctx.retryTargetServers
}
Y
Your Name 已提交
45 46

//SetRetryTargetServers 设置重试目标服务器
E
eoLinker API Management 已提交
47 48 49 50
func (ctx *Context) SetRetryTargetServers(retryTargetServers string) {
	ctx.retryTargetServers = retryTargetServers
}

Y
Your Name 已提交
51
//Finish 请求结束
Y
Your Name 已提交
52
func (ctx *Context) Finish() (n int, statusCode int) {
E
eoLinker API Management 已提交
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 94 95 96 97 98 99 100 101 102

	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 已提交
103
		return 0, statusCode
E
eoLinker API Management 已提交
104 105
	}
	n, _ = ctx.w.Write(ctx.Body)
Y
Your Name 已提交
106
	return n, statusCode
E
eoLinker API Management 已提交
107
}
Y
Your Name 已提交
108 109

//RequestId 获取请求ID
E
eoLinker API Management 已提交
110
func (ctx *Context) RequestId() string {
Y
Your Name 已提交
111
	return ctx.requestID
E
eoLinker API Management 已提交
112 113
}

Y
Your Name 已提交
114 115
//NewContext 创建context
func NewContext(r *http.Request, requestID string, w http.ResponseWriter) *Context {
E
eoLinker API Management 已提交
116 117 118 119 120 121 122 123 124
	requestreader := NewRequestReader(r)
	return &Context{
		CookiesHandler:       newCookieHandle(r.Header),
		PriorityHeader:       NewPriorityHeader(),
		StatusHandler:        NewStatusHandler(),
		StoreHandler:         NewStoreHandler(),
		RequestOrg:           requestreader,
		ProxyRequest:         NewRequest(requestreader),
		ProxyResponseHandler: nil,
Y
Your Name 已提交
125
		requestID:            requestID,
E
eoLinker API Management 已提交
126 127 128
		w:                    w,
	}
}
Y
Your Name 已提交
129 130

//SetProxyResponse 设置转发响应
E
eoLinker API Management 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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())

}

Y
Your Name 已提交
153
//GetBody 获取body内容
E
eoLinker API Management 已提交
154 155 156
func (ctx *Context) GetBody() []byte {
	return ctx.Body
}
Y
Your Name 已提交
157 158

//SetBody 设置body内容
E
eoLinker API Management 已提交
159 160 161 162
func (ctx *Context) SetBody(data []byte) {
	ctx.Body = data
}

Y
Your Name 已提交
163
//ProxyResponse 转发响应
E
eoLinker API Management 已提交
164 165 166 167
func (ctx *Context) ProxyResponse() goku_plugin.ResponseReader {
	return ctx.ProxyResponseHandler
}

Y
Your Name 已提交
168
//StrategyId 获取策略ID
E
eoLinker API Management 已提交
169
func (ctx *Context) StrategyId() string {
Y
Your Name 已提交
170
	return ctx.strategyID
E
eoLinker API Management 已提交
171
}
Y
Your Name 已提交
172 173 174 175

//SetStrategyId 设置策略ID
func (ctx *Context) SetStrategyId(strategyID string) {
	ctx.strategyID = strategyID
E
eoLinker API Management 已提交
176
}
Y
Your Name 已提交
177 178

//StrategyName 获取策略名称
E
eoLinker API Management 已提交
179 180 181
func (ctx *Context) StrategyName() string {
	return ctx.strategyName
}
Y
Your Name 已提交
182 183

//SetStrategyName 设置策略名称
E
eoLinker API Management 已提交
184 185 186
func (ctx *Context) SetStrategyName(strategyName string) {
	ctx.strategyName = strategyName
}
Y
Your Name 已提交
187 188

//ApiID 获取接口ID
E
eoLinker API Management 已提交
189
func (ctx *Context) ApiID() int {
Y
Your Name 已提交
190
	return ctx.apiID
E
eoLinker API Management 已提交
191
}
Y
Your Name 已提交
192 193 194 195

//SetAPIID 设置接口ID
func (ctx *Context) SetAPIID(apiID int) {
	ctx.apiID = apiID
E
eoLinker API Management 已提交
196
}
Y
Your Name 已提交
197 198

//Request 获取请求原始数据
E
eoLinker API Management 已提交
199 200 201 202
func (ctx *Context) Request() goku_plugin.RequestReader {
	return ctx.RequestOrg
}

Y
Your Name 已提交
203
//Proxy 获取代理请求
E
eoLinker API Management 已提交
204 205 206
func (ctx *Context) Proxy() goku_plugin.Request {
	return ctx.ProxyRequest
}