-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
martian: fix Excess found: excess = 2 url = / (zero-length body) in H…
…EAD response This works around golang/go#62015 by manually writing response to HEAD requests. Fixes #357
- Loading branch information
Showing
2 changed files
with
90 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2023 Sauce Labs Inc. All rights reserved. | ||
// | ||
// Copyright 2015 Google Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package martian | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"golang.org/x/exp/maps" | ||
) | ||
|
||
// writeHeadResponse writes the status line and header of r to w. | ||
func writeHeadResponse(w io.Writer, res *http.Response) error { | ||
// Status line | ||
text := res.Status | ||
if text == "" { | ||
text = http.StatusText(res.StatusCode) | ||
if text == "" { | ||
text = "status code " + strconv.Itoa(res.StatusCode) | ||
} | ||
} else { | ||
// Just to reduce stutter, if user set res.Status to "200 OK" and StatusCode to 200. | ||
// Not important. | ||
text = strings.TrimPrefix(text, strconv.Itoa(res.StatusCode)+" ") | ||
} | ||
|
||
if _, err := fmt.Fprintf(w, "HTTP/%d.%d %03d %s\r\n", res.ProtoMajor, res.ProtoMinor, res.StatusCode, text); err != nil { | ||
return err | ||
} | ||
|
||
// Header | ||
if err := res.Header.Write(w); err != nil { | ||
return err | ||
} | ||
|
||
// Add Trailer header if needed | ||
if len(res.Trailer) > 0 { | ||
if _, err := io.WriteString(w, "Trailer: "); err != nil { | ||
return err | ||
} | ||
|
||
for i, k := range maps.Keys(res.Trailer) { | ||
if i > 0 { | ||
if _, err := io.WriteString(w, ", "); err != nil { | ||
return err | ||
} | ||
} | ||
if _, err := io.WriteString(w, k); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
// End-of-header | ||
if _, err := io.WriteString(w, "\r\n"); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters