Skip to content

Commit

Permalink
Next batch
Browse files Browse the repository at this point in the history
  • Loading branch information
buger committed Jul 17, 2015
1 parent 28540f4 commit 125d9db
Show file tree
Hide file tree
Showing 17 changed files with 82 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: go
language: go
go: 1.4.2
script: sudo -E bash -c "source /etc/profile && eval '$(gimme 1.4)' && export GOPATH=$HOME/gopath:$GOPATH && go get && GORACE='halt_on_error=1' go test -race -v"
script: sudo -E bash -c "source /etc/profile && eval '$(gimme 1.4.2)' && export GOPATH=$HOME/gopath:$GOPATH && go get && GORACE='halt_on_error=1' go test -v"
3 changes: 2 additions & 1 deletion emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"
)

// Start initialize loop for sending data from inputs to outputs
func Start(stop chan int) {
for _, in := range Plugins.Inputs {
go CopyMulty(in, Plugins.Outputs...)
Expand All @@ -19,7 +20,7 @@ func Start(stop chan int) {
}
}

// Copy from 1 reader to multiple writers
// CopyMulty copies from 1 reader to multiple writers
func CopyMulty(src io.Reader, writers ...io.Writer) (err error) {
buf := make([]byte, 5*1024*1024)
wIndex := 0
Expand Down
2 changes: 1 addition & 1 deletion http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) {

// 3xx requests
if status[0] == '3' {
c.redirectsCount += 1
c.redirectsCount++

location := proto.Header(payload, []byte("Location"))
redirectPayload := []byte("GET " + string(location) + " HTTP/1.1\r\n\r\n")
Expand Down
24 changes: 12 additions & 12 deletions http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestHTTPClientHTTPSSend(t *testing.T) {
func TestHTTPClientServerInstantDisconnect(t *testing.T) {
wg := new(sync.WaitGroup)

GET_payload := []byte("GET / HTTP/1.1\r\n\r\n")
GETPayload := []byte("GET / HTTP/1.1\r\n\r\n")

ln, _ := net.Listen("tcp", ":0")

Expand All @@ -148,16 +148,16 @@ func TestHTTPClientServerInstantDisconnect(t *testing.T) {
client := NewHTTPClient(ln.Addr().String(), &HTTPClientConfig{})

wg.Add(2)
client.Send(GET_payload)
client.Send(GET_payload)
client.Send(GETPayload)
client.Send(GETPayload)

wg.Wait()
}

func TestHTTPClientServerNoKeepAlive(t *testing.T) {
wg := new(sync.WaitGroup)

GET_payload := []byte("GET / HTTP/1.1\r\n\r\n")
GETPayload := []byte("GET / HTTP/1.1\r\n\r\n")

ln, _ := net.Listen("tcp", ":0")

Expand Down Expand Up @@ -186,16 +186,16 @@ func TestHTTPClientServerNoKeepAlive(t *testing.T) {
client := NewHTTPClient(ln.Addr().String(), &HTTPClientConfig{})

wg.Add(2)
client.Send(GET_payload)
client.Send(GET_payload)
client.Send(GETPayload)
client.Send(GETPayload)

wg.Wait()
}

func TestHTTPClientRedirect(t *testing.T) {
wg := new(sync.WaitGroup)

GET_payload := []byte("GET / HTTP/1.1\r\n\r\n")
GETPayload := []byte("GET / HTTP/1.1\r\n\r\n")

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Expand All @@ -210,15 +210,15 @@ func TestHTTPClientRedirect(t *testing.T) {

// Should do 2 queries
wg.Add(2)
client.Send(GET_payload)
client.Send(GETPayload)

wg.Wait()
}

func TestHTTPClientRedirectLimit(t *testing.T) {
wg := new(sync.WaitGroup)

GET_payload := []byte("GET / HTTP/1.1\r\n\r\n")
GETPayload := []byte("GET / HTTP/1.1\r\n\r\n")

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Expand All @@ -241,15 +241,15 @@ func TestHTTPClientRedirectLimit(t *testing.T) {

// Have 3 redirects + 1 GET, but should do only 2 redirects + GET
wg.Add(3)
client.Send(GET_payload)
client.Send(GETPayload)

wg.Wait()
}

func TestHTTPClientHandleHTTP10(t *testing.T) {
wg := new(sync.WaitGroup)

GET_payload := []byte("GET http://foobar.com/path HTTP/1.0\r\n\r\n")
GETPayload := []byte("GET http://foobar.com/path HTTP/1.0\r\n\r\n")

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Expand All @@ -263,7 +263,7 @@ func TestHTTPClientHandleHTTP10(t *testing.T) {
client := NewHTTPClient(server.URL, &HTTPClientConfig{Debug: true})

wg.Add(1)
client.Send(GET_payload)
client.Send(GETPayload)

wg.Wait()
}
6 changes: 4 additions & 2 deletions http_modifier_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
)

// HTTPModifierConfig holds configuration options for built-in traffic modifier
type HTTPModifierConfig struct {
urlNegativeRegexp HTTPUrlRegexp
urlRegexp HTTPUrlRegexp
Expand All @@ -29,6 +30,7 @@ type headerFilter struct {
regexp *regexp.Regexp
}

// HTTPHeaderFilters holds list of headers and their regexps
type HTTPHeaderFilters []headerFilter

func (h *HTTPHeaderFilters) String() string {
Expand Down Expand Up @@ -67,7 +69,7 @@ func (h *HTTPHashFilters) String() string {
func (h *HTTPHashFilters) Set(value string) error {
valArr := strings.SplitN(value, ":", 2)
if len(valArr) < 2 {
return errors.New("need both header and value, colon-delimited (ex. user_id:50%).")
return errors.New("need both header and value, colon-delimited (ex. user_id:50%)")
}

f := hashFilter{name: []byte(valArr[0])}
Expand Down Expand Up @@ -180,7 +182,7 @@ func (r *UrlRewriteMap) String() string {
func (r *UrlRewriteMap) Set(value string) error {
valArr := strings.SplitN(value, ":", 2)
if len(valArr) < 2 {
return errors.New("need both src and target, colon-delimited (ex. /a:/b).")
return errors.New("need both src and target, colon-delimited (ex. /a:/b)")
}
regexp, err := regexp.Compile(valArr[0])
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions http_modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestHTTPModifierHeaderFilters(t *testing.T) {
}

func TestHTTPModifierURLRewrite(t *testing.T) {
var url, new_url []byte
var url, newURL []byte

rewrites := UrlRewriteMap{}

Expand All @@ -58,13 +58,13 @@ func TestHTTPModifierURLRewrite(t *testing.T) {
})

url = []byte("/v1/user/joe/ping")
if new_url = proto.Path(modifier.Rewrite(payload(url))); bytes.Equal(new_url, url) {
t.Error("Request url should have been rewritten, wasn't", string(new_url))
if newURL = proto.Path(modifier.Rewrite(payload(url))); bytes.Equal(newURL, url) {
t.Error("Request url should have been rewritten, wasn't", string(newURL))
}

url = []byte("/v1/user/ping")
if new_url = proto.Path(modifier.Rewrite(payload(url))); !bytes.Equal(new_url, url) {
t.Error("Request url should have been rewritten, wasn't", string(new_url))
if newURL = proto.Path(modifier.Rewrite(payload(url))); !bytes.Equal(newURL, url) {
t.Error("Request url should have been rewritten, wasn't", string(newURL))
}
}

Expand Down Expand Up @@ -128,9 +128,9 @@ func TestHTTPModifierHeaders(t *testing.T) {
})

payload := []byte("POST /post HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")
new_payload := []byte("POST /post HTTP/1.1\r\nHeader1: 1\r\nContent-Length: 7\r\nHost: localhost\r\n\r\na=1&b=2")
newPayload := []byte("POST /post HTTP/1.1\r\nHeader1: 1\r\nContent-Length: 7\r\nHost: localhost\r\n\r\na=1&b=2")

if payload = modifier.Rewrite(payload); !bytes.Equal(payload, new_payload) {
if payload = modifier.Rewrite(payload); !bytes.Equal(payload, newPayload) {
t.Error("Should update request headers", string(payload))
}
}
Expand Down Expand Up @@ -196,9 +196,9 @@ func TestHTTPModifierSetHeader(t *testing.T) {
})

payload := []byte("POST /post HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")
payload_after := []byte("POST /post HTTP/1.1\r\nUser-Agent: Gor\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")
payloadAfter := []byte("POST /post HTTP/1.1\r\nUser-Agent: Gor\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")

if payload = modifier.Rewrite(payload); !bytes.Equal(payload_after, payload) {
if payload = modifier.Rewrite(payload); !bytes.Equal(payloadAfter, payload) {
t.Error("Should add new header", string(payload))
}
}
Expand All @@ -212,9 +212,9 @@ func TestHTTPModifierSetParam(t *testing.T) {
})

payload := []byte("POST /post?api_key=1234 HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")
payload_after := []byte("POST /post?api_key=1 HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")
payloadAfter := []byte("POST /post?api_key=1 HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")

if payload = modifier.Rewrite(payload); !bytes.Equal(payload_after, payload) {
if payload = modifier.Rewrite(payload); !bytes.Equal(payloadAfter, payload) {
t.Error("Should override param", string(payload))
}
}
2 changes: 2 additions & 0 deletions input_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"time"
)

// DummyInput used for debugging. It generate 1 "GET /"" request per second.
type DummyInput struct {
data chan []byte
}

// NewDummyInput constructor for DummyInput
func NewDummyInput(options string) (di *DummyInput) {
di = new(DummyInput)
di.data = make(chan []byte)
Expand Down
6 changes: 4 additions & 2 deletions input_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@ import (
"time"
)

// FileInput can read requests generated by FileOutput
type FileInput struct {
data chan []byte
path string
decoder *gob.Decoder
speedFactor float64
}

// NewFileInput constructor for FileInput. Accepts file path as argument.
func NewFileInput(path string) (i *FileInput) {
i = new(FileInput)
i.data = make(chan []byte)
i.path = path
i.speedFactor = 1
i.Init(path)
i.init(path)

go i.emit()

return
}

func (i *FileInput) Init(path string) {
func (i *FileInput) init(path string) {
file, err := os.Open(path)

if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions input_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"net/http/httputil"
)

// HTTPInput used for sending requests to Gor via http
type HTTPInput struct {
data chan []byte
address string
listener net.Listener
}

// NewHTTPInput constructor for HTTPInput. Accepts address with port which he will listen on.
func NewHTTPInput(address string) (i *HTTPInput) {
i = new(HTTPInput)
i.data = make(chan []byte)
Expand Down
2 changes: 2 additions & 0 deletions input_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"strings"
)

// RAWInput used for intercepting traffic for given address
type RAWInput struct {
data chan []byte
address string
}

// NewRAWInput constructor for RAWInput. Accepts address with port as argument.
func NewRAWInput(address string) (i *RAWInput) {
i = new(RAWInput)
i.data = make(chan []byte)
Expand Down
Loading

0 comments on commit 125d9db

Please sign in to comment.