2015-02-11 02:35:30 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-17 22:32:51 +02:00
|
|
|
"encoding/json"
|
2016-03-14 18:06:30 +01:00
|
|
|
"errors"
|
2015-02-11 02:35:30 +01:00
|
|
|
"fmt"
|
2015-05-20 04:18:30 +02:00
|
|
|
"io"
|
2015-04-17 22:32:51 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-02-11 02:35:30 +01:00
|
|
|
|
2015-05-15 03:21:39 +02:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
2015-02-11 02:35:30 +01:00
|
|
|
)
|
|
|
|
|
2016-06-02 07:31:13 +02:00
|
|
|
// ErrNoErrorsInBody is returned when an HTTP response body parses to an empty
|
2016-03-14 18:06:30 +01:00
|
|
|
// errcode.Errors slice.
|
|
|
|
var ErrNoErrorsInBody = errors.New("no error details found in HTTP response body")
|
|
|
|
|
2015-02-11 02:35:30 +01:00
|
|
|
// UnexpectedHTTPStatusError is returned when an unexpected HTTP status is
|
|
|
|
// returned when making a registry api call.
|
|
|
|
type UnexpectedHTTPStatusError struct {
|
|
|
|
Status string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnexpectedHTTPStatusError) Error() string {
|
2016-03-14 18:06:30 +01:00
|
|
|
return fmt.Sprintf("received unexpected HTTP status: %s", e.Status)
|
2015-02-11 02:35:30 +01:00
|
|
|
}
|
2015-04-17 22:32:51 +02:00
|
|
|
|
|
|
|
// UnexpectedHTTPResponseError is returned when an expected HTTP status code
|
|
|
|
// is returned, but the content was unexpected and failed to be parsed.
|
|
|
|
type UnexpectedHTTPResponseError struct {
|
2016-03-15 17:03:56 +01:00
|
|
|
ParseErr error
|
|
|
|
StatusCode int
|
|
|
|
Response []byte
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnexpectedHTTPResponseError) Error() string {
|
2016-03-15 17:03:56 +01:00
|
|
|
return fmt.Sprintf("error parsing HTTP %d response body: %s: %q", e.StatusCode, e.ParseErr.Error(), string(e.Response))
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 23:45:08 +01:00
|
|
|
func parseHTTPErrorResponse(statusCode int, r io.Reader) error {
|
2015-05-15 03:21:39 +02:00
|
|
|
var errors errcode.Errors
|
2015-05-20 04:18:30 +02:00
|
|
|
body, err := ioutil.ReadAll(r)
|
2015-04-17 22:32:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-08 01:11:04 +02:00
|
|
|
|
2016-01-20 23:45:08 +01:00
|
|
|
// For backward compatibility, handle irregularly formatted
|
|
|
|
// messages that contain a "details" field.
|
|
|
|
var detailsErr struct {
|
|
|
|
Details string `json:"details"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &detailsErr)
|
|
|
|
if err == nil && detailsErr.Details != "" {
|
2016-05-03 10:28:40 +02:00
|
|
|
switch statusCode {
|
|
|
|
case http.StatusUnauthorized:
|
2016-01-20 23:45:08 +01:00
|
|
|
return errcode.ErrorCodeUnauthorized.WithMessage(detailsErr.Details)
|
2016-05-03 21:24:43 +02:00
|
|
|
// FIXME: go1.5 doesn't export http.StatusTooManyRequests while
|
|
|
|
// go1.6 does. Update the hardcoded value to the constant once
|
|
|
|
// Docker updates golang version to 1.6.
|
|
|
|
case 429:
|
2016-05-03 10:28:40 +02:00
|
|
|
return errcode.ErrorCodeTooManyRequests.WithMessage(detailsErr.Details)
|
|
|
|
default:
|
|
|
|
return errcode.ErrorCodeUnknown.WithMessage(detailsErr.Details)
|
2016-01-20 23:45:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 01:11:04 +02:00
|
|
|
if err := json.Unmarshal(body, &errors); err != nil {
|
2015-04-17 22:32:51 +02:00
|
|
|
return &UnexpectedHTTPResponseError{
|
2016-03-15 17:03:56 +01:00
|
|
|
ParseErr: err,
|
|
|
|
StatusCode: statusCode,
|
|
|
|
Response: body,
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-14 18:06:30 +01:00
|
|
|
|
|
|
|
if len(errors) == 0 {
|
|
|
|
// If there was no error specified in the body, return
|
|
|
|
// UnexpectedHTTPResponseError.
|
|
|
|
return &UnexpectedHTTPResponseError{
|
2016-03-15 17:03:56 +01:00
|
|
|
ParseErr: ErrNoErrorsInBody,
|
|
|
|
StatusCode: statusCode,
|
|
|
|
Response: body,
|
2016-03-14 18:06:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 02:18:32 +02:00
|
|
|
return errors
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
2015-05-09 02:40:30 +02:00
|
|
|
|
2015-12-08 23:24:03 +01:00
|
|
|
// HandleErrorResponse returns error parsed from HTTP response for an
|
|
|
|
// unsuccessful HTTP response code (in the range 400 - 499 inclusive). An
|
|
|
|
// UnexpectedHTTPStatusError returned for response code outside of expected
|
|
|
|
// range.
|
|
|
|
func HandleErrorResponse(resp *http.Response) error {
|
2015-05-20 19:05:44 +02:00
|
|
|
if resp.StatusCode == 401 {
|
2016-01-20 23:45:08 +01:00
|
|
|
err := parseHTTPErrorResponse(resp.StatusCode, resp.Body)
|
2015-05-20 19:05:44 +02:00
|
|
|
if uErr, ok := err.(*UnexpectedHTTPResponseError); ok {
|
2015-08-07 01:25:08 +02:00
|
|
|
return errcode.ErrorCodeUnauthorized.WithDetail(uErr.Response)
|
2015-05-20 19:05:44 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2015-05-09 02:40:30 +02:00
|
|
|
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
|
2016-01-20 23:45:08 +01:00
|
|
|
return parseHTTPErrorResponse(resp.StatusCode, resp.Body)
|
2015-05-09 02:40:30 +02:00
|
|
|
}
|
|
|
|
return &UnexpectedHTTPStatusError{Status: resp.Status}
|
|
|
|
}
|
2015-07-25 01:14:04 +02:00
|
|
|
|
|
|
|
// SuccessStatus returns true if the argument is a successful HTTP response
|
|
|
|
// code (in the range 200 - 399 inclusive).
|
|
|
|
func SuccessStatus(status int) bool {
|
|
|
|
return status >= 200 && status <= 399
|
|
|
|
}
|