mirror of
https://github.com/golang/go
synced 2024-11-25 21:07:56 -07:00
add io.LimitReader.
use it to limit data read from http.Request Body R=r DELTA=32 (8 added, 6 deleted, 18 changed) OCL=33899 CL=33916
This commit is contained in:
parent
29415d0557
commit
90dcd5face
@ -142,6 +142,12 @@ func send(req *Request) (resp *Response, err os.Error) {
|
|||||||
r := io.Reader(reader);
|
r := io.Reader(reader);
|
||||||
if v := resp.GetHeader("Transfer-Encoding"); v == "chunked" {
|
if v := resp.GetHeader("Transfer-Encoding"); v == "chunked" {
|
||||||
r = newChunkedReader(reader);
|
r = newChunkedReader(reader);
|
||||||
|
} else if v := resp.GetHeader("Content-Length"); v != "" {
|
||||||
|
n, err := strconv.Atoi64(v);
|
||||||
|
if err != nil {
|
||||||
|
return nil, &badStringError{"invalid Content-Length", v};
|
||||||
|
}
|
||||||
|
r = io.LimitReader(r, n);
|
||||||
}
|
}
|
||||||
resp.Body = readClose{ r, conn };
|
resp.Body = readClose{ r, conn };
|
||||||
|
|
||||||
|
@ -214,29 +214,25 @@ func Copy(src Reader, dst Writer) (written int64, err os.Error) {
|
|||||||
return written, err
|
return written, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// A ByteReader satisfies Reads by consuming data from a slice of bytes.
|
// LimitReader returns a Reader that reads from r
|
||||||
// Clients can call NewByteReader to create one or wrap pointers
|
// but stops with os.EOF after n bytes.
|
||||||
// to their own slices: r := ByteReader{&data}.
|
func LimitReader(r Reader, n int64) Reader {
|
||||||
type ByteReader struct {
|
return &limitedReader{r, n};
|
||||||
Data *[]byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r ByteReader) Read(p []byte) (int, os.Error) {
|
type limitedReader struct {
|
||||||
n := len(p);
|
r Reader;
|
||||||
b := *r.Data;
|
n int64;
|
||||||
if len(b) == 0 {
|
}
|
||||||
|
|
||||||
|
func (l *limitedReader) Read(p []byte) (n int, err os.Error) {
|
||||||
|
if l.n <= 0 {
|
||||||
return 0, os.EOF;
|
return 0, os.EOF;
|
||||||
}
|
}
|
||||||
if n > len(b) {
|
if int64(len(p)) > l.n {
|
||||||
n = len(b);
|
p = p[0:l.n];
|
||||||
}
|
}
|
||||||
bytes.Copy(p, b[0:n]);
|
n, err = l.r.Read(p);
|
||||||
*r.Data = b[n:len(b)];
|
l.n -= int64(n);
|
||||||
return n, nil;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewByteReader returns a new ByteReader reading from data.
|
|
||||||
func NewByteReader(data []byte) ByteReader {
|
|
||||||
return ByteReader{ &data };
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user