diff --git a/heff/http.go b/heff/http.go index bbcc44f..e3c00b0 100644 --- a/heff/http.go +++ b/heff/http.go @@ -7,8 +7,11 @@ import ( "sync" ) +// DefaultHoneypot is an http.HandlerFunc that serves random HTML from the +// DefaultMarkovMap, 100KB at a time. var DefaultHoneypot = NewHoneypot(DefaultMarkovMap, 100*1<<10) +// NewHoneypot creates an http.HandlerFunc from a MarkovMap func NewHoneypot(mm MarkovMap, buffsize int) http.HandlerFunc { var pool sync.Pool @@ -16,9 +19,8 @@ func NewHoneypot(mm MarkovMap, buffsize int) http.HandlerFunc { x := pool.Get() if buf, ok := x.([]byte); ok { return buf - } else { - return make([]byte, buffsize) } + return make([]byte, buffsize) } putBuffer := func(buf []byte) { diff --git a/heff/markov.go b/heff/markov.go index 261bd46..609649d 100644 --- a/heff/markov.go +++ b/heff/markov.go @@ -53,16 +53,20 @@ func ScanHTML(data []byte, atEOF bool) (advance int, token []byte, err error) { type tokenPair [2]string +// DefaultMarkovMap is a Markov chain based on Src. var DefaultMarkovMap = MakeMarkovMap(strings.NewReader(Src)) +// MarkovMap is a map that acts as a Markov chain generator. type MarkovMap map[tokenPair][]string +// MakeMarkovMap makes an empty MakeMarkov and fills it with r. func MakeMarkovMap(r io.Reader) MarkovMap { m := MarkovMap{} m.Fill(r) return m } +// Fill adds all the tokens in r to a MarkovMap func (mm MarkovMap) Fill(r io.Reader) { var w1, w2, w3 string @@ -77,11 +81,13 @@ func (mm MarkovMap) Fill(r io.Reader) { mm.Add(w1, w2, w3) } +// Add adds a three token sequence to the map. func (mm MarkovMap) Add(w1, w2, w3 string) { p := tokenPair{w1, w2} mm[p] = append(mm[p], w3) } +// Get psuedo-randomly chooses a possible suffix to w1 and w2. func (mm MarkovMap) Get(w1, w2 string) string { p := tokenPair{w1, w2} suffix, ok := mm[p] @@ -93,6 +99,7 @@ func (mm MarkovMap) Get(w1, w2 string) string { return suffix[r] } +// Read fills p with data from calling Get on the MarkovMap. func (mm MarkovMap) Read(p []byte) (n int, err error) { var w1, w2, w3 string diff --git a/heff/src.go b/heff/src.go index fcc85e8..5c199de 100644 --- a/heff/src.go +++ b/heff/src.go @@ -1,5 +1,6 @@ package heff +// Src is A. A. Milne's "Once on a Time" as transcribed by Project Gutenberg. const Src = `

ONCE ON A TIME