| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"compress/gzip" |
| 10 |
"encoding/json" |
| 11 |
"io" |
| 12 |
"io/ioutil" |
| 13 |
"net/http" |
| 14 |
"runtime" |
| 15 |
"strings" |
| 16 |
) |
| 17 |
|
| 18 |
// Elasticsearch-specific HTTP request |
| 19 |
type Request http.Request |
| 20 |
|
| 21 |
// NewRequest is a http.Request and adds features such as encoding the body. |
| 22 |
func NewRequest(method, url string) (*Request, error) { |
| 23 |
req, err := http.NewRequest(method, url, nil) |
| 24 |
if err != nil { |
| 25 |
return nil, err |
| 26 |
} |
| 27 |
req.Header.Add("User-Agent", "elastic/"+Version+" ("+runtime.GOOS+"-"+runtime.GOARCH+")") |
| 28 |
req.Header.Add("Accept", "application/json") |
| 29 |
req.Header.Set("Content-Type", "application/json") |
| 30 |
return (*Request)(req), nil |
| 31 |
} |
| 32 |
|
| 33 |
// SetBasicAuth wraps http.Request's SetBasicAuth. |
| 34 |
func (r *Request) SetBasicAuth(username, password string) { |
| 35 |
((*http.Request)(r)).SetBasicAuth(username, password) |
| 36 |
} |
| 37 |
|
| 38 |
// SetBody encodes the body in the request. Optionally, it performs GZIP compression. |
| 39 |
func (r *Request) SetBody(body interface{}, gzipCompress bool) error { |
| 40 |
switch b := body.(type) { |
| 41 |
case string: |
| 42 |
if gzipCompress { |
| 43 |
return r.setBodyGzip(b) |
| 44 |
} else { |
| 45 |
return r.setBodyString(b) |
| 46 |
} |
| 47 |
default: |
| 48 |
if gzipCompress { |
| 49 |
return r.setBodyGzip(body) |
| 50 |
} else { |
| 51 |
return r.setBodyJson(body) |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// setBodyJson encodes the body as a struct to be marshaled via json.Marshal. |
| 57 |
func (r *Request) setBodyJson(data interface{}) error { |
| 58 |
body, err := json.Marshal(data) |
| 59 |
if err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
r.Header.Set("Content-Type", "application/json") |
| 63 |
r.setBodyReader(bytes.NewReader(body)) |
| 64 |
return nil |
| 65 |
} |
| 66 |
|
| 67 |
// setBodyString encodes the body as a string. |
| 68 |
func (r *Request) setBodyString(body string) error { |
| 69 |
return r.setBodyReader(strings.NewReader(body)) |
| 70 |
} |
| 71 |
|
| 72 |
// setBodyGzip gzip's the body. It accepts both strings and structs as body. |
| 73 |
// The latter will be encoded via json.Marshal. |
| 74 |
func (r *Request) setBodyGzip(body interface{}) error { |
| 75 |
switch b := body.(type) { |
| 76 |
case string: |
| 77 |
buf := new(bytes.Buffer) |
| 78 |
w := gzip.NewWriter(buf) |
| 79 |
if _, err := w.Write([]byte(b)); err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
if err := w.Close(); err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
r.Header.Add("Content-Encoding", "gzip") |
| 86 |
r.Header.Add("Vary", "Accept-Encoding") |
| 87 |
return r.setBodyReader(bytes.NewReader(buf.Bytes())) |
| 88 |
default: |
| 89 |
data, err := json.Marshal(b) |
| 90 |
if err != nil { |
| 91 |
return err |
| 92 |
} |
| 93 |
buf := new(bytes.Buffer) |
| 94 |
w := gzip.NewWriter(buf) |
| 95 |
if _, err := w.Write(data); err != nil { |
| 96 |
return err |
| 97 |
} |
| 98 |
if err := w.Close(); err != nil { |
| 99 |
return err |
| 100 |
} |
| 101 |
r.Header.Add("Content-Encoding", "gzip") |
| 102 |
r.Header.Add("Vary", "Accept-Encoding") |
| 103 |
r.Header.Set("Content-Type", "application/json") |
| 104 |
return r.setBodyReader(bytes.NewReader(buf.Bytes())) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// setBodyReader writes the body from an io.Reader. |
| 109 |
func (r *Request) setBodyReader(body io.Reader) error { |
| 110 |
rc, ok := body.(io.ReadCloser) |
| 111 |
if !ok && body != nil { |
| 112 |
rc = ioutil.NopCloser(body) |
| 113 |
} |
| 114 |
r.Body = rc |
| 115 |
if body != nil { |
| 116 |
switch v := body.(type) { |
| 117 |
case *strings.Reader: |
| 118 |
r.ContentLength = int64(v.Len()) |
| 119 |
case *bytes.Buffer: |
| 120 |
r.ContentLength = int64(v.Len()) |
| 121 |
} |
| 122 |
} |
| 123 |
return nil |
| 124 |
} |