本文整理汇总了Golang中github.com/google/martian/session.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: FromContext
// FromContext retrieves the auth.Context from the session.
func FromContext(ctx *session.Context) *Context {
if v, ok := ctx.GetSession().Get(key); ok {
return v.(*Context)
}
actx := &Context{}
ctx.GetSession().Set(key, actx)
return actx
}
开发者ID:haijianren,项目名称:martian,代码行数:11,代码来源:context.go
示例2: roundTrip
func (p *Proxy) roundTrip(ctx *session.Context, req *http.Request) (*http.Response, error) {
if ctx.SkippingRoundTrip() {
log.Debugf("martian: skipping round trip")
return proxyutil.NewResponse(200, nil, req), nil
}
if tr, ok := p.roundTripper.(*http.Transport); ok {
tr.Proxy = http.ProxyURL(p.proxyURL)
}
return p.roundTripper.RoundTrip(req)
}
开发者ID:xichen124,项目名称:martian,代码行数:12,代码来源:proxy.go
示例3: handle
func (p *Proxy) handle(ctx *session.Context, conn net.Conn, brw *bufio.ReadWriter) error {
log.Debugf("martian: waiting for request: %v", conn.RemoteAddr())
req, err := http.ReadRequest(brw.Reader)
if err != nil {
if isCloseable(err) {
log.Debugf("martian: connection closed prematurely: %v", err)
} else {
log.Errorf("martian: failed to read request: %v", err)
}
// TODO: TCPConn.WriteClose() to avoid sending an RST to the client.
return errClose
}
defer req.Body.Close()
if h, pattern := p.mux.Handler(req); pattern != "" {
defer brw.Flush()
closing := req.Close || p.Closing()
log.Infof("martian: intercepted configuration request: %s", req.URL)
rw := newResponseWriter(brw, closing)
defer rw.Close()
h.ServeHTTP(rw, req)
// Call WriteHeader to ensure a response is sent, since the handler isn't
// required to call WriteHeader/Write.
rw.WriteHeader(200)
if closing {
return errClose
}
return nil
}
ctx, err = session.FromContext(ctx)
if err != nil {
log.Errorf("martian: failed to derive context: %v", err)
return err
}
SetContext(req, ctx)
defer RemoveContext(req)
if tconn, ok := conn.(*tls.Conn); ok {
ctx.GetSession().MarkSecure()
cs := tconn.ConnectionState()
req.TLS = &cs
}
req.URL.Scheme = "http"
if ctx.GetSession().IsSecure() {
log.Debugf("martian: forcing HTTPS inside secure session")
req.URL.Scheme = "https"
}
req.RemoteAddr = conn.RemoteAddr().String()
if req.URL.Host == "" {
req.URL.Host = req.Host
}
log.Infof("martian: received request: %s", req.URL)
if req.Method == "CONNECT" {
if err := p.reqmod.ModifyRequest(req); err != nil {
log.Errorf("martian: error modifying CONNECT request: %v", err)
proxyutil.Warning(req.Header, err)
}
if p.mitm != nil {
log.Debugf("martian: attempting MITM for connection: %s", req.Host)
res := proxyutil.NewResponse(200, nil, req)
if err := p.resmod.ModifyResponse(res); err != nil {
log.Errorf("martian: error modifying CONNECT response: %v", err)
proxyutil.Warning(res.Header, err)
}
res.Write(brw)
brw.Flush()
log.Debugf("martian: completed MITM for connection: %s", req.Host)
tlsconn := tls.Server(conn, p.mitm.TLSForHost(req.Host))
brw.Writer.Reset(tlsconn)
brw.Reader.Reset(tlsconn)
return p.handle(ctx, tlsconn, brw)
}
log.Debugf("martian: attempting to establish CONNECT tunnel: %s", req.URL.Host)
res, cconn, cerr := p.connect(req)
if cerr != nil {
log.Errorf("martian: failed to CONNECT: %v", err)
res = proxyutil.NewResponse(502, nil, req)
//.........这里部分代码省略.........
开发者ID:xichen124,项目名称:martian,代码行数:101,代码来源:proxy.go
注:本文中的github.com/google/martian/session.Context类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论