@@ -105,6 +105,78 @@ type Context struct {
105105}
106106```
107107
108+ ### Demo code
109+
110+ ``` go
111+ package main
112+
113+ import (
114+ " log"
115+ " net/http"
116+
117+ " github.com/go-httpproxy/httpproxy"
118+ )
119+
120+ func OnError (ctx *httpproxy.Context , when string ,
121+ err *httpproxy.Error , opErr error ) {
122+ // Log errors.
123+ log.Printf (" ERR: %s : %s [%s ]" , when, err, opErr)
124+ }
125+
126+ func OnAccept (ctx *httpproxy.Context , w http.ResponseWriter ,
127+ r *http.Request ) bool {
128+ // Handle local request has path "/info"
129+ if r.Method == " GET" && !r.URL .IsAbs () && r.URL .Path == " /info" {
130+ w.Write ([]byte (" This is go-httpproxy." ))
131+ return true
132+ }
133+ return false
134+ }
135+
136+ func OnAuth (ctx *httpproxy .Context , user string , pass string ) bool {
137+ // Auth test user.
138+ if user == " test" && pass == " 1234" {
139+ return true
140+ }
141+ return false
142+ }
143+
144+ func OnConnect (ctx *httpproxy .Context , host string ) (
145+ ConnectAction httpproxy.ConnectAction, newHost string) {
146+ // Apply "Man in the Middle" to all ssl connections. Never change host.
147+ return httpproxy.ConnectMitm , host
148+ }
149+
150+ func OnRequest (ctx *httpproxy .Context , req *http .Request ) (
151+ resp *http.Response) {
152+ // Log proxying requests.
153+ log.Printf (" INFO: Proxy: %s %s " , req.Method , req.URL .String ())
154+ return
155+ }
156+
157+ func OnResponse (ctx *httpproxy.Context , req *http.Request ,
158+ resp *http.Response ) {
159+ // Add header "Via: go-httpproxy".
160+ resp.Header .Add (" Via" , " go-httpproxy" )
161+ }
162+
163+ func main () {
164+ // Create a new proxy with default certificate pair.
165+ prx , _ := httpproxy.NewProxy ()
166+
167+ // Set handlers.
168+ prx.OnError = OnError
169+ prx.OnAccept = OnAccept
170+ prx.OnAuth = OnAuth
171+ prx.OnConnect = OnConnect
172+ prx.OnRequest = OnRequest
173+ prx.OnResponse = OnResponse
174+
175+ // Listen...
176+ http.ListenAndServe (" :8080" , prx)
177+ }
178+ ```
179+
108180## GoDoc
109181
110182[ https://godoc.org/github.com/go-httpproxy/httpproxy ] ( https://godoc.org/github.com/go-httpproxy/httpproxy )
0 commit comments