Implement limitedRead function for client side

MaxBytesReader is only documented for server side reads, so we're using
a local limitedRead function instead that uses an io.LimitedReader.

Declared limits in a commented constant
This commit is contained in:
Cecylia Bocovich 2019-05-10 17:16:35 -04:00
parent ce3101d016
commit 1d76d3ca2e
3 changed files with 44 additions and 5 deletions

View file

@ -11,6 +11,7 @@ package lib
import (
"bytes"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
@ -23,6 +24,7 @@ const (
BrokerError503 string = "No snowflake proxies currently available."
BrokerError400 string = "You sent an invalid offer in the request."
BrokerErrorUnexpected string = "Unexpected error, no answer."
readLimit = 100000 //Maximum number of bytes to be read from an HTTP response
)
// Signalling Channel to the Broker.
@ -64,6 +66,23 @@ func NewBrokerChannel(broker string, front string, transport http.RoundTripper)
return bc
}
func limitedRead(r io.Reader, limit int64) ([]byte, error) {
p, err := ioutil.ReadAll(&io.LimitedReader{r, limit})
if err != nil {
return p, err
}
//Check to see if limit was exceeded
var tmp [1]byte
_, err = io.ReadFull(r, tmp[:])
if err == io.EOF {
err = nil
} else if err == nil {
err = io.ErrUnexpectedEOF
}
return p, err
}
// Roundtrip HTTP POST using WebRTC SessionDescriptions.
//
// Send an SDP offer to the broker, which assigns a proxy and responds
@ -91,7 +110,7 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
switch resp.StatusCode {
case http.StatusOK:
body, err := ioutil.ReadAll(http.MaxBytesReader(nil, resp.Body, 100000))
body, err := limitedRead(resp.Body, readLimit)
if nil != err {
return nil, err
}