Golang?HTTP編程的源碼解析詳解
1、網(wǎng)絡(luò)基礎(chǔ)
基本TCP客戶-服務(wù)器程序Socket編程流程如如下圖所示。
TCP服務(wù)器綁定到特定端口并阻塞監(jiān)聽客戶端端連接,
TCP客戶端則通過IP+端口向服務(wù)器發(fā)起請求,客戶-服務(wù)器建立連接之后就能開始進(jìn)行數(shù)據(jù)傳輸。
Golang的TCP編程也是基于上述流程的。
2、Golang HTTP編程
2.1 代碼示例
func timeHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%v", time.Now().Format(time.RFC3339)) } func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%v", "hello world.") } func main() { // 1. 新建路由解碼器 h := http.NewServeMux() // 2. 路由注冊 h.HandleFunc("/hello", helloHandler) h.HandleFunc("/time", timeHandler) // 3. 服務(wù)啟動 阻塞監(jiān)聽 http.ListenAndServe(":8000", h) }
運(yùn)行上述程序,在瀏覽器地址欄分別輸入 http://localhost:8000/hello http://localhost:8000/time 結(jié)果分別如下圖所示。
2.2 源碼分析
分析從路由注冊到響應(yīng)用戶請求的流程。
2.2.1 新建解碼器 h := http.NewServeMux()
type ServeMux struct { mu sync.RWMutex m map[string]muxEntry es []muxEntry // slice of entries sorted from longest to shortest. hosts bool // whether any patterns contain hostnames } type muxEntry struct { h Handler pattern string } // NewServeMux allocates and returns a new ServeMux. func NewServeMux() *ServeMux { return new(ServeMux) }
Handler是interface,定義如下
type Handler interface { ServeHTTP(ResponseWriter, *Request) }
ServeMux實(shí)現(xiàn)了Handler接口。
2.2.2 路由注冊 h.HandleFunc("/hello", helloHandler)
// HandleFunc registers the handler function for the given pattern. func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { ... mux.Handle(pattern, HandlerFunc(handler)) } func (mux *ServeMux) Handle(pattern string, handler Handler) { ... e := muxEntry{h: handler, pattern: pattern} mux.m[pattern] = e if pattern[len(pattern)-1] == '/' { mux.es = appendSorted(mux.es, e) } ... }
timeHandler和helloHandler函數(shù)被強(qiáng)制轉(zhuǎn)換為type HandlerFunc func(ResponseWriter, *Request)類型,且實(shí)現(xiàn)了Handler接口。
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { f(w, r) }
mux.m建立了路由到處理函數(shù)timeHandler和helloHandler的映射。
2.2.3 服務(wù)啟動阻塞監(jiān)聽 http.ListenAndServe(":8000", h)
包裝Server結(jié)構(gòu)體,HTTP使用TCP協(xié)議。
func ListenAndServe(addr string, handler Handler) error { server := &Server{Addr: addr, Handler: handler} return server.ListenAndServe() } func (srv *Server) ListenAndServe() error { ... ln, err := net.Listen("tcp", addr) if err != nil { return err } return srv.Serve(ln) }
net.Listen封裝了Socket編程的socket,bind,listen的調(diào)用,極大的方便了使用者。
阻塞監(jiān)聽請求,新建goroutine處理每個新請求。
func (srv *Server) Serve(l net.Listener) error { ... for { rw, err := l.Accept() ... c := srv.newConn(rw) c.setState(c.rwc, StateNew, runHooks) // before Serve can return go c.serve(connCtx) } } // Serve a new connection. func (c *conn) serve(ctx context.Context) { ... serverHandler{c.server}.ServeHTTP(w, w.req) ... } func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) { handler := sh.srv.Handler ... handler.ServeHTTP(rw, req) }
通過前面的流程推導(dǎo)可知,handler是http.ListenAndServe的第二個參數(shù)ServeMux
// ServeHTTP dispatches the request to the handler whose // pattern most closely matches the request URL. func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) { ... h, _ := mux.Handler(r) // 通過路由獲取處理函數(shù) h.ServeHTTP(w, r) }
mux.Handler使用mux.m這個map通過請求URL找到對應(yīng)處理函數(shù)的。
h的實(shí)際類型為HandlerFunc,根據(jù)2.2.2會調(diào)用到具體函數(shù)timeHandler或者h(yuǎn)elloHandler。
3. 總結(jié)
golang對socket編程進(jìn)行了封裝,給HTTP編程帶來了極大的便利。
但是不支持以下特性
1. 路由分組 對路由進(jìn)行分組,可以方便分組鑒權(quán)
2. 動態(tài)路由 如動態(tài)路由/user/:username/post/:postid不支持
以上就是Golang HTTP編程的源碼解析詳解的詳細(xì)內(nèi)容,更多關(guān)于Golang HTTP編程的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go網(wǎng)絡(luò)編程TCP抓包實(shí)操示例探究
作為一名軟件開發(fā)者,網(wǎng)絡(luò)編程是必備知識,本文通過?Go?語言實(shí)現(xiàn)?TCP?套接字編程,并結(jié)合?tcpdump?工具,展示它的三次握手、數(shù)據(jù)傳輸以及四次揮手的過程,幫助讀者更好地理解?TCP?協(xié)議與?Go?網(wǎng)絡(luò)編程2024-01-01搭建Go語言的ORM框架Gorm的具體步驟(從Java到go)
很多朋友不知道如何使用Goland軟件,搭建一個ORM框架GORM,今天小編給大家分享一篇教程關(guān)于搭建Go語言的ORM框架Gorm的具體步驟(從Java到go),感興趣的朋友跟隨小編一起學(xué)習(xí)下吧2022-09-09解決golang post文件時Content-Type出現(xiàn)的問題
這篇文章主要介紹了解決golang post文件時Content-Type出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05