mirror of
https://github.com/eolinker/apinto
synced 2025-11-02 21:04:01 +08:00
Feature/plugin
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package http_context
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
|
||||
http_service "github.com/eolinker/eosc/http-service"
|
||||
@@ -13,113 +10,36 @@ var _ http_service.IRequest = (*ProxyRequest)(nil)
|
||||
|
||||
type ProxyRequest struct {
|
||||
*RequestReader
|
||||
headers http.Header
|
||||
form url.Values
|
||||
file map[string]*http_service.FileHeader
|
||||
contentType string
|
||||
body []byte
|
||||
uri *url.URL
|
||||
method string
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) Finish() error {
|
||||
fasthttp.ReleaseRequest(r.req)
|
||||
return nil
|
||||
}
|
||||
func (r *ProxyRequest) Header() http_service.IHeaderWriter {
|
||||
return r.headers
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) Body() http_service.IBodyDataWriter {
|
||||
return r.body
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) URI() http_service.IURIWriter {
|
||||
return r.uri
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) SetPath(s string) {
|
||||
r.initUrl()
|
||||
r.uri.Path = s
|
||||
r.Request().URI().SetPath(s)
|
||||
}
|
||||
|
||||
func NewProxyRequest(request *fasthttp.Request) *ProxyRequest {
|
||||
func NewProxyRequest(request *fasthttp.Request, remoteAddr string) *ProxyRequest {
|
||||
proxyRequest := fasthttp.AcquireRequest()
|
||||
request.CopyTo(proxyRequest)
|
||||
return &ProxyRequest{
|
||||
RequestReader: NewRequestReader(request, ""),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) SetHeader(key, value string) {
|
||||
if r.headers == nil {
|
||||
r.headers = r.Headers()
|
||||
}
|
||||
r.headers.Set(key, value)
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) AddHeader(key, value string) {
|
||||
if r.headers == nil {
|
||||
r.headers = r.Headers()
|
||||
}
|
||||
r.headers.Add(key, value)
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) DelHeader(key string) {
|
||||
if r.headers == nil {
|
||||
r.headers = r.Headers()
|
||||
}
|
||||
r.headers.Del(key)
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) SetForm(values url.Values) error {
|
||||
r.form = values
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) SetToForm(key, value string) error {
|
||||
if r.form == nil {
|
||||
form, err := r.BodyForm()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.form = form
|
||||
}
|
||||
r.form.Set(key, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) AddForm(key, value string) error {
|
||||
if r.form == nil {
|
||||
form, err := r.BodyForm()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.form = form
|
||||
}
|
||||
r.form.Set(key, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) AddFile(key string, file *http_service.FileHeader) error {
|
||||
if r.form == nil {
|
||||
file, err := r.Files()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.file = file
|
||||
}
|
||||
r.file[key] = file
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) SetRaw(contentType string, body []byte) {
|
||||
r.contentType, r.body = contentType, body
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) TargetServer() string {
|
||||
r.initUrl()
|
||||
return r.uri.Host
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) TargetURL() string {
|
||||
r.initUrl()
|
||||
return r.uri.Path
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) initUrl() {
|
||||
if r.uri == nil {
|
||||
uri := r.URL()
|
||||
r.uri = &uri
|
||||
RequestReader: NewRequestReader(proxyRequest, remoteAddr),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) SetMethod(s string) {
|
||||
r.method = s
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) SetURL(url url.URL) {
|
||||
r.uri = &url
|
||||
r.Request().Header.SetMethod(s)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user