-
Notifications
You must be signed in to change notification settings - Fork 112
/
cgiProvider.go
183 lines (149 loc) · 4.26 KB
/
cgiProvider.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
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
175
176
177
178
179
180
181
182
183
package provider
// CGI RFC: https://datatracker.ietf.org/doc/rfc3875/?include_text=1
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
cgi "github.com/beberlei/fastcgi-serve/fcgiclient"
motan "github.com/weibocom/motan-go/core"
"github.com/weibocom/motan-go/log"
// "github.com/yangchenxing/go-nginx-conf-parser"
)
const (
CGIKeyPrefix = "CGI_"
DefaultCGIHost = "127.0.0.1"
DefaultCGIPort = 9000
HTTPMethodPOST = "POST"
HTTPMethodGET = "GET"
)
var serverEnvironment = map[string]string{"SERVER_SOFTWARE": "Motan / CGI"}
var NeededCGIEnv = []string{"REQUEST_METHOD", "SCRIPT_FILENAME", "DOCUMENT_ROOT"}
type CgiProvider struct {
url *motan.URL
}
func (c *CgiProvider) Initialize() {
}
func (c *CgiProvider) Destroy() {
}
func (c *CgiProvider) SetSerialization(s motan.Serialization) {}
func (c *CgiProvider) SetProxy(proxy bool) {}
func (c *CgiProvider) Call(request motan.Request) motan.Response {
// @TODO if cgi server die, should let server node unavailable
t := time.Now().UnixNano()
resp := &motan.MotanResponse{Attachment: motan.NewStringMap(motan.DefaultAttachmentSize)}
toType := make([]interface{}, 1)
if err := request.ProcessDeserializable(toType); err != nil {
fillException(resp, t, err)
return resp
}
env := make(map[string]string)
reqParams := ""
for name, value := range serverEnvironment {
env[name] = value
}
cgiKey := ""
for _, key := range NeededCGIEnv {
cgiKey = CGIKeyPrefix + key
if info, ok := c.url.Parameters[cgiKey]; ok {
env[key] = info
} else {
vlog.Infof("NeededCGIEnv %s is not exist", cgiKey)
}
}
request.GetAttachments().Range(func(k, v string) bool {
env["MOTAN_"+k] = v
return true
})
if env["REQUEST_METHOD"] == HTTPMethodGET {
if queryStr, err := buildQueryStr(request, c.url, nil); err == nil {
env["QUERY_STRING"] = queryStr
}
} else if env["REQUEST_METHOD"] == HTTPMethodPOST {
if getReqParams, err := buildQueryStr(request, c.url, nil); err == nil {
reqParams = getReqParams
}
env["CONTENT_TYPE"] = "application/x-www-form-urlencoded"
env["CONTENT_LENGTH"] = strconv.Itoa(len(reqParams))
}
cgiHost := DefaultCGIHost
cgiPort := DefaultCGIPort
if host, ok := c.url.Parameters["CGI_HOST"]; ok {
cgiHost = host
}
if portStr, ok := c.url.Parameters["CGI_PORT"]; ok {
cgiPort, _ = strconv.Atoi(portStr)
}
ccgi, err := cgi.New(cgiHost, cgiPort)
if err != nil {
vlog.Errorf("new CGI err: %v", err)
fillException(resp, t, err)
return resp
}
content, _, err := ccgi.Request(env, reqParams)
if err != nil {
vlog.Errorf("CGI Call error: %+v", err)
fillException(resp, t, err)
return resp
}
statusCode, headers, body, err := ParseFastCgiResponse(string(content))
resp.RequestID = request.GetRequestID()
resp.ProcessTime = int64((time.Now().UnixNano() - t) / 1000000)
if err != nil {
//@TODO ErrTYpe
resp.Exception = &motan.Exception{ErrCode: statusCode, ErrMsg: fmt.Sprintf("%s", err), ErrType: statusCode}
return resp
}
for k, v := range headers {
resp.SetAttachment(k, v)
}
// resp.Value, _ = MarshalX(body)
resp.Value = body
return resp
}
func ParseFastCgiResponse(content string) (int, map[string]string, string, error) {
var headers map[string]string
parts := strings.SplitN(content, "\r\n\r\n", 2)
if len(parts) < 2 {
return 502, headers, "", errors.New("Cannot parse FastCGI Response")
}
headerParts := strings.Split(parts[0], "\r\n")
headers = make(map[string]string, len(headerParts))
body := parts[1]
status := 200
if strings.HasPrefix(headerParts[0], "Status:") {
lineParts := strings.SplitN(headerParts[0], " ", 3)
status, _ = strconv.Atoi(lineParts[1])
}
for _, line := range headerParts {
lineParts := strings.SplitN(line, ":", 2)
if len(lineParts) < 2 {
continue
}
lineParts[1] = strings.TrimSpace(lineParts[1])
if lineParts[0] == "Status" {
continue
}
headers[lineParts[0]] = lineParts[1]
}
return status, headers, body, nil
}
func (c *CgiProvider) GetName() string {
return "CgiProvider"
}
func (c *CgiProvider) GetURL() *motan.URL {
return c.url
}
func (c *CgiProvider) SetURL(url *motan.URL) {
c.url = url
}
func (c *CgiProvider) IsAvailable() bool {
//TODO Provider 是否可用
return true
}
func (c *CgiProvider) SetService(s interface{}) {
}
func (c *CgiProvider) GetPath() string {
return c.url.Path
}