ctxhttp.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // Package ctxhttp provides helper functions for performing context-aware HTTP requests.
  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. // Do sends an HTTP request with the provided http.Client and returns an HTTP response.
  14. // If the client is nil, http.DefaultClient is used.
  15. // If the context is canceled or times out, ctx.Err() will be returned.
  16. func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
  17. if client == nil {
  18. client = http.DefaultClient
  19. }
  20. // Request cancelation changed in Go 1.5, see cancelreq.go and cancelreq_go14.go.
  21. cancel := canceler(client, req)
  22. type responseAndError struct {
  23. resp *http.Response
  24. err error
  25. }
  26. result := make(chan responseAndError, 1)
  27. go func() {
  28. resp, err := client.Do(req)
  29. result <- responseAndError{resp, err}
  30. }()
  31. var resp *http.Response
  32. select {
  33. case <-ctx.Done():
  34. cancel()
  35. return nil, ctx.Err()
  36. case r := <-result:
  37. var err error
  38. resp, err = r.resp, r.err
  39. if err != nil {
  40. return resp, err
  41. }
  42. }
  43. c := make(chan struct{})
  44. go func() {
  45. select {
  46. case <-ctx.Done():
  47. cancel()
  48. case <-c:
  49. // The response's Body is closed.
  50. }
  51. }()
  52. resp.Body = &notifyingReader{resp.Body, c}
  53. return resp, nil
  54. }
  55. // Get issues a GET request via the Do function.
  56. func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
  57. req, err := http.NewRequest("GET", url, nil)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return Do(ctx, client, req)
  62. }
  63. // Head issues a HEAD request via the Do function.
  64. func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
  65. req, err := http.NewRequest("HEAD", url, nil)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return Do(ctx, client, req)
  70. }
  71. // Post issues a POST request via the Do function.
  72. func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) {
  73. req, err := http.NewRequest("POST", url, body)
  74. if err != nil {
  75. return nil, err
  76. }
  77. req.Header.Set("Content-Type", bodyType)
  78. return Do(ctx, client, req)
  79. }
  80. // PostForm issues a POST request via the Do function.
  81. func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error) {
  82. return Post(ctx, client, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
  83. }
  84. // notifyingReader is an io.ReadCloser that closes the notify channel after
  85. // Close is called or a Read fails on the underlying ReadCloser.
  86. type notifyingReader struct {
  87. io.ReadCloser
  88. notify chan<- struct{}
  89. }
  90. func (r *notifyingReader) Read(p []byte) (int, error) {
  91. n, err := r.ReadCloser.Read(p)
  92. if err != nil && r.notify != nil {
  93. close(r.notify)
  94. r.notify = nil
  95. }
  96. return n, err
  97. }
  98. func (r *notifyingReader) Close() error {
  99. err := r.ReadCloser.Close()
  100. if r.notify != nil {
  101. close(r.notify)
  102. r.notify = nil
  103. }
  104. return err
  105. }