从TCP连接获取HTTP头部

huangapple go评论88阅读模式
英文:

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]))
		} 
}

huangapple
  • 本文由 发表于 2015年12月30日 21:37:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/34530628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定