forked from go-icap/icap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
214 lines (180 loc) · 5.05 KB
/
response.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2011 Andy Balholm. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Responding to ICAP requests.
package icap
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"time"
)
type ResponseWriter interface {
// Header returns the header map that will be sent by WriteHeader.
// Changing the header after a call to WriteHeader (or Write) has
// no effect.
Header() http.Header
// Write writes the data to the connection as part of an ICAP reply.
// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK, nil)
// before writing the data.
Write([]byte) (int, error)
// WriteHeader sends an ICAP response header with status code.
// Then it sends an HTTP header if httpMessage is not nil.
// httpMessage may be an *http.Request or an *http.Response.
// hasBody should be true if there will be calls to Write(), generating a message body.
WriteHeader(code int, httpMessage interface{}, hasBody bool)
}
type respWriter struct {
conn *conn // information on the connection
req *Request // the request that is being responded to
header http.Header // the ICAP header to write for the response
wroteHeader bool // true if the headers have already been written
cw io.WriteCloser // the chunked writer used to write the body
}
func (w *respWriter) Header() http.Header {
return w.header
}
func (w *respWriter) Write(p []byte) (n int, err error) {
if !w.wroteHeader {
w.WriteHeader(http.StatusOK, nil, true)
}
if w.cw == nil {
return 0, errors.New("called Write() on an icap.ResponseWriter that should not have a body")
}
return w.cw.Write(p)
}
func (w *respWriter) WriteHeader(code int, httpMessage interface{}, hasBody bool) {
if w.wroteHeader {
log.Println("Called WriteHeader twice on the same connection")
return
}
// Make the HTTP header and the Encapsulated: header.
var header []byte
var encap string
var err error
switch msg := httpMessage.(type) {
case *http.Request:
header, err = httpRequestHeader(msg)
if err != nil {
break
}
if hasBody {
encap = fmt.Sprintf("req-hdr=0, req-body=%d", len(header))
} else {
encap = fmt.Sprintf("req-hdr=0, null-body=%d", len(header))
}
case *http.Response:
header, err = httpResponseHeader(msg)
if err != nil {
break
}
if hasBody {
encap = fmt.Sprintf("res-hdr=0, res-body=%d", len(header))
} else {
encap = fmt.Sprintf("res-hdr=0, null-body=%d", len(header))
}
}
if encap == "" {
if hasBody {
method := w.req.Method
if len(method) > 3 {
method = method[0:3]
}
method = strings.ToLower(method)
encap = fmt.Sprintf("%s-body=0", method)
} else {
encap = "null-body=0"
}
}
w.header.Set("Encapsulated", encap)
if _, ok := w.header["Date"]; !ok {
w.Header().Set("Date", time.Now().UTC().Format(http.TimeFormat))
}
w.header.Set("Connection", "close")
bw := w.conn.buf.Writer
status := StatusText(code)
if status == "" {
status = fmt.Sprintf("status code %d", code)
}
fmt.Fprintf(bw, "ICAP/1.0 %d %s\r\n", code, status)
w.header.Write(bw)
io.WriteString(bw, "\r\n")
if header != nil {
bw.Write(header)
}
w.wroteHeader = true
if hasBody {
w.cw = httputil.NewChunkedWriter(w.conn.buf.Writer)
}
}
func (w *respWriter) finishRequest() {
if !w.wroteHeader {
w.WriteHeader(http.StatusOK, nil, false)
}
if w.cw != nil {
w.cw.Close()
w.cw = nil
io.WriteString(w.conn.buf, "\r\n")
}
w.conn.buf.Flush()
}
// httpRequestHeader returns the headers for an HTTP request
// as a slice of bytes in a form suitable for including in an ICAP message.
func httpRequestHeader(req *http.Request) (hdr []byte, err error) {
buf := new(bytes.Buffer)
if req.URL == nil {
if err != nil {
return nil, errors.New("icap: httpRequestHeader called on Request with no URL")
}
}
host := req.URL.Host
if host == "" {
host = req.Host
}
req.Header.Set("Host", host)
uri := req.URL.String()
fmt.Fprintf(buf, "%s %s %s\r\n", valueOrDefault(req.Method, "GET"), uri, valueOrDefault(req.Proto, "HTTP/1.1"))
req.Header.WriteSubset(buf, map[string]bool{
"Transfer-Encoding": true,
"Content-Length": true,
})
io.WriteString(buf, "\r\n")
return buf.Bytes(), nil
}
// httpResponseHeader returns the headers for an HTTP response
// as a slice of bytes.
func httpResponseHeader(resp *http.Response) (hdr []byte, err error) {
buf := new(bytes.Buffer)
// Status line
text := resp.Status
if text == "" {
text = http.StatusText(resp.StatusCode)
if text == "" {
text = "status code " + strconv.Itoa(resp.StatusCode)
}
}
proto := resp.Proto
if proto == "" {
proto = "HTTP/1.1"
}
fmt.Fprintf(buf, "%s %d %s\r\n", proto, resp.StatusCode, text)
resp.Header.WriteSubset(buf, map[string]bool{
"Transfer-Encoding": true,
"Content-Length": true,
})
io.WriteString(buf, "\r\n")
return buf.Bytes(), nil
}
// Return value if nonempty, def otherwise.
func valueOrDefault(value, def string) string {
if value != "" {
return value
}
return def
}