ctxhttp_pre17.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2015 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. // +build !go1.7
  5. package ctxhttp // import "golang.org/x/net/context/ctxhttp"
  6. import (
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "golang.org/x/net/context"
  12. )
  13. func nop() {}
  14. var (
  15. testHookContextDoneBeforeHeaders = nop
  16. testHookDoReturned = nop
  17. testHookDidBodyClose = nop
  18. )
  19. // Do sends an HTTP request with the provided http.Client and returns an HTTP response.
  20. // If the client is nil, http.DefaultClient is used.
  21. // If the context is canceled or times out, ctx.Err() will be returned.
  22. func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
  23. if client == nil {
  24. client = http.DefaultClient
  25. }
  26. // TODO(djd): Respect any existing value of req.Cancel.
  27. cancel := make(chan struct{})
  28. req.Cancel = cancel
  29. type responseAndError struct {
  30. resp *http.Response
  31. err error
  32. }
  33. result := make(chan responseAndError, 1)
  34. // Make local copies of test hooks closed over by goroutines below.
  35. // Prevents data races in tests.
  36. testHookDoReturned := testHookDoReturned
  37. testHookDidBodyClose := testHookDidBodyClose
  38. go func() {
  39. resp, err := client.Do(req)
  40. testHookDoReturned()
  41. result <- responseAndError{resp, err}
  42. }()
  43. var resp *http.Response
  44. select {
  45. case <-ctx.Done():
  46. testHookContextDoneBeforeHeaders()
  47. close(cancel)
  48. // Clean up after the goroutine calling client.Do:
  49. go func() {
  50. if r := <-result; r.resp != nil {
  51. testHookDidBodyClose()
  52. r.resp.Body.Close()
  53. }
  54. }()
  55. return nil, ctx.Err()
  56. case r := <-result:
  57. var err error
  58. resp, err = r.resp, r.err
  59. if err != nil {
  60. return resp, err
  61. }
  62. }
  63. c := make(chan struct{})
  64. go func() {
  65. select {
  66. case <-ctx.Done():
  67. close(cancel)
  68. case <-c:
  69. // The response's Body is closed.
  70. }
  71. }()
  72. resp.Body = &notifyingReader{resp.Body, c}
  73. return resp, nil
  74. }
  75. // Get issues a GET request via the Do function.
  76. func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
  77. req, err := http.NewRequest("GET", url, nil)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return Do(ctx, client, req)
  82. }
  83. // Head issues a HEAD request via the Do function.
  84. func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
  85. req, err := http.NewRequest("HEAD", url, nil)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return Do(ctx, client, req)
  90. }
  91. // Post issues a POST request via the Do function.
  92. func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) {
  93. req, err := http.NewRequest("POST", url, body)
  94. if err != nil {
  95. return nil, err
  96. }
  97. req.Header.Set("Content-Type", bodyType)
  98. return Do(ctx, client, req)
  99. }
  100. // PostForm issues a POST request via the Do function.
  101. func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error) {
  102. return Post(ctx, client, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
  103. }
  104. // notifyingReader is an io.ReadCloser that closes the notify channel after
  105. // Close is called or a Read fails on the underlying ReadCloser.
  106. type notifyingReader struct {
  107. io.ReadCloser
  108. notify chan<- struct{}
  109. }
  110. func (r *notifyingReader) Read(p []byte) (int, error) {
  111. n, err := r.ReadCloser.Read(p)
  112. if err != nil && r.notify != nil {
  113. close(r.notify)
  114. r.notify = nil
  115. }
  116. return n, err
  117. }
  118. func (r *notifyingReader) Close() error {
  119. err := r.ReadCloser.Close()
  120. if r.notify != nil {
  121. close(r.notify)
  122. r.notify = nil
  123. }
  124. return err
  125. }