2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-10-15 00:43:48 -04:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package browser // import "miniflux.app/v2/internal/reader/browser"
|
2018-10-15 00:43:48 -04:00
|
|
|
|
|
|
|
import (
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/errors"
|
|
|
|
"miniflux.app/v2/internal/http/client"
|
2018-10-15 00:43:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errRequestFailed = "Unable to open this link: %v"
|
|
|
|
errServerFailure = "Unable to fetch this resource (Status Code = %d)"
|
|
|
|
errEncoding = "Unable to normalize encoding: %q"
|
|
|
|
errEmptyFeed = "This feed is empty"
|
2021-10-17 18:25:04 -04:00
|
|
|
errResourceNotFound = "Resource not found (404), this feed doesn't exist anymore, check the feed URL"
|
2018-10-15 00:43:48 -04:00
|
|
|
errNotAuthorized = "You are not authorized to access this resource (invalid username/password)"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Exec executes a HTTP request and handles errors.
|
|
|
|
func Exec(request *client.Client) (*client.Response, *errors.LocalizedError) {
|
|
|
|
response, err := request.Get()
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*errors.LocalizedError); ok {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return nil, errors.NewLocalizedError(errRequestFailed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.IsNotFound() {
|
|
|
|
return nil, errors.NewLocalizedError(errResourceNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.IsNotAuthorized() {
|
|
|
|
return nil, errors.NewLocalizedError(errNotAuthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.HasServerFailure() {
|
|
|
|
return nil, errors.NewLocalizedError(errServerFailure, response.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.StatusCode != 304 {
|
|
|
|
// Content-Length = -1 when no Content-Length header is sent.
|
|
|
|
if response.ContentLength == 0 {
|
|
|
|
return nil, errors.NewLocalizedError(errEmptyFeed)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := response.EnsureUnicodeBody(); err != nil {
|
|
|
|
return nil, errors.NewLocalizedError(errEncoding, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|