forked from juneym/gor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"log" | ||
"net/http/httputil" | ||
) | ||
|
||
type HTTPInput struct { | ||
data chan []byte | ||
address string | ||
listener net.Listener | ||
} | ||
|
||
func NewHTTPInput(address string) (i *HTTPInput) { | ||
i = new(HTTPInput) | ||
i.data = make(chan []byte) | ||
i.address = address | ||
|
||
i.listen(address) | ||
|
||
return | ||
} | ||
|
||
func (i *HTTPInput) Read(data []byte) (int, error) { | ||
buf := <-i.data | ||
copy(data, buf) | ||
|
||
return len(buf), nil | ||
} | ||
|
||
func (i *HTTPInput) handler(w http.ResponseWriter, r *http.Request) { | ||
buf, _ := httputil.DumpRequest(r, true) | ||
|
||
i.data <- buf | ||
|
||
http.Error(w, http.StatusText(200), 200) | ||
} | ||
|
||
func (i *HTTPInput) listen(address string) { | ||
var err error | ||
|
||
mux := http.NewServeMux() | ||
|
||
mux.HandleFunc("/", i.handler) | ||
|
||
i.listener, err = net.Listen("tcp", address) | ||
if err != nil { | ||
log.Fatal("HTTP input listener failure:", err) | ||
} | ||
|
||
go func(){ | ||
err = http.Serve(i.listener, mux) | ||
if err != nil { | ||
log.Fatal("HTTP input serve failure:", err) | ||
} | ||
}() | ||
} | ||
|
||
func (i *HTTPInput) String() string { | ||
return "HTTP input: " + i.address | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
"testing" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func TestHTTPInput(t *testing.T) { | ||
wg := new(sync.WaitGroup) | ||
quit := make(chan int) | ||
|
||
input := NewHTTPInput(":0") | ||
output := NewTestOutput(func(data []byte) { | ||
wg.Done() | ||
}) | ||
|
||
Plugins.Inputs = []io.Reader{input} | ||
Plugins.Outputs = []io.Writer{output} | ||
|
||
go Start(quit) | ||
|
||
address := strings.Replace(input.listener.Addr().String(), "[::]", "127.0.0.1", -1) | ||
|
||
for i := 0; i < 100; i++ { | ||
wg.Add(1) | ||
http.Get("http://" + address + "/") | ||
} | ||
|
||
wg.Wait() | ||
|
||
close(quit) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters