Skip to content

Commit

Permalink
Merge pull request #6 from loveholidays/do-not-panic-on-req-err
Browse files Browse the repository at this point in the history
Add the strictMode option to not terminate the current ripley session if an invalid HTTP request is received
  • Loading branch information
eugenepaniot authored Oct 4, 2022
2 parents ef43eab + 2b88e11 commit 24a7748
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
8 changes: 7 additions & 1 deletion etc/dummyweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ package main
import (
"log"
"net/http"
"net/http/httputil"
"time"
)

func handler(w http.ResponseWriter, r *http.Request) {
log.Printf("%v\n", time.Now().Format(time.UnixDate))
dump, err := httputil.DumpRequest(r, true)
if err != nil {
panic(err)
}

log.Printf("%v: %s\n", time.Now().Format(time.UnixDate), string(dump))
w.Write([]byte("hi\n"))
}

Expand Down
5 changes: 5 additions & 0 deletions etc/requests.jsonl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T18:59:57.9Z"}
{"url": "http://localhost:8080/", "method": "POST", "body": "{\"foo\": \"bar\"}", "headers": {"Accept": "text/plain"}, "timestamp": "2021-11-08T18:59:58.9Z"}
{"url": "http://localhost:8080/", "method": "GET", "headers": {"Accept": "text/plain"}, "timestamp": "2021-11-08T18:59:59.9Z"}
{"url": "http://localhost:8080/", "method": "HEAD", "timestamp": "2021-11-08T19:00:00.00Z"}
{"url": "http://localhost:8080/", "method": "OPTIONS", "timestamp": "2021-11-08T19:00:00.01Z"}
{"url": "http://localhost:8080/", "method": "TRACE", "timestamp": "2021-11-08T19:00:00.02Z"}
{"url": "http://localhost:8080/", "method": "PROPFIND", "timestamp": "2021-11-08T19:00:00.03Z"}
{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T19:00:00.04Z"}
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ package main

import (
"flag"
"github.com/loveholidays/ripley/pkg"
"os"
"runtime"
"runtime/pprof"

ripley "github.com/loveholidays/ripley/pkg"
)

func main() {
paceStr := flag.String("pace", "10s@1", `[duration]@[rate], e.g. "1m@1 [email protected] 1h@2"`)
silent := flag.Bool("silent", false, "Suppress output")
dryRun := flag.Bool("dry-run", false, "Consume input but do not send HTTP requests to targets")
timeout := flag.Int("timeout", 10, "HTTP client timeout in seconds")
strict := flag.Bool("strict", false, "Panic on bad input")
memprofile := flag.String("memprofile", "", "Write memory profile to `file` before exit")

flag.Parse()

ripley.Replay(*paceStr, *silent, *dryRun, *timeout)
exitCode := ripley.Replay(*paceStr, *silent, *dryRun, *timeout, *strict)
defer os.Exit(exitCode)

if *memprofile != "" {
f, err := os.Create(*memprofile)
Expand Down
21 changes: 18 additions & 3 deletions pkg/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
"time"
)

func Replay(phasesStr string, silent, dryRun bool, timeout int) {
func Replay(phasesStr string, silent, dryRun bool, timeout int, strict bool) int {
// Default exit code
var exitCode int = 0
// Ensures we have handled all HTTP request results before exiting
var waitGroup sync.WaitGroup

Expand Down Expand Up @@ -55,9 +57,20 @@ func Replay(phasesStr string, silent, dryRun bool, timeout int) {

for scanner.Scan() {
req, err := unmarshalRequest(scanner.Bytes())

if err != nil {
panic(err)
exitCode = 126
result, _ := json.Marshal(Result{
StatusCode: 0,
Latency: 0,
Request: req,
ErrorMsg: fmt.Sprintf("%v", err),
})
fmt.Println(string(result))

if strict {
panic(err)
}
continue
}

if pacer.done {
Expand Down Expand Up @@ -98,4 +111,6 @@ func Replay(phasesStr string, silent, dryRun bool, timeout int) {
}

waitGroup.Wait()

return exitCode
}
10 changes: 4 additions & 6 deletions pkg/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package ripley
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -62,21 +61,20 @@ func unmarshalRequest(jsonRequest []byte) (*request, error) {
err := json.Unmarshal(jsonRequest, &req)

if err != nil {
return nil, err
return req, err
}

// Validate

if !validMethod(req.Method) {
return nil, errors.New(fmt.Sprintf("Invalid method: %s", req.Method))
return req, fmt.Errorf("invalid method: %s", req.Method)
}

if req.Url == "" {
return nil, errors.New("Missing required key: url")
return req, fmt.Errorf("missing required key: url")
}

if req.Timestamp.IsZero() {
return nil, errors.New("Missing required key: timestamp")
return req, fmt.Errorf("missing required key: timestamp")
}

return req, nil
Expand Down

0 comments on commit 24a7748

Please sign in to comment.