Skip to content

Commit

Permalink
SMTP: Unifying output of multipart metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jul 2, 2024
1 parent 696cbef commit deee3f5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2713,7 +2713,30 @@ the corresponding index is made available as JSON:
},
"idx": 1,
"isMultipart": true,
"receivedAt": "2024-07-02T11:23:31.212523916+02:00",
"multiparts": [
{
"bodySize": 15,
"headers": {
"Content-Type": [
"text/plain; charset=utf-8"
]
},
"idx": 0,
"isMultipart": false
},
{
"bodySize": 39,
"headers": {
"Content-Type": [
"text/html; charset=utf-8"
]
},
"idx": 1,
"isMultipart": false
}
],
"multipartsCount": 2,
"receivedAt": "2024-07-02T11:44:32.767921301+02:00",
"smtpFrom": "[email protected]",
"smtpRcptTo": [
"[email protected]"
Expand Down Expand Up @@ -2745,7 +2768,6 @@ following schema:

```json
{
"count": 2,
"multiparts": [
{
"bodySize": 15,
Expand All @@ -2754,6 +2776,7 @@ following schema:
"text/plain; charset=utf-8"
]
},
"idx": 0,
"isMultipart": false
},
{
Expand All @@ -2763,9 +2786,11 @@ following schema:
"text/html; charset=utf-8"
]
},
"idx": 1,
"isMultipart": false
}
]
],
"multipartsCount": 2
}
```

Expand Down
59 changes: 37 additions & 22 deletions internal/smtp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,33 +223,13 @@ func (h *smtpHTTPHandler) handleMultipartIndex(w http.ResponseWriter, r *http.Re
multiparts = SearchByHeader(multiparts, headerSearchRgx)
}

multipartsOut := make([]any, 0)

for _, part := range multiparts {
multipartsOut = append(multipartsOut, buildContentMeta(part.Content()))
}

out := make(map[string]any)
out["count"] = len(multiparts)
out["multiparts"] = multipartsOut

handlerutil.RespondWithJSON(w, http.StatusOK, out)
handlerutil.RespondWithJSON(w, http.StatusOK, buildMultipartIndex(multiparts))
}

func (h *smtpHTTPHandler) handleMultipartMeta(
w http.ResponseWriter, r *http.Request, part *ReceivedPart,
) {
out := map[string]any{
"idx": part.Index(),
}

contentMeta := buildContentMeta(part.Content())

for k, v := range contentMeta {
out[k] = v
}

handlerutil.RespondWithJSON(w, http.StatusOK, out)
handlerutil.RespondWithJSON(w, http.StatusOK, buildMultipartMeta(part))
}

func buildContentMeta(c *ReceivedContent) map[string]any {
Expand All @@ -264,6 +244,13 @@ func buildContentMeta(c *ReceivedContent) map[string]any {
}
out["headers"] = headers

if c.IsMultipart() {
multipartIndex := buildMultipartIndex(c.Multiparts())
for k, v := range multipartIndex {
out[k] = v
}
}

return out
}

Expand Down Expand Up @@ -296,6 +283,34 @@ func buildMessageBasicMeta(msg *ReceivedMessage) map[string]any {
return out
}

func buildMultipartIndex(parts []*ReceivedPart) map[string]any {
multipartsOut := make([]any, len(parts))

for i, part := range parts {
multipartsOut[i] = buildMultipartMeta(part)
}

out := make(map[string]any)
out["multipartsCount"] = len(parts)
out["multiparts"] = multipartsOut

return out
}

func buildMultipartMeta(part *ReceivedPart) map[string]any {
out := map[string]any{
"idx": part.Index(),
}

contentMeta := buildContentMeta(part.Content())

for k, v := range contentMeta {
out[k] = v
}

return out
}

// extractSearchRegex tries to extract a regular expression from the referenced
// query parameter. If no query parameter is given and otherwise no error has
// occurred, this function returns (nil, nil).
Expand Down

0 comments on commit deee3f5

Please sign in to comment.