Skip to content

Commit

Permalink
Revert "fix headers/body order (#55)" (#74)
Browse files Browse the repository at this point in the history
This reverts commit e63e657.

The commit above introduced sane-ordering to the terminal output by
prioritizing headers first and then the response body. But unfortunately
it introduces a deadlock in cases where we have no headers to print.

Commands like `curlie` or `curlie -h` will hang.

Sometimes it also causes the program to deadlock even when headers are
present. It probably has something to do with the code failing to
properly detect where the headers' section ends. But this is difficult
to reproduce.
  • Loading branch information
js-everts authored Jul 3, 2023
1 parent 961dad4 commit c589190
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 36 deletions.
11 changes: 0 additions & 11 deletions formatter/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ type HeaderCleaner struct {
// Post is inserted after the request headers.
Post *bytes.Buffer

HeadersDone chan<- struct{}

buf []byte
line []byte
}
Expand All @@ -31,8 +29,6 @@ func (c *HeaderCleaner) Write(p []byte) (n int, err error) {
n = len(p)
cp := c.buf
p = bytes.Replace(p, capath, ccapath, 1) // Fix curl misformatted line

closeAfterWrite := false
for len(p) > 0 {
idx := bytes.IndexByte(p, '\n')
if idx == -1 {
Expand All @@ -52,9 +48,6 @@ func (c *HeaderCleaner) Write(p []byte) (n int, err error) {
}
case '<':
c.line = c.line[i+2:]
if bytes.Equal(c.line, []byte("\r\n")) && c.HeadersDone != nil {
closeAfterWrite = true
}
case '}', '{':
ignore = true
if c.Verbose && c.Post != nil {
Expand All @@ -69,13 +62,9 @@ func (c *HeaderCleaner) Write(p []byte) (n int, err error) {
if !ignore {
cp = append(cp, c.line...)
}

c.line = c.line[:0]
}
_, err = c.Out.Write(cp)
if closeAfterWrite {
close(c.HeadersDone)
}
return
}

Expand Down
26 changes: 1 addition & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,28 +138,14 @@ func main() {
fmt.Println()
return
}

cmd := exec.Command("curl", opts...)
cmd.Stdin = stdin
cmd.Stdout = stdout

tmpOut := &formatter.HeaderCleaner{
cmd.Stderr = &formatter.HeaderCleaner{
Out: stderr,
Verbose: verbose,
Post: input,
}

if terminal.IsTerminal(stdoutFd) && terminal.IsTerminal(stderrFd) && !quiet {
headerBlock := make(chan struct{})
cmd.Stdout = &blockedWrite{
ch: headerBlock,
out: stdout,
}
tmpOut.HeadersDone = headerBlock
}

cmd.Stderr = tmpOut

if (opts.Has("I") || opts.Has("head")) && terminal.IsTerminal(stdoutFd) {
cmd.Stdout = ioutil.Discard
}
Expand Down Expand Up @@ -189,13 +175,3 @@ func headerSupplied(opts args.Opts, header string) bool {
}
return false
}

type blockedWrite struct {
ch <-chan struct{}
out io.Writer
}

func (c *blockedWrite) Write(p []byte) (n int, err error) {
<-c.ch
return c.out.Write(p)
}

0 comments on commit c589190

Please sign in to comment.