refactor: change deprecated "io/ioutil" package to recommended "io" package

This commit is contained in:
am3o 2024-02-17 12:47:22 +01:00
parent 35984c0876
commit acce1f1fd9
No known key found for this signature in database
GPG key ID: F76214F50EEA64B5
14 changed files with 25 additions and 39 deletions

View file

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -94,7 +93,7 @@ func debugHandler(i *IPC, w http.ResponseWriter, r *http.Request) {
For snowflake proxies to request a client from the Broker. For snowflake proxies to request a client from the Broker.
*/ */
func proxyPolls(i *IPC, w http.ResponseWriter, r *http.Request) { func proxyPolls(i *IPC, w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, readLimit)) body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, readLimit))
if err != nil { if err != nil {
log.Println("Invalid data.", err.Error()) log.Println("Invalid data.", err.Error())
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -132,7 +131,7 @@ snowflake proxy, which responds with the SDP answer to be sent in
the HTTP response back to the client. the HTTP response back to the client.
*/ */
func clientOffers(i *IPC, w http.ResponseWriter, r *http.Request) { func clientOffers(i *IPC, w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, readLimit)) body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, readLimit))
if err != nil { if err != nil {
log.Printf("Error reading client request: %s", err.Error()) log.Printf("Error reading client request: %s", err.Error())
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -212,7 +211,7 @@ an offer from proxyHandler to respond with an answer in an HTTP POST,
which the broker will pass back to the original client. which the broker will pass back to the original client.
*/ */
func proxyAnswers(i *IPC, w http.ResponseWriter, r *http.Request) { func proxyAnswers(i *IPC, w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, readLimit)) body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, readLimit))
if err != nil { if err != nil {
log.Println("Invalid data.", err.Error()) log.Println("Invalid data.", err.Error())
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)

View file

@ -6,7 +6,6 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -22,7 +21,7 @@ import (
func NullLogger() *log.Logger { func NullLogger() *log.Logger {
logger := log.New(os.Stdout, "", 0) logger := log.New(os.Stdout, "", 0)
logger.SetOutput(ioutil.Discard) logger.SetOutput(io.Discard)
return logger return logger
} }
@ -78,7 +77,7 @@ func decodeAMPArmorToString(r io.Reader) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
p, err := ioutil.ReadAll(dec) p, err := io.ReadAll(dec)
return string(p), err return string(p), err
} }
@ -460,7 +459,7 @@ client-sqs-count 0
So(err, ShouldBeNil) So(err, ShouldBeNil)
proxyAnswers(i, w, r) proxyAnswers(i, w, r)
So(w.Code, ShouldEqual, http.StatusOK) So(w.Code, ShouldEqual, http.StatusOK)
b, err := ioutil.ReadAll(w.Body) b, err := io.ReadAll(w.Body)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(b, ShouldResemble, []byte(`{"Status":"client gone"}`)) So(b, ShouldResemble, []byte(`{"Status":"client gone"}`))
}) })

View file

