-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.http.go
92 lines (72 loc) · 2.56 KB
/
api.http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2022 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webhdfs
import (
"fmt"
"io"
"net/http"
"time"
"github.com/searKing/golang/go/exp/types"
)
type HttpRequest struct {
// Close indicates whether to close the connection after
// replying to this request (for servers) or after sending this
// request and reading its response (for clients).
//
// some proxy does not support reuse connection, set Close true to disable it.
Close bool
PreSendHandler func(req *http.Request) (*http.Request, error)
}
type HttpResponse struct {
// Indicates that a range of bytes was specified.
AcceptRanges *string
// Object data.
// We guarantee that Body is always non-nil, even on responses without a body or responses with
// a zero-length body. It is the caller's responsibility to close Body.
Body io.ReadCloser
// Specifies caching behavior along the request/reply chain.
CacheControl *string
// Specifies presentational information for the object.
ContentDisposition *string
// Specifies what content encodings have been applied to the object and thus
// what decoding mechanisms must be applied to obtain the media-type referenced
// by the Content-Type header field.
ContentEncoding *string
// The language the content is in.
ContentLanguage *string
// Size of the body in bytes.
ContentLength *int64
// The portion of the object returned in the response.
ContentRange *string
// A standard MIME type describing the format of the object data.
ContentType *string
// An ETag is an opaque identifier assigned by a web server to a specific version
// of a resource found at a URL.
ETag *string
// The date and time at which the object is no longer cacheable.
Expires *string `location:"header" locationName:"Expires" type:"string"`
// Last modified date of the object
LastModified *time.Time `location:"header" locationName:"Last-Modified" type:"timestamp"`
}
func (resp *HttpResponse) UnmarshalHTTP(httpResp *http.Response) {
resp.ContentLength = types.Pointer(httpResp.ContentLength)
{
ct := httpResp.Header.Get("Content-Type")
if ct != "" {
resp.ContentType = types.Pointer(httpResp.Header.Get("Content-Type"))
}
}
resp.Body = httpResp.Body
httpResp.Body = http.NoBody
return
}
func ErrorFromHttpResponse(resp *http.Response) error {
if resp == nil {
return nil
}
if isSuccessHttpCode(resp.StatusCode) {
return nil
}
return fmt.Errorf("unexpected http status code: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}