~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/resource/api/upload.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
        // Name is the resource name.
25
25
        Name string
26
26
 
 
27
        // Filename is the name of the file as it exists on disk.
 
28
        Filename string
 
29
 
27
30
        // Size is the size of the uploaded data, in bytes.
28
31
        Size int64
29
32
 
35
38
}
36
39
 
37
40
// NewUploadRequest generates a new upload request for the given resource.
38
 
func NewUploadRequest(service, name string, r io.ReadSeeker) (UploadRequest, error) {
 
41
func NewUploadRequest(service, name, filename string, r io.ReadSeeker) (UploadRequest, error) {
39
42
        if !names.IsValidService(service) {
40
43
                return UploadRequest{}, errors.Errorf("invalid service %q", service)
41
44
        }
48
51
        ur := UploadRequest{
49
52
                Service:     service,
50
53
                Name:        name,
 
54
                Filename:    filename,
51
55
                Size:        content.Size,
52
56
                Fingerprint: content.Fingerprint,
53
57
        }
58
62
func ExtractUploadRequest(req *http.Request) (UploadRequest, error) {
59
63
        var ur UploadRequest
60
64
 
61
 
        if req.Header.Get("Content-Length") == "" {
62
 
                req.Header.Set("Content-Length", fmt.Sprint(req.ContentLength))
 
65
        if req.Header.Get(HeaderContentLength) == "" {
 
66
                req.Header.Set(HeaderContentLength, fmt.Sprint(req.ContentLength))
63
67
        }
64
68
 
65
 
        ctype := req.Header.Get("Content-Type")
 
69
        ctype := req.Header.Get(HeaderContentType)
66
70
        if ctype != ContentTypeRaw {
67
71
                return ur, errors.Errorf("unsupported content type %q", ctype)
68
72
        }
69
73
 
70
74
        service, name := ExtractEndpointDetails(req.URL)
71
 
        fingerprint := req.Header.Get("Content-Sha384") // This parallels "Content-MD5".
72
 
        sizeRaw := req.Header.Get("Content-Length")
73
 
        pendingID := req.URL.Query().Get("pendingid")
 
75
        fingerprint := req.Header.Get(HeaderContentSha384) // This parallels "Content-MD5".
 
76
        sizeRaw := req.Header.Get(HeaderContentLength)
 
77
        pendingID := req.URL.Query().Get(QueryParamPendingID)
74
78
 
75
79
        fp, err := charmresource.ParseFingerprint(fingerprint)
76
80
        if err != nil {
77
81
                return ur, errors.Annotate(err, "invalid fingerprint")
78
82
        }
79
83
 
 
84
        filename, err := extractFilename(req)
 
85
        if err != nil {
 
86
                return ur, errors.Trace(err)
 
87
        }
 
88
 
80
89
        size, err := strconv.ParseInt(sizeRaw, 10, 64)
81
90
        if err != nil {
82
91
                return ur, errors.Annotate(err, "invalid size")
85
94
        ur = UploadRequest{
86
95
                Service:     service,
87
96
                Name:        name,
 
97
                Filename:    filename,
88
98
                Size:        size,
89
99
                Fingerprint: fp,
90
100
                PendingID:   pendingID,
92
102
        return ur, nil
93
103
}
94
104
 
 
105
func extractFilename(req *http.Request) (string, error) {
 
106
        disp := req.Header.Get(HeaderContentDisposition)
 
107
 
 
108
        // the first value returned here is the media type name (e.g. "form-data"),
 
109
        // but we don't really care.
 
110
        _, vals, err := parseMediaType(disp)
 
111
        if err != nil {
 
112
                return "", errors.Annotate(err, "badly formatted Content-Disposition")
 
113
        }
 
114
 
 
115
        param, ok := vals[filenameParamForContentDispositionHeader]
 
116
        if !ok {
 
117
                return "", errors.Errorf("missing filename in resource upload request")
 
118
        }
 
119
 
 
120
        filename, err := decodeParam(param)
 
121
        if err != nil {
 
122
                return "", errors.Annotatef(err, "couldn't decode filename %q from upload request", param)
 
123
        }
 
124
        return filename, nil
 
125
}
 
126
 
 
127
func setFilename(filename string, req *http.Request) {
 
128
        filename = encodeParam(filename)
 
129
 
 
130
        disp := formatMediaType(
 
131
                MediaTypeFormData,
 
132
                map[string]string{filenameParamForContentDispositionHeader: filename},
 
133
        )
 
134
 
 
135
        req.Header.Set(HeaderContentDisposition, disp)
 
136
}
 
137
 
 
138
// filenameParamForContentDispositionHeader is the name of the parameter that
 
139
// contains the name of the file being uploaded, see mime.FormatMediaType and
 
140
// RFC 1867 (http://tools.ietf.org/html/rfc1867):
 
141
//
 
142
//   The original local file name may be supplied as well, either as a
 
143
//  'filename' parameter either of the 'content-disposition: form-data'
 
144
//   header or in the case of multiple files in a 'content-disposition:
 
145
//   file' header of the subpart.
 
146
const filenameParamForContentDispositionHeader = "filename"
 
147
 
95
148
// HTTPRequest generates a new HTTP request.
96
149
func (ur UploadRequest) HTTPRequest() (*http.Request, error) {
97
150
        // TODO(ericsnow) What about the rest of the URL?
98
151
        urlStr := NewEndpointPath(ur.Service, ur.Name)
99
 
        req, err := http.NewRequest("PUT", urlStr, nil)
 
152
 
 
153
        // TODO(natefinch): Use http.MethodPut when we upgrade to go1.5+.
 
154
        req, err := http.NewRequest(MethodPut, urlStr, nil)
100
155
        if err != nil {
101
156
                return nil, errors.Trace(err)
102
157
        }
103
158
 
104
 
        req.Header.Set("Content-Type", ContentTypeRaw)
105
 
        req.Header.Set("Content-Sha384", ur.Fingerprint.String())
106
 
        req.Header.Set("Content-Length", fmt.Sprint(ur.Size))
 
159
        req.Header.Set(HeaderContentType, ContentTypeRaw)
 
160
        req.Header.Set(HeaderContentSha384, ur.Fingerprint.String())
 
161
        req.Header.Set(HeaderContentLength, fmt.Sprint(ur.Size))
 
162
        setFilename(ur.Filename, req)
 
163
 
107
164
        req.ContentLength = ur.Size
108
165
 
109
166
        if ur.PendingID != "" {
110
167
                query := req.URL.Query()
111
 
                query.Set("pendingid", ur.PendingID)
 
168
                query.Set(QueryParamPendingID, ur.PendingID)
112
169
                req.URL.RawQuery = query.Encode()
113
170
        }
114
171
 
115
172
        return req, nil
116
173
}
 
174
 
 
175
type encoder interface {
 
176
        Encode(charset, s string) string
 
177
}
 
178
 
 
179
type decoder interface {
 
180
        Decode(s string) (string, error)
 
181
}
 
182
 
 
183
func encodeParam(s string) string {
 
184
        return getEncoder().Encode("utf-8", s)
 
185
}
 
186
 
 
187
func decodeParam(s string) (string, error) {
 
188
        decoded, err := getDecoder().Decode(s)
 
189
 
 
190
        // If encoding is not required, the encoder will return the original string.
 
191
        // However, the decoder doesn't expect that, so it barfs on non-encoded
 
192
        // strings. To detect if a string was not encoded, we simply try encoding
 
193
        // again, if it returns the same string, we know it wasn't encoded.
 
194
        if err != nil && s == encodeParam(s) {
 
195
                return s, nil
 
196
        }
 
197
        return decoded, err
 
198
}