forked from mtusi2026/mtusi2026lab5
fix: validate message request fields
This commit is contained in:
@@ -20,6 +20,26 @@ const maxRequestBytes = 1 << 20
|
||||
|
||||
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
type createMessageRequest struct {
|
||||
Text json.RawMessage `json:"text"`
|
||||
AttachmentIDs json.RawMessage `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
func (r createMessageRequest) values() (string, []string, bool) {
|
||||
var text *string
|
||||
if len(r.Text) == 0 || json.Unmarshal(r.Text, &text) != nil || text == nil {
|
||||
return "", nil, false
|
||||
}
|
||||
if len(r.AttachmentIDs) == 0 {
|
||||
return *text, nil, true
|
||||
}
|
||||
var attachmentIDs *[]string
|
||||
if json.Unmarshal(r.AttachmentIDs, &attachmentIDs) != nil || attachmentIDs == nil {
|
||||
return "", nil, false
|
||||
}
|
||||
return *text, *attachmentIDs, true
|
||||
}
|
||||
|
||||
func readJSON(w http.ResponseWriter, r *http.Request, target any) *apiError {
|
||||
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil || mediaType != "application/json" || (params["charset"] != "" && !strings.EqualFold(params["charset"], "utf-8")) {
|
||||
|
||||
Reference in New Issue
Block a user