aboutsummaryrefslogtreecommitdiff
path: root/headers.go
blob: eb65488e7bdeebd2bbefd5aa9deb6109f6d3978f (plain)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/******************************************************************************
*
*  Copyright 2018 Stefan Majewsky <majewsky@gmx.net>
*
*  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 schwift

import (
	"fmt"
	"net/http"
	"net/textproto"
	"reflect"
	"strconv"
	"strings"
)

//AccountHeaders contains the headers for an account. The Raw attribute
//contains the original set of headers returned from a HEAD or GET request on
//the account. The other attributes contain the parsed values of common
//headers, as noted in the tags next to each field. Well-known metadata headers
//can be accessed in a type-safe way using the methods on this type.
type AccountHeaders struct {
	BytesUsed      uint64   `schwift:"ro,X-Account-Bytes-Used"`
	ContainerCount uint64   `schwift:"ro,X-Account-Container-Count"`
	ObjectCount    uint64   `schwift:"ro,X-Account-Object-Count"`
	Metadata       Metadata `schwift:"rw,X-Account-Meta-"`
	Raw            http.Header
}

//QuotaBytes returns a handle to read or write the X-Account-Meta-Quota-Bytes field.
func (a AccountHeaders) QuotaBytes() UnsignedIntField {
	return UnsignedIntField{
		a.Metadata,
		"X-Account-Meta-", "Quota-Bytes",
	}
}

//TempURLKey returns a handle to read or write the X-Account-Meta-Temp-URL-Key field.
func (a AccountHeaders) TempURLKey() StringField {
	return StringField{
		a.Metadata,
		"X-Account-Meta-", "Temp-URL-Key",
	}
}

//TempURLKey2 returns a handle to read or write the X-Account-Meta-Temp-URL-Key-2 field.
func (a AccountHeaders) TempURLKey2() StringField {
	return StringField{
		a.Metadata,
		"X-Account-Meta-", "Temp-URL-Key-2",
	}
}

////////////////////////////////////////////////////////////////////////////////
// field types

//StringField is a helper type used in the interface of AccountHeaders,
//ContainerHeaders and ObjectHeaders. For example:
//
//    var headers AccountHeaders
//    ...
//    value := headers.TempURLKey().Get()
//    headers.TempURLKey().Set(value + " changed")
//    headers.TempURLKey().Clear()
type StringField struct {
	metadata Metadata
	prefix   string
	key      string
}

//Get returns the value for this key, or the empty string if the key does not exist.
func (f StringField) Get() string {
	return f.metadata.Get(f.key)
}

//Set writes a new value for this key into the original AccountHeaders,
//ContainerHeaders or ObjectHeaders instance.
func (f StringField) Set(value string) {
	f.metadata.Set(f.key, value)
}

//Clear removes this key from the original AccountHeaders, ContainerHeaders or
//ObjectHeaders instance.
func (f StringField) Clear() {
	f.metadata.Set(f.key, "")
}

//UnsignedIntField is a helper type used in the interface of AccountHeaders,
//ContainerHeaders and ObjectHeaders. For example:
//
//    var headers AccountHeaders
//    ...
//    if headers.QuotaBytes.Exists() {
//        value, err := headers.QuotaBytes().Get()
//        headers.QuotaBytes().Set(value * 2)
//    }
//    ....
//    headers.QuotaBytes().Clear()
type UnsignedIntField struct {
	metadata Metadata
	prefix   string
	key      string
}

//Exists returns whether there is a value for this key.
func (f UnsignedIntField) Exists() bool {
	return f.metadata.Get(f.key) != ""
}

//Get returns the value for this key, or 0 if the key does not exist.
func (f UnsignedIntField) Get() (uint64, error) {
	str := f.metadata.Get(f.key)
	if str == "" {
		return 0, nil
	}
	value, err := strconv.ParseUint(str, 10, 64)
	if err != nil {
		err = MalformedHeaderError{Key: f.prefix + f.key, ParseError: err}
	}
	return value, err
}

//Set writes a new value for this key into the original AccountHeaders,
//ContainerHeaders or ObjectHeaders instance.
func (f UnsignedIntField) Set(value uint64) {
	f.metadata.Set(f.key, strconv.FormatUint(value, 10))
}

//Clear removes this key from the original AccountHeaders, ContainerHeaders or
//ObjectHeaders instance.
func (f UnsignedIntField) Clear() {
	f.metadata.Set(f.key, "")
}

////////////////////////////////////////////////////////////////////////////////
// generic parsing functions

func parseHeaders(hdr http.Header, target interface{}) error {
	return foreachField(target, func(fieldPtr interface{}, info fieldInfo) error {
		//populate the .Raw field that all input types share
		if info.FieldName == "Raw" {
			*(fieldPtr.(*http.Header)) = hdr
			return nil
		}

		//skip over fields without schwift field tag
		if info.HeaderName == "" {
			return nil
		}

		//decode header value into field depending on type
		switch fieldPtr := fieldPtr.(type) {
		case *string:
			*fieldPtr = hdr.Get(info.HeaderName)
		case *uint64:
			value, err := strconv.ParseUint(hdr.Get(info.HeaderName), 10, 64)
			if err != nil {
				return MalformedHeaderError{info.HeaderName, err}
			}
			*fieldPtr = value
		case *Metadata:
			//collect all headers with a prefix equal to `headerName`
			values := make(Metadata)
			for key, value := range hdr {
				key = textproto.CanonicalMIMEHeaderKey(key)
				if strings.HasPrefix(key, info.HeaderName) {
					key = strings.TrimPrefix(key, info.HeaderName)
					values[key] = value[0]
				}
			}
			*fieldPtr = values
		default:
			panic(fmt.Sprintf("parseHeaders: cannot handle field type %T", fieldPtr))
		}

		return nil
	})
}

func compileHeaders(headers interface{}, opts *RequestOptions) RequestOptions {
	hdr := make(http.Header)

	foreachField(headers, func(fieldPtr interface{}, info fieldInfo) error {
		//skip over fields without schwift field tag, and readonly fields
		if info.HeaderName == "" || info.Access != "rw" {
			return nil
		}

		//decode header value into field depending on type
		switch fieldPtr := fieldPtr.(type) {
		case *string:
			hdr.Set(info.HeaderName, *fieldPtr)
		case *uint64:
			hdr.Set(info.HeaderName, strconv.FormatUint(*fieldPtr, 10))
		case *Metadata:
			for key, value := range *fieldPtr {
				//empty string means that this key shall be removed
				if value == "" {
					//for object metadata, a key is removed by just omitting it...
					if info.HeaderName != "X-Object-Meta-" {
						//...for container and account metadata, a key is removed by
						//setting its value to the empty string
						hdr.Set(info.HeaderName+key, "")
					} else {
						//for object metadata, you just leave out the metadata fields that
						//you want to clear, so we do nothing
					}
				} else {
					//NOTE: The spec says that `value` needs to be percent-encoded, but
					//neither python-swiftclient nor ncw/swift do so. If in doubt, we
					//follow the de-facto standards rather than the spec.
					hdr.Set(info.HeaderName+key, value)
				}
			}
		default:
			panic(fmt.Sprintf("compileHeaders: cannot handle field type %T", fieldPtr))
		}
		return nil
	})

	//contents of `opts` overrides contents of `headers`
	result := RequestOptions{Headers: hdr}
	if opts != nil {
		result.Values = opts.Values
		for k, v := range opts.Headers {
			result.Headers[k] = v
		}
	}
	return result
}

type fieldInfo struct {
	FieldName  string
	Access     string
	HeaderName string
}

func foreachField(value interface{}, callback func(fieldPtr interface{}, info fieldInfo) error) error {
	rv := reflect.ValueOf(value)
	//unpack pointer type if necessary
	if rv.Type().Kind() == reflect.Ptr {
		rv = rv.Elem()
	}

	//iterate over the struct fields
	for idx := 0; idx < rv.NumField(); idx++ {
		fieldType := rv.Type().Field(idx)
		fieldPtr := rv.Field(idx).Addr().Interface()

		//decode schwift:"<access>,<header-name>" tag
		tagValues := strings.SplitN(fieldType.Tag.Get("schwift"), ",", 2)
		fieldInfo := fieldInfo{
			FieldName: fieldType.Name,
		}
		if len(tagValues) == 2 {
			fieldInfo.Access = tagValues[0]
			fieldInfo.HeaderName = tagValues[1]
		}

		err := callback(fieldPtr, fieldInfo)
		if err != nil {
			return err
		}
	}

	return nil
}