Source file src/net/http/clone.go

     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  	"mime/multipart"
     9  	"net/textproto"
    10  	"net/url"
    11  )
    12  
    13  func cloneURLValues(v url.Values) url.Values {
    14  	if v == nil {
    15  		return nil
    16  	}
    17  	// http.Header and url.Values have the same representation, so temporarily
    18  	// treat it like http.Header, which does have a clone:
    19  	return url.Values(Header(v).Clone())
    20  }
    21  
    22  func cloneURL(u *url.URL) *url.URL {
    23  	if u == nil {
    24  		return nil
    25  	}
    26  	u2 := new(url.URL)
    27  	*u2 = *u
    28  	if u.User != nil {
    29  		u2.User = new(url.Userinfo)
    30  		*u2.User = *u.User
    31  	}
    32  	return u2
    33  }
    34  
    35  func cloneMultipartForm(f *multipart.Form) *multipart.Form {
    36  	if f == nil {
    37  		return nil
    38  	}
    39  	f2 := &multipart.Form{
    40  		Value: (map[string][]string)(Header(f.Value).Clone()),
    41  	}
    42  	if f.File != nil {
    43  		m := make(map[string][]*multipart.FileHeader)
    44  		for k, vv := range f.File {
    45  			vv2 := make([]*multipart.FileHeader, len(vv))
    46  			for i, v := range vv {
    47  				vv2[i] = cloneMultipartFileHeader(v)
    48  			}
    49  			m[k] = vv2
    50  		}
    51  		f2.File = m
    52  	}
    53  	return f2
    54  }
    55  
    56  func cloneMultipartFileHeader(fh *multipart.FileHeader) *multipart.FileHeader {
    57  	if fh == nil {
    58  		return nil
    59  	}
    60  	fh2 := new(multipart.FileHeader)
    61  	*fh2 = *fh
    62  	fh2.Header = textproto.MIMEHeader(Header(fh.Header).Clone())
    63  	return fh2
    64  }
    65  
    66  // cloneOrMakeHeader invokes Header.Clone but if the
    67  // result is nil, it'll instead make and return a non-nil Header.
    68  func cloneOrMakeHeader(hdr Header) Header {
    69  	clone := hdr.Clone()
    70  	if clone == nil {
    71  		clone = make(Header)
    72  	}
    73  	return clone
    74  }
    75  

View as plain text