forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
147 lines (128 loc) · 3.94 KB
/
helpers.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package api
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
apifs "github.com/puppetlabs/wash/api/fs"
apitypes "github.com/puppetlabs/wash/api/types"
"github.com/puppetlabs/wash/plugin"
)
func findEntry(ctx context.Context, root plugin.Entry, segments []string) (plugin.Entry, *errorResponse) {
path := strings.Join(segments, "/")
curEntry, err := plugin.FindEntry(ctx, root, segments)
if err != nil {
if cnameErr, ok := err.(plugin.DuplicateCNameErr); ok {
return nil, duplicateCNameResponse(cnameErr)
}
return nil, entryNotFoundResponse(path, err.Error())
}
return curEntry, nil
}
// Common helper to get path query param from request and validate it.
func getPathFromRequest(r *http.Request) (string, *errorResponse) {
path := r.URL.Query().Get("path")
if path == "" {
return "", invalidPathsResponse()
}
if !filepath.IsAbs(path) {
return "", relativePathResponse(path)
}
return path, nil
}
// Common subset used by getEntryFromRequest and getWashPathFromRequest.
// getEntryFromRequest needs both the path and the wash path.
func toWashPath(ctx context.Context, path string) (string, *errorResponse) {
mountpoint := ctx.Value(mountpointKey).(string)
trimmedPath := strings.TrimPrefix(path, mountpoint)
if trimmedPath == path {
return path, nonWashPathResponse(path)
}
return trimmedPath, nil
}
// Simpler function to transform the path when an actual entry isn't needed.
func getWashPathFromRequest(r *http.Request) (string, *errorResponse) {
path, err := getPathFromRequest(r)
if err != nil {
return "", err
}
return toWashPath(r.Context(), path)
}
func getEntryFromRequest(r *http.Request) (plugin.Entry, string, *errorResponse) {
path, errResp := getPathFromRequest(r)
if errResp != nil {
return nil, "", errResp
}
ctx := r.Context()
trimmedPath, errResp := toWashPath(ctx, path)
if errResp != nil {
if errResp.body.Kind != apitypes.NonWashPath {
panic("Unexpected error from getWashPathFromFullPath")
}
// Local file/directory, so convert it to a Wash entry
//
// TODO: The code here means that the Wash server cannot be
// mounted on a remote machine. This is not an immediate issue,
// but it does mean that we'll need to re-evaluate this code once
// we get to the point where supporting remote Wash servers is
// desirable.
e, err := apifs.NewEntry(ctx, path)
if err != nil {
if os.IsNotExist(err) {
return nil, "", entryNotFoundResponse(path, err.Error())
}
err = fmt.Errorf("could not stat the regular file/dir pointed to by %v: %v", path, err)
return nil, "", unknownErrorResponse(err)
}
return e, path, nil
}
// Don't interpret trailing slash as a new segment, and ignore optional leading slash
trimmedPath = strings.Trim(trimmedPath, "/")
// Get the registry from context (added by registry middleware).
registry := ctx.Value(pluginRegistryKey).(*plugin.Registry)
if trimmedPath == "" {
// Return the registry
return registry, path, nil
}
// Split into plugin name and an optional list of segments.
segments := strings.Split(trimmedPath, "/")
pluginName := segments[0]
segments = segments[1:]
root, ok := registry.Plugins()[pluginName]
if !ok {
return nil, "", pluginDoesNotExistResponse(pluginName)
}
if len(segments) == 0 {
// Listing the plugin itself, so return it's root
return root, path, nil
}
entry, err := findEntry(ctx, root, segments)
return entry, path, err
}
func getBoolParam(u *url.URL, key string) (bool, *errorResponse) {
val := u.Query().Get(key)
if val != "" {
b, err := strconv.ParseBool(val)
if err != nil {
return false, invalidBoolParam(key, val)
}
return b, nil
}
return false, nil
}
// return is (n, found, err)
func getIntParam(u *url.URL, key string) (int, bool, *errorResponse) {
val := u.Query().Get(key)
if val != "" {
n, err := strconv.ParseInt(val, 10, 32)
if err != nil {
return 0, false, invalidIntParam(key, val)
}
return int(n), true, nil
}
return 0, false, nil
}