MCPcopy Create free account
hub / github.com/bepass-org/proxy / embedHandleHTTP

Method embedHandleHTTP

pkg/http/server.go:183–238  ·  view source on GitHub ↗
(conn net.Conn, req *http.Request, isConnectMethod bool)

Source from the content-addressed store, hash-verified

181}
182
183func (s *Server) embedHandleHTTP(conn net.Conn, req *http.Request, isConnectMethod bool) error {
184 defer func() {
185 _ = conn.Close()
186 }()
187
188 targetAddr := req.URL.Host
189 host, portStr, err := net.SplitHostPort(targetAddr)
190 if err != nil {
191 host = targetAddr
192 if req.URL.Scheme == "https" || isConnectMethod {
193 portStr = "443"
194 } else {
195 portStr = "80"
196 }
197 targetAddr = net.JoinHostPort(host, portStr)
198 }
199
200 target, err := s.ProxyDial(s.Context, "tcp", targetAddr)
201 if err != nil {
202 http.Error(
203 NewHTTPResponseWriter(conn),
204 err.Error(),
205 http.StatusServiceUnavailable,
206 )
207 return err
208 }
209 defer func() {
210 _ = target.Close()
211 }()
212
213 if isConnectMethod {
214 _, err = conn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))
215 if err != nil {
216 return err
217 }
218 } else {
219 err = req.Write(target)
220 if err != nil {
221 return err
222 }
223 }
224
225 var buf1, buf2 []byte
226 if s.BytesPool != nil {
227 buf1 = s.BytesPool.Get()
228 buf2 = s.BytesPool.Get()
229 defer func() {
230 s.BytesPool.Put(buf1)
231 s.BytesPool.Put(buf2)
232 }()
233 } else {
234 buf1 = make([]byte, 32*1024)
235 buf2 = make([]byte, 32*1024)
236 }
237 return statute.Tunnel(s.Context, target, conn, buf1, buf2)
238}

Callers 1

handleHTTPMethod · 0.95

Calls 7

TunnelFunction · 0.92
NewHTTPResponseWriterFunction · 0.85
CloseMethod · 0.80
GetMethod · 0.80
PutMethod · 0.80
ErrorMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected