Skip to content

Commit

Permalink
Add content length check when refreshing feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillot committed Jan 5, 2018
1 parent c57cafb commit 7d278d4
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 23 deletions.
13 changes: 7 additions & 6 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ func (c *Client) executeRequest(request *http.Request) (*Response, error) {
}

response := &Response{
Body: resp.Body,
StatusCode: resp.StatusCode,
EffectiveURL: resp.Request.URL.String(),
LastModified: resp.Header.Get("Last-Modified"),
ETag: resp.Header.Get("ETag"),
ContentType: resp.Header.Get("Content-Type"),
Body: resp.Body,
StatusCode: resp.StatusCode,
EffectiveURL: resp.Request.URL.String(),
LastModified: resp.Header.Get("Last-Modified"),
ETag: resp.Header.Get("ETag"),
ContentType: resp.Header.Get("Content-Type"),
ContentLength: resp.ContentLength,
}

logger.Debug("[HttpClient:%s] OriginalURL=%s, StatusCode=%d, ContentLength=%d, ETag=%s, LastModified=%s, EffectiveURL=%s",
Expand Down
13 changes: 7 additions & 6 deletions http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (

// Response wraps a server response.
type Response struct {
Body io.Reader
StatusCode int
EffectiveURL string
LastModified string
ETag string
ContentType string
Body io.Reader
StatusCode int
EffectiveURL string
LastModified string
ETag string
ContentType string
ContentLength int64
}

// HasServerFailure returns true if the status code represents a failure.
Expand Down
7 changes: 4 additions & 3 deletions locale/translations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion locale/translations/fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,6 @@
"Save article": "Sauvegarder l'article",
"There is already someone associated with this provider!": "Il y a déjà quelqu'un d'associé avec ce provider !",
"There is already someone else with the same Fever username!": "Il y a déjà quelqu'un d'autre avec le même nom d'utilisateur Fever !",
"Mark all as read": "Tout marquer comme lu"
"Mark all as read": "Tout marquer comme lu",
"This feed is empty": "Cet abonnement est vide"
}
16 changes: 16 additions & 0 deletions reader/feed/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
errNotFound = "Feed %d not found"
errEncoding = "Unable to normalize encoding: %v."
errCategoryNotFound = "Category not found for this user."
errEmptyFeed = "This feed is empty"
)

// Handler contains all the logic to create and refresh feeds.
Expand All @@ -50,6 +51,11 @@ func (h *Handler) CreateFeed(userID, categoryID int64, url string, crawler bool)
return nil, errors.NewLocalizedError(errServerFailure, response.StatusCode)
}

// Content-Length = -1 when no Content-Length header is sent
if response.ContentLength == 0 {
return nil, errors.NewLocalizedError(errEmptyFeed)
}

if h.store.FeedURLExists(userID, response.EffectiveURL) {
return nil, errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
}
Expand Down Expand Up @@ -133,6 +139,16 @@ func (h *Handler) RefreshFeed(userID, feedID int64) error {

if response.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
logger.Debug("[Handler:RefreshFeed] Feed #%d has been modified", feedID)

// Content-Length = -1 when no Content-Length header is sent
if response.ContentLength == 0 {
err := errors.NewLocalizedError(errEmptyFeed)
originalFeed.ParsingErrorCount++
originalFeed.ParsingErrorMsg = err.Error()
h.store.UpdateFeed(originalFeed)
return err
}

body, err := response.NormalizeBodyEncoding()
if err != nil {
return errors.NewLocalizedError(errEncoding, err)
Expand Down
2 changes: 1 addition & 1 deletion template/html/edit_feed.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h1>{{ .feed.Title }}</h1>
{{ if ne .feed.ParsingErrorCount 0 }}
<div class="alert alert-error">
<h3>{{ t "Last Parsing Error" }}</h3>
{{ .feed.ParsingErrorMsg }}
<p>{{ t .feed.ParsingErrorMsg }}</p>
</div>
{{ end }}

Expand Down
2 changes: 1 addition & 1 deletion template/html/feed_entries.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h1>{{ .feed.Title }} ({{ .total }})</h1>
{{ if ne .feed.ParsingErrorCount 0 }}
<div class="alert alert-error">
<h3>{{ t "There is a problem with this feed" }}</h3>
{{ .feed.ParsingErrorMsg }}
<p>{{ t .feed.ParsingErrorMsg }}</p>
</div>
{{ else if not .entries }}
<p class="alert">{{ t "There is no article for this feed." }}</p>
Expand Down
10 changes: 5 additions & 5 deletions template/views.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7d278d4

Please sign in to comment.