douao1858
2018-10-10 15:31
浏览 15
已采纳

从net.Conn检索URI路径

I'm working with someone else's code and I need some assistance.

Here we have a tcp load balancer. What I need is access to the request uri from this piece of code before we do the net dial.

I looked through the API Documentation here: https://golang.org/pkg/net/ but was unable to find any relevant methods under the net.Conn namespace for retrieving the current request path.

func copy(wc io.WriteCloser, r io.Reader) {
    defer wc.Close()
    io.Copy(wc, r)    
}

func handleConnection(us net.Conn, backend BA.Backend) {
    if backend == nil {
        log.Printf("no backend available for connection from %s", us.RemoteAddr())
        us.Close()
        return
    }           

    ip:=us.RemoteAddr().String()
    parts:=strings.Split(ip,":")
    ip=parts[0]

    //w := bufio.NewWriter(us)
    //w.WriteString(+"
")
    //w.Flush()    
    ds, err := net.Dial("tcp", backend.String())
    if err != nil {
        log.Printf("failed to dial %s: %s", backend, err)
        us.Close()
        return
    }

    // Ignore errors
    go copy(ds, us)
    go copy(us, ds)
}

func tcpBalance(bind string, backends BA.Backends) error {    
    log.Println("using tcp balancing")
    ln, err := net.Listen("tcp", bind)
    if err != nil {
        return fmt.Errorf("failed to bind: %s", err)
    }

    log.Printf("listening on %s, balancing %d backends", bind, backends.Len())

    for {
        conn, err := ln.Accept()
        if err != nil {
            log.Printf("failed to accept: %s", err)
            continue
        }        
        go handleConnection(conn, backends.Choose())
    }

    return err
}

图片转代码服务由CSDN问答提供 功能建议

我正在使用其他人的代码,我需要一些帮助。

在这里,我们有一个tcp负载平衡器。 我需要的是在我们进行网络拨号之前从这段代码访问请求uri。

我在这里浏览了API文档: https:// golang。 org / pkg / net / ,但无法在 net.Conn 命名空间下找到用于检索当前请求路径的任何相关方法。

 < 代码> func copy(wc io.WriteCloser,r io.Reader){
延迟wc.Close()
 io.Copy(wc,r)
} 
 
func handleConnection(我们net.Conn,后端BA  .Backend){
,如果backend == nil {
 log.Printf(“没有后端可用于从%s连接”,us.RemoteAddr())
 us.Close()
 return 
} 
  
 ip:= us.RemoteAddr()。String()
部分:= strings.Split(ip,“:”)
 ip = parts [0] 
 
 // w:= bufio.NewWriter(  us)
 //w.WriteString(+"
")
 //w.Flush()
 ds,err:= net.Dial(“ tcp”,backend.String())
如果出现错误 != nil {
 log.Printf(“无法拨打%s:%s”,后端,错误)
 us.Close()
 return 
} 
 
 //忽略错误
复制 (ds,us)  
去复制(我们,ds)
} 
 
func tcpBalance(绑定字符串,后端BA.Backends)错误{
 log.Println(“使用tcp平衡”)
 ln,err:= net.Listen  (“ tcp”,bind)
如果错误!= nil {
返回fmt.Errorf(“绑定失败:%s”,err)
} 
 
 log.Printf(“正在%s监听, 平衡%d个后端”,绑定后端。Len())
 
 for {
 conn,err:= ln.Accept()
如果err!= nil {
 log.Printf(“无法接受 :%s“,err)
继续
} 
 go handleConnection(conn,backends.Choose())
} 
 
返回err 
} 
   
  
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhi6365 2018-10-10 15:35
    已采纳

    This is not possible because not every TCP connection is an HTTP connection and the TCP protocol has no concept of URIs. Many other protocols are built on TCP and it's not that protocol's responsibility to know about them.

    HTTP is an Application Layer (layer 7) protocol which uses the TCP Transport Layer (layer 4) protocol. As such, TCP connections have no concept of HTTP because lower layer protocols have no concept of the layers that may be built on top of them.

    You can learn more by reading about the OSI model.

    点赞 打赏 评论

相关推荐 更多相似问题