|
|
@@ -1044,8 +1044,11 @@ func (s *Server) parsePublishParams(r *http.Request, m *message) (cache bool, fi
|
|
|
// Body must be attachment, because we passed a filename
|
|
|
// 5. curl -T file.txt ntfy.sh/mytopic
|
|
|
// If file.txt is <= 4096 (message limit) and valid UTF-8, treat it as a message
|
|
|
-// 6. curl -T file.txt ntfy.sh/mytopic
|
|
|
-// If file.txt is > message limit, treat it as an attachment
|
|
|
+// 6. curl -H "Template: yes" -T file.txt ntfy.sh/mytopic
|
|
|
+// If file.txt is < 4096*2 (message limit*2) and a template is used, try parsing under the assumption
|
|
|
+// that the message generated by the template will be less than 4096
|
|
|
+// 7. curl -T file.txt ntfy.sh/mytopic
|
|
|
+// If file.txt is > message limit or template && file.txt > message limit*2, treat it as an attachment
|
|
|
func (s *Server) handlePublishBody(r *http.Request, v *visitor, m *message, body *util.PeekedReadCloser, template bool, unifiedpush bool) error {
|
|
|
if m.Event == pollRequestEvent { // Case 1
|
|
|
return s.handleBodyDiscard(body)
|
|
|
@@ -1057,8 +1060,16 @@ func (s *Server) handlePublishBody(r *http.Request, v *visitor, m *message, body
|
|
|
return s.handleBodyAsAttachment(r, v, m, body) // Case 4
|
|
|
} else if !body.LimitReached && utf8.Valid(body.PeekedBytes) {
|
|
|
return s.handleBodyAsTextMessage(m, body, template) // Case 5
|
|
|
+ } else if template {
|
|
|
+ templateBody, err := util.Peek(body, s.config.MessageSizeLimit*2)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if !templateBody.LimitReached {
|
|
|
+ return s.handleBodyAsTextMessage(m, templateBody, template) // Case 6
|
|
|
+ }
|
|
|
}
|
|
|
- return s.handleBodyAsAttachment(r, v, m, body) // Case 6
|
|
|
+ return s.handleBodyAsAttachment(r, v, m, body) // Case 7
|
|
|
}
|
|
|
|
|
|
func (s *Server) handleBodyDiscard(body *util.PeekedReadCloser) error {
|
|
|
@@ -1104,6 +1115,10 @@ func (s *Server) handleBodyAsTextMessage(m *message, body *util.PeekedReadCloser
|
|
|
if m.Attachment != nil && m.Attachment.Name != "" && m.Message == "" {
|
|
|
m.Message = fmt.Sprintf(defaultAttachmentMessage, m.Attachment.Name)
|
|
|
}
|
|
|
+ // Ensure message is less than message limit after templating
|
|
|
+ if len(m.Message) > s.config.MessageSizeLimit {
|
|
|
+ return errHTTPBadRequestTemplatedMessageTooLarge
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|
|
|
|