Skip to content

Commit

Permalink
Fix post length bug
Browse files Browse the repository at this point in the history
  • Loading branch information
coyove committed Apr 4, 2018
1 parent 12f87cb commit d1d4778
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 50 deletions.
26 changes: 20 additions & 6 deletions cmd/goflyway/lib/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,17 @@ func ParseHeadersAndPostBody(headers, post string, multipartPOST bool, req *http
}

buf := bufio.NewReader(f)
line, _ := buf.Peek(256)

if multipartPOST {
req.Header.Set("Content-Type", mw.FormDataContentType())

line, _ := buf.Peek(256)
if idx := bytes.IndexByte(line, '\n'); idx > -1 {
ct := strings.Replace(string(line[:idx]), "\r", "", -1)
req.Header.Set("Content-Type", ct)
}
req.Header.Set("Content-Length", "0")
} else if req.Method == "POST" {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", "0")
}

req.Body = ioutil.NopCloser(buf)
Expand Down Expand Up @@ -143,15 +147,19 @@ func ParseHeadersAndPostBody(headers, post string, multipartPOST bool, req *http

mw.Close()
req.Header.Set("Content-Type", mw.FormDataContentType())
req.Header.Set("Content-Length", "0")
req.Body = ioutil.NopCloser(buf)
} else {
if req.Method == "POST" {
form := _url.Values{}
for _, kv := range ppost {
form.Add(kv[0], kv[1])
}

x := form.Encode()
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Body = ioutil.NopCloser(strings.NewReader(form.Encode()))
req.Header.Set("Content-Length", strconv.Itoa(len(x)))
req.Body = ioutil.NopCloser(strings.NewReader(x))
} else {
form := req.URL.Query()
for _, kv := range ppost {
Expand All @@ -162,12 +170,12 @@ func ParseHeadersAndPostBody(headers, post string, multipartPOST bool, req *http
}
}

pform, err := parse(headers)
pheader, err := parse(headers)
if err != nil {
return err
}

for _, kv := range pform {
for _, kv := range pheader {
req.Header.Add(kv[0], kv[1])
}

Expand All @@ -191,6 +199,12 @@ func IOCopy(w io.Writer, r *ResponseRecorder, reformatJSON bool) {
}
}

type NullReader struct{}

func (r *NullReader) Read(p []byte) (int, error) { return 0, io.EOF }

func (r *NullReader) Close() error { return nil }

type readerProgress struct {
r io.ReadCloser
callback func(bytes int64)
Expand Down
25 changes: 23 additions & 2 deletions cmd/goflyway/lib/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@ package lib
import (
"fmt"
"os"
"strings"
)

var Slient = false

var Prefix = "*"

func Println(args ...interface{}) {
if !Slient {
fmt.Println(args...)
if Slient {
return
}

fmt.Print(Prefix + " ")

for _, arg := range args {
s := fmt.Sprintf("%v", arg)
p := strings.Split(s, "\n")

fmt.Print(p[0])

for i := 1; i < len(p); i++ {
fmt.Println()
fmt.Print(Prefix + " ")
fmt.Print(p[i])
}

fmt.Print(" ")
}
fmt.Println()
}

func PrintInErr(args ...interface{}) {
Expand Down
Loading

0 comments on commit d1d4778

Please sign in to comment.