PageRenderTime 142ms CodeModel.GetById 80ms app.highlight 5ms RepoModel.GetById 56ms app.codeStats 0ms

/example/redirect.go

https://code.google.com/p/go-icap/
Go | 65 lines | 38 code | 6 blank | 21 comment | 2 complexity | 27854b49a14398f626d13859bb962149 MD5 | raw file
 1/*
 2An example of how to use go-icap.
 3
 4Run this program and Squid on the same machine.
 5Put the following lines in squid.conf:
 6
 7icap_enable on
 8icap_service service_req reqmod_precache icap://127.0.0.1:11344/golang
 9adaptation_access service_req allow all
10
11(The ICAP server needs to be started before Squid is.)
12
13Set your browser to use the Squid proxy.
14
15Try browsing to http://gateway/ and http://java.com/
16*/
17package main
18
19import (
20	"code.google.com/p/go-icap"
21	"fmt"
22	"net/http"
23	"os"
24)
25
26var ISTag = "\"GOLANG\""
27
28func main() {
29	// Set the files to be made available under http://gateway/
30	http.Handle("/", http.FileServer(http.Dir(os.Getenv("HOME")+"/Sites")))
31
32	icap.HandleFunc("/golang", toGolang)
33	icap.ListenAndServe(":11344", icap.HandlerFunc(toGolang))
34}
35
36func toGolang(w icap.ResponseWriter, req *icap.Request) {
37	h := w.Header()
38	h.Set("ISTag", ISTag)
39	h.Set("Service", "Golang redirector")
40
41	switch req.Method {
42	case "OPTIONS":
43		h.Set("Methods", "REQMOD")
44		h.Set("Allow", "204")
45		w.WriteHeader(200, nil, false)
46	case "REQMOD":
47		switch req.Request.Host {
48		case "gateway":
49			// Run a fake HTTP server called gateway.
50			icap.ServeLocally(w, req)
51		case "java.com", "www.java.com":
52			// Redirect the user to a more interesting language.
53			req.Request.Host = "golang.org"
54			req.Request.URL.Host = "golang.org"
55			w.WriteHeader(200, req.Request, false)
56			// TODO: copy the body (if any) from the original request.
57		default:
58			// Return the request unmodified.
59			w.WriteHeader(204, nil, false)
60		}
61	default:
62		w.WriteHeader(405, nil, false)
63		fmt.Println("Invalid request method")
64	}
65}