Skip to content
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

chore(utils): refactor & enhance custom header handling #116

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions teler.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,9 @@ func (t *Teler) postAnalyze(w http.ResponseWriter, r *http.Request, k threat.Thr
// Get the error message & convert to string as a message
msg := err.Error()

// Set custom headers
setCustomHeaders(w, msg, k)
// Set custom headers ("X-Teler-Msg" and "X-Teler-Threat")
setCustomHeader(w, xTelerMsg, msg)
setCustomHeader(w, xTelerThreat, k.String())

// Send the logs
t.sendLogs(r, k, id, msg)
Expand Down
12 changes: 5 additions & 7 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,18 @@ func normalizeRawStringReader(raw string) *strings.Reader {
return strings.NewReader(builder.String())
}

// setCustomHeader such as message and threat category to the header response
func setCustomHeaders(w http.ResponseWriter, msg string, cat threat.Threat) {
// Set the "X-Teler-Msg" and "X-Teler-Threat" header in the response
w.Header().Set(xTelerMsg, msg)
w.Header().Set(xTelerThreat, cat.String())
// setCustomHeader to set custom header to the response writer
func setCustomHeader(w http.ResponseWriter, key, value string) {
w.Header().Set(key, value)
}

// setReqIdHeader to set teler request ID header response
// setReqIdHeader to set teler request ID header response and return it
func setReqIdHeader(w http.ResponseWriter) string {
// Generate a unique ID using the gouid package.
id := gouid.Bytes(10)

// Set the "X-Teler-Req-Id" header in the response with the unique ID.
w.Header().Set(xTelerReqId, id.String())
setCustomHeader(w, xTelerReqId, id.String())

return id.String()
}
Expand Down
Loading