Skip to content

Commit

Permalink
feat: use go1.20.4 (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone committed May 29, 2023
2 parents 628246d + 29a36ef commit 4a8e776
Show file tree
Hide file tree
Showing 43 changed files with 3,647 additions and 2,348 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Expand Up @@ -11,12 +11,12 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: ">=1.20.4"

- name: Build
run: go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion UPSTREAM
@@ -1 +1 @@
go1.19.6
go1.20.4
6 changes: 4 additions & 2 deletions cgi/child.go
Expand Up @@ -83,10 +83,12 @@ func RequestFromMap(params map[string]string) (*http.Request, error) {

// Copy "HTTP_FOO_BAR" variables to "Foo-Bar" Headers
for k, v := range params {
if !strings.HasPrefix(k, "HTTP_") || k == "HTTP_HOST" {
if k == "HTTP_HOST" {
continue
}
r.Header.Add(strings.ReplaceAll(k[5:], "_", "-"), v)
if after, found := strings.CutPrefix(k, "HTTP_"); found {
r.Header.Add(strings.ReplaceAll(after, "_", "-"), v)
}
}

uriStr := params["REQUEST_URI"]
Expand Down
7 changes: 6 additions & 1 deletion cgi/host.go
Expand Up @@ -138,7 +138,6 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

env := []string{
"SERVER_SOFTWARE=go",
"SERVER_NAME=" + req.Host,
"SERVER_PROTOCOL=HTTP/1.1",
"HTTP_HOST=" + req.Host,
"GATEWAY_INTERFACE=CGI/1.1",
Expand All @@ -158,6 +157,12 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
env = append(env, "REMOTE_ADDR="+req.RemoteAddr, "REMOTE_HOST="+req.RemoteAddr)
}

if hostDomain, _, err := net.SplitHostPort(req.Host); err == nil {
env = append(env, "SERVER_NAME="+hostDomain)
} else {
env = append(env, "SERVER_NAME="+req.Host)
}

if req.TLS != nil {
env = append(env, "HTTPS=on")
}
Expand Down
7 changes: 3 additions & 4 deletions cgi/host_test.go
Expand Up @@ -8,7 +8,6 @@ package cgi

import (
"bufio"
"bytes"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -115,7 +114,7 @@ func TestCGIBasicGet(t *testing.T) {
"param-a": "b",
"param-foo": "bar",
"env-GATEWAY_INTERFACE": "CGI/1.1",
"env-HTTP_HOST": "example.com",
"env-HTTP_HOST": "example.com:80",
"env-PATH_INFO": "",
"env-QUERY_STRING": "foo=bar&a=b",
"env-REMOTE_ADDR": "1.2.3.4",
Expand All @@ -129,7 +128,7 @@ func TestCGIBasicGet(t *testing.T) {
"env-SERVER_PORT": "80",
"env-SERVER_SOFTWARE": "go",
}
replay := runCgiTest(t, h, "GET /test.cgi?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
replay := runCgiTest(t, h, "GET /test.cgi?foo=bar&a=b HTTP/1.0\nHost: example.com:80\n\n", expectedMap)

if expected, got := "text/html", replay.Header().Get("Content-Type"); got != expected {
t.Errorf("got a Content-Type of %q; expected %q", got, expected)
Expand Down Expand Up @@ -541,7 +540,7 @@ func TestEnvOverride(t *testing.T) {

func TestHandlerStderr(t *testing.T) {
check(t)
var stderr bytes.Buffer
var stderr strings.Builder
h := &Handler{
Path: "testdata/test.cgi",
Root: "/test.cgi",
Expand Down
11 changes: 6 additions & 5 deletions client.go
Expand Up @@ -22,6 +22,7 @@ import (
"sort"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/ooni/oohttp/internal/ascii"
Expand Down Expand Up @@ -362,7 +363,7 @@ func setRequestCancel(req *Request, rt RoundTripper, deadline time.Time) (stopTi
initialReqCancel := req.Cancel // the user's original Request.Cancel, if any

var cancelCtx func()
if oldCtx := req.Context(); timeBeforeContextDeadline(deadline, oldCtx) {
if timeBeforeContextDeadline(deadline, oldCtx) {
req.ctx, cancelCtx = context.WithDeadline(oldCtx, deadline)
}

Expand Down Expand Up @@ -392,22 +393,22 @@ func setRequestCancel(req *Request, rt RoundTripper, deadline time.Time) (stopTi
}

timer := time.NewTimer(time.Until(deadline))
var timedOut atomicBool
var timedOut atomic.Bool

go func() {
select {
case <-initialReqCancel:
doCancel()
timer.Stop()
case <-timer.C:
timedOut.setTrue()
timedOut.Store(true)
doCancel()
case <-stopTimerCh:
timer.Stop()
}
}()

return stopTimer, timedOut.isSet
return stopTimer, timedOut.Load
}

// See 2 (end of page 4) https://www.ietf.org/rfc/rfc2617.txt
Expand Down Expand Up @@ -499,7 +500,7 @@ func (c *Client) checkRedirect(req *Request, via []*Request) error {
}

// redirectBehavior describes what should happen when the
// client encounters a 3xx status code from the server
// client encounters a 3xx status code from the server.
func redirectBehavior(reqMethod string, resp *Response, ireq *Request) (redirectMethod string, shouldRedirect, includeBody bool) {
switch resp.StatusCode {
case 301, 302, 303:
Expand Down

0 comments on commit 4a8e776

Please sign in to comment.