-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.go
More file actions
201 lines (168 loc) · 4.75 KB
/
content.go
File metadata and controls
201 lines (168 loc) · 4.75 KB
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
package rsvp
import (
"io"
"iter"
"net/http"
"path/filepath"
"slices"
"strings"
"github.com/Teajey/rsvp/internal/content"
"github.com/Teajey/rsvp/internal/dev"
)
const (
SupportedMediaTypePlaintext string = "text/plain"
SupportedMediaTypeHtml string = "text/html"
SupportedMediaTypeCsv string = "text/csv"
SupportedMediaTypeBytes string = "application/octet-stream"
SupportedMediaTypeJson string = "application/json"
SupportedMediaTypeXml string = "application/xml"
SupportedMediaTypeGob string = "application/vnd.golang.gob"
)
var mediaTypeToContentType = map[string]string{
// TODO: Why did I insist on specifying utf-8 here? There should be note. I think it might just be because it's inline with what net/http does
SupportedMediaTypePlaintext: "text/plain; charset=utf-8",
SupportedMediaTypeHtml: "text/html; charset=utf-8",
SupportedMediaTypeCsv: "text/csv; charset=utf-8",
SupportedMediaTypeBytes: "application/octet-stream",
SupportedMediaTypeJson: "application/json",
SupportedMediaTypeXml: "application/xml",
SupportedMediaTypeGob: "application/vnd.golang.gob",
}
var extToProposalMap = map[string]string{
"txt": SupportedMediaTypePlaintext,
"html": SupportedMediaTypeHtml,
"htm": SupportedMediaTypeHtml,
"csv": SupportedMediaTypeCsv,
"json": SupportedMediaTypeJson,
"xml": SupportedMediaTypeXml,
"bin": SupportedMediaTypeBytes,
"gob": SupportedMediaTypeGob,
}
var extendedMediaTypes []string = nil
type mediaTypeExtensionHandler = func(mediaType string, w io.Writer, res *Body) (bool, error)
var mediaTypeExtensionHandlers []mediaTypeExtensionHandler = nil
func (res *Body) determineSupported(cfg Config) []string {
supported := slices.Collect(res.MediaTypes(cfg))
dev.Log("supported %v", supported)
return supported
}
func determineExt(r *http.Request) string {
var ext string
if r.Method == http.MethodGet {
ext = strings.TrimPrefix(filepath.Ext(r.URL.Path), ".")
}
return ext
}
func (res *Body) determineMediaType(ext, accept string, supported []string) string {
mediaType := chooseMediaType(ext, supported, content.ParseAccept(accept))
dev.Log("mediaType %#v", mediaType)
return mediaType
}
func (res *Body) determineContentType(mediaType string, wh http.Header) {
contentType := mediaTypeToContentType[mediaType]
dev.Log("Setting content-type to %#v", contentType)
wh.Set("Content-Type", contentType)
}
// mediatype string m must be well-formed
func splitMediaType(m string) (string, string) {
sp := strings.SplitN(m, "/", 2)
return sp[0], sp[1]
}
// mediatype string b must be well-formed
func mediaTypesEqual(a string, b string) bool {
if strings.HasPrefix(b, "*/") {
return true
}
b1, b2 := splitMediaType(b)
s := a
s1, s2 := splitMediaType(s)
if b1 == s1 {
return b2 == "*" || b2 == s2
}
return false
}
func chooseMediaType(ext string, supported []string, accept iter.Seq[string]) string {
if ext != "" {
dev.Log("Checking extension: %#v", ext)
if a, ok := extToProposalMap[ext]; ok {
dev.Log("proposing %#v", a)
for _, s := range supported {
dev.Log("offering %#v", s)
if mediaTypesEqual(s, a) {
return s
}
}
}
return ""
}
dev.Log("Checking accept list")
for a := range accept {
dev.Log("proposing %#v", a)
for _, s := range supported {
dev.Log("offering %#v", s)
if mediaTypesEqual(s, a) {
return s
}
}
}
return ""
}
// MediaTypes returns the sequence of media types (e.g. text/plain) in the order that this [Body] will propose.
//
// The order generally follows this pattern:
// 1. Type-specific (Html wrapper, string, bytes)
// 2. Generic structured (JSON, XML)
// 3. Interface implementations (CSV)
// 4. Template-based (HTML template, text template)
// 5. Golang-native fallback (Gob)
func (res *Body) MediaTypes(cfg Config) iter.Seq[string] {
return func(yield func(string) bool) {
if res.predeterminedMediaType != "" {
dev.Log("Overriding media-types with %s", res.predeterminedMediaType)
yield(string(res.predeterminedMediaType))
return
}
switch res.Data.(type) {
case string:
if !yield(SupportedMediaTypePlaintext) {
return
}
case []byte:
if !yield(SupportedMediaTypeBytes) {
return
}
}
if !yield(SupportedMediaTypeJson) {
return
}
if !yield(SupportedMediaTypeXml) {
return
}
_, ok := res.Data.(Csv)
if ok {
if !yield(SupportedMediaTypeCsv) {
return
}
}
if res.TemplateName != "" {
if cfg.HtmlTemplate != nil {
if !yield(SupportedMediaTypeHtml) {
return
}
}
if cfg.TextTemplate != nil {
if !yield(SupportedMediaTypePlaintext) {
return
}
}
}
for _, mediaType := range extendedMediaTypes {
if !yield(mediaType) {
return
}
}
if !yield(SupportedMediaTypeGob) {
return
}
}
}