From 12230a96de179655036fa214320dd6810a17cac8 Mon Sep 17 00:00:00 2001 From: Lars Bahner Date: Thu, 9 May 2024 18:53:02 +0200 Subject: [PATCH] Add content_type parsing --- msg/errors.go | 3 +-- msg/message.go | 6 ++--- msg/mime.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ msg/reply.go | 2 +- 4 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 msg/mime.go diff --git a/msg/errors.go b/msg/errors.go index 929a9b3..3975265 100644 --- a/msg/errors.go +++ b/msg/errors.go @@ -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") diff --git a/msg/message.go b/msg/message.go index f211e92..5fdda31 100644 --- a/msg/message.go +++ b/msg/message.go @@ -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. diff --git a/msg/mime.go b/msg/mime.go new file mode 100644 index 0000000..a09b634 --- /dev/null +++ b/msg/mime.go @@ -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 +} diff --git a/msg/reply.go b/msg/reply.go index e62ce8c..ea2e5d3 100644 --- a/msg/reply.go +++ b/msg/reply.go @@ -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{