-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_http.go
More file actions
49 lines (38 loc) · 820 Bytes
/
handler_http.go
File metadata and controls
49 lines (38 loc) · 820 Bytes
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
package ztp
import (
"bufio"
"io"
"net"
"net/http"
)
type HTTPProxyHandler struct{}
func NewHTTPProxyHandler() *HTTPProxyHandler {
return &HTTPProxyHandler{}
}
var httpDial = net.Dial
func (_ *HTTPProxyHandler) Protocol() Protocol { return ProtocolHTTP }
func (_ *HTTPProxyHandler) Handle(client net.Conn, svc *Service, id *Identity) error {
buf := bufio.NewReader(client)
req, err := http.ReadRequest(buf)
if err != nil {
return err
}
backend, err := httpDial("tcp", svc.Address)
if err != nil {
return err
}
defer backend.Close()
if err = req.Write(backend); err == nil {
errCh := make(chan error, 1)
go func() {
_, err = io.Copy(backend, buf)
errCh <- err
}()
_, copyErr := io.Copy(client, backend)
_ = <-errCh
if copyErr != nil {
return copyErr
}
}
return nil
}