|
| 1 | +// Copyright 2023 Sauce Labs Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Copyright 2015 Google Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package martian |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "net/http" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "golang.org/x/exp/maps" |
| 27 | +) |
| 28 | + |
| 29 | +// writeHeadResponse writes the status line and header of r to w. |
| 30 | +func writeHeadResponse(w io.Writer, res *http.Response) error { |
| 31 | + // Status line |
| 32 | + text := res.Status |
| 33 | + if text == "" { |
| 34 | + text = http.StatusText(res.StatusCode) |
| 35 | + if text == "" { |
| 36 | + text = "status code " + strconv.Itoa(res.StatusCode) |
| 37 | + } |
| 38 | + } else { |
| 39 | + // Just to reduce stutter, if user set res.Status to "200 OK" and StatusCode to 200. |
| 40 | + // Not important. |
| 41 | + text = strings.TrimPrefix(text, strconv.Itoa(res.StatusCode)+" ") |
| 42 | + } |
| 43 | + |
| 44 | + if _, err := fmt.Fprintf(w, "HTTP/%d.%d %03d %s\r\n", res.ProtoMajor, res.ProtoMinor, res.StatusCode, text); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + // Header |
| 49 | + if err := res.Header.Write(w); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + // Add Trailer header if needed |
| 54 | + if len(res.Trailer) > 0 { |
| 55 | + if _, err := io.WriteString(w, "Trailer: "); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + for i, k := range maps.Keys(res.Trailer) { |
| 60 | + if i > 0 { |
| 61 | + if _, err := io.WriteString(w, ", "); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + } |
| 65 | + if _, err := io.WriteString(w, k); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // End-of-header |
| 72 | + if _, err := io.WriteString(w, "\r\n"); err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
0 commit comments