-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshal.go
38 lines (34 loc) · 1.13 KB
/
marshal.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
// Copyright 2015 Luke Shumaker
package httpentity
import (
"io"
"locale"
"mime"
)
// If the Response has an entity, write it to the given output stream.
func (response Response) writeEntity(w io.Writer) locale.Error {
if response.encoder == nil {
return nil
}
return response.encoder.Write(w, locale.Spec(response.Headers.Get("Content-Language")))
}
// Read an entity from the input stream, using the given content type.
func (req *Request) readEntity(router *Router, reader io.Reader, contenttype string) *Response {
mimetype, params, uerr := mime.ParseMediaType(contenttype)
if uerr != nil {
res := router.responseBadRequest(locale.Errorf("Could not parse Content-Type: %v", locale.UntranslatedError(uerr)))
return &res
}
decoder, foundDecoder := router.Decoders[mimetype]
if !foundDecoder {
res := router.responseUnsupportedMediaType(locale.Errorf("Unsupported MIME type: " + mimetype))
return &res
}
entity, err := decoder(reader, params)
if err != nil {
res := router.responseUnsupportedMediaType(locale.Errorf("Error reading request body (%s): %v", mimetype, err))
return &res
}
req.Entity = entity
return nil
}