2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package client // import "miniflux.app/http/client"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2017-11-22 07:55:19 +01:00
|
|
|
import (
|
|
|
|
"io"
|
2018-01-20 07:42:55 +01:00
|
|
|
"mime"
|
2017-11-22 07:55:19 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/html/charset"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/logger"
|
2017-11-22 07:55:19 +01:00
|
|
|
)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2017-11-21 02:12:37 +01:00
|
|
|
// Response wraps a server response.
|
|
|
|
type Response struct {
|
2018-01-05 03:32:36 +01:00
|
|
|
Body io.Reader
|
|
|
|
StatusCode int
|
|
|
|
EffectiveURL string
|
|
|
|
LastModified string
|
|
|
|
ETag string
|
|
|
|
ContentType string
|
|
|
|
ContentLength int64
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-06-30 21:42:12 +02:00
|
|
|
// IsNotFound returns true if the resource doesn't exists anymore.
|
|
|
|
func (r *Response) IsNotFound() bool {
|
|
|
|
return r.StatusCode == 404 || r.StatusCode == 410
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotAuthorized returns true if the resource require authentication.
|
|
|
|
func (r *Response) IsNotAuthorized() bool {
|
|
|
|
return r.StatusCode == 401
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:12:37 +01:00
|
|
|
// HasServerFailure returns true if the status code represents a failure.
|
|
|
|
func (r *Response) HasServerFailure() bool {
|
|
|
|
return r.StatusCode >= 400
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 02:12:37 +01:00
|
|
|
// IsModified returns true if the resource has been modified.
|
|
|
|
func (r *Response) IsModified(etag, lastModified string) bool {
|
|
|
|
if r.StatusCode == 304 {
|
2017-11-20 06:10:04 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:25:45 +01:00
|
|
|
if r.ETag != "" && r.ETag == etag {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.LastModified != "" && r.LastModified == lastModified {
|
2017-11-20 06:10:04 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2017-11-21 02:12:37 +01:00
|
|
|
|
|
|
|
// NormalizeBodyEncoding make sure the body is encoded in UTF-8.
|
2018-01-20 07:42:55 +01:00
|
|
|
//
|
|
|
|
// If a charset other than UTF-8 is detected, we convert the document to UTF-8.
|
|
|
|
// This is used by the scraper and feed readers.
|
|
|
|
//
|
|
|
|
// Do not forget edge cases:
|
|
|
|
// - Some non-utf8 feeds specify encoding only in Content-Type, not in XML document.
|
2017-11-21 02:12:37 +01:00
|
|
|
func (r *Response) NormalizeBodyEncoding() (io.Reader, error) {
|
2018-01-20 07:42:55 +01:00
|
|
|
_, params, err := mime.ParseMediaType(r.ContentType)
|
|
|
|
if err == nil {
|
|
|
|
if enc, found := params["charset"]; found {
|
|
|
|
enc = strings.ToLower(enc)
|
|
|
|
if enc != "utf-8" && enc != "utf8" && enc != "" {
|
|
|
|
logger.Debug("[NormalizeBodyEncoding] Convert body to UTF-8 from %s", enc)
|
|
|
|
return charset.NewReader(r.Body, r.ContentType)
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 07:55:19 +01:00
|
|
|
}
|
|
|
|
return r.Body, nil
|
2017-11-21 02:12:37 +01:00
|
|
|
}
|