-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvalidate.go
More file actions
63 lines (49 loc) · 1.37 KB
/
validate.go
File metadata and controls
63 lines (49 loc) · 1.37 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
package icapclient
import (
"errors"
"net/http"
"net/url"
)
// validMethod validates the ICAP method
func validMethod(method string) (bool, error) {
if _, registered := registeredMethods[method]; !registered {
return false, errors.New(ErrMethodNotRegistered)
}
return true, nil
}
// validURL validates the Server URL provided
func validURL(url *url.URL) (bool, error) {
if url.Scheme != SchemeICAP {
return false, errors.New(ErrInvalidScheme)
}
if url.Host == "" {
return false, errors.New(ErrInvalidHost)
}
return true, nil
}
// validMethodWithHTTP validates if the ICAP request method and the http messages are alligned or not
func validMethodWithHTTP(httpReq *http.Request, httpResp *http.Response, method string) (bool, error) {
if method == MethodREQMOD && httpReq == nil {
return false, errors.New(ErrREQMODWithNoReq)
}
if method == MethodREQMOD && httpResp != nil {
return false, errors.New(ErrREQMODWithResp)
}
if method == MethodRESPMOD && httpResp == nil {
return false, errors.New(ErrRESPMODWithNoResp)
}
return true, nil
}
// Validate validates the ICAP request
func (r *Request) Validate() error {
if valid, err := validMethod(r.Method); !valid {
return err
}
if valid, err := validURL(r.URL); !valid {
return err
}
if valid, err := validMethodWithHTTP(r.HTTPRequest, r.HTTPResponse, r.Method); !valid {
return err
}
return nil
}