From e50779664d8fd662fc38b17bfcd666c888f7d82c Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Fri, 14 Jan 2022 12:13:14 -0500 Subject: [PATCH] Remove peaking, addresses #93 --- server/server.go | 25 +++++++++++----- server/server_test.go | 12 ++++---- server/util.go | 69 ------------------------------------------- server/util_test.go | 19 ------------ 4 files changed, 24 insertions(+), 101 deletions(-) delete mode 100644 server/util.go delete mode 100644 server/util_test.go diff --git a/server/server.go b/server/server.go index f0de4b99..f0250d96 100644 --- a/server/server.go +++ b/server/server.go @@ -18,7 +18,9 @@ import ( "net" "net/http" "net/http/httptest" + "net/url" "os" + "path" "path/filepath" "regexp" "strconv" @@ -459,9 +461,6 @@ func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request, v *visito if err != nil { return err } - if err := maybePeakAttachmentURL(m); err != nil { - return err - } if err := s.handlePublishBody(r, v, m, body); err != nil { return err } @@ -507,19 +506,31 @@ func (s *Server) parsePublishParams(r *http.Request, v *visitor, m *message) (ca firebase = readParam(r, "x-firebase", "firebase") != "no" m.Title = readParam(r, "x-title", "title", "t") m.Click = readParam(r, "x-click", "click") - attach := readParam(r, "x-attach", "attach", "a") filename := readParam(r, "x-filename", "filename", "file", "f") + attach := readParam(r, "x-attach", "attach", "a") if attach != "" || filename != "" { m.Attachment = &attachment{} } + if filename != "" { + m.Attachment.Name = filename + } if attach != "" { if !attachURLRegex.MatchString(attach) { return false, false, "", errHTTPBadRequestAttachmentURLInvalid } m.Attachment.URL = attach - } - if filename != "" { - m.Attachment.Name = filename + if m.Attachment.Name == "" { + u, err := url.Parse(m.Attachment.URL) + if err == nil { + m.Attachment.Name = path.Base(u.Path) + if m.Attachment.Name == "." || m.Attachment.Name == "/" { + m.Attachment.Name = "" + } + } + } + if m.Attachment.Name == "" { + m.Attachment.Name = "attachment" + } } email = readParam(r, "x-email", "x-e-mail", "email", "e-mail", "mail", "e") if email != "" { diff --git a/server/server_test.go b/server/server_test.go index 4867e4a1..7043a7cd 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -743,10 +743,10 @@ func TestServer_PublishAttachmentExternalWithoutFilename(t *testing.T) { msg := toMessage(t, response.Body.String()) require.Equal(t, "You received a file: Pink_flower.jpg", msg.Message) require.Equal(t, "Pink_flower.jpg", msg.Attachment.Name) - require.Equal(t, "image/jpeg", msg.Attachment.Type) - require.Equal(t, int64(190173), msg.Attachment.Size) - require.Equal(t, int64(0), msg.Attachment.Expires) require.Equal(t, "https://upload.wikimedia.org/wikipedia/commons/f/fd/Pink_flower.jpg", msg.Attachment.URL) + require.Equal(t, "", msg.Attachment.Type) + require.Equal(t, int64(0), msg.Attachment.Size) + require.Equal(t, int64(0), msg.Attachment.Expires) require.Equal(t, "", msg.Attachment.Owner) // Slightly unrelated cross-test: make sure we don't add an owner for external attachments @@ -764,10 +764,10 @@ func TestServer_PublishAttachmentExternalWithFilename(t *testing.T) { msg := toMessage(t, response.Body.String()) require.Equal(t, "This is a custom message", msg.Message) require.Equal(t, "some file.jpg", msg.Attachment.Name) - require.Equal(t, "image/jpeg", msg.Attachment.Type) - require.Equal(t, int64(190173), msg.Attachment.Size) - require.Equal(t, int64(0), msg.Attachment.Expires) require.Equal(t, "https://upload.wikimedia.org/wikipedia/commons/f/fd/Pink_flower.jpg", msg.Attachment.URL) + require.Equal(t, "", msg.Attachment.Type) + require.Equal(t, int64(0), msg.Attachment.Size) + require.Equal(t, int64(0), msg.Attachment.Expires) require.Equal(t, "", msg.Attachment.Owner) } diff --git a/server/util.go b/server/util.go deleted file mode 100644 index d36e3976..00000000 --- a/server/util.go +++ /dev/null @@ -1,69 +0,0 @@ -package server - -import ( - "fmt" - "heckel.io/ntfy/util" - "io" - "net/http" - "net/url" - "path" - "strconv" - "time" -) - -const ( - peakAttachmentTimeout = 2500 * time.Millisecond - peakAttachmentReadBytes = 128 -) - -func maybePeakAttachmentURL(m *message) error { - return maybePeakAttachmentURLInternal(m, peakAttachmentTimeout) -} - -func maybePeakAttachmentURLInternal(m *message, timeout time.Duration) error { - if m.Attachment == nil || m.Attachment.URL == "" { - return nil - } - client := http.Client{ - Timeout: timeout, - Transport: &http.Transport{ - DisableCompression: true, // Disable "Accept-Encoding: gzip", otherwise we won't get the Content-Length - Proxy: http.ProxyFromEnvironment, - }, - } - req, err := http.NewRequest(http.MethodGet, m.Attachment.URL, nil) - if err != nil { - return err - } - req.Header.Set("User-Agent", "ntfy") - resp, err := client.Do(req) - if err != nil { - return errHTTPBadRequestAttachmentURLPeakGeneral - } - defer resp.Body.Close() - if resp.StatusCode < 200 || resp.StatusCode > 299 { - return errHTTPBadRequestAttachmentURLPeakNon2xx - } - if size, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64); err == nil { - m.Attachment.Size = size - } - buf := make([]byte, peakAttachmentReadBytes) - io.ReadFull(resp.Body, buf) // Best effort: We don't care about the error - mimeType, ext := util.DetectContentType(buf, m.Attachment.URL) - m.Attachment.Type = resp.Header.Get("Content-Type") - if m.Attachment.Type == "" { - m.Attachment.Type = mimeType - } - if m.Attachment.Name == "" { - u, err := url.Parse(m.Attachment.URL) - if err != nil { - m.Attachment.Name = fmt.Sprintf("attachment%s", ext) - } else { - m.Attachment.Name = path.Base(u.Path) - if m.Attachment.Name == "." || m.Attachment.Name == "/" { - m.Attachment.Name = fmt.Sprintf("attachment%s", ext) - } - } - } - return nil -} diff --git a/server/util_test.go b/server/util_test.go deleted file mode 100644 index a20cfb64..00000000 --- a/server/util_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package server - -import ( - "github.com/stretchr/testify/require" - "testing" -) - -func TestMaybePeakAttachmentURL_Success(t *testing.T) { - m := &message{ - Attachment: &attachment{ - URL: "https://ntfy.sh/static/img/ntfy.png", - }, - } - require.Nil(t, maybePeakAttachmentURL(m)) - require.Equal(t, "ntfy.png", m.Attachment.Name) - require.Equal(t, int64(3627), m.Attachment.Size) - require.Equal(t, "image/png", m.Attachment.Type) - require.Equal(t, int64(0), m.Attachment.Expires) -}