英文:
Getting HTTP headers from TCP Connections
问题
我正在使用golang为Linux编写一个类似于代理的应用程序。当执行该应用程序时,它将监听所有的TCP连接并将它们重定向到代理服务器地址。在应用程序中,还会在HTTP头部中添加一个"Proxy-Authorisation: Basic ..."的头部。当我查看TCP头部时,无法获取到HTTP头部。我做错了什么或者如何提取HTTP数据?是否有其他方法可以实现这个功能?
英文:
I was writing a proxifier like app for Linux in golang. The app when executed will listen for all the TCP connections and redirect them to the proxy server address. In between the app also adds a "Proxy-Authorisation : Basic ..." header to the HTTP Headers. When I saw the TCP headers I was unable to get the HTTP Headers. Where am I going wrong or How can I extract the HTTP Data? Is there any other way to achieve this?
答案1
得分: 1
我也是对golang不太熟悉,但是下面的代码对于从TCP套接字获取HTTP数据来说是有效的。
package main
import "net"
import "fmt"
import "io"
func main() {
fmt.Println("Launching server...")
ln, _ := net.Listen("tcp", ":8081")
conn, _ := ln.Accept()
tmp := make([]byte, 256)
for {
n, err := conn.Read(tmp)
if err != nil {
if err != io.EOF {
fmt.Println("read error:", err)
}
break
}
fmt.Println("rx:", string(tmp[:n]))
}
}
希望对你有帮助!
英文:
I am also new to golang but below code works for me as far as getting HTTP data from tcp socket is consern.
package main
import "net"
import "fmt"
import "io"
func main() {
fmt.Println("Launching server...")
ln, _ := net.Listen("tcp", ":8081")
conn, _ := ln.Accept()
tmp := make([]byte,256)
for {
n,err := conn.Read(tmp)
if err != nil {
if err != io.EOF {
fmt.Println("read error:", err)
}
break
}
fmt.Println("rx:", string(tmp[:n]))
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论