mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-06-17 16:31:45 +00:00
json: Fixed a race condition with erroneous JSON streams
This commit is contained in:
parent
1202a17359
commit
38d7af77f9
|
@ -78,25 +78,14 @@ func WithBody(body io.ReadCloser) RequestOption {
|
||||||
// WithJSONBody inserts a JSON body into the request. This ignores JSON errors.
|
// WithJSONBody inserts a JSON body into the request. This ignores JSON errors.
|
||||||
func WithJSONBody(v interface{}) RequestOption {
|
func WithJSONBody(v interface{}) RequestOption {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return func(httpdriver.Request) error {
|
return func(httpdriver.Request) error { return nil }
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var rp, wp = io.Pipe()
|
var rp, wp = io.Pipe()
|
||||||
var err error
|
|
||||||
|
|
||||||
go func() {
|
go func() { wp.CloseWithError(json.EncodeStream(wp, v)) }()
|
||||||
err = json.EncodeStream(wp, v)
|
|
||||||
wp.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
return func(r httpdriver.Request) error {
|
return func(r httpdriver.Request) error {
|
||||||
// TODO: maybe do something to this?
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
r.AddHeader(http.Header{
|
r.AddHeader(http.Header{
|
||||||
"Content-Type": {"application/json"},
|
"Content-Type": {"application/json"},
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue