github.com/Kolosok86/http@v0.1.2/clone.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package http
     6  
     7  import (
     8  	"net/url"
     9  
    10  	"github.com/Kolosok86/http/internal/multipart"
    11  	"github.com/Kolosok86/http/textproto"
    12  )
    13  
    14  func cloneURLValues(v url.Values) url.Values {
    15  	if v == nil {
    16  		return nil
    17  	}
    18  	// http.Header and url.Values have the same representation, so temporarily
    19  	// treat it like http.Header, which does have a clone:
    20  	return url.Values(Header(v).Clone())
    21  }
    22  
    23  func cloneURL(u *url.URL) *url.URL {
    24  	if u == nil {
    25  		return nil
    26  	}
    27  	u2 := new(url.URL)
    28  	*u2 = *u
    29  	if u.User != nil {
    30  		u2.User = new(url.Userinfo)
    31  		*u2.User = *u.User
    32  	}
    33  	return u2
    34  }
    35  
    36  func cloneMultipartForm(f *multipart.Form) *multipart.Form {
    37  	if f == nil {
    38  		return nil
    39  	}
    40  	f2 := &multipart.Form{
    41  		Value: (map[string][]string)(Header(f.Value).Clone()),
    42  	}
    43  	if f.File != nil {
    44  		m := make(map[string][]*multipart.FileHeader)
    45  		for k, vv := range f.File {
    46  			vv2 := make([]*multipart.FileHeader, len(vv))
    47  			for i, v := range vv {
    48  				vv2[i] = cloneMultipartFileHeader(v)
    49  			}
    50  			m[k] = vv2
    51  		}
    52  		f2.File = m
    53  	}
    54  	return f2
    55  }
    56  
    57  func cloneMultipartFileHeader(fh *multipart.FileHeader) *multipart.FileHeader {
    58  	if fh == nil {
    59  		return nil
    60  	}
    61  	fh2 := new(multipart.FileHeader)
    62  	*fh2 = *fh
    63  	fh2.Header = textproto.MIMEHeader(Header(fh.Header).Clone())
    64  	return fh2
    65  }
    66  
    67  // cloneOrMakeHeader invokes Header.Clone but if the
    68  // result is nil, it'll instead make and return a non-nil Header.
    69  func cloneOrMakeHeader(hdr Header) Header {
    70  	clone := hdr.Clone()
    71  	if clone == nil {
    72  		clone = make(Header)
    73  	}
    74  	return clone
    75  }