...

Source file src/github.com/sigstore/timestamp-authority/pkg/client/options.go

Documentation: github.com/sigstore/timestamp-authority/pkg/client

     1  // Copyright 2022 The Sigstore Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package client
    16  
    17  import "net/http"
    18  
    19  const (
    20  	TimestampQueryMediaType = "application/timestamp-query"
    21  	JSONMediaType           = "application/json"
    22  )
    23  
    24  // Option is a functional option for customizing static signatures.
    25  type Option func(*options)
    26  
    27  type options struct {
    28  	UserAgent   string
    29  	ContentType string
    30  }
    31  
    32  func makeOptions(opts ...Option) *options {
    33  	o := &options{
    34  		UserAgent:   "",
    35  		ContentType: "",
    36  	}
    37  
    38  	for _, opt := range opts {
    39  		opt(o)
    40  	}
    41  
    42  	return o
    43  }
    44  
    45  // WithUserAgent sets the media type of the signature.
    46  func WithUserAgent(userAgent string) Option {
    47  	return func(o *options) {
    48  		o.UserAgent = userAgent
    49  	}
    50  }
    51  
    52  // WithContentType sets the content type of the request.
    53  func WithContentType(contentType string) Option {
    54  	return func(o *options) {
    55  		o.ContentType = contentType
    56  	}
    57  }
    58  
    59  type roundTripper struct {
    60  	http.RoundTripper
    61  	UserAgent   string
    62  	ContentType string
    63  }
    64  
    65  // RoundTrip implements `http.RoundTripper`
    66  func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    67  	req.Header.Set("User-Agent", rt.UserAgent)
    68  	req.Header.Set("Content-Type", rt.ContentType)
    69  	return rt.RoundTripper.RoundTrip(req)
    70  }
    71  
    72  func createRoundTripper(inner http.RoundTripper, o *options) http.RoundTripper {
    73  	if inner == nil {
    74  		inner = http.DefaultTransport
    75  	}
    76  	if o.UserAgent == "" {
    77  		// There's nothing to do...
    78  		return inner
    79  	}
    80  	if o.ContentType == "" {
    81  		// There's nothing to do...
    82  		return inner
    83  	}
    84  	return &roundTripper{
    85  		RoundTripper: inner,
    86  		UserAgent:    o.UserAgent,
    87  		ContentType:  o.ContentType,
    88  	}
    89  }
    90  

View as plain text