forked from go-icap/icap
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.go
More file actions
244 lines (216 loc) · 5.85 KB
/
server.go
File metadata and controls
244 lines (216 loc) · 5.85 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// 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.
// Network connections and request dispatch for the ICAP server.
package icap
import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"net/textproto"
"net/url"
"runtime/debug"
"time"
)
// Handler Objects implementing the Handler interface can be registered
// to serve ICAP requests.
//
// ServeICAP should write reply headers and data to the ResponseWriter
// and then return.
type Handler interface {
ServeICAP(ResponseWriter, *Request)
}
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as ICAP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler object that calls f.
type HandlerFunc func(ResponseWriter, *Request)
// ServeICAP calls f(w, r).
func (f HandlerFunc) ServeICAP(w ResponseWriter, r *Request) {
f(w, r)
}
// A conn represents the server side of an ICAP connection.
type conn struct {
remoteAddr string // network address of remote side
handler Handler // request handler
rwc net.Conn // i/o connection
buf *bufio.ReadWriter // buffered rwc
}
// Create new connection from rwc.
func newConn(rwc net.Conn, handler Handler) (c *conn, err error) {
c = new(conn)
c.remoteAddr = rwc.RemoteAddr().String()
c.handler = handler
c.rwc = rwc
br := bufio.NewReader(rwc)
bw := bufio.NewWriter(rwc)
c.buf = bufio.NewReadWriter(br, bw)
return c, nil
}
// Read next request from connection.
func (c *conn) readRequest() (w *respWriter, err error) {
var req *Request
if req, err = ReadRequest(c.buf); err != nil {
//return req, err
}
if req == nil {
req = new(Request)
} else {
req.RemoteAddr = c.remoteAddr
}
w = new(respWriter)
w.conn = c
w.req = req
w.header = make(http.Header)
return w, err
}
// Close the connection.
func (c *conn) close() {
if c.buf != nil {
c.buf.Flush()
c.buf = nil
}
if c.rwc != nil {
c.rwc.Close()
c.rwc = nil
}
}
// Serve a new connection.
func (c *conn) serve(debugLevel int) {
defer func() {
err := recover()
if err == nil {
return
}
c.rwc.Close()
var buf bytes.Buffer
fmt.Fprintf(&buf, "icap: panic serving %v: %v\n", c.remoteAddr, err)
buf.Write(debug.Stack())
log.Print(buf.String())
}()
var w *respWriter
w, err := c.readRequest()
// In a case of parsing error there should be an option to handle a dummy request to not fail the whole service.
if err != nil {
if debugLevel > 0 {
log.Println("error while reading request:", err)
log.Println("error while reading request:", w)
log.Println("error while reading request:", w.conn)
log.Println("error while reading request:", w.req)
log.Println("error while reading request:", w.header)
}
// c.rwc.Close()
// return
if w == nil {
w = new(respWriter)
}
w.conn = c
w.req = new(Request)
w.req.Method = "ERRDUMMY"
w.req.RawURL = "/"
w.req.Proto = "ICAP/1.0"
w.req.URL, _ = url.ParseRequestURI("icap://localhost/")
w.req.Header = textproto.MIMEHeader{
"Connection": {"close"},
// "Error:": {err.Error()},
}
}
c.handler.ServeICAP(w, w.req)
w.finishRequest()
c.close()
}
// A Server defines parameters for running an ICAP server.
type Server struct {
Addr string // TCP address to listen on, ":1344" if empty
Handler Handler // handler to invoke
ReadTimeout time.Duration
WriteTimeout time.Duration
DebugLevel int
}
// ListenAndServe listens on the TCP network address srv.Addr and then
// calls Serve to handle requests on incoming connections. If
// srv.Addr is blank, ":1344" is used.
func (srv *Server) ListenAndServe() error {
addr := srv.Addr
if addr == "" {
addr = ":1344"
}
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
return srv.Serve(l)
}
// ListenAndServeTLS ---
func (srv *Server) ListenAndServeTLS(cert, key string) error {
cer, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return err
}
addr := srv.Addr
if addr == "" {
addr = ":1344"
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
l, err := tls.Listen("tcp", addr, config)
if err != nil {
return err
}
return srv.Serve(l)
}
// Serve accepts incoming connections on the Listener l, creating a
// new service thread for each. The service threads read requests and
// then call srv.Handler to reply to them.
func (srv *Server) Serve(l net.Listener) error {
defer l.Close()
handler := srv.Handler
if handler == nil {
handler = DefaultServeMux
}
for {
rw, err := l.Accept()
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Temporary() {
log.Printf("icap: Accept error: %v", err)
continue
}
return err
}
if srv.ReadTimeout != 0 {
rw.SetReadDeadline(time.Now().Add(srv.ReadTimeout))
}
if srv.WriteTimeout != 0 {
rw.SetWriteDeadline(time.Now().Add(srv.WriteTimeout))
}
c, err := newConn(rw, handler)
if err != nil {
continue
}
go c.serve(srv.DebugLevel)
}
// The next line is only there to see one specific edge case whichc should never happen.
panic("Shuold never be reached")
}
// Serve accepts incoming ICAP connections on the listener l,
// creating a new service thread for each. The service threads
// read requests and then call handler to reply to them.
func Serve(l net.Listener, handler Handler) error {
srv := &Server{Handler: handler}
return srv.Serve(l)
}
// ListenAndServe listens on the TCP network address addr
// and then calls Serve with handler to handle requests
// on incoming connections.
func ListenAndServe(addr string, handler Handler) error {
server := &Server{Addr: addr, Handler: handler}
return server.ListenAndServe()
}
// ListenAndServeTLS --
func ListenAndServeTLS(addr, cert, key string, handler Handler) error {
server := &Server{Addr: addr, Handler: handler}
return server.ListenAndServeTLS(cert, key)
}