1package s3
2
3import (
4	"bytes"
5	"io"
6	"io/ioutil"
7	"net/http"
8
9	"github.com/aws/aws-sdk-go/aws/awserr"
10	"github.com/aws/aws-sdk-go/aws/request"
11	"github.com/aws/aws-sdk-go/internal/sdkio"
12)
13
14func copyMultipartStatusOKUnmarshalError(r *request.Request) {
15	b, err := ioutil.ReadAll(r.HTTPResponse.Body)
16	r.HTTPResponse.Body.Close()
17	if err != nil {
18		r.Error = awserr.NewRequestFailure(
19			awserr.New(request.ErrCodeSerialization, "unable to read response body", err),
20			r.HTTPResponse.StatusCode,
21			r.RequestID,
22		)
23		// Note, some middleware later in the stack like restxml.Unmarshal expect a valid, non-closed Body
24		// even in case of an error, so we replace it with an empty Reader.
25		r.HTTPResponse.Body = ioutil.NopCloser(bytes.NewBuffer(nil))
26		return
27	}
28
29	body := bytes.NewReader(b)
30	r.HTTPResponse.Body = ioutil.NopCloser(body)
31	defer body.Seek(0, sdkio.SeekStart)
32
33	unmarshalError(r)
34	if err, ok := r.Error.(awserr.Error); ok && err != nil {
35		if err.Code() == request.ErrCodeSerialization &&
36			err.OrigErr() != io.EOF {
37			r.Error = nil
38			return
39		}
40		// if empty payload
41		if err.OrigErr() == io.EOF {
42			r.HTTPResponse.StatusCode = http.StatusInternalServerError
43		} else {
44			r.HTTPResponse.StatusCode = http.StatusServiceUnavailable
45		}
46	}
47}
48