aboutsummaryrefslogtreecommitdiff
path: root/headers.go
blob: ec953d28a7b9078ffcf47d78a44a7e921ed96873 (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
/******************************************************************************
*
*  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 (
	"net/http"
	"net/textproto"
)

// Headers represents a set of request headers or response headers.
//
// Users will typically use one of the subtypes (AccountHeaders,
// ContainerHeaders, ObjectHeaders) instead, which provide type-safe access to
// well-known headers. The http.Header-like interface on this type can be used
// read and write arbitary headers. For example, the following calls are
// equivalent:
//
//	h := make(AccountHeaders)
//	h.Headers.Set("X-Account-Meta-Quota-Bytes", "1048576")
//	h.BytesUsedQuota().Set(1048576)
type Headers map[string]string

// Clear sets the value for the specified header to the empty string. When the
// Headers instance is then sent to the server with Update(), the server will
// delete the value for that header; cf. Del().
func (h Headers) Clear(key string) {
	h[textproto.CanonicalMIMEHeaderKey(key)] = ""
}

// Del deletes a key from the Headers instance. When the Headers instance is
// then sent to the server with Update(), a key which has been deleted with
// Del() will remain unchanged on the server.
//
// For most writable attributes, a key which has been deleted with Del() will
// remain unchanged on the server. To remove the key on the server, use Clear()
// instead.
//
// For object metadata (but not other object attributes), deleting a key will
// cause that key to be deleted on the server. Del() is identical to Clear() in
// this case.
func (h Headers) Del(key string) {
	delete(h, textproto.CanonicalMIMEHeaderKey(key))
}

// Get returns the value for the specified header.
func (h Headers) Get(key string) string {
	return h[textproto.CanonicalMIMEHeaderKey(key)]
}

// Set sets a new value for the specified header. Any existing value will be
// overwritten.
func (h Headers) Set(key, value string) {
	h[textproto.CanonicalMIMEHeaderKey(key)] = value
}

// ToHTTP converts this Headers instance into the equivalent http.Header
// instance. The return value is guaranteed to be non-nil.
func (h Headers) ToHTTP() http.Header {
	dest := make(http.Header, len(h))
	for k, v := range h {
		dest.Set(k, v)
	}
	return dest
}

// ToOpts wraps this Headers instance into a RequestOpts instance, so that it
// can be passed to Schwift's various request methods.
//
//	hdr := NewObjectHeaders()
//	hdr.ContentType().Set("image/png")
//	hdr.Metadata().Set("color", "blue")
//	obj.Upload(content, nil, hdr.ToOpts())
func (h Headers) ToOpts() *RequestOptions {
	return &RequestOptions{Headers: h}
}

func headersFromHTTP(src http.Header) Headers {
	h := make(Headers, len(src))
	for k, v := range src {
		if len(v) > 0 {
			h.Set(k, v[0])
		}
	}
	return h
}

////////////////////////////////////////////////////////////////////////////////
// specialized accessors on Headers subtypes that are not autogenerated

// IsDynamicLargeObject returns true if this set of headers belongs to a Dynamic
// Large Object (DLO).
func (h ObjectHeaders) IsDynamicLargeObject() bool {
	return h.Headers.Get("X-Object-Manifest") != ""
}

// IsStaticLargeObject returns true if this set of headers belongs to a Static
// Large Object (SLO).
func (h ObjectHeaders) IsStaticLargeObject() bool {
	return h.Headers.Get("X-Static-Large-Object") == "True"
}

// IsLargeObject returns true if this set of headers belongs to a large object
// (either an SLO or a DLO).
func (h ObjectHeaders) IsLargeObject() bool {
	return h.IsDynamicLargeObject() || h.IsStaticLargeObject()
}