2017-11-20 00:10:04 -05: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.
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
2017-11-22 01:55:19 -05:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/html/charset"
|
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2017-11-20 20:12:37 -05:00
|
|
|
// Response wraps a server response.
|
|
|
|
type Response struct {
|
2017-11-20 00:10:04 -05:00
|
|
|
Body io.Reader
|
|
|
|
StatusCode int
|
|
|
|
EffectiveURL string
|
|
|
|
LastModified string
|
|
|
|
ETag string
|
|
|
|
ContentType string
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:12:37 -05:00
|
|
|
// HasServerFailure returns true if the status code represents a failure.
|
|
|
|
func (r *Response) HasServerFailure() bool {
|
|
|
|
return r.StatusCode >= 400
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 20:12:37 -05: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 00:10:04 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:25:45 -05:00
|
|
|
if r.ETag != "" && r.ETag == etag {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.LastModified != "" && r.LastModified == lastModified {
|
2017-11-20 00:10:04 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2017-11-20 20:12:37 -05:00
|
|
|
|
|
|
|
// NormalizeBodyEncoding make sure the body is encoded in UTF-8.
|
|
|
|
func (r *Response) NormalizeBodyEncoding() (io.Reader, error) {
|
2017-11-22 01:55:19 -05:00
|
|
|
if strings.Contains(r.ContentType, "charset=") {
|
|
|
|
return charset.NewReader(r.Body, r.ContentType)
|
|
|
|
}
|
|
|
|
return r.Body, nil
|
2017-11-20 20:12:37 -05:00
|
|
|
}
|