Skip to content

Commit

Permalink
Simplify /payload implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sharat87 committed Nov 4, 2024
1 parent db9aff6 commit 499f24a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
6 changes: 3 additions & 3 deletions exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Exchange struct {
Request *http.Request
ResponseWriter http.ResponseWriter
fields map[string]string
CappedBody io.Reader
cappedBody io.Reader
RoutedPath string
ServerSpec spec.Spec
}
Expand All @@ -35,7 +35,7 @@ func New(w http.ResponseWriter, req *http.Request, serverSpec spec.Spec) *Exchan
Request: req,
ResponseWriter: w,
fields: map[string]string{},
CappedBody: io.LimitReader(req.Body, 10000),
cappedBody: io.LimitReader(req.Body, 10000),
RoutedPath: strings.TrimPrefix(req.URL.EscapedPath(), serverSpec.PathPrefix),
ServerSpec: serverSpec,
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func (ex Exchange) FindIncomingIPAddress() string {
}

func (ex Exchange) BodyBytes() []byte {
if bodyBytes, err := io.ReadAll(ex.CappedBody); err != nil {
if bodyBytes, err := io.ReadAll(ex.cappedBody); err != nil {
fmt.Println("Error reading request payload", err)
return nil
} else {
Expand Down
24 changes: 6 additions & 18 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/sharat87/httpbun/routes/run"
"github.com/sharat87/httpbun/routes/static"
"github.com/sharat87/httpbun/util"
"io"
"log"
"maps"
"math/rand"
Expand All @@ -40,8 +39,6 @@ func GetRoutes() []Route {
allRoutes := map[string]exchange.HandlerFn{
"/health": handleHealth,

"/payload": handlePayload,

"/b(ase)?64(/(?P<encoded>.*))?": handleDecodeBase64,
"/bytes(/(?P<size>.+))?": handleRandomBytes,
"/delay/(?P<delay>[^/]+)": handleDelayedResponse,
Expand All @@ -59,6 +56,8 @@ func GetRoutes() []Route {

`(/(index\.html)?)?`: handleIndex,

"/payload": handlePayload,

"/status/(?P<codes>[\\w,]+)": handleStatus,

"/ip(\\.(?P<format>txt|json))?": handleIp,
Expand Down Expand Up @@ -111,21 +110,10 @@ func handleHealth(ex *exchange.Exchange) {
ex.WriteLn("ok")
}

func handlePayload(ex *exchange.Exchange) {
ex.ResponseWriter.Header()[c.ContentType] = ex.Request.Header[c.ContentType]

payload, err := io.ReadAll(ex.CappedBody)
if err != nil {
log.Printf("Error reading request payload %v", err)
return
}

ex.ResponseWriter.Header().Set("Content-Length", fmt.Sprint(len(payload)))

_, err = ex.ResponseWriter.Write(payload)
if err != nil {
fmt.Println("Error reading request payload", err)
}
func handlePayload(ex *exchange.Exchange) response.Response {
return response.New(http.StatusOK, http.Header{
c.ContentType: ex.Request.Header[c.ContentType],
}, ex.BodyBytes())
}

func handleStatus(ex *exchange.Exchange) response.Response {
Expand Down

0 comments on commit 499f24a

Please sign in to comment.