|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Moriyoshi Koizumi |
| 3 | + * Copyright (c) 2012 Elazar Leibovich. |
| 4 | + * Copyright (c) 2012 The Go Authors. |
| 5 | + * |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are |
| 10 | + * met: |
| 11 | + * |
| 12 | + * * Redistributions of source code must retain the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer. |
| 14 | + * * Redistributions in binary form must reproduce the above |
| 15 | + * copyright notice, this list of conditions and the following disclaimer |
| 16 | + * in the documentation and/or other materials provided with the |
| 17 | + * distribution. |
| 18 | + * * Neither the name of Elazar Leibovich. nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived from |
| 20 | + * this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | + */ |
| 34 | +package main |
| 35 | + |
| 36 | +import ( |
| 37 | + "bytes" |
| 38 | + "fmt" |
| 39 | + "io/ioutil" |
| 40 | + "net/http" |
| 41 | + "strconv" |
| 42 | + "strings" |
| 43 | + |
| 44 | + "github.com/sirupsen/logrus" |
| 45 | +) |
| 46 | + |
| 47 | +var redirStatusCodes = map[int]string{ |
| 48 | + 301: "Moved Permanently", |
| 49 | + 302: "Found", |
| 50 | + 303: "See Other", |
| 51 | + 307: "Temporary Redirect", |
| 52 | + 308: "Permanent edirect", |
| 53 | +} |
| 54 | + |
| 55 | +type redirector struct { |
| 56 | + Logger *logrus.Logger |
| 57 | +} |
| 58 | + |
| 59 | +var emptyReadCloser = ioutil.NopCloser(&bytes.Reader{}) |
| 60 | + |
| 61 | +func (redir *redirector) RoundTrip(req *http.Request) (*http.Response, error) { |
| 62 | + redirStatusCode := 302 |
| 63 | + statusText := "" |
| 64 | + var location string |
| 65 | + |
| 66 | + if req.URL.Host == "" { |
| 67 | + components := strings.Split(req.URL.Opaque, ":") |
| 68 | + if len(components) < 1 { |
| 69 | + return nil, fmt.Errorf("invalid URL: %v", req.URL) |
| 70 | + } |
| 71 | + if components[0] != "http" && components[0] != "https" { |
| 72 | + var err error |
| 73 | + redirStatusCode, err = strconv.Atoi(components[0]) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("invalid URL: %v", req.URL) |
| 76 | + } |
| 77 | + var ok bool |
| 78 | + statusText, ok = redirStatusCodes[redirStatusCode] |
| 79 | + if !ok { |
| 80 | + return nil, fmt.Errorf("invalid status code %d in URL %v", redirStatusCode, req.URL) |
| 81 | + } |
| 82 | + components = components[1:] |
| 83 | + } else { |
| 84 | + redirStatusCode = 302 |
| 85 | + } |
| 86 | + if len(components) < 1 { |
| 87 | + return nil, fmt.Errorf("invalid URL: %v", req.URL) |
| 88 | + } |
| 89 | + location = strings.Join(components, ":") |
| 90 | + if req.URL.RawQuery != "" { |
| 91 | + location += "&" + req.URL.RawQuery |
| 92 | + } |
| 93 | + if req.URL.Fragment != "" { |
| 94 | + location += "#" + req.URL.Fragment |
| 95 | + } |
| 96 | + } else { |
| 97 | + proxyCtx := req.Context().Value(proxyContextKey).(*OurProxyCtx) |
| 98 | + redirStatusCode = 302 |
| 99 | + _location := req.URL |
| 100 | + _location.Scheme = proxyCtx.OrigReq.URL.Scheme |
| 101 | + location = _location.String() |
| 102 | + } |
| 103 | + if statusText == "" { |
| 104 | + statusText = redirStatusCodes[redirStatusCode] |
| 105 | + } |
| 106 | + return &http.Response{ |
| 107 | + Status: fmt.Sprintf("%d %s", redirStatusCode, statusText), |
| 108 | + StatusCode: redirStatusCode, |
| 109 | + Proto: req.Proto, |
| 110 | + ProtoMajor: req.ProtoMajor, |
| 111 | + ProtoMinor: req.ProtoMinor, |
| 112 | + Header: http.Header{ |
| 113 | + "Location": []string{location}, |
| 114 | + }, |
| 115 | + Trailer: http.Header{}, |
| 116 | + ContentLength: 0, |
| 117 | + Body: emptyReadCloser, |
| 118 | + Close: true, |
| 119 | + Request: req, |
| 120 | + TLS: req.TLS, |
| 121 | + }, nil |
| 122 | +} |
0 commit comments