From e016fba507fc1e1d1e63c837d7569dfd86b1274b Mon Sep 17 00:00:00 2001 From: Jonathan de Jong Date: Wed, 31 Jan 2024 14:06:12 +0100 Subject: [PATCH 1/2] add JSON hell, and enable more kinds of hell in the future --- heffalump/heffalump.go | 27 ++++++++++++++++++++++++--- internal/http/router.go | 17 ++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/heffalump/heffalump.go b/heffalump/heffalump.go index 67c99b9..b161e09 100644 --- a/heffalump/heffalump.go +++ b/heffalump/heffalump.go @@ -41,8 +41,16 @@ func NewDefaultHeffalump() *Heffalump { return NewHeffalump(NewDefaultMarkovMap(), DefaultBuffSize) } +type ContentType int + +const ( + PlainText ContentType = iota + HTML + JSON +) + // WriteHell writes markov chain heffalump hell to the provided io.Writer -func (h *Heffalump) WriteHell(bw *bufio.Writer) (int64, error) { +func (h *Heffalump) WriteHell(bw *bufio.Writer, cType ContentType) (int64, error) { var n int64 var err error @@ -55,8 +63,21 @@ func (h *Heffalump) WriteHell(bw *bufio.Writer) (int64, error) { buf := h.pool.Get().([]byte) defer h.pool.Put(buf) - if _, err = bw.WriteString("\n\n"); err != nil { - return n, err + switch cType { + case PlainText: + break + case HTML: + if _, err = bw.WriteString("\n\n"); err != nil { + return n, err + } + break + case JSON: + if _, err = bw.WriteString("[\""); err != nil { + return n, err + } + break + default: + panic("unhandled default case") } if n, err = io.CopyBuffer(bw, h.mm, buf); err != nil { return n, nil diff --git a/internal/http/router.go b/internal/http/router.go index 18842c2..f4cee1b 100644 --- a/internal/http/router.go +++ b/internal/http/router.go @@ -30,6 +30,20 @@ func getRealRemote(ctx *fasthttp.RequestCtx) string { return ctx.RemoteIP().String() } +func detectContentType(ctx *fasthttp.RequestCtx) (cType heffalump.ContentType) { + cType = heffalump.PlainText + + acceptHeader := string(ctx.Request.Header.Peek("Accept")) + + if strings.Contains(acceptHeader, "text/html") { + cType = heffalump.HTML + } else if strings.Contains(acceptHeader, "application/json") { + cType = heffalump.JSON + } + + return +} + func hellPot(ctx *fasthttp.RequestCtx) { path, pok := ctx.UserValue("path").(string) if len(path) < 1 || !pok { @@ -57,6 +71,7 @@ func hellPot(ctx *fasthttp.RequestCtx) { slog.Info().Msg("NEW") + var cType = detectContentType(ctx) s := time.Now() var n int64 @@ -65,7 +80,7 @@ func hellPot(ctx *fasthttp.RequestCtx) { var wn int64 for { - wn, err = hellpotHeffalump.WriteHell(bw) + wn, err = hellpotHeffalump.WriteHell(bw, cType) n += wn if err != nil { slog.Trace().Err(err).Msg("END_ON_ERR") From 54e492c21cde0459b0c3f94c4954ec2704ad3868 Mon Sep 17 00:00:00 2001 From: Jonathan de Jong Date: Wed, 31 Jan 2024 16:22:13 +0100 Subject: [PATCH 2/2] fix plaintext --- heffalump/heffalump.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/heffalump/heffalump.go b/heffalump/heffalump.go index b161e09..513aa44 100644 --- a/heffalump/heffalump.go +++ b/heffalump/heffalump.go @@ -65,6 +65,9 @@ func (h *Heffalump) WriteHell(bw *bufio.Writer, cType ContentType) (int64, error switch cType { case PlainText: + if _, err = bw.WriteString("# Chapter 1\n"); err != nil { + return n, err + } break case HTML: if _, err = bw.WriteString("\n\n"); err != nil {