forked from gemaraproj/go-gemara
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuri.go
More file actions
48 lines (43 loc) · 1.41 KB
/
uri.go
File metadata and controls
48 lines (43 loc) · 1.41 KB
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
// SPDX-License-Identifier: Apache-2.0
package fetcher
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
)
// URI routes to File or HTTP based on the source string.
//
// Recognized forms:
// - http:// or https:// URLs are fetched via [HTTP].
// - file:// URIs are fetched via [File].
// - Any other input without a scheme (absolute or relative local paths,
// including Windows drive paths) is treated as a local file path.
// - Inputs with any other <scheme>:// prefix return an unsupported-scheme error.
//
// For HTTP(S) sources it delegates to [HTTP]; see that type's
// documentation for security considerations.
type URI struct {
Client *http.Client
}
// schemePrefix matches a leading "<scheme>://" per RFC 3986 scheme syntax.
var schemePrefix = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9+.\-]*://`)
func (u *URI) Fetch(ctx context.Context, source string) (io.ReadCloser, error) {
switch {
case strings.HasPrefix(source, "http://"), strings.HasPrefix(source, "https://"):
return (&HTTP{Client: u.Client}).Fetch(ctx, source)
case strings.HasPrefix(source, "file://"):
parsed, err := url.Parse(source)
if err != nil {
return nil, fmt.Errorf("invalid file URI %q: %w", source, err)
}
return (&File{}).Fetch(ctx, parsed.Path)
case schemePrefix.MatchString(source):
return nil, fmt.Errorf("unsupported URI scheme in %q", source)
default:
return (&File{}).Fetch(ctx, source)
}
}