-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add advanced filtering and test utilities for trap servers #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4bbe31d
feat: add monotonically increasing seq field to store entries
lambdalisue 4e5299b
feat(trap-smtp): add SHA-256 hash to email attachments
lambdalisue d11930e
feat: add regex filter support for entry queries
lambdalisue c8e65cf
feat: add /api/count endpoint for filtered entry counting
lambdalisue 39946a6
feat: add /api/await endpoint for blocking entry polling
lambdalisue ddaac9c
chore(trap-smtp): remove accidentally committed binary
lambdalisue 4e02950
fix: rename module paths from state-* to trap-* to match directory names
lambdalisue 5db83b0
fix(trap-smtp): use descriptive error message in count handler marsha…
lambdalisue b0ab29f
test: add input validation tests for /api/await endpoint
lambdalisue 22b47cc
fix: marshal JSON before writing headers in await handlers
lambdalisue 4ddd95e
fix(trap-webhook): handle w.Write error in count handler
lambdalisue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/probitas-test/state-servers/trap-smtp/store" | ||
| ) | ||
|
|
||
| // AwaitHandler blocks until the specified number of entries match the filter, | ||
| // or the timeout is reached. Returns matched entries on success, 408 on timeout. | ||
| // | ||
| // Query parameters: | ||
| // - count: minimum number of matching entries to wait for (default: 1) | ||
| // - timeout: maximum wait duration, e.g. "5s", "500ms" (default: 10s) | ||
| // - All email filter parameters (from, to, subject, body, etc.) | ||
| func AwaitHandler(w http.ResponseWriter, r *http.Request) { | ||
| filter, err := parseAwaitFilter(r) | ||
| if err != nil { | ||
| http.Error(w, err.Error(), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| count := 1 | ||
| if c := r.URL.Query().Get("count"); c != "" { | ||
| n, err := strconv.Atoi(c) | ||
| if err != nil || n <= 0 { | ||
| http.Error(w, "invalid 'count' parameter: must be a positive integer", http.StatusBadRequest) | ||
| return | ||
| } | ||
| count = n | ||
| } | ||
|
|
||
| timeout := 10 * time.Second | ||
| if t := r.URL.Query().Get("timeout"); t != "" { | ||
| d, err := time.ParseDuration(t) | ||
| if err != nil || d <= 0 { | ||
| http.Error(w, "invalid 'timeout' parameter: must be a positive duration (e.g. '5s', '500ms')", http.StatusBadRequest) | ||
| return | ||
| } | ||
| timeout = d | ||
| } | ||
|
|
||
| // Subscribe before checking existing entries to avoid missing entries | ||
| // added between the check and subscribe. | ||
| ch := emailStore.Subscribe() | ||
| defer emailStore.Unsubscribe(ch) | ||
|
|
||
| // Check existing entries | ||
| entries := emailStore.ListWithFilter(filter) | ||
| if len(entries) >= count { | ||
| resp, err := json.Marshal(entries) | ||
| if err != nil { | ||
| http.Error(w, "Failed to encode entries", http.StatusInternalServerError) | ||
| return | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if _, err := w.Write(resp); err != nil { | ||
| // Unable to write response; nothing more we can do here. | ||
| return | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // Wait for new entries | ||
| timer := time.NewTimer(timeout) | ||
| defer timer.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case _, ok := <-ch: | ||
| if !ok { | ||
| http.Error(w, "Store closed", http.StatusInternalServerError) | ||
| return | ||
| } | ||
| entries = emailStore.ListWithFilter(filter) | ||
| if len(entries) >= count { | ||
| resp, err := json.Marshal(entries) | ||
| if err != nil { | ||
| http.Error(w, "Failed to encode entries", http.StatusInternalServerError) | ||
| return | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if _, err := w.Write(resp); err != nil { | ||
| // Unable to write response; nothing more we can do here. | ||
| return | ||
| } | ||
| return | ||
| } | ||
|
lambdalisue marked this conversation as resolved.
|
||
| case <-timer.C: | ||
| resp, err := json.Marshal(map[string]interface{}{ | ||
| "error": "timeout", | ||
| "matched": len(emailStore.ListWithFilter(filter)), | ||
| "expected": count, | ||
| }) | ||
| if err != nil { | ||
| http.Error(w, "Failed to encode timeout response", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
lambdalisue marked this conversation as resolved.
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(http.StatusRequestTimeout) | ||
| _, _ = w.Write(resp) | ||
| return | ||
| case <-r.Context().Done(): | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // parseAwaitFilter extracts filter parameters without pagination (limit/offset). | ||
| func parseAwaitFilter(r *http.Request) (*store.EmailFilter, error) { | ||
| filter, err := parseEmailFilter(r) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| // Clear pagination fields (not supported by await) | ||
| filter.Limit = 0 | ||
| filter.Offset = 0 | ||
| return filter, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.