Debian Codegraph @

signature/sigstore/rekor/openapi_infra.go

raw
1 package rekor
2
3 // The following code is the essence of the relevant code paths from github.com/go-openapi/runtime,
4 // heavily modified since.
5
6 // Copyright 2015 go-swagger maintainers
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19
20 import (
21 "bytes"
22 "context"
23 "encoding/json"
24 "io"
25 "net/http"
26 "path"
27 )
28
29 // makeRequest makes a http request to the requested requestPath, and returns the received response.
30 func (r *rekorClient) makeRequest(ctx context.Context, method, requestPath string, bodyContent any) (*http.Response, error) {
31 var body io.Reader
32 headers := http.Header{}
33
34 headers.Set("Accept", "application/json")
35 if bodyContent != nil {
36 buf := bytes.NewBuffer(nil)
37 body = buf
38 headers.Set("Content-Type", "application/json")
39 enc := json.NewEncoder(buf)
40 enc.SetEscapeHTML(false)
41 if err := enc.Encode(bodyContent); err != nil {
42 return nil, err
43 }
44 }
45
46 req, err := http.NewRequestWithContext(ctx, method, path.Join(r.basePath, requestPath), body)
47 if err != nil {
48 return nil, err
49 }
50 // Only Scheme and Host are used from rekorURL.
51 // Really this should probabbly use r.rekorURL.JoinPath(requestPath) (which, notably, correctly deals with path escaping),
52 // and pass that to NewRequestWithContext, but this use of path.Join is consistent with go-openapi/runtime v0.24.1 .
53 req.URL.Scheme = r.rekorURL.Scheme
54 req.URL.Host = r.rekorURL.Host
55 req.Header = headers
56
57 res, err := r.httpClient.Do(req)
58 if err != nil {
59 return nil, err
60 }
61 // Note that we don’t care to even read the Content-Type: header; we blindly assume the format is the requested JSON.
62 return res, nil
63 }
64
65 // decodeHTTPResponseBodyAsJSON decodes the body of a HTTP response in a manner compatible with github.com/go-openapi/runtime.
66 func decodeHTTPResponseBodyAsJSON(res *http.Response, data any) error {
67 dec := json.NewDecoder(res.Body)
68 dec.UseNumber()
69 err := dec.Decode(data)
70 if err == io.EOF {
71 // This seems unwanted at a first glance; go-swagger added it in https://github.com/go-swagger/go-swagger/issues/192 , it’s unclear
72 // whether it’s correct or still necessary.
73 err = nil
74 }
75 return err
76 }