Skip to content
Myr

Revert "Add GET endpoints for getting the state of the finder"

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This reverts commit 750b2a63130fc6b67aaa64c59d42cff428c26b4a. This can cause a deadlock if the endpoints are accessed in the core event loop via execute action. fzf --listen 6266 --bind 'space:execute:curl localhost:6266' Technically, there's no reason to use the API because the information is already available via `{}` and `{q}`, but I'd like to completely remove the risk of misuse.

authored by Junegunn Choi · committed by Junegunn Choi · parent 750b2a6 · Browse files · 6 files+22 added−64 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+22 added−64 removed

  • tests:+2 added−5 removed
  • other:+20 added−59 removed

6 files+22 added−64 removed

modified:man/man1/fzf.1+3 added-10 removed
721 unmodified lines
722.RE723.TP724.B "--listen=HTTP_PORT"725Start HTTP server on the given port. It allows external processes to send726actions to perform via POST method, or retrieve the current state of the727finder.726actions to perform via POST method.727728e.g.729 \fB# Start HTTP server on port 6266730 fzf --listen 6266731733 # Send action to the server via POST method734 curl localhost:6266 -d 'reload(seq 100)+change-prompt(hundred> )'735736 # Retrieve the current item737 curl localhost:6266738739 # Retrieve the query string740 curl localhost:6266/query732 # Send action to the server733 curl -XPOST localhost:6266 -d 'reload(seq 100)+change-prompt(hundred> )'734 \fR735736The port number is exported as \fB$FZF_LISTEN_PORT\fR on the child processes.737.TP
428 unmodified lines
modified:src/options.go+1 added-1 removed
111 unmodified lines
112 --expect=KEYS Comma-separated list of keys to complete fzf113 --read0 Read input delimited by ASCII NUL characters114 --print0 Print output delimited by ASCII NUL characters115 --sync Synchronous search for multi-staged filtering116 --listen=HTTP_PORT Start HTTP server to receive actions116 --listen=HTTP_PORT Start HTTP server to receive actions (POST /)117 --version Display version information and exit118119 Environment variables120 FZF_DEFAULT_COMMAND Default command to use when input is tty
1850 unmodified lines
modified:src/server.go+9 added-22 removed
4 unmodified lines
5 "bytes"6 "errors"7 "fmt"8 "net"9 "regexp"9 "strconv"10 "strings"11 "time"12)1314const (15 crlf = "\r\n"17 httpPattern = "^(GET|POST) (/[^ ]*) HTTP"16 httpOk = "HTTP/1.1 200 OK" + crlf17 httpBadRequest = "HTTP/1.1 400 Bad Request" + crlf18 httpReadTimeout = 10 * time.Second19 maxContentLength = 1024 * 102420)2124var (25 httpRegexp *regexp.Regexp26)2728func startHttpServer(port int, requestChan chan []*action, responseChan chan string) error {22func startHttpServer(port int, channel chan []*action) error {23 if port == 0 {24 return nil25 }26
1 unmodified line
28 if err != nil {29 return fmt.Errorf("port not available: %d", port)30 }3138 httpRegexp = regexp.MustCompile(httpPattern)32 go func() {33 for {34 conn, err := listener.Accept()35 if err != nil {
2 unmodified lines
38 } else {39 continue40 }41 }49 conn.Write([]byte(handleHttpRequest(conn, requestChan, responseChan)))42 conn.Write([]byte(handleHttpRequest(conn, channel)))43 conn.Close()44 }45 listener.Close()46 }()
6 unmodified lines
53//54// * No --listen: 2.8MB55// * --listen with net/http: 5.7MB56// * --listen w/o net/http: 3.3MB64func handleHttpRequest(conn net.Conn, requestChan chan []*action, responseChan chan string) string {57func handleHttpRequest(conn net.Conn, channel chan []*action) string {58 contentLength := 059 body := ""67 response := func(header string, message string) string {68 return header + fmt.Sprintf("Content-Length: %d%s", len(message), crlf+crlf+message)69 }60 bad := func(message string) string {71 return response(httpBadRequest, strings.TrimSpace(message)+"\n")61 message += "\n"62 return httpBadRequest + fmt.Sprintf("Content-Length: %d%s", len(message), crlf+crlf+message)63 }64 conn.SetReadDeadline(time.Now().Add(httpReadTimeout))65 scanner := bufio.NewScanner(conn)66 scanner.Split(func(data []byte, atEOF bool) (int, []byte, error) {
12 unmodified lines
79 for scanner.Scan() {80 text := scanner.Text()81 switch section {82 case 0:92 httpMatch := httpRegexp.FindStringSubmatch(text)93 if len(httpMatch) != 3 {94 return bad("invalid HTTP request: " + text)83 if !strings.HasPrefix(text, "POST / HTTP") {84 return bad("invalid request method")85 }96 if httpMatch[1] == "GET" {97 requestChan <- []*action{{t: actEvaluate, a: httpMatch[2][1:]}}98 return response(httpOk, <-responseChan)99 }86 section++87 case 1:88 if text == crlf {89 if contentLength == 0 {
29 unmodified lines
119 }120 if len(actions) == 0 {121 return bad("no action specified")122 }137 requestChan <- actions123124 channel <- actions125 return httpOk126}
modified:src/terminal.go+4 added-16 removed
199 unmodified lines
200 suppress bool201 sigstop bool202 startChan chan fitpad203 killChan chan int204 serverRequestChan chan []*action205 serverResponseChan chan string204 serverChan chan []*action205 slab *util.Slab206 theme *tui.ColorTheme207 tui tui.Renderer208 executing *util.AtomicBool
66 unmodified lines
275 actClose276 actDeleteChar277 actDeleteCharEOF278 actEndOfLine280 actEvaluate279 actForwardChar280 actForwardWord281 actKillLine282 actKillWord
315 unmodified lines
598 slab: util.MakeSlab(slab16Size, slab32Size),599 theme: opts.Theme,600 startChan: make(chan fitpad, 1),601 killChan: make(chan int),604 serverRequestChan: make(chan []*action),605 serverResponseChan: make(chan string),602 serverChan: make(chan []*action),603 tui: renderer,604 initFunc: func() { renderer.Init() },605 executing: util.NewAtomicBool(false)}606 t.prompt, t.promptLen = t.parsePrompt(opts.Prompt)
13 unmodified lines
620 }621 t.separator, t.separatorLen = t.ansiLabelPrinter(bar, &tui.ColSeparator, true)622 }623627 if err := startHttpServer(t.listenPort, t.serverRequestChan, t.serverResponseChan); err != nil {624 if err := startHttpServer(t.listenPort, t.serverChan); err != nil {625 errorExit(err.Error())626 }627628 return &t
1901 unmodified lines
2530 }2531 select {2532 case event = <-eventChan:2533 needBarrier = true2537 case actions = <-t.serverRequestChan:2534 case actions = <-t.serverChan:2535 event = tui.Invalid.AsEvent()2536 needBarrier = false2537 }2538 }
74 unmodified lines
2613 case actExecute, actExecuteSilent:2614 t.executeCommand(a.a, false, a.t == actExecuteSilent)2615 case actExecuteMulti:2616 t.executeCommand(a.a, true, false)2620 case actEvaluate:2621 response := ""2622 switch a.a {2623 case "", "current":2624 response = t.currentItem().AsString(t.ansi)2625 case "query":2626 response = string(t.input)2627 }2628 t.serverResponseChan <- response2617 case actInvalid:2618 t.mutex.Unlock()2619 return false2620 case actTogglePreview:
581 unmodified lines
modified:test/test_go.rb+2 added-5 removed
2438 unmodified lines
24392440 def test_listen2441 tmux.send_keys 'seq 10 | fzf --listen 6266', :Enter2442 tmux.until { |lines| assert_equal 10, lines.item_count }2443 Net::HTTP.post(URI('http://localhost:6266'), 'change-query(00)+reload(seq 100)+change-prompt:hundred> ')2443 Net::HTTP.post(URI('http://localhost:6266'), 'change-query(yo)+reload(seq 100)+change-prompt:hundred> ')2444 tmux.until { |lines| assert_equal 100, lines.item_count }2445 tmux.until { |lines| assert_equal 'hundred> 00', lines[-1] }2446 assert_equal '100', Net::HTTP.get(URI('http://localhost:6266'))2447 assert_equal '100', Net::HTTP.get(URI('http://localhost:6266/current'))2448 assert_equal '00', Net::HTTP.get(URI('http://localhost:6266/query'))2445 tmux.until { |lines| assert_equal 'hundred> yo', lines[-1] }2446 end2447end24482449module TestShell
480 unmodified lines
modified:CHANGELOG.md+3 added-10 removed
2 unmodified lines
340.36.05------6- Added `--listen=HTTP_PORT` option to start HTTP server. It allows external7 processes to send actions to perform via POST method, or retrieve the8 current state of the finder.7 processes to send actions to perform via POST method.8 ```sh9 # Start HTTP server on port 626610 fzf --listen 62661113 # Send action to the server via POST method14 curl localhost:6266 -d 'reload(seq 100)+change-prompt(hundred> )'1516 # Retrieve the current item17 curl localhost:62661819 # Retrieve the query string20 curl localhost:6266/query12 # Send actions to the server13 curl -XPOST localhost:6266 -d 'reload(seq 100)+change-prompt(hundred> )'14 ```15- Added `next-selected` and `prev-selected` actions to move between selected16 items17 ```sh
1407 unmodified lines