forked from hidu/pproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
req_redo.go
135 lines (118 loc) · 3.32 KB
/
req_redo.go
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
91
92
93
94
95
96
97
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
package serve
import (
"fmt"
"net/http"
"net/url"
"strings"
)
const (
REDO_FLAG = "Proxy-pproxy_redo"
REDO_REMOTEADDR = "Proxy-pproxy_remoteaddr"
REDO_USER_NAME = "Proxy-pproxy_user"
)
func (ser *ProxyServe) req_redo(w http.ResponseWriter, req *http.Request, values map[string]interface{}) {
if req.Method == "POST" {
ser.req_redoPost(w, req, values)
return
}
docid_str := strings.TrimSpace(req.FormValue("id"))
if docid_str == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("empty id param"))
return
}
docid, err_int := parseDocId(docid_str)
if err_int != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("param id[%s] error:\n%s", docid_str, err_int)))
return
}
req_doc := ser.GetRequestByDocid(docid)
if req_doc == nil {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("request doc not found!"))
return
}
_url := fmt.Sprintf("%s", req_doc["url"])
u, err := url.Parse(_url)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("parse url[%s] error\n%s", _url, err)))
return
}
u.RawQuery = ""
values["req"] = req_doc
values["action_url"] = u.String()
values["subTitle"] = "redo|" + u.String() + "|"
html := render_html("redo.html", values, true)
w.Write([]byte(html))
}
var redo_skip_headers = map[string]int{"Content-Length": 1}
func (ser *ProxyServe) req_redoPost(w http.ResponseWriter, req *http.Request, values map[string]interface{}) {
redo := req.FormValue("redo")
basic := make(map[string]string)
basic["action_url"] = strings.TrimSpace(req.FormValue("basic_action_url"))
method := strings.TrimSpace(strings.ToUpper(req.FormValue("basic_method")))
basic["method"] = method
host := strings.TrimSpace(req.FormValue("basic_host"))
basic_remoteAddr := req.FormValue("basic_RemoteAddr")
basic_user := req.FormValue("basic_user")
header := GetFormValuesWithPrefix(req.Form, "header_")
get := GetFormValuesWithPrefix(req.Form, "get_")
post := GetFormValuesWithPrefix(req.Form, "post_")
formData := make(map[string]interface{})
formData["basic"] = basic
formData["header"] = header
formData["get"] = get
formData["post"] = post
values["form"] = formData
if redo == "direct" {
html := render_html("redo_direct.html", values, true)
w.Write([]byte(html))
return
} else {
req_bd := ""
_url := basic["action_url"]
if len(get) > 0 {
form_values := make(url.Values)
for k, v := range get {
for _, _v := range v {
form_values.Add(k, _v)
}
}
if strings.Contains(_url, "?") {
_url += "&"
} else {
_url += "?"
}
_url += form_values.Encode()
}
if len(post) > 0 {
form_values := make(url.Values)
for k, v := range post {
for _, _v := range v {
form_values.Add(k, _v)
}
}
req_bd = form_values.Encode()
}
redo_req, err := http.NewRequest(method, _url, strings.NewReader(req_bd))
if err != nil {
w.Write([]byte("build request failed\n" + err.Error()))
return
}
if host != "" {
redo_req.Host = host
}
redo_req.Header.Set(REDO_FLAG, "redo")
redo_req.Header.Set(REDO_REMOTEADDR, basic_remoteAddr)
redo_req.Header.Set(REDO_USER_NAME, basic_user)
for k, v := range header {
if _, has := redo_skip_headers[k]; has {
continue
}
redo_req.Header.Set(k, strings.Join(v, ";"))
}
ser.httpProxy.ServeHTTP(w, redo_req)
}
}