Skip to content

Commit

Permalink
Add content_type parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed May 9, 2024
1 parent 8d82934 commit 9349c83
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
48 changes: 48 additions & 0 deletions msg/content_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package msg

import (
"fmt"
"mime"
)

var (
ErrInvalidContentType = fmt.Errorf("invalid Message ContentType")
ErrMissingContentType = fmt.Errorf("empty ContentType")
)

// Returns the message type as defined in the contenttype, not the message as such
func (m *Message) MessageType() (string, error) {

_, params, err := m.ParseContentType()
if err != nil {
return "", ErrInvalidContentType
}

for k, v := range params {
if k == "type" {
return v, nil
}
}

return "", ErrInvalidContentType
}

// A parser which add a little specific functionality to the standard mime.ParseMediaType
// Use this instead.
func (m *Message) ParseContentType() (string, map[string]string, error) {

contentType, params, err := mime.ParseMediaType(m.ContentType)
if err != nil {
return contentType, params, fmt.Errorf("msg_parse_content_type: %w", err)
}

if contentType == "" {
return contentType, params, ErrMissingContentType
}

if contentType != MESSAGE_CONTENT_TYPE {
return contentType, params, fmt.Errorf("%s: %w", contentType, ErrInvalidContentType)
}

return contentType, params, nil
}
3 changes: 1 addition & 2 deletions msg/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ var (
ErrFetchDoc = fmt.Errorf("failed to fetch entity document")
ErrInvalidMessageType = fmt.Errorf("invalid Message type")
ErrInvalidRecipient = fmt.Errorf("invalid recipient")
ErrMissingContentType = fmt.Errorf("empty ContentType")
ErrMissingContent = fmt.Errorf("empty ContentType")
ErrMissingContent = fmt.Errorf("empty Content")
ErrMissingFrom = fmt.Errorf("mmissing From sender")
ErrMissinSignature = fmt.Errorf("mmissing signature")
ErrNilMessage = fmt.Errorf("nil Message provided")
Expand Down

0 comments on commit 9349c83

Please sign in to comment.