forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
66 lines (55 loc) · 1.49 KB
/
write.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package pipes
import (
"errors"
"io"
"net/http"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/auth"
)
func (s *Server) WritePipe(w http.ResponseWriter, r *http.Request) {
logger := s.logger.Session("write-pipe")
pipeID := r.FormValue(":pipe_id")
authTeam, found := auth.GetTeam(r)
if !found {
logger.Error("failed-to-get-team", errors.New("failed-to-get-team"))
w.WriteHeader(http.StatusInternalServerError)
return
}
dbPipe, err := s.db.GetPipe(pipeID)
if err != nil {
logger.Error("failed-to-get-pipe", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if authTeam.Name() != dbPipe.TeamName {
logger.Error("team-not-authorized-to-read-pipe",
errors.New("team-not-authorized-to-read-pipe"),
lager.Data{"TeamName": authTeam.Name(), "PipeID": dbPipe.ID})
w.WriteHeader(http.StatusForbidden)
return
}
if dbPipe.URL == s.url {
s.pipesL.RLock()
pipe, found := s.pipes[pipeID]
s.pipesL.RUnlock()
if !found {
w.WriteHeader(http.StatusNotFound)
return
}
io.Copy(pipe.write, r.Body)
pipe.write.Close()
s.pipesL.Lock()
delete(s.pipes, pipeID)
s.pipesL.Unlock()
} else {
logger.Debug("forwarding-pipe-write-request", lager.Data{"pipe-url": dbPipe.URL})
response, err := s.forwardRequest(w, r, dbPipe.URL, atc.WritePipe, dbPipe.ID)
if err != nil {
logger.Error("failed-to-forward-request", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(response.StatusCode)
}
}