1
1
package corehttp
2
2
3
3
import (
4
- "context"
5
4
"fmt"
6
5
"net"
7
6
"net/http"
8
- "sort"
9
7
10
- coreiface "github.com/ipfs/interface-go-ipfs-core"
11
8
options "github.com/ipfs/interface-go-ipfs-core/options"
12
- path "github.com/ipfs/interface-go-ipfs-core/path"
13
9
version "github.com/ipfs/kubo"
14
10
core "github.com/ipfs/kubo/core"
15
11
coreapi "github.com/ipfs/kubo/core/coreapi"
12
+ "github.com/ipfs/kubo/core/corehttp/gateway"
16
13
id "github.com/libp2p/go-libp2p/p2p/protocol/identify"
17
14
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
18
15
)
19
16
20
- type GatewayConfig struct {
21
- Headers map [string ][]string
22
- Writable bool
23
- }
24
-
25
- // NodeAPI defines the minimal set of API services required by a gateway handler
26
- type NodeAPI interface {
27
- // Unixfs returns an implementation of Unixfs API
28
- Unixfs () coreiface.UnixfsAPI
29
-
30
- // Block returns an implementation of Block API
31
- Block () coreiface.BlockAPI
32
-
33
- // Dag returns an implementation of Dag API
34
- Dag () coreiface.APIDagService
35
-
36
- // Routing returns an implementation of Routing API.
37
- // Used for returning signed IPNS records, see IPIP-0328
38
- Routing () coreiface.RoutingAPI
39
-
40
- // ResolvePath resolves the path using Unixfs resolver
41
- ResolvePath (context.Context , path.Path ) (path.Resolved , error )
42
- }
43
-
44
- // A helper function to clean up a set of headers:
45
- // 1. Canonicalizes.
46
- // 2. Deduplicates.
47
- // 3. Sorts.
48
- func cleanHeaderSet (headers []string ) []string {
49
- // Deduplicate and canonicalize.
50
- m := make (map [string ]struct {}, len (headers ))
51
- for _ , h := range headers {
52
- m [http .CanonicalHeaderKey (h )] = struct {}{}
53
- }
54
- result := make ([]string , 0 , len (m ))
55
- for k := range m {
56
- result = append (result , k )
57
- }
58
-
59
- // Sort
60
- sort .Strings (result )
61
- return result
62
- }
63
-
64
17
func GatewayOption (writable bool , paths ... string ) ServeOption {
65
18
return func (n * core.IpfsNode , _ net.Listener , mux * http.ServeMux ) (* http.ServeMux , error ) {
66
19
cfg , err := n .Repo .Config ()
@@ -78,14 +31,14 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
78
31
headers [http .CanonicalHeaderKey (h )] = v
79
32
}
80
33
81
- AddAccessControlHeaders (headers )
34
+ gateway . AddAccessControlHeaders (headers )
82
35
83
36
offlineAPI , err := api .WithOptions (options .Api .Offline (true ))
84
37
if err != nil {
85
38
return nil , err
86
39
}
87
40
88
- gateway := NewGatewayHandler ( GatewayConfig {
41
+ gateway := gateway . NewHandler (gateway. Config {
89
42
Headers : headers ,
90
43
Writable : writable ,
91
44
}, api , offlineAPI )
@@ -99,50 +52,6 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
99
52
}
100
53
}
101
54
102
- // AddAccessControlHeaders adds default headers used for controlling
103
- // cross-origin requests. This function adds several values to the
104
- // Access-Control-Allow-Headers and Access-Control-Expose-Headers entries.
105
- // If the Access-Control-Allow-Origin entry is missing a value of '*' is
106
- // added, indicating that browsers should allow requesting code from any
107
- // origin to access the resource.
108
- // If the Access-Control-Allow-Methods entry is missing a value of 'GET' is
109
- // added, indicating that browsers may use the GET method when issuing cross
110
- // origin requests.
111
- func AddAccessControlHeaders (headers map [string ][]string ) {
112
- // Hard-coded headers.
113
- const ACAHeadersName = "Access-Control-Allow-Headers"
114
- const ACEHeadersName = "Access-Control-Expose-Headers"
115
- const ACAOriginName = "Access-Control-Allow-Origin"
116
- const ACAMethodsName = "Access-Control-Allow-Methods"
117
-
118
- if _ , ok := headers [ACAOriginName ]; ! ok {
119
- // Default to *all*
120
- headers [ACAOriginName ] = []string {"*" }
121
- }
122
- if _ , ok := headers [ACAMethodsName ]; ! ok {
123
- // Default to GET
124
- headers [ACAMethodsName ] = []string {http .MethodGet }
125
- }
126
-
127
- headers [ACAHeadersName ] = cleanHeaderSet (
128
- append ([]string {
129
- "Content-Type" ,
130
- "User-Agent" ,
131
- "Range" ,
132
- "X-Requested-With" ,
133
- }, headers [ACAHeadersName ]... ))
134
-
135
- headers [ACEHeadersName ] = cleanHeaderSet (
136
- append ([]string {
137
- "Content-Length" ,
138
- "Content-Range" ,
139
- "X-Chunked-Output" ,
140
- "X-Stream-Output" ,
141
- "X-Ipfs-Path" ,
142
- "X-Ipfs-Roots" ,
143
- }, headers [ACEHeadersName ]... ))
144
- }
145
-
146
55
func VersionOption () ServeOption {
147
56
return func (_ * core.IpfsNode , _ net.Listener , mux * http.ServeMux ) (* http.ServeMux , error ) {
148
57
mux .HandleFunc ("/version" , func (w http.ResponseWriter , r * http.Request ) {
0 commit comments