forked from egirna/icap-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_server.go
More file actions
152 lines (120 loc) · 3.08 KB
/
test_server.go
File metadata and controls
152 lines (120 loc) · 3.08 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
package icapclient
import (
"bytes"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/egirna/icap"
)
var (
stop = make(chan os.Signal, 1)
port = 1344
)
const (
previewBytes = 24
goodFileDetectStr = "GOOD FILE"
badFileDetectStr = "BAD FILE"
goodURL = "http://goodifle.com"
badURL = "http://badfile.com"
)
func startTestServer() {
icap.HandleFunc("/respmod", respmodHandler)
icap.HandleFunc("/reqmod", reqmodHandler)
log.Println("Starting ICAP test server...")
signal.Notify(stop, syscall.SIGKILL, syscall.SIGINT, syscall.SIGQUIT)
go func() {
if err := icap.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
log.Println("Failed to start ICAP test server: ", err.Error())
return
}
}()
time.Sleep(5 * time.Millisecond)
log.Printf("ICAP test server is running on localhost:%d\n...\n", port)
<-stop
log.Println("ICAP test server is shut down!")
}
func stopTestServer() {
stop <- syscall.SIGKILL
}
func respmodHandler(w icap.ResponseWriter, req *icap.Request) {
h := w.Header()
h.Set("ISTag", "ICAP-TEST")
h.Set("Service", "ICAP-TEST-SERVICE")
switch req.Method {
case "OPTIONS":
h.Set("Methods", "RESPMOD")
h.Set("Allow", "204")
if previewBytes > 0 {
h.Set("Preview", strconv.Itoa(previewBytes))
}
h.Set("Transfer-Preview", "*")
w.WriteHeader(http.StatusOK, nil, false)
case "RESPMOD":
defer req.Response.Body.Close()
if val, exist := req.Header["Allow"]; !exist || (len(val) > 0 && val[0] != "204") {
w.WriteHeader(http.StatusNoContent, nil, false)
return
}
// log.Println("The preview data: ", string(req.Preview))
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, req.Response.Body); err != nil {
log.Println("Failed to copy the response body to buffer: ", err.Error())
w.WriteHeader(http.StatusNoContent, nil, false)
return
}
status := 0
if strings.Contains(buf.String(), goodFileDetectStr) {
status = http.StatusNoContent
}
if strings.Contains(buf.String(), badFileDetectStr) {
status = http.StatusOK
}
w.WriteHeader(status, nil, false)
}
}
func reqmodHandler(w icap.ResponseWriter, req *icap.Request) {
h := w.Header()
h.Set("ISTag", "ICAP-TEST")
h.Set("Service", "ICAP-TEST-SERVICE")
switch req.Method {
case "OPTIONS":
h.Set("Methods", "REQMOD")
h.Set("Allow", "204")
if previewBytes > 0 {
h.Set("Preview", strconv.Itoa(previewBytes))
}
h.Set("Transfer-Preview", "*")
w.WriteHeader(http.StatusOK, nil, false)
case "REQMOD":
if val, exist := req.Header["Allow"]; !exist || (len(val) > 0 && val[0] != "204") {
w.WriteHeader(http.StatusNoContent, nil, false)
return
}
// log.Println("The preview data: ", string(req.Preview))
fileURL := req.Request.RequestURI
status := 0
if fileURL == goodURL {
status = http.StatusNoContent
}
if fileURL == badURL {
status = http.StatusOK
}
w.WriteHeader(status, nil, false)
}
}
func testServerRunning() bool {
lstnr, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return true
}
lstnr.Close()
return false
}