-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirect.go
65 lines (54 loc) · 1.57 KB
/
redirect.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
/*
An example of how to use go-icap.
Run this program and Squid on the same machine.
Put the following lines in squid.conf:
icap_enable on
icap_service service_req reqmod_precache icap://127.0.0.1:11344/golang
adaptation_access service_req allow all
(The ICAP server needs to be started before Squid is.)
Set your browser to use the Squid proxy.
Try browsing to http://gateway/ and http://java.com/
*/
package main
import (
"code.google.com/p/go-icap"
"fmt"
"net/http"
"os"
)
var ISTag = "\"GOLANG\""
func main() {
// Set the files to be made available under http://gateway/
http.Handle("/", http.FileServer(http.Dir(os.Getenv("HOME")+"/Sites")))
icap.HandleFunc("/golang", toGolang)
icap.ListenAndServe(":11344", icap.HandlerFunc(toGolang))
}
func toGolang(w icap.ResponseWriter, req *icap.Request) {
h := w.Header()
h.Set("ISTag", ISTag)
h.Set("Service", "Golang redirector")
switch req.Method {
case "OPTIONS":
h.Set("Methods", "REQMOD")
h.Set("Allow", "204")
w.WriteHeader(200, nil, false)
case "REQMOD":
switch req.Request.Host {
case "gateway":
// Run a fake HTTP server called gateway.
icap.ServeLocally(w, req)
case "java.com", "www.java.com":
// Redirect the user to a more interesting language.
req.Request.Host = "golang.org"
req.Request.URL.Host = "golang.org"
w.WriteHeader(200, req.Request, false)
// TODO: copy the body (if any) from the original request.
default:
// Return the request unmodified.
w.WriteHeader(204, nil, false)
}
default:
w.WriteHeader(405, nil, false)
fmt.Println("Invalid request method")
}
}