fix(all): introduce and use iox.ReadAllContext (#379)

* fix(all): introduce and use iox.ReadAllContext

This improvement over the ioutil.ReadAll utility returns early
if the context expires. This enables us to unblock stuck code in
case there's censorship confounding the TCP stack.

See https://github.com/ooni/probe/issues/1417.

Compared to the functionality postulated in the above mentioned
issue, I choose to be more generic and separate limiting the
maximum body size (not implemented here) from using the context
to return early when reading a body (or any other reader).

After implementing iox.ReadAllContext, I made sure we always
use it everywhere in the tree instead of ioutil.ReadAll.

This includes many parts of the codebase where in theory we don't
need iox.ReadAllContext. Though, changing all the places makes
checking whether we're not using ioutil.ReadAll where we should
not be using it easy: `git grep` should return no lines.

* Update internal/iox/iox_test.go

* fix(ndt7): treat context errors as non-errors

The rationale is explained by the comment documenting reduceErr.

* Update internal/engine/experiment/ndt7/download.go
This commit is contained in:
Simone Basso
2021-06-15 11:57:40 +02:00
committed by GitHub
parent 576e035b20
commit 0fdc9cafb5
76 changed files with 440 additions and 223 deletions
+3 -2
View File
@@ -6,16 +6,17 @@
package badproxy
import (
"context"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"io"
"io/ioutil"
"net"
"strings"
"time"
"github.com/google/martian/v3/mitm"
"github.com/ooni/probe-cli/v3/internal/iox"
)
// CensoringProxy is a proxy that does not behave correctly.
@@ -56,7 +57,7 @@ func (p *CensoringProxy) serve(conn net.Conn) {
} else {
const maxread = 1 << 17
reader := io.LimitReader(conn, maxread)
ioutil.ReadAll(reader)
iox.ReadAllContext(context.Background(), reader)
}
conn.Close()
}
@@ -3,12 +3,12 @@ package httpproxy
import (
"bytes"
"context"
"io/ioutil"
"net"
"net/http"
"testing"
"github.com/ooni/probe-cli/v3/internal/cmd/jafar/uncensored"
"github.com/ooni/probe-cli/v3/internal/iox"
)
func TestPass(t *testing.T) {
@@ -89,7 +89,7 @@ func checkrequest(
t.Fatal("unexpected value of status code")
}
t.Log(resp)
values, _ := resp.Header["Via"]
values := resp.Header["Via"]
var foundProduct bool
for _, value := range values {
if value == product {
@@ -102,7 +102,7 @@ func checkrequest(
if !foundProduct && expectVia {
t.Fatal("Via header not found")
}
proxiedData, err := ioutil.ReadAll(resp.Body)
proxiedData, err := iox.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -120,7 +120,7 @@ func checkbody(t *testing.T, proxiedData []byte, host string) {
t.Fatal("unexpected status code")
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
data, err := iox.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -3,10 +3,11 @@ package uncensored
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/url"
"testing"
"github.com/ooni/probe-cli/v3/internal/iox"
)
func TestGood(t *testing.T) {
@@ -55,7 +56,7 @@ func TestGood(t *testing.T) {
if resp.StatusCode != 200 {
t.Fatal("invalid status-code")
}
data, err := ioutil.ReadAll(resp.Body)
data, err := iox.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}