mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-27 01:04:47 +08:00
支持重定以及cookie功能
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
|||||||
"github.com/duanhf2012/origin/originnode"
|
"github.com/duanhf2012/origin/originnode"
|
||||||
"github.com/duanhf2012/origin/service"
|
"github.com/duanhf2012/origin/service"
|
||||||
"github.com/duanhf2012/origin/sysservice/originhttp"
|
"github.com/duanhf2012/origin/sysservice/originhttp"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type InputData struct {
|
type InputData struct {
|
||||||
@@ -24,7 +26,7 @@ func init() {
|
|||||||
//OnInit ...
|
//OnInit ...
|
||||||
func (ws *SubNet1_Service1) OnInit() error {
|
func (ws *SubNet1_Service1) OnInit() error {
|
||||||
originhttp.Post("", ws.HTTP_UserIntegralInfo)
|
originhttp.Post("", ws.HTTP_UserIntegralInfo)
|
||||||
originhttp.Post(" /aaa/bbb", ws.Test)
|
originhttp.Get("/aaa/bbb", ws.HTTP_Test)
|
||||||
originhttp.Get("/Login/bbb", ws.HTTP_UserIntegralInfo)
|
originhttp.Get("/Login/bbb", ws.HTTP_UserIntegralInfo)
|
||||||
originhttp.SetStaticResource(originhttp.METHOD_GET, "/file/", "d:\\")
|
originhttp.SetStaticResource(originhttp.METHOD_GET, "/file/", "d:\\")
|
||||||
|
|
||||||
@@ -44,12 +46,21 @@ func (slf *SubNet1_Service1) RPC_Add(arg *InputData, ret *int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *SubNet1_Service1) Test(request *originhttp.HttpRequest, resp *originhttp.HttpRespone) error {
|
|
||||||
|
|
||||||
|
func (slf *SubNet1_Service1) HTTP_Test(request *originhttp.HttpRequest, resp *originhttp.HttpRespone) error {
|
||||||
|
var cookieList []*http.Cookie
|
||||||
|
cookieList = append(cookieList,&http.Cookie{Name: "X-Xsrftoken",Value: "df41ba54db5011e89861002324e63af81", HttpOnly: true,Domain:"urquant.net"})
|
||||||
|
resp.Redirect("https://www.urquant.net?sdaf=1",cookieList)
|
||||||
|
//redirects(&resp.Resp, &request.Req)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *SubNet1_Service1) HTTP_UserIntegralInfo(request *originhttp.HttpRequest, resp *originhttp.HttpRespone) error {
|
func (slf *SubNet1_Service1) HTTP_UserIntegralInfo(request *originhttp.HttpRequest, resp *originhttp.HttpRespone) error {
|
||||||
ret, ok := request.Query("a")
|
ret, ok := request.Query("a")
|
||||||
fmt.Print(ret, ok)
|
fmt.Print(ret, ok)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,16 +21,28 @@ import (
|
|||||||
"github.com/duanhf2012/origin/service"
|
"github.com/duanhf2012/origin/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type HttpRedirectData struct {
|
||||||
|
Url string
|
||||||
|
//Cookies map[string]string
|
||||||
|
|
||||||
|
CookieList []*http.Cookie
|
||||||
|
}
|
||||||
|
|
||||||
type HttpRequest struct {
|
type HttpRequest struct {
|
||||||
Header http.Header
|
Header http.Header
|
||||||
Body string
|
Body string
|
||||||
|
|
||||||
ParamStr string
|
ParamStr string
|
||||||
mapParam map[string]string
|
mapParam map[string]string
|
||||||
|
//Req http.Request
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type HttpRespone struct {
|
type HttpRespone struct {
|
||||||
Respone []byte
|
Respone []byte
|
||||||
|
RedirectData HttpRedirectData
|
||||||
|
//Resp http.ResponseWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServeHTTPRouterMux struct {
|
type ServeHTTPRouterMux struct {
|
||||||
@@ -218,7 +230,7 @@ func (slf *ServeHTTPRouterMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
|
|||||||
|
|
||||||
request := HttpRequest{r.Header, string(msg), r.URL.RawQuery, nil}
|
request := HttpRequest{r.Header, string(msg), r.URL.RawQuery, nil}
|
||||||
var resp HttpRespone
|
var resp HttpRespone
|
||||||
|
//resp.Resp = w
|
||||||
timeFuncStart := time.Now()
|
timeFuncStart := time.Now()
|
||||||
err = cluster.InstanceClusterMgr().Call(strCallPath, &request, &resp)
|
err = cluster.InstanceClusterMgr().Call(strCallPath, &request, &resp)
|
||||||
|
|
||||||
@@ -229,7 +241,12 @@ func (slf *ServeHTTPRouterMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
writeRespone(w, http.StatusBadRequest, fmt.Sprint(err))
|
writeRespone(w, http.StatusBadRequest, fmt.Sprint(err))
|
||||||
} else {
|
} else {
|
||||||
writeRespone(w, http.StatusOK, string(resp.Respone))
|
if resp.RedirectData.Url != ""{
|
||||||
|
resp.redirects(&w,r)
|
||||||
|
}else {
|
||||||
|
writeRespone(w, http.StatusOK, string(resp.Respone))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -439,3 +456,23 @@ func (slf *HttpRespone) WriteRespones(Code int32, Msg string, Data interface{})
|
|||||||
}
|
}
|
||||||
slf.Respone = []byte(StrRet)
|
slf.Respone = []byte(StrRet)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (slf *HttpRespone) Redirect(url string,cookieList []*http.Cookie) {
|
||||||
|
slf.RedirectData.Url = url
|
||||||
|
slf.RedirectData.CookieList = cookieList
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func (slf *HttpRespone) redirects(w *http.ResponseWriter, req *http.Request) {
|
||||||
|
if slf.RedirectData.CookieList != nil {
|
||||||
|
for _,v := range slf.RedirectData.CookieList{
|
||||||
|
req.AddCookie(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(*w, req, slf.RedirectData.Url,
|
||||||
|
// see @andreiavrammsd comment: often 307 > 301
|
||||||
|
http.StatusTemporaryRedirect)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user