You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

105 lines
2.5 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Package proxyutil provides functionality for building proxies.
  16. */
  17. package proxyutil
  18. import (
  19. "bytes"
  20. "fmt"
  21. "io"
  22. "io/ioutil"
  23. "net/http"
  24. "regexp"
  25. "strconv"
  26. "strings"
  27. "time"
  28. )
  29. // NewResponse builds new HTTP responses.
  30. // If body is nil, an empty byte.Buffer will be provided to be consistent with
  31. // the guarantees provided by http.Transport and http.Client.
  32. func NewResponse(code int, body io.Reader, req *http.Request) *http.Response {
  33. if body == nil {
  34. body = &bytes.Buffer{}
  35. }
  36. rc, ok := body.(io.ReadCloser)
  37. if !ok {
  38. rc = ioutil.NopCloser(body)
  39. }
  40. res := &http.Response{
  41. StatusCode: code,
  42. Status: fmt.Sprintf("%d %s", code, http.StatusText(code)),
  43. Proto: "HTTP/1.1",
  44. ProtoMajor: 1,
  45. ProtoMinor: 1,
  46. Header: http.Header{},
  47. Body: rc,
  48. Request: req,
  49. }
  50. if req != nil {
  51. res.Close = req.Close
  52. res.Proto = req.Proto
  53. res.ProtoMajor = req.ProtoMajor
  54. res.ProtoMinor = req.ProtoMinor
  55. }
  56. return res
  57. }
  58. // Warning adds an error to the Warning header in the format: 199 "martian"
  59. // "error message" "date".
  60. func Warning(header http.Header, err error) {
  61. date := header.Get("Date")
  62. if date == "" {
  63. date = time.Now().Format(http.TimeFormat)
  64. }
  65. w := fmt.Sprintf(`199 "martian" %q %q`, err.Error(), date)
  66. header.Add("Warning", w)
  67. }
  68. // GetRangeStart returns the byte index of the start of the range, if it has one.
  69. // Returns 0 if the range header is absent, and -1 if the range header is invalid or
  70. // has multi-part ranges.
  71. func GetRangeStart(res *http.Response) int64 {
  72. if res.StatusCode != http.StatusPartialContent {
  73. return 0
  74. }
  75. if strings.Contains(res.Header.Get("Content-Type"), "multipart/byteranges") {
  76. return -1
  77. }
  78. re := regexp.MustCompile(`bytes (\d+)-\d+/\d+`)
  79. matchSlice := re.FindStringSubmatch(res.Header.Get("Content-Range"))
  80. if len(matchSlice) < 2 {
  81. return -1
  82. }
  83. num, err := strconv.ParseInt(matchSlice[1], 10, 64)
  84. if err != nil {
  85. return -1
  86. }
  87. return num
  88. }