Skip to content

Commit

Permalink
Fix race condition when processing request
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaley committed Sep 29, 2016
1 parent 7788709 commit fbd8422
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
15 changes: 8 additions & 7 deletions rackhd/proxy/controller.go
Expand Up @@ -73,15 +73,16 @@ func (p *Server) HandleNodes(w http.ResponseWriter, r *http.Request) {
}
if err != nil {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("%s", err)))
return
}
if (r.Method != "GET") && (len(addrMap) > 1) {
w.WriteHeader(400)
msg := Err{Msg: "Unsupported api call to multiple hosts. Use query string method."}
msg := Err{Msg: "Internal error fetching endpoint addresses."}
json.NewEncoder(w).Encode(msg)
return
}
// if (r.Method != "GET") && (len(addrMap) > 1) {
// w.WriteHeader(400)
// msg := Err{Msg: "Unsupported api call to multiple hosts. Use query string method."}
// json.NewEncoder(w).Encode(msg)
// return
// }
ar := p.GetResp(r, addrMap)
p.RespCheck(r, w, ar)
elapsed := time.Since(start)
Expand Down Expand Up @@ -200,7 +201,7 @@ func (p *Server) RespCheck(r *http.Request, w http.ResponseWriter, ar Responses)
p.RespHeaderWriter(r, w, ar)
w.Write([]byte("["))
for i, r := range ar {
if r.Body == nil {
if r.Body == nil || ((r.Body[0] == '[') && (r.Body[1] == ']')) {
continue
}
if r.Body[0] == '[' {
Expand Down
13 changes: 12 additions & 1 deletion rackhd/proxy/helper.go
@@ -1,6 +1,7 @@
package proxy

import (
"bytes"
"io/ioutil"
"log"
"net/http"
Expand All @@ -15,6 +16,10 @@ type Response struct {
Error error
}

//Request is the interanal proxy request object
type Request struct {
}

// NewResponse copies a http.Response into a proxy Response
func NewResponse(resp *http.Response) (*Response, error) {
body, err := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -43,7 +48,13 @@ func NewResponseFromError(err error) *Response {

// NewRequest copies a http.Request & Header and sets the new host
func NewRequest(r *http.Request, host string) (*http.Request, error) {
req, err := http.NewRequest(r.Method, "http://"+host+r.URL.Path, r.Body)
buff, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading Request.Body %s\n", err)
return nil, err
}
reader := bytes.NewReader(buff)
req, err := http.NewRequest(r.Method, "http://"+host+r.URL.Path, reader)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit fbd8422

Please sign in to comment.