Skip to content

Commit

Permalink
trying chunking with a bufio.scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Jul 14, 2015
1 parent b47ddb0 commit d04ca3f
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 247 deletions.
125 changes: 0 additions & 125 deletions proxy/chunked.go

This file was deleted.

119 changes: 0 additions & 119 deletions proxy/chunked_test.go

This file was deleted.

63 changes: 60 additions & 3 deletions proxy/proxy_intercept.go
Expand Up @@ -2,17 +2,31 @@ package proxy

import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"strconv"

"github.com/fsouza/go-dockerclient"
. "github.com/weaveworks/weave/common"
)

const (
maxLineLength = 4096 // assumed <= bufio.defaultBufSize
maxChunkSize = bufio.MaxScanTokenSize
)

var (
ErrChunkTooLong = errors.New("chunk too long")
ErrInvalidChunkLength = errors.New("invalid byte in chunk length")
ErrLineTooLong = errors.New("header line too long")
ErrMalformedChunkEncoding = errors.New("malformed chunked encoding")
)

func (proxy *Proxy) Intercept(i interceptor, w http.ResponseWriter, r *http.Request) {
if err := i.InterceptRequest(r); err != nil {
switch err.(type) {
Expand Down Expand Up @@ -133,9 +147,10 @@ func doChunkedResponse(w http.ResponseWriter, resp *http.Response, client *httpu
defer up.Close()

var err error
chunks := NewChunkedReader(io.MultiReader(remaining, up))
for chunks.Next() && err == nil {
_, err = io.Copy(wf, chunks.Chunk())
chunks := bufio.NewScanner(io.MultiReader(remaining, up))
chunks.Split(splitChunks)
for chunks.Scan() && err == nil {
_, err = wf.Write(chunks.Bytes())
wf.Flush()
}
if err == nil {
Expand All @@ -146,6 +161,48 @@ func doChunkedResponse(w http.ResponseWriter, resp *http.Response, client *httpu
}
}

// a bufio.SplitFunc for http chunks
func splitChunks(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}

i := bytes.IndexByte(data, '\n')
if i < 0 {
return 0, nil, nil
}
if i > maxLineLength {
return 0, nil, ErrLineTooLong
}

chunkSize64, err := strconv.ParseInt(
string(bytes.TrimRight(data[:i], " \t\r\n")),
16,
64,
)
switch {
case err != nil:
return 0, nil, ErrInvalidChunkLength
case chunkSize64 > maxChunkSize:
return 0, nil, ErrChunkTooLong
case chunkSize64 == 0:
return 0, nil, io.EOF
}
chunkSize := int(chunkSize64)

data = data[i+1:]

if len(data) < chunkSize+2 {
return 0, nil, nil
}

if data[chunkSize] != '\r' || data[chunkSize+1] != '\n' {
return 0, nil, ErrMalformedChunkEncoding
}

return i + chunkSize + 3, data[:chunkSize], nil
}

func hijack(w http.ResponseWriter, client *httputil.ClientConn) (down net.Conn, downBuf *bufio.ReadWriter, up net.Conn, remaining io.Reader, err error) {
hj, ok := w.(http.Hijacker)
if !ok {
Expand Down

0 comments on commit d04ca3f

Please sign in to comment.