Skip to content

Instantly share code, notes, and snippets.

@igungor
Created September 19, 2017 06:51
Show Gist options
  • Save igungor/0dc1597dc8279c356d6a15b4f7ac4800 to your computer and use it in GitHub Desktop.
Save igungor/0dc1597dc8279c356d6a15b4f7ac4800 to your computer and use it in GitHub Desktop.
goreplay middleware that adds a query string to a URL
package main
import (
"bufio"
"bytes"
"encoding/hex"
"log"
"os"
"strconv"
"time"
"github.com/buger/goreplay/proto"
)
func main() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
encoded := s.Bytes()
buf := make([]byte, len(encoded)/2)
hex.Decode(buf, encoded)
process(buf)
}
if err := s.Err(); err != nil {
log.Fatal(err)
}
}
func process(buf []byte) {
// First byte indicate payload type, possible values:
// 1 - Request
// 2 - Response
// 3 - ReplayedResponse
payloadtype := buf[0]
headersize := bytes.IndexByte(buf, '\n') + 1
payload := buf[headersize:]
switch payloadtype {
case '1': // Request
// Unix timestamp is unique enough for us.
id := strconv.FormatInt(time.Now().UnixNano(), 10)
payload = proto.SetPathParam(payload, []byte("_from"), []byte(id))
buf = append(buf[:headersize], payload...)
os.Stdout.Write(encode(buf))
case '2': // Original response
case '3': // Replayed response
}
}
func encode(buf []byte) []byte {
dst := make([]byte, len(buf)*2+1)
hex.Encode(dst, buf)
dst[len(dst)-1] = '\n'
return dst
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment