5538: Merge FailHandler and FailThenSucceedHandler into one APIStub to facilitate...
[arvados.git] / sdk / go / httpserver / responsewriter.go
1 package httpserver
2
3 import (
4         "net/http"
5 )
6
7 // ResponseWriter wraps http.ResponseWriter and exposes the status
8 // sent, the number of bytes sent to the client, and the last write
9 // error.
10 type ResponseWriter struct {
11         http.ResponseWriter
12         wroteStatus *int        // Last status given to WriteHeader()
13         wroteBodyBytes *int     // Bytes successfully written
14         err *error              // Last error returned from Write()
15 }
16
17 func WrapResponseWriter(orig http.ResponseWriter) ResponseWriter {
18         return ResponseWriter{orig, new(int), new(int), new(error)}
19 }
20
21 func (w ResponseWriter) WriteHeader(s int) {
22         *w.wroteStatus = s
23         w.ResponseWriter.WriteHeader(s)
24 }
25
26 func (w ResponseWriter) Write(data []byte) (n int, err error) {
27         n, err = w.ResponseWriter.Write(data)
28         *w.wroteBodyBytes += n
29         *w.err = err
30         return
31 }
32
33 func (w ResponseWriter) WroteStatus() int {
34         return *w.wroteStatus
35 }
36
37 func (w ResponseWriter) WroteBodyBytes() int {
38         return *w.wroteBodyBytes
39 }
40
41 func (w ResponseWriter) Err() error {
42         return *w.err
43 }