Skip to content

Commit

Permalink
Add CTRL-G to send the current replay request
Browse files Browse the repository at this point in the history
  • Loading branch information
denandz committed Jan 28, 2021
1 parent 8416615 commit a83c045
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ ctrl-e | Proxy - highlighted request/response | Open the request/response data i
ctrl-b | Replay | Create a new blank replay item - useful for assembling requests from scratch
ctrl-e | Replay - highlighted request/response | Edit request in `vi`, responses will open with `view`
ctrl-x | Replay | Rename replay item
ctrl-g | Replay | Send the request


Ctrl-N and Ctrl-P cycle between the different pages, Tab/Shift+tab is used to cycle between each item within a page.
Expand Down
87 changes: 48 additions & 39 deletions views/replayview.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ReplayView struct {
request *tview.TextView // http request box
response *tview.TextView // http response box
responseMeta *tview.Table // metadata for size recieved and time taken
goButton *tview.Button // send button

host *tview.InputField // host field input
port *tview.InputField // port input
Expand Down Expand Up @@ -97,7 +98,7 @@ func (view *ReplayView) Init(app *tview.Application) {
view.request.SetWrap(false).SetBorder(true).SetTitle("Request")

// go and cancel buttons
goButton := tview.NewButton("Go")
view.goButton = tview.NewButton("Go")

// Host, Port, TLS and Auto-content-length fields
view.host = tview.NewInputField()
Expand Down Expand Up @@ -134,42 +135,8 @@ func (view *ReplayView) Init(app *tview.Application) {
view.response = tview.NewTextView().SetWrap(false)
view.response.SetBorder(true).SetTitle("Response")

goButton.SetSelectedFunc(func() {
if req, ok := view.entries[id]; ok {
view.response.Clear()
c := false

go func() {
size, err := req.SendRequest()
if req == view.entries[id] {
if size > 0 {
view.refreshReplay(req)
} else {
view.responseMeta.SetCell(0, 1, tview.NewTableCell("ERROR"))
view.responseMeta.SetCell(0, 3, tview.NewTableCell("ERROR"))
view.response.Clear()
fmt.Fprint(view.response, err)
}
app.Draw()
}
c = true
}()

spindex := 0
spinner := []string{"|", "/", "-", "\\"}
go func() {
for !c {
goButton.SetLabel("Go " + spinner[spindex])
app.Draw()
spindex++
if spindex == 3 {
spindex = 0
}
time.Sleep(100 * time.Millisecond)
}
goButton.SetLabel("Go")
}()
}
view.goButton.SetSelectedFunc(func() {
view.sendRequest(app, id)
})

view.Table = tview.NewTable()
Expand Down Expand Up @@ -294,7 +261,7 @@ func (view *ReplayView) Init(app *tview.Application) {
requestFlexView.SetDirection(tview.FlexRow)
requestFlexView.AddItem(connectionForm, 2, 1, false)
requestFlexView.AddItem(view.request, 0, 8, false)
requestFlexView.AddItem(goButton, 1, 1, false)
requestFlexView.AddItem(view.goButton, 1, 1, false)

responseFlexView := tview.NewFlex()
responseFlexView.SetDirection(tview.FlexRow)
Expand All @@ -305,7 +272,7 @@ func (view *ReplayView) Init(app *tview.Application) {
replayFlexView.AddItem(requestFlexView, 0, 4, false)
replayFlexView.AddItem(responseFlexView, 0, 4, false)

items := []tview.Primitive{view.Table, view.host, view.port, view.tls, view.updateContentLength, view.request, goButton, view.response}
items := []tview.Primitive{view.Table, view.host, view.port, view.tls, view.updateContentLength, view.request, view.goButton, view.response}
mainLayout.AddItem(replayFlexView, 0, 1, true)

view.Layout.AddPage("mainLayout", mainLayout, true, true)
Expand Down Expand Up @@ -357,6 +324,8 @@ func (view *ReplayView) Init(app *tview.Application) {
replayData.ID = "new"
view.AddItem(replayData)

case tcell.KeyCtrlG:
view.sendRequest(app, id)
}
return event
})
Expand Down Expand Up @@ -386,3 +355,43 @@ func (view *ReplayView) refreshReplay(r *replay.Request) {
view.responseMeta.SetCell(0, 1, tview.NewTableCell(strconv.Itoa(len(r.RawResponse))))
view.responseMeta.SetCell(0, 3, tview.NewTableCell(r.ResponseTime))
}

// Send the request

func (view *ReplayView) sendRequest(app *tview.Application, id string) {
if req, ok := view.entries[id]; ok {
view.response.Clear()
c := false

go func() {
size, err := req.SendRequest()
if req == view.entries[id] {
if size > 0 {
view.refreshReplay(req)
} else {
view.responseMeta.SetCell(0, 1, tview.NewTableCell("ERROR"))
view.responseMeta.SetCell(0, 3, tview.NewTableCell("ERROR"))
view.response.Clear()
fmt.Fprint(view.response, err)
}
app.Draw()
}
c = true
}()

spindex := 0
spinner := []string{"|", "/", "-", "\\"}
go func() {
for !c {
view.goButton.SetLabel("Go " + spinner[spindex])
app.Draw()
spindex++
if spindex == 3 {
spindex = 0
}
time.Sleep(100 * time.Millisecond)
}
view.goButton.SetLabel("Go")
}()
}
}

0 comments on commit a83c045

Please sign in to comment.