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  	_ "unsafe" // for linkname
    12  )
    13  
    14  // cloneURLValues should be an internal detail,
    15  // but widely used packages access it using linkname.
    16  // Notable members of the hall of shame include:
    17  //   - github.com/searKing/golang
    18  //
    19  // Do not remove or change the type signature.
    20  // See go.dev/issue/67401.
    21  //
    22  //go:linkname cloneURLValues
    23  func cloneURLValues(v url.Values) url.Values {
    24  	if v == nil {
    25  		return nil
    26  	}
    27  	// http.Header and url.Values have the same representation, so temporarily
    28  	// treat it like http.Header, which does have a clone:
    29  	return url.Values(Header(v).Clone())
    30  }
    31  
    32  // cloneURL should be an internal detail,
    33  // but widely used packages access it using linkname.
    34  // Notable members of the hall of shame include:
    35  //   - github.com/searKing/golang
    36  //
    37  // Do not remove or change the type signature.
    38  // See go.dev/issue/67401.
    39  //
    40  //go:linkname cloneURL
    41  func cloneURL(u *url.URL) *url.URL {
    42  	return u.Clone()
    43  }
    44  
    45  // cloneMultipartForm should be an internal detail,
    46  // but widely used packages access it using linkname.
    47  // Notable members of the hall of shame include:
    48  //   - github.com/searKing/golang
    49  //
    50  // Do not remove or change the type signature.
    51  // See go.dev/issue/67401.
    52  //
    53  //go:linkname cloneMultipartForm
    54  func cloneMultipartForm(f *multipart.Form) *multipart.Form {
    55  	if f == nil {
    56  		return nil
    57  	}
    58  	f2 := &multipart.Form{
    59  		Value: (map[string][]string)(Header(f.Value).Clone()),
    60  	}
    61  	if f.File != nil {
    62  		m := make(map[string][]*multipart.FileHeader, len(f.File))
    63  		for k, vv := range f.File {
    64  			vv2 := make([]*multipart.FileHeader, len(vv))
    65  			for i, v := range vv {
    66  				vv2[i] = cloneMultipartFileHeader(v)
    67  			}
    68  			m[k] = vv2
    69  		}
    70  		f2.File = m
    71  	}
    72  	return f2
    73  }
    74  
    75  // cloneMultipartFileHeader should be an internal detail,
    76  // but widely used packages access it using linkname.
    77  // Notable members of the hall of shame include:
    78  //   - github.com/searKing/golang
    79  //
    80  // Do not remove or change the type signature.
    81  // See go.dev/issue/67401.
    82  //
    83  //go:linkname cloneMultipartFileHeader
    84  func cloneMultipartFileHeader(fh *multipart.FileHeader) *multipart.FileHeader {
    85  	if fh == nil {
    86  		return nil
    87  	}
    88  	fh2 := new(multipart.FileHeader)
    89  	*fh2 = *fh
    90  	fh2.Header = textproto.MIMEHeader(Header(fh.Header).Clone())
    91  	return fh2
    92  }
    93  
    94  // cloneOrMakeHeader invokes Header.Clone but if the
    95  // result is nil, it'll instead make and return a non-nil Header.
    96  //
    97  // cloneOrMakeHeader should be an internal detail,
    98  // but widely used packages access it using linkname.
    99  // Notable members of the hall of shame include:
   100  //   - github.com/searKing/golang
   101  //
   102  // Do not remove or change the type signature.
   103  // See go.dev/issue/67401.
   104  //
   105  //go:linkname cloneOrMakeHeader
   106  func cloneOrMakeHeader(hdr Header) Header {
   107  	clone := hdr.Clone()
   108  	if clone == nil {
   109  		clone = make(Header)
   110  	}
   111  	return clone
   112  }
   113  

View as plain text