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 12230a9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
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
6 changes: 3 additions & 3 deletions msg/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

const (
PREFIX = "/ma/"
MESSAGE = PREFIX + ma.VERSION
MESSAGE_CONTENT_TYPE = "application/x.ma"
PREFIX = "/ma/"
MESSAGE = PREFIX + ma.VERSION
CONTENT_TYPE = "application/x.ma"
)

// This struct mimicks the Message format, but it's *not* Message.
Expand Down
66 changes: 66 additions & 0 deletions msg/mime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package msg

import (
"fmt"
"mime"
"strings"
)

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

// 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
}

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

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

elements := strings.Split(contentType, "+")
if len(elements) == 1 {
return contentType, fmt.Errorf("%s: %w", contentType, ErrMissingSerialiser)
}

return elements[1], nil
}

// 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 != CONTENT_TYPE {
return contentType, params, fmt.Errorf("%s: %w", contentType, ErrInvalidContentType)
}

return contentType, params, nil
}
2 changes: 1 addition & 1 deletion msg/reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (m *Message) Reply(ctx context.Context, replyBytes []byte, privKey ed25519.
return err
}

mimeType := MESSAGE_CONTENT_TYPE + "+" + REPLY_SERIALIZATION
mimeType := CONTENT_TYPE + "+" + REPLY_SERIALIZATION
contentType := mime.FormatMediaType(mimeType, REPLY_CONTENT_TYPE_PARAMS)

reply := &Message{
Expand Down

0 comments on commit 12230a9

Please sign in to comment.