Retrieving the Underlying Socket of a net/http Response
In the realm of network programming with Go's net/http package,有時我們可能需要直接存取底層的網路連線(net.Conn)。
問題陳述
一位開發者正在嘗試使用net/http為文件提供服務,卻遇到了一個問題:需要訪問處理程序函數中http.ResponseWriter的底層套接字( net.Conn),以便進行特定於平台的系統呼叫。
解決方法
在Go 1.13及更高版本中,net.Conn可以透過以下步驟儲存在請求上下文中:
- 使用SaveConnInContext函數將net .Conn保存到請求上下文中。
- 使用GetConn函數從請求中擷取net.Conn。
在此版本之前,有兩種替代方法:
使用遠端位址字串
對於在TCP連接埠上偵聽的伺服器,net.Conn.RemoteAddr().String()對於每個連線都是唯一的,可以用作全域連線映射的鍵。
覆蓋net.Listener.Accept ()
對於在UNIX套接字上偵聽的伺服器,我們可以重寫net.Listener.Accept()以使用文件描述符來傳回一個更獨特的值。
程式碼範例
Go 1.13及更高版本
<code class="go">// SaveConnInContext stores the net.Conn in the request context. func SaveConnInContext(ctx context.Context, c net.Conn) context.Context { return context.WithValue(ctx, ConnContextKey, c) } // GetConn retrieves the net.Conn from the request context. func GetConn(r *http.Request) net.Conn { return r.Context().Value(ConnContextKey).(net.Conn) }</code>
對於TCP.連接
<code class="go">// ConnStateEvent handles connection state events. func ConnStateEvent(conn net.Conn, event http.ConnState) { if event == http.StateActive { conns[conn.RemoteAddr().String()] = conn } else if event == http.StateHijacked || event == http.StateClosed { delete(conns, conn.RemoteAddr().String()) } } // GetConn retrieves the net.Conn from a map using the remote address as a key. func GetConn(r *http.Request) net.Conn { return conns[r.RemoteAddr] }</code>
對於TCP連接
<code class="go">// NewUnixListener creates a new UNIX listener with a modified Accept() method. func NewUnixListener(path string) (net.Listener, error) { // ... (setup code) l, err := net.Listen("unix", path) if err != nil { return nil, err } return NewConnSaveListener(l), nil } // NewConnSaveListener wraps a listener and overrides the Accept() method. func NewConnSaveListener(wrap net.Listener) net.Listener { return connSaveListener{wrap} } // remoteAddrPtrConn overrides the RemoteAddr() method to return a unique value. type remoteAddrPtrConn struct { net.Conn ptrStr string } func (self remoteAddrPtrConn) RemoteAddr() net.Addr { return remoteAddrPtr{self.ptrStr} } // remoteAddrPtr implements the net.Addr interface. type remoteAddrPtr struct { ptrStr string } func (remoteAddrPtr) Network() string { return "" } func (self remoteAddrPtr) String() string { return self.ptrStr } // Accept overrides the default Accept() method to store the net.Conn in a map. func (self connSaveListener) Accept() (net.Conn, error) { conn, err := self.Listener.Accept() ptrStr := fmt.Sprintf("%d", &conn) conns[ptrStr] = conn return remoteAddrPtrConn{conn, ptrStr}, err }</code>對於TCP. 對於UNIX連接透過這些方法,開發人員可以輕鬆存取http.ResponseWriter的底層套接字,從而為其自訂網路處理開啟了一系列可能性。
以上是如何在 Go 中存取 net/http 回應的底層套接字?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文演示了創建模擬和存根進行單元測試。 它強調使用接口,提供模擬實現的示例,並討論最佳實踐,例如保持模擬集中並使用斷言庫。 文章

本文探討了GO的仿製藥自定義類型約束。 它詳細介紹了界面如何定義通用功能的最低類型要求,從而改善了類型的安全性和代碼可重複使用性。 本文還討論了局限性和最佳實踐

本文討論了GO的反思軟件包,用於運行時操作代碼,對序列化,通用編程等有益。它警告性能成本,例如較慢的執行和更高的內存使用,建議明智的使用和最佳

本文討論了GO中使用表驅動的測試,該方法使用測試用例表來測試具有多個輸入和結果的功能。它突出了諸如提高的可讀性,降低重複,可伸縮性,一致性和A

本文使用跟踪工具探討了GO應用程序執行流。 它討論了手冊和自動儀器技術,比較諸如Jaeger,Zipkin和Opentelemetry之類的工具,並突出顯示有效的數據可視化

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。