Skip to content

Commit 8703059

Browse files
rdimitrovalexellis
authored andcommitted
Ensure closing of the body request
Signed-off-by: Radoslav Dimitrov <[email protected]>
1 parent dbd29f2 commit 8703059

File tree

3 files changed

+61
-26
lines changed

3 files changed

+61
-26
lines changed

README.md

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,31 @@ import (
165165
)
166166

167167
func Handle(w http.ResponseWriter, r *http.Request) {
168-
// read request payload
169-
reqBody, err := ioutil.ReadAll(r.Body)
170-
if err != nil {
171-
http.Error(w, err.Error(), http.StatusInternalServerError)
172-
return
168+
var input []byte
169+
170+
if r.Body != nil {
171+
defer r.Body.Close()
172+
173+
// read request payload
174+
reqBody, err := ioutil.ReadAll(r.Body)
175+
176+
if err != nil {
177+
http.Error(w, err.Error(), http.StatusInternalServerError)
178+
return
179+
180+
input = reqBody
181+
}
173182
}
174-
175-
// log to stdout
176-
fmt.Printf("request body: %s", string(reqBody))
183+
184+
// log to stdout
185+
fmt.Printf("request body: %s", string(input))
177186

178187
response := struct {
179188
Payload string `json:"payload"`
180189
Headers map[string][]string `json:"headers"`
181190
Environment []string `json:"environment"`
182191
}{
183-
Payload: string(reqBody),
192+
Payload: string(input),
184193
Headers: r.Header,
185194
Environment: os.Environ(),
186195
}
@@ -228,16 +237,24 @@ func init() {
228237
}
229238

230239
func Handle(w http.ResponseWriter, r *http.Request) {
231-
// read request payload
232-
body, err := ioutil.ReadAll(r.Body)
233-
if err != nil {
234-
http.Error(w, err.Error(), http.StatusInternalServerError)
235-
return
240+
var query string
241+
242+
if r.Body != nil {
243+
defer r.Body.Close()
244+
245+
// read request payload
246+
body, err := ioutil.ReadAll(r.Body)
247+
248+
if err != nil {
249+
http.Error(w, err.Error(), http.StatusInternalServerError)
250+
return
251+
}
252+
253+
query = string(body)
236254
}
237-
query := string(body)
238255

239256
// log to stdout
240-
fmt.Printf("executing query: %s", query)
257+
fmt.Printf("Executing query: %s", query)
241258

242259
rows, err := db.Query(query)
243260
if err != nil {

template/golang-middleware-armhf/function/handler.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ import (
77
)
88

99
func Handle(w http.ResponseWriter, r *http.Request) {
10-
body, err := ioutil.ReadAll(r.Body)
11-
if err != nil {
12-
http.Error(w, err.Error(), http.StatusInternalServerError)
13-
return
10+
var input []byte
11+
12+
if r.Body != nil {
13+
defer r.Body.Close()
14+
15+
body, err := ioutil.ReadAll(r.Body)
16+
17+
if err != nil {
18+
http.Error(w, err.Error(), http.StatusInternalServerError)
19+
return
20+
}
21+
22+
input = body
1423
}
1524

1625
w.WriteHeader(http.StatusOK)
17-
w.Write([]byte(fmt.Sprintf("Hello world, input was: %s", string(body))))
26+
w.Write([]byte(fmt.Sprintf("Hello world, input was: %s", string(input))))
1827
}

template/golang-middleware/function/handler.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ import (
77
)
88

99
func Handle(w http.ResponseWriter, r *http.Request) {
10-
body, err := ioutil.ReadAll(r.Body)
11-
if err != nil {
12-
http.Error(w, err.Error(), http.StatusInternalServerError)
13-
return
10+
var input []byte
11+
12+
if r.Body != nil {
13+
defer r.Body.Close()
14+
15+
body, err := ioutil.ReadAll(r.Body)
16+
17+
if err != nil {
18+
http.Error(w, err.Error(), http.StatusInternalServerError)
19+
return
20+
}
21+
22+
input = body
1423
}
1524

1625
w.WriteHeader(http.StatusOK)
17-
w.Write([]byte(fmt.Sprintf("Hello world, input was: %s", string(body))))
26+
w.Write([]byte(fmt.Sprintf("Hello world, input was: %s", string(input))))
1827
}

0 commit comments

Comments
 (0)