diff --git a/api/category.go b/api/category.go index 30b12f47..b7b5c028 100644 --- a/api/category.go +++ b/api/category.go @@ -83,7 +83,7 @@ func (c *Controller) GetCategories(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, categories) + json.OK(w, r, categories) } // RemoveCategory is the API handler to remove a category. diff --git a/api/entry.go b/api/entry.go index a6785fbb..b2a4d9dc 100644 --- a/api/entry.go +++ b/api/entry.go @@ -48,7 +48,7 @@ func (c *Controller) GetFeedEntry(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, entry) + json.OK(w, r, entry) } // GetEntry is the API handler to get a single entry. @@ -73,7 +73,7 @@ func (c *Controller) GetEntry(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, entry) + json.OK(w, r, entry) } // GetFeedEntries is the API handler to get all feed entries. @@ -132,7 +132,7 @@ func (c *Controller) GetFeedEntries(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, &entriesResponse{Total: count, Entries: entries}) + json.OK(w, r, &entriesResponse{Total: count, Entries: entries}) } // GetEntries is the API handler to fetch entries. @@ -184,7 +184,7 @@ func (c *Controller) GetEntries(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, &entriesResponse{Total: count, Entries: entries}) + json.OK(w, r, &entriesResponse{Total: count, Entries: entries}) } // SetEntryStatus is the API handler to change the status of entries. diff --git a/api/feed.go b/api/feed.go index b7c46ea1..b6b63578 100644 --- a/api/feed.go +++ b/api/feed.go @@ -146,7 +146,7 @@ func (c *Controller) GetFeeds(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, feeds) + json.OK(w, r, feeds) } // GetFeed is the API handler to get a feed. @@ -168,7 +168,7 @@ func (c *Controller) GetFeed(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, feed) + json.OK(w, r, feed) } // RemoveFeed is the API handler to remove a feed. diff --git a/api/icon.go b/api/icon.go index 0f5aa1bd..052cdbc3 100644 --- a/api/icon.go +++ b/api/icon.go @@ -37,7 +37,7 @@ func (c *Controller) FeedIcon(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, &feedIcon{ + json.OK(w, r, &feedIcon{ ID: icon.ID, MimeType: icon.MimeType, Data: icon.DataURL(), diff --git a/api/subscription.go b/api/subscription.go index 19adc9ef..a28c3b25 100644 --- a/api/subscription.go +++ b/api/subscription.go @@ -36,5 +36,5 @@ func (c *Controller) GetSubscriptions(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, subscriptions) + json.OK(w, r, subscriptions) } diff --git a/api/user.go b/api/user.go index 4e5fa828..b880ba30 100644 --- a/api/user.go +++ b/api/user.go @@ -22,7 +22,7 @@ func (c *Controller) CurrentUser(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, user) + json.OK(w, r, user) } // CreateUser is the API handler to create a new user. @@ -119,7 +119,7 @@ func (c *Controller) Users(w http.ResponseWriter, r *http.Request) { } users.UseTimezone(ctx.UserTimezone()) - json.OK(w, users) + json.OK(w, r, users) } // UserByID is the API handler to fetch the given user by the ID. @@ -148,7 +148,7 @@ func (c *Controller) UserByID(w http.ResponseWriter, r *http.Request) { } user.UseTimezone(ctx.UserTimezone()) - json.OK(w, user) + json.OK(w, r, user) } // UserByUsername is the API handler to fetch the given user by the username. @@ -171,7 +171,7 @@ func (c *Controller) UserByUsername(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, user) + json.OK(w, r, user) } // RemoveUser is the API handler to remove an existing user. diff --git a/fever/fever.go b/fever/fever.go index 9fb6bdbc..44e155a9 100644 --- a/fever/fever.go +++ b/fever/fever.go @@ -155,7 +155,7 @@ func (c *Controller) Handler(w http.ResponseWriter, r *http.Request) { case r.FormValue("mark") == "group": c.handleWriteGroups(w, r) default: - json.OK(w, newBaseResponse()) + json.OK(w, r, newBaseResponse()) } } @@ -203,7 +203,7 @@ func (c *Controller) handleGroups(w http.ResponseWriter, r *http.Request) { result.FeedsGroups = c.buildFeedGroups(feeds) result.SetCommonValues() - json.OK(w, result) + json.OK(w, r, result) } /* @@ -262,7 +262,7 @@ func (c *Controller) handleFeeds(w http.ResponseWriter, r *http.Request) { result.FeedsGroups = c.buildFeedGroups(feeds) result.SetCommonValues() - json.OK(w, result) + json.OK(w, r, result) } /* @@ -304,7 +304,7 @@ func (c *Controller) handleFavicons(w http.ResponseWriter, r *http.Request) { } result.SetCommonValues() - json.OK(w, result) + json.OK(w, r, result) } /* @@ -414,7 +414,7 @@ func (c *Controller) handleItems(w http.ResponseWriter, r *http.Request) { } result.SetCommonValues() - json.OK(w, result) + json.OK(w, r, result) } /* @@ -445,7 +445,7 @@ func (c *Controller) handleUnreadItems(w http.ResponseWriter, r *http.Request) { var result unreadResponse result.ItemIDs = strings.Join(itemIDs, ",") result.SetCommonValues() - json.OK(w, result) + json.OK(w, r, result) } /* @@ -477,7 +477,7 @@ func (c *Controller) handleSavedItems(w http.ResponseWriter, r *http.Request) { result := &savedResponse{ItemIDs: strings.Join(itemsIDs, ",")} result.SetCommonValues() - json.OK(w, result) + json.OK(w, r, result) } /* @@ -534,7 +534,7 @@ func (c *Controller) handleWriteItems(w http.ResponseWriter, r *http.Request) { }() } - json.OK(w, newBaseResponse()) + json.OK(w, r, newBaseResponse()) } /* @@ -575,7 +575,7 @@ func (c *Controller) handleWriteFeeds(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, newBaseResponse()) + json.OK(w, r, newBaseResponse()) } /* @@ -616,7 +616,7 @@ func (c *Controller) handleWriteGroups(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, newBaseResponse()) + json.OK(w, r, newBaseResponse()) } /* diff --git a/http/response/json/json.go b/http/response/json/json.go index ff0f06be..6f3eb15c 100644 --- a/http/response/json/json.go +++ b/http/response/json/json.go @@ -9,14 +9,14 @@ import ( "errors" "net/http" + "github.com/miniflux/miniflux/http/response" "github.com/miniflux/miniflux/logger" ) // OK sends a JSON response with the status code 200. -func OK(w http.ResponseWriter, v interface{}) { +func OK(w http.ResponseWriter, r *http.Request, v interface{}) { commonHeaders(w) - w.WriteHeader(http.StatusOK) - w.Write(toJSON(v)) + response.Compress(w, r, toJSON(v)) } // Created sends a JSON response with the status code 201. diff --git a/http/response/response.go b/http/response/response.go index c5ae3fa1..f7926254 100644 --- a/http/response/response.go +++ b/http/response/response.go @@ -23,7 +23,7 @@ func NotModified(w http.ResponseWriter) { } // Cache returns a response with caching headers. -func Cache(w http.ResponseWriter, r *http.Request, mimeType, etag string, content []byte, duration time.Duration) { +func Cache(w http.ResponseWriter, r *http.Request, mimeType, etag string, data []byte, duration time.Duration) { w.Header().Set("Content-Type", mimeType) w.Header().Set("ETag", etag) w.Header().Set("Cache-Control", "public") @@ -31,8 +31,14 @@ func Cache(w http.ResponseWriter, r *http.Request, mimeType, etag string, conten if etag == r.Header.Get("If-None-Match") { w.WriteHeader(http.StatusNotModified) - } else { - w.Write(content) + return + } + + switch mimeType { + case "text/javascript; charset=utf-8", "text/css; charset=utf-8": + Compress(w, r, data) + default: + w.Write(data) } } diff --git a/middleware/fever.go b/middleware/fever.go index 78217e4a..6211fb79 100644 --- a/middleware/fever.go +++ b/middleware/fever.go @@ -20,13 +20,13 @@ func (m *Middleware) FeverAuth(next http.Handler) http.Handler { user, err := m.store.UserByFeverToken(apiKey) if err != nil { logger.Error("[Middleware:Fever] %v", err) - json.OK(w, map[string]int{"api_version": 3, "auth": 0}) + json.OK(w, r, map[string]int{"api_version": 3, "auth": 0}) return } if user == nil { logger.Info("[Middleware:Fever] Fever authentication failure") - json.OK(w, map[string]int{"api_version": 3, "auth": 0}) + json.OK(w, r, map[string]int{"api_version": 3, "auth": 0}) return } diff --git a/ui/entry_scraper.go b/ui/entry_scraper.go index 75e4040f..aea9b209 100644 --- a/ui/entry_scraper.go +++ b/ui/entry_scraper.go @@ -49,5 +49,5 @@ func (c *Controller) FetchContent(w http.ResponseWriter, r *http.Request) { entry.Content = sanitizer.Sanitize(entry.URL, content) c.store.UpdateEntryContent(entry) - json.Created(w, map[string]string{"content": entry.Content}) + json.OK(w, r, map[string]string{"content": entry.Content}) } diff --git a/ui/entry_toggle_bookmark.go b/ui/entry_toggle_bookmark.go index 423fde87..9d94cd18 100644 --- a/ui/entry_toggle_bookmark.go +++ b/ui/entry_toggle_bookmark.go @@ -28,5 +28,5 @@ func (c *Controller) ToggleBookmark(w http.ResponseWriter, r *http.Request) { return } - json.OK(w, "OK") + json.OK(w, r, "OK") } diff --git a/ui/entry_update_status.go b/ui/entry_update_status.go index 3174d85f..bafa0a09 100644 --- a/ui/entry_update_status.go +++ b/ui/entry_update_status.go @@ -35,5 +35,5 @@ func (c *Controller) UpdateEntriesStatus(w http.ResponseWriter, r *http.Request) return } - json.OK(w, "OK") + json.OK(w, r, "OK") } diff --git a/ui/static_manifest.go b/ui/static_manifest.go index 27abaec6..96814fe1 100644 --- a/ui/static_manifest.go +++ b/ui/static_manifest.go @@ -50,5 +50,5 @@ func (c *Controller) WebManifest(w http.ResponseWriter, r *http.Request) { }, } - json.OK(w, manifest) + json.OK(w, r, manifest) }