Skip to content

Commit

Permalink
Do not set http header if it explicitly set
Browse files Browse the repository at this point in the history
  • Loading branch information
buger committed Jul 31, 2015
1 parent 27f83a6 commit a79e54c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ drace:
docker run -v `pwd`:$(SOURCE_PATH) -t -i --env GORACE="halt_on_error=1" gor go test ./... $(ARGS) -v -race -timeout 15s

dtest:
docker run -v `pwd`:$(SOURCE_PATH) -t -i gor go test ./... $(ARGS) -v -timeout 5s
docker run -v `pwd`:$(SOURCE_PATH) -t -i gor go test ./... $(ARGS) -v -timeout 10s

dcover:
docker run -v `pwd`:$(SOURCE_PATH) -t -i --env GORACE="halt_on_error=1" gor go test $(ARGS) -race -v -timeout 15s -coverprofile=coverage.out
Expand Down
5 changes: 4 additions & 1 deletion http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var defaultPorts = map[string]string{
type HTTPClientConfig struct {
FollowRedirects int
Debug bool
OriginalHost bool
}

type HTTPClient struct {
Expand Down Expand Up @@ -115,7 +116,9 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) {

c.conn.SetWriteDeadline(timeout)

data = proto.SetHost(data, []byte(c.baseURL), []byte(c.host))
if !c.config.OriginalHost {
data = proto.SetHost(data, []byte(c.baseURL), []byte(c.host))
}

if c.config.Debug {
Debug("[HTTPClient] Sending:", string(data))
Expand Down
1 change: 0 additions & 1 deletion output_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type FileOutput struct {
file *os.File
}


// NewFileOutput constructor for FileOutput, accepts path
func NewFileOutput(path string) io.Writer {
o := new(FileOutput)
Expand Down
3 changes: 3 additions & 0 deletions output_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type HTTPOutputConfig struct {

elasticSearch string

OriginalHost bool

Debug bool
}

Expand Down Expand Up @@ -94,6 +96,7 @@ func (o *HTTPOutput) startWorker() {
client := NewHTTPClient(o.address, &HTTPClientConfig{
FollowRedirects: o.config.redirectLimit,
Debug: o.config.Debug,
OriginalHost: o.config.OriginalHost,
})

deathCount := 0
Expand Down
34 changes: 34 additions & 0 deletions output_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,40 @@ func TestHTTPOutput(t *testing.T) {
Settings.modifierConfig = HTTPModifierConfig{}
}

func TestHTTPOutputKeepOriginalHost(t *testing.T) {
wg := new(sync.WaitGroup)
quit := make(chan int)

input := NewTestInput()

listener := startHTTP(func(req *http.Request) {
if req.Host != "custom-host.com" {
t.Error("Wrong header", req.Host)
}

wg.Done()
})

headers := HTTPHeaders{HTTPHeader{"Host", "custom-host.com"}}
Settings.modifierConfig = HTTPModifierConfig{headers: headers}

output := NewHTTPOutput(listener.Addr().String(), &HTTPOutputConfig{Debug: false, OriginalHost: true})

Plugins.Inputs = []io.Reader{input}
Plugins.Outputs = []io.Writer{output}

go Start(quit)

wg.Add(1)
input.EmitGET()

wg.Wait()

close(quit)

Settings.modifierConfig = HTTPModifierConfig{}
}

func TestOutputHTTPSSL(t *testing.T) {
wg := new(sync.WaitGroup)
quit := make(chan int)
Expand Down
9 changes: 9 additions & 0 deletions plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ func InitPlugins() {
registerPlugin(NewHTTPInput, options)
}

// If we explicitly set Host header http output should not rewrite it
// Fix: https://github.com/buger/gor/issues/174
for _, header := range Settings.modifierConfig.headers {
if header.Name == "Host" {
Settings.outputHTTPConfig.OriginalHost = true
break
}
}

for _, options := range Settings.outputHTTP {
registerPlugin(NewHTTPOutput, options, &Settings.outputHTTPConfig)
}
Expand Down
4 changes: 2 additions & 2 deletions raw_socket_listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ type Listener struct {

// Expect: 100-continue request is send in 2 tcp messages
// We store ACK aliases to merge this packets together
ackAliases map[uint32]uint32
ackAliases map[uint32]uint32
// To get ACK of second message we need to compute its Seq and wait for them message
seqWithData map[uint32]uint32

// Messages ready to be send to client
packetsChan chan *TCPPacket
packetsChan chan *TCPPacket

// Messages ready to be send to client
messagesChan chan *TCPMessage
Expand Down

0 comments on commit a79e54c

Please sign in to comment.