Documentation ¶
Index ¶
- Constants
- type AcceptHeader
- func (ah *AcceptHeader) Add(mh MimeHeader)
- func (ah AcceptHeader) Len() int
- func (ah AcceptHeader) Less(i, j int) bool
- func (ah AcceptHeader) Match(mtype string) bool
- func (ah AcceptHeader) Negotiate(ctypes []string, dtype string) (accept MimeHeader, mimeType string, matched bool)
- func (ah *AcceptHeader) Set(mhs []MimeHeader)
- func (ah *AcceptHeader) Swap(i, j int)
- type MimeHeader
- type MimeParseErr
- type MimeType
- type MimeTypePartsErr
- type MimeTypeWildcardErr
Examples ¶
Constants ¶
const ( MimeParseErrMsg = "error in a parse media type" MimeTypePartsErrMsg = "wrong number of mime type parts" MimeTypeWildcardErrMsg = "mimetype cannot be as */plain" )
Error messages.
const DefaultQuality float32 = 1.0
const MimeAny = "*"
MimeAny represented as the asterisk.
const MimeParts = 2
MimeParts MUST contain two parts <MIME_type>/<MIME_subtype>.
const MimeSeparator = "/"
MimeSeparator MUST be separated forward slash.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AcceptHeader ¶
type AcceptHeader struct {
MHeaders []MimeHeader
}
func NewAcceptHeader ¶
func NewAcceptHeader(mheaders []MimeHeader) AcceptHeader
func NewAcceptHeaderPlain ¶
func NewAcceptHeaderPlain(mheaders []MimeHeader) AcceptHeader
func ParseAcceptHeader ¶
func ParseAcceptHeader(header string) AcceptHeader
Example ¶
package main import ( "fmt" "github.com/aohorodnyk/mimeheader" ) func main() { header := "image/*; q=0.9; s=4, application/json; q=0.9; b=3;, text/plain,image/png;q=0.9, image/jpeg,image/svg;q=0.8" ah := mimeheader.ParseAcceptHeader(header) fmt.Println(ah.Negotiate([]string{"application/xml", "image/tiff"}, "text/javascript")) fmt.Println(ah.Negotiate([]string{"application/xml", "image/png"}, "text/javascript")) fmt.Println(ah.Negotiate([]string{"application/xml", "image/svg"}, "text/javascript")) fmt.Println(ah.Negotiate([]string{"text/dart", "application/dart"}, "text/javascript")) fmt.Println(ah.Match("image/svg")) fmt.Println(ah.Match("text/javascript")) }
Output: image/* image/tiff true image/png image/png true image/* image/svg true text/javascript false true false
func (*AcceptHeader) Add ¶
func (ah *AcceptHeader) Add(mh MimeHeader)
Add mime header to accept header. MimeHeader will be validated and added ONLY if valid. AcceptHeader will be sorted. For performance reasons better to use Set, instead of Add.
Example ¶
package main import ( "fmt" "github.com/aohorodnyk/mimeheader" ) func main() { ah := mimeheader.ParseAcceptHeader("image/png") fmt.Println(ah.Match("application/json")) ah.Add(mimeheader.MimeHeader{MimeType: mimeheader.MimeType{Type: "application", Subtype: "*"}}) fmt.Println(ah.Match("application/json")) }
Output: false true
func (AcceptHeader) Len ¶
func (ah AcceptHeader) Len() int
Len function for sort.Interface interface.
func (AcceptHeader) Less ¶
func (ah AcceptHeader) Less(i, j int) bool
Less function for sort.Interface interface.
func (AcceptHeader) Match ¶
func (ah AcceptHeader) Match(mtype string) bool
Match is the same function as AcceptHeader.Negotiate. It implements simplified interface to match only one type and return only matched or not information.
Example ¶
package main import ( "fmt" "github.com/aohorodnyk/mimeheader" ) func main() { ah := mimeheader.ParseAcceptHeader("image/png") fmt.Println(ah.Match("application/json")) ah.Add(mimeheader.MimeHeader{MimeType: mimeheader.MimeType{Type: "application", Subtype: "*"}}) fmt.Println(ah.Match("application/json")) }
Output: false true
func (AcceptHeader) Negotiate ¶
func (ah AcceptHeader) Negotiate(ctypes []string, dtype string) (accept MimeHeader, mimeType string, matched bool)
Negotiate return appropriate type fot current accept list from supported (common) mime types. First parameter returns matched value from accept header. Second parameter returns matched common type. Third parameter returns matched common type or default type applied.
Example ¶
package main import ( "fmt" "github.com/aohorodnyk/mimeheader" ) func main() { header := "image/*; q=0.9; s=4, application/json; q=0.9; b=3;, text/plain,image/png;q=0.9, image/jpeg,image/svg;q=0.8" ah := mimeheader.ParseAcceptHeader(header) fmt.Println(ah.Negotiate([]string{"application/xml", "image/tiff"}, "text/javascript")) fmt.Println(ah.Negotiate([]string{"application/xml", "image/png"}, "text/javascript")) fmt.Println(ah.Negotiate([]string{"application/xml", "image/svg"}, "text/javascript")) fmt.Println(ah.Negotiate([]string{"text/dart", "application/dart"}, "text/javascript")) }
Output: image/* image/tiff true image/png image/png true image/* image/svg true text/javascript false
func (*AcceptHeader) Set ¶
func (ah *AcceptHeader) Set(mhs []MimeHeader)
Set all valid headers to AcceprHeader (override old ones). Sorting will be applied.
Example ¶
package main import ( "fmt" "github.com/aohorodnyk/mimeheader" ) func main() { ah := mimeheader.ParseAcceptHeader("image/png") fmt.Println(ah.Match("application/json")) ah.Add(mimeheader.MimeHeader{MimeType: mimeheader.MimeType{Type: "application", Subtype: "*"}}) fmt.Println(ah.Match("application/json")) }
Output:
func (*AcceptHeader) Swap ¶
func (ah *AcceptHeader) Swap(i, j int)
Swap function for sort.Interface interface.
type MimeHeader ¶
type MimeParseErr ¶ added in v0.0.2
func (MimeParseErr) Error ¶ added in v0.0.2
func (e MimeParseErr) Error() string
func (MimeParseErr) Unwrap ¶ added in v0.0.2
func (e MimeParseErr) Unwrap() error
type MimeType ¶
MimeType structure for media type (mime type).
func ParseMediaType ¶
ParseMediaType parses media type to MimeType structure.
Example (Exact) ¶
package main import ( "fmt" "github.com/aohorodnyk/mimeheader" ) func main() { // Parse media type mediaType := "application/json; q=1; param=test;" mimeType, err := mimeheader.ParseMediaType(mediaType) if err != nil { panic(err) } // Print string without params. fmt.Println(mimeType.String()) // Print string with params. fmt.Println(mimeType.StringWithParams()) // Parse input and match it. fmt.Println(mimeType.MatchText("application/json; param=test")) fmt.Println(mimeType.MatchText("application/xml; q=1")) fmt.Println(mimeType.MatchText("text/plain")) // Parse mime type. tmtype, err := mimeheader.ParseMediaType("application/json;q=0.3") if err != nil { panic(err) } // Match mime types. fmt.Println(mimeType.Match(tmtype)) }
Output: application/json application/json; param=test; q=1 true false false true
Example (Wildcard) ¶
package main import ( "fmt" "github.com/aohorodnyk/mimeheader" ) func main() { // Parse media type mediaType := "application/*; q=1; param=test;" mimeType, err := mimeheader.ParseMediaType(mediaType) if err != nil { panic(err) } // Print string without params. fmt.Println(mimeType.String()) // Print string with params. fmt.Println(mimeType.StringWithParams()) // Parse input and match it. fmt.Println(mimeType.MatchText("application/json; param=test")) fmt.Println(mimeType.MatchText("application/xml; q=1")) fmt.Println(mimeType.MatchText("text/plain")) // Parse mime type. tmtype, err := mimeheader.ParseMediaType("application/json;q=0.3") if err != nil { panic(err) } // Match mime types. fmt.Println(mimeType.Match(tmtype)) }
Output: application/* application/*; param=test; q=1 true true false true
func (MimeType) Match ¶
Match matches current structure with possible wildcards. MimeType structure (current) can be wildcard or specific type, like "text/*", "*/*", "text/plain".
Example ¶
package main import ( "fmt" "github.com/aohorodnyk/mimeheader" ) func main() { // Parse media type mediaType := "application/xml; q=1; param=test;" mimeType, err := mimeheader.ParseMediaType(mediaType) if err != nil { panic(err) } // Parse input and match it. fmt.Println(mimeType.Match(mimeheader.MimeType{Type: "application", Subtype: "xml"})) fmt.Println(mimeType.Match(mimeheader.MimeType{Type: "application", Subtype: "json"})) }
Output: true false
func (MimeType) MatchText ¶
MatchText matches current structure with possible wildcards. Target MUST be specific type, like "application/json", "text/plain" MimeType structure (current) can be wildcard or specific type, like "text/*", "*/*", "text/plain".
Example ¶
package main import ( "fmt" "github.com/aohorodnyk/mimeheader" ) func main() { // Parse media type mediaType := "application/json; q=1; param=test;" mimeType, err := mimeheader.ParseMediaType(mediaType) if err != nil { panic(err) } // Parse input and match it. fmt.Println(mimeType.MatchText("application/json; param=test")) fmt.Println(mimeType.MatchText("application/xml; q=1")) fmt.Println(mimeType.MatchText("text/plain")) }
Output: true false false
func (MimeType) StringWithParams ¶
StringWithParams builds mime type from type and subtype with params.
type MimeTypePartsErr ¶ added in v0.0.2
type MimeTypePartsErr struct {
Msg string
}
func (MimeTypePartsErr) Error ¶ added in v0.0.2
func (e MimeTypePartsErr) Error() string
type MimeTypeWildcardErr ¶ added in v0.0.2
type MimeTypeWildcardErr struct {
Msg string
}
func (MimeTypeWildcardErr) Error ¶ added in v0.0.2
func (e MimeTypeWildcardErr) Error() string