switch to new path format in p2p http proxy
License: MIT Signed-off-by: Ian Preston <ianopolous@protonmail.com>
Dr Ian Preston committed
Nov 10, 2018 at 22:08 UTC
fd43f473b4698fe027dd93f3eac96dd47564d243
1 file changed
+16
-5
core/corehttp/proxy.go
+16
-5
@@ -17,7 +17,7 @@ import (
17
// ProxyOption is an endpoint for proxying a HTTP request to another ipfs peer
18
func ProxyOption() ServeOption {
19
return func(ipfsNode *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
20
- mux.HandleFunc("/proxy/http/", func(w http.ResponseWriter, request *http.Request) {
20
+ mux.HandleFunc("/p2p/", func(w http.ResponseWriter, request *http.Request) {
21
// parse request
22
parsedRequest, err := parseRequest(request)
23
if err != nil {
@@ -49,16 +49,27 @@ type proxyRequest struct {
49
}
50
51
// from the url path parse the peer-ID, name and http path
52
-// /proxy/http/$peer_id/$name/$http_path
52
+// /p2p/$peer_id/http/$http_path
53
+// or
54
+// /p2p/$peer_id/x/$protocol/http/$http_path
55
func parseRequest(request *http.Request) (*proxyRequest, error) {
56
path := request.URL.Path
57
56
- split := strings.SplitN(path, "/", 6)
57
- if len(split) < 6 {
58
+ split := strings.SplitN(path, "/", 5)
59
+ if len(split) < 5 {
60
return nil, fmt.Errorf("Invalid request path '%s'", path)
61
}
62
61
- return &proxyRequest{split[3], protocol.ID(split[4]), split[5]}, nil
63
+ if split[3] == "http" {
64
+ return &proxyRequest{split[2], protocol.ID("/http"), split[4]}, nil
65
+ }
66
+
67
+ split = strings.SplitN(path, "/", 7)
68
+ if split[3] != "x" || split[5] != "http" {
69
+ return nil, fmt.Errorf("Invalid request path '%s'", path)
70
+ }
71
+
72
+ return &proxyRequest{split[2], protocol.ID("/x/" + split[4] + "/http"), split[6]}, nil
73
}
74
75
func handleError(w http.ResponseWriter, msg string, err error, code int) {