MCPcopy
hub / github.com/coyove/goflyway / ServeHTTP

Method ServeHTTP

proxy/server.go:146–354  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

144}
145
146func (proxy *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
147 replySomething := func() {
148 if proxy.rp == nil {
149 w.WriteHeader(404)
150 w.Write([]byte(`<html>
151<head><title>404 Not Found</title></head>
152<body bgcolor="white">
153<center><h1>404 Not Found</h1></center>
154<hr><center>nginx</center>
155</body>
156</html>`))
157 } else {
158 proxy.rp.ServeHTTP(w, r)
159 }
160 }
161
162 addr, _, err := net.SplitHostPort(r.RemoteAddr)
163 if err != nil {
164 proxy.Logger.W("Server", "Unknown address", r.RemoteAddr)
165 replySomething()
166 return
167 }
168
169 var rawReq []byte
170 if proxy.localRP.waiting != nil {
171 rawReq, _ = httputil.DumpRequest(r, true)
172 }
173
174 dst, cr := proxy.decryptHost(proxy.stripURI(r.RequestURI))
175
176 if dst == "" || cr == nil {
177 if proxy.localRP.waiting != nil {
178 userConn := proxy.hijack(w)
179 cb := make(chan localRPCtrlSrvResp, 1)
180
181 proxy.localRP.requests <- localRPCtrlSrvReq{
182 dst: dst,
183 conn: userConn,
184 callback: cb,
185 rawReq: rawReq,
186 }
187
188 proxy.Logger.D("Local RP", "Client", "Wait client to response")
189 select {
190 case resp := <-cb:
191 if resp.err != nil {
192 userConn.Write([]byte("HTTP/1.1 400 Bad Request\r\n\r\nError: " + resp.err.Error()))
193 return
194 }
195 case <-time.After(time.Duration(proxy.LBindTimeout) * time.Second):
196 proxy.Logger.E("Local RP", "Client", "client didn't response")
197 userConn.Write([]byte("HTTP/1.1 502 Bad Gateway\r\n\r\nError: localrp timed out"))
198 }
199 proxy.Logger.D("Local RP", "Client", "OK")
200
201 proxy.localRP.Lock()
202 for k, resp := range proxy.localRP.waiting {
203 if resp.req.conn == userConn {

Callers 1

curlFunction · 0.45

Calls 15

WriteMethod · 0.95
decryptHostMethod · 0.95
stripURIMethod · 0.95
hijackMethod · 0.95
authMethod · 0.95
getIOConfigMethod · 0.95
replyGoodMethod · 0.95
decryptRequestMethod · 0.95
copyHeadersFunction · 0.85
tryCloseFunction · 0.85
WriteHeaderMethod · 0.80

Tested by

no test coverage detected