@ -3,7 +3,6 @@ package snowflake_client
import ( import (
"errors" "errors"
"io" "io"
"io/ioutil"
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
@ -115,7 +114,7 @@ func (r *ampCacheRendezvous) Exchange(encPollReq []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
encPollResp, err := ioutil.ReadAll(dec) encPollResp, err := io.ReadAll(dec)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"io" "io"
"io/ioutil"
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
@ -71,7 +70,7 @@ func (r *httpRendezvous) Exchange(encPollReq []byte) ([]byte, error) {
} }
func limitedRead(r io.Reader, limit int64) ([]byte, error) { func limitedRead(r io.Reader, limit int64) ([]byte, error) {
p, err := ioutil.ReadAll(&io.LimitedReader{R: r, N: limit + 1}) p, err := io.ReadAll(&io.LimitedReader{R: r, N: limit + 1})
if err != nil { if err != nil {
return p, err return p, err
} else if int64(len(p)) == limit+1 { } else if int64(len(p)) == limit+1 {

View file

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"testing" "testing"
@ -32,7 +31,7 @@ func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return &http.Response{ return &http.Response{
Status: fmt.Sprintf("%d %s", t.statusCode, http.StatusText(t.statusCode)), Status: fmt.Sprintf("%d %s", t.statusCode, http.StatusText(t.statusCode)),
StatusCode: t.statusCode, StatusCode: t.statusCode,
Body: ioutil.NopCloser(bytes.NewReader(t.body)), Body: io.NopCloser(bytes.NewReader(t.body)),
}, nil }, nil
} }

View file

@ -5,7 +5,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net" "net"
"os" "os"
@ -198,7 +197,7 @@ func main() {
// buffer is full. // buffer is full.
// https://bugs.torproject.org/26360 // https://bugs.torproject.org/26360
// https://bugs.torproject.org/25600#comment:14 // https://bugs.torproject.org/25600#comment:14
var logOutput = ioutil.Discard var logOutput = io.Discard
if *logFilename != "" { if *logFilename != "" {
if *logToStateDir || *oldLogToStateDir { if *logToStateDir || *oldLogToStateDir {
stateDir, err := pt.MakeStateDir() stateDir, err := pt.MakeStateDir()
@ -298,8 +297,8 @@ func main() {
// This environment variable means we should treat EOF on stdin // This environment variable means we should treat EOF on stdin
// just like SIGTERM: https://bugs.torproject.org/15435. // just like SIGTERM: https://bugs.torproject.org/15435.
go func() { go func() {
if _, err := io.Copy(ioutil.Discard, os.Stdin); err != nil { if _, err := io.Copy(io.Discard, os.Stdin); err != nil {
log.Printf("calling io.Copy(ioutil.Discard, os.Stdin) returned error: %v", err) log.Printf("calling io.Copy(io.Discard, os.Stdin) returned error: %v", err)
} }
log.Printf("synthesizing SIGTERM because of stdin close") log.Printf("synthesizing SIGTERM because of stdin close")
sigChan <- syscall.SIGTERM sigChan <- syscall.SIGTERM

View file

@ -2,7 +2,6 @@ package amp
import ( import (
"io" "io"
"io/ioutil"
"math/rand" "math/rand"
"strings" "strings"
"testing" "testing"
@ -13,7 +12,7 @@ func armorDecodeToString(src string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
p, err := ioutil.ReadAll(dec) p, err := io.ReadAll(dec)
return string(p), err return string(p), err
} }

View file

@ -43,7 +43,6 @@ package encapsulation
import ( import (
"errors" "errors"
"io" "io"
"io/ioutil"
) )
// ErrTooLong is the error returned when an encoded length prefix is longer than // ErrTooLong is the error returned when an encoded length prefix is longer than
@ -93,7 +92,7 @@ func ReadData(r io.Reader, p []byte) (int, error) {
// If the caller's buffer was too short, discard // If the caller's buffer was too short, discard
// the rest of the data and return // the rest of the data and return
// io.ErrShortBuffer. // io.ErrShortBuffer.
_, err = io.CopyN(ioutil.Discard, r, int64(n-numData)) _, err = io.CopyN(io.Discard, r, int64(n-numData))
if err == nil { if err == nil {
err = io.ErrShortBuffer err = io.ErrShortBuffer
} }
@ -103,7 +102,7 @@ func ReadData(r io.Reader, p []byte) (int, error) {
} }
return numData, err return numData, err
} else if n > 0 { } else if n > 0 {
_, err := io.CopyN(ioutil.Discard, r, int64(n)) _, err := io.CopyN(io.Discard, r, int64(n))
if err == io.EOF { if err == io.EOF {
err = io.ErrUnexpectedEOF err = io.ErrUnexpectedEOF
} }

View file

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -87,7 +86,7 @@ func TestWrite(t *testing.T) {
// goroutine, reading from the Conn s and writing to the dataCh // goroutine, reading from the Conn s and writing to the dataCh
// and errCh channels, whose ultimate effect in the select loop // and errCh channels, whose ultimate effect in the select loop
// below is like // below is like
// data, err := ioutil.ReadAll(s) // data, err := io.ReadAll(s)
dataCh := make(chan []byte) dataCh := make(chan []byte)
errCh := make(chan error) errCh := make(chan error)
go func() { go func() {
@ -162,7 +161,7 @@ func TestConcurrentRead(t *testing.T) {
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
go func() { go func() {
defer wg.Done() defer wg.Done()
_, err := io.Copy(ioutil.Discard, s) _, err := io.Copy(io.Discard, s)
if err != nil { if err != nil {
errCh <- err errCh <- err
} }
@ -224,7 +223,7 @@ func TestConcurrentWrite(t *testing.T) {
}() }()
// Read from the other end. // Read from the other end.
_, err = io.Copy(ioutil.Discard, c) _, err = io.Copy(io.Discard, c)
c.Close() c.Close()
if err != nil { if err != nil {
t.Fatalf("Read: %v", err) t.Fatalf("Read: %v", err)
@ -336,7 +335,7 @@ func BenchmarkUpgradeBufferSize(b *testing.B) {
func BenchmarkReadWrite(b *testing.B) { func BenchmarkReadWrite(b *testing.B) {
trial := func(b *testing.B, readConn, writeConn *Conn, msgSize int) { trial := func(b *testing.B, readConn, writeConn *Conn, msgSize int) {
go func() { go func() {
io.Copy(ioutil.Discard, readConn) io.Copy(io.Discard, readConn)
}() }()
data := make([]byte, msgSize) data := make([]byte, msgSize)
b.ResetTimer() b.ResetTimer()

View file

@ -13,7 +13,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -110,7 +109,7 @@ func makePeerConnectionFromOffer(stunURL string, sdp *webrtc.SessionDescription,
func probeHandler(stunURL string, w http.ResponseWriter, r *http.Request) { func probeHandler(stunURL string, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Origin", "*")
resp, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, readLimit)) resp, err := io.ReadAll(http.MaxBytesReader(w, r.Body, readLimit))
if nil != err { if nil != err {
log.Println("Invalid data.") log.Println("Invalid data.")
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)

View file

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
@ -25,7 +24,7 @@ type MockTransport struct {
// Just returns a response with fake SDP answer. // Just returns a response with fake SDP answer.
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
s := ioutil.NopCloser(bytes.NewReader(m.body)) s := io.NopCloser(bytes.NewReader(m.body))
r := &http.Response{ r := &http.Response{
StatusCode: m.statusOverride, StatusCode: m.statusOverride,
Body: s, Body: s,

View file

@ -31,7 +31,6 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -163,7 +162,7 @@ func genSessionID() string {
} }
func limitedRead(r io.Reader, limit int64) ([]byte, error) { func limitedRead(r io.Reader, limit int64) ([]byte, error) {
p, err := ioutil.ReadAll(&io.LimitedReader{R: r, N: limit + 1}) p, err := io.ReadAll(&io.LimitedReader{R: r, N: limit + 1})
if err != nil { if err != nil {
return p, err return p, err
} else if int64(len(p)) == limit+1 { } else if int64(len(p)) == limit+1 {

View file

@ -4,7 +4,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net" "net"
"os" "os"
@ -102,7 +101,7 @@ func main() {
SummaryInterval: *summaryInterval, SummaryInterval: *summaryInterval,
} }
var logOutput = ioutil.Discard var logOutput = io.Discard
var eventlogOutput io.Writer = os.Stderr var eventlogOutput io.Writer = os.Stderr
log.SetFlags(log.LstdFlags | log.LUTC) log.SetFlags(log.LstdFlags | log.LUTC)

View file

@ -7,7 +7,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -320,8 +319,8 @@ func main() {
// This environment variable means we should treat EOF on stdin // This environment variable means we should treat EOF on stdin
// just like SIGTERM: https://bugs.torproject.org/15435. // just like SIGTERM: https://bugs.torproject.org/15435.
go func() { go func() {
if _, err := io.Copy(ioutil.Discard, os.Stdin); err != nil { if _, err := io.Copy(io.Discard, os.Stdin); err != nil {
log.Printf("error copying os.Stdin to ioutil.Discard: %v", err) log.Printf("error copying os.Stdin to io.Discard: %v", err)
} }
log.Printf("synthesizing SIGTERM because of stdin close") log.Printf("synthesizing SIGTERM because of stdin close")
sigChan <- syscall.SIGTERM sigChan <- syscall.SIGTERM