-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
http.go
252 lines (208 loc) · 6.4 KB
/
http.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright 2018 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scp
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/httplib"
"github.com/gravitational/trace"
)
const (
// 644 means that files are readable and writeable by the owner of
// the file and readable by users in the group owner of that file
// and readable by everyone else.
httpUploadFileMode = 0644
)
// HTTPTransferRequest describes HTTP file transfer request
type HTTPTransferRequest struct {
// RemoteLocation is a destination location of the file
RemoteLocation string
// FileName is a file name
FileName string
// HTTPRequest is HTTP request
HTTPRequest *http.Request
// HTTPRequest is HTTP request
HTTPResponse http.ResponseWriter
// ProgressWriter is a writer for printing the progress
Progress io.Writer
// User is a user name
User string
// AuditLog is AuditLog log
AuditLog events.IAuditLog
}
func (r *HTTPTransferRequest) parseRemoteLocation() (string, string, error) {
dir, filename := filepath.Split(r.RemoteLocation)
if filename == "" {
return "", "", trace.BadParameter("failed to parse file remote location: %q", r.RemoteLocation)
}
return dir, filename, nil
}
// CreateHTTPUpload creates HTTP download command
func CreateHTTPUpload(req HTTPTransferRequest) (Command, error) {
if req.HTTPRequest == nil {
return nil, trace.BadParameter("missing parameter HTTPRequest")
}
if req.FileName == "" {
return nil, trace.BadParameter("missing file name")
}
if req.RemoteLocation == "" {
return nil, trace.BadParameter("missing remote location")
}
contentLength := req.HTTPRequest.Header.Get("Content-Length")
fileSize, err := strconv.ParseInt(contentLength, 10, 0)
if err != nil {
return nil, trace.BadParameter("failed to parse Content-Length header: %q", contentLength)
}
fs := &httpFileSystem{
reader: req.HTTPRequest.Body,
fileName: req.FileName,
fileSize: fileSize,
}
flags := Flags{
// scp treats it as a list of files to upload
Target: []string{req.FileName},
}
cfg := Config{
Flags: flags,
FileSystem: fs,
User: req.User,
ProgressWriter: req.Progress,
RemoteLocation: req.RemoteLocation,
AuditLog: req.AuditLog,
}
cmd, err := CreateUploadCommand(cfg)
if err != nil {
return nil, trace.Wrap(err)
}
return cmd, nil
}
// CreateHTTPDownload creates HTTP upload command
func CreateHTTPDownload(req HTTPTransferRequest) (Command, error) {
_, filename, err := req.parseRemoteLocation()
if err != nil {
return nil, trace.Wrap(err)
}
flags := Flags{
Target: []string{filename},
}
cfg := Config{
Flags: flags,
User: req.User,
ProgressWriter: req.Progress,
RemoteLocation: req.RemoteLocation,
FileSystem: &httpFileSystem{
writer: req.HTTPResponse,
},
}
cmd, err := CreateDownloadCommand(cfg)
if err != nil {
return nil, trace.Wrap(err)
}
return cmd, nil
}
// httpFileSystem simulates file system calls while using HTTP response/request streams.
type httpFileSystem struct {
writer http.ResponseWriter
reader io.ReadCloser
fileName string
fileSize int64
}
// SetChmod sets file permissions. It does nothing as there are no permissions
// while processing HTTP downloads
func (l *httpFileSystem) SetChmod(path string, mode int) error {
return nil
}
// MkDir creates a directory. This method is not implemented as creating directories
// is not supported during HTTP downloads.
func (l *httpFileSystem) MkDir(path string, mode int) error {
return trace.BadParameter("directories are not supported in http file transfer")
}
// IsDir tells if this file is a directory. It always returns false as
// directories are not supported in HTTP file transfer
func (l *httpFileSystem) IsDir(path string) bool {
return false
}
// OpenFile returns file reader
func (l *httpFileSystem) OpenFile(filePath string) (io.ReadCloser, error) {
if l.reader == nil {
return nil, trace.BadParameter("missing reader")
}
return l.reader, nil
}
// CreateFile sets proper HTTP headers and returns HTTP writer to stream incoming
// file content
func (l *httpFileSystem) CreateFile(filePath string, length uint64) (io.WriteCloser, error) {
_, filename := filepath.Split(filePath)
contentLength := strconv.FormatUint(length, 10)
header := l.writer.Header()
httplib.SetNoCacheHeaders(header)
httplib.SetNoSniff(header)
header.Set("Content-Length", contentLength)
header.Set("Content-Type", "application/octet-stream")
filename = url.QueryEscape(filename)
header.Set("Content-Disposition", fmt.Sprintf(`attachment;filename="%v"`, filename))
return &nopWriteCloser{Writer: l.writer}, nil
}
// GetFileInfo returns file information
func (l *httpFileSystem) GetFileInfo(filePath string) (FileInfo, error) {
return &httpFileInfo{
name: l.fileName,
path: l.fileName,
size: l.fileSize,
}, nil
}
// httpFileInfo is implementation of FileInfo interface used during HTTP
// file transfer
type httpFileInfo struct {
path string
name string
size int64
}
// IsDir tells if this file in a directory
func (l *httpFileInfo) IsDir() bool {
return false
}
// GetName returns file name
func (l *httpFileInfo) GetName() string {
return l.name
}
// GetPath returns file path
func (l *httpFileInfo) GetPath() string {
return l.path
}
// GetSize returns file size
func (l *httpFileInfo) GetSize() int64 {
return l.size
}
// ReadDir returns an slice of files in the directory.
// This method is not supported in HTTP file transfer
func (l *httpFileInfo) ReadDir() ([]FileInfo, error) {
return nil, trace.BadParameter("directories are not supported in http file transfer")
}
// GetModePerm returns file permissions that will be set on the
// file created on the remote host during HTTP upload.
func (l *httpFileInfo) GetModePerm() os.FileMode {
return httpUploadFileMode
}
type nopWriteCloser struct {
io.Writer
}
func (wr *nopWriteCloser) Close() error {
return nil
}