1. http包建立web服务器

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "strings"
  7. )
  8. func sayhelloName(w http.ResponseWriter, r *http.Request) {
  9. r.ParseForm()
  10. fmt.Println(r.Form)
  11. fmt.Println("path", r.URL.Path)
  12. fmt.Println("scheme", r.URL.Scheme)
  13. fmt.Println(r.Form["url_long"])
  14. for k, v := range r.Form {
  15. fmt.Println("key:", k)
  16. fmt.Println("val:", strings.Join((v), ""))
  17. }
  18. fmt.Println(w, "hello world")
  19. }
  20. func main() {
  21. http.HandleFunc("/", sayhelloName)
  22. err := http.ListenAndServe(":9090", nil)
  23. if err != nil {
  24. log.Fatal("ListenAndServe:", err)
  25. }
  26. }

2. http包的运行机制

相关源码位于:/src/net/http/server.go

服务端的几个概念

  • Request:用户请求的信息,用来解析用户的请求信息,包括post,get,Cookie,url等信息。
  • Response:服务器需要反馈给客户端的信息。
  • Conn:用户的每次请求链接。
  • Handle:处理请求和生成返回信息的处理逻辑。

Go实现web服务的流程

  1. 创建Listen Socket,监听指定的端口,等待客户端请求到来。
  2. Listen Socket接受客户端的请求,得到Client Socket,接下来通过Client Socket与客户端通信。
  3. 处理客户端请求,首先从Client Socket读取HTTP请求的协议头,如果是POST方法,还可能要读取客户端提交的数据,然后交给相应的handler处理请求,handler处理完,将数据通过Client Socket返回给客户端。

2.1. http包执行流程图

image2017-3-5 22-46-35

2.2. 注册路由[HandleFunc]

http.HandlerFunc类型默认实现了ServeHTTP的接口。

  1. // The HandlerFunc type is an adapter to allow the use of
  2. // ordinary functions as HTTP handlers. If f is a function
  3. // with the appropriate signature, HandlerFunc(f) is a
  4. // Handler that calls f.
  5. type HandlerFunc func(ResponseWriter, *Request)
  6. // ServeHTTP calls f(w, r).
  7. func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
  8. f(w, r)
  9. }
  1. // HandleFunc registers the handler function for the given pattern
  2. // in the DefaultServeMux.
  3. // The documentation for ServeMux explains how patterns are matched.
  4. func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
  5. DefaultServeMux.HandleFunc(pattern, handler)
  6. }
  7. ...
  8. // HandleFunc registers the handler function for the given pattern.
  9. func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
  10. mux.Handle(pattern, HandlerFunc(handler))
  11. }

Handle

  1. // Handle registers the handler for the given pattern.
  2. // If a handler already exists for pattern, Handle panics.
  3. func (mux *ServeMux) Handle(pattern string, handler Handler) {
  4. mux.mu.Lock()
  5. defer mux.mu.Unlock()
  6. if pattern == "" {
  7. panic("http: invalid pattern " + pattern)
  8. }
  9. if handler == nil {
  10. panic("http: nil handler")
  11. }
  12. if mux.m[pattern].explicit {
  13. panic("http: multiple registrations for " + pattern)
  14. }
  15. mux.m[pattern] = muxEntry{explicit: true, h: handler, pattern: pattern}
  16. if pattern[0] != '/' {
  17. mux.hosts = true
  18. }
  19. // Helpful behavior:
  20. // If pattern is /tree/, insert an implicit permanent redirect for /tree.
  21. // It can be overridden by an explicit registration.
  22. n := len(pattern)
  23. if n > 0 && pattern[n-1] == '/' && !mux.m[pattern[0:n-1]].explicit {
  24. // If pattern contains a host name, strip it and use remaining
  25. // path for redirect.
  26. path := pattern
  27. if pattern[0] != '/' {
  28. // In pattern, at least the last character is a '/', so
  29. // strings.Index can't be -1.
  30. path = pattern[strings.Index(pattern, "/"):]
  31. }
  32. url := &url.URL{Path: path}
  33. mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(url.String(), StatusMovedPermanently), pattern: pattern}
  34. }
  35. }

2.3. 如何监听端口

通过ListenAndServe来监听,底层实现:初始化一个server对象,调用net.Listen(“tcp”,addr),也就是底层用TCP协议搭建了一个服务,监听设置的端口。然后调用srv.Serve(net.Listener)函数,这个函数处理接收客户端的请求信息。这个函数里起了一个for循环,通过Listener接收请求,创建conn,开一个goroutine,把请求的数据当作参数给conn去服务:go c.serve(),即每次请求都是在新的goroutine中去服务,利于高并发。

src/net/http/server.go

  1. // ListenAndServe always returns a non-nil error.
  2. func ListenAndServe(addr string, handler Handler) error {
  3. server := &Server{Addr: addr, Handler: handler}
  4. return server.ListenAndServe()
  5. }
  6. ...
  7. // ListenAndServe listens on the TCP network address srv.Addr and then
  8. // calls Serve to handle requests on incoming connections.
  9. // Accepted connections are configured to enable TCP keep-alives.
  10. // If srv.Addr is blank, ":http" is used.
  11. // ListenAndServe always returns a non-nil error.
  12. func (srv *Server) ListenAndServe() error {
  13. addr := srv.Addr
  14. if addr == "" {
  15. addr = ":http"
  16. }
  17. ln, err := net.Listen("tcp", addr)
  18. if err != nil {
  19. return err
  20. }
  21. return srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
  22. }

2.4. 如何接收客户端的请求

srv.Serve

  1. // Serve accepts incoming connections on the Listener l, creating a
  2. // new service goroutine for each. The service goroutines read requests and
  3. // then call srv.Handler to reply to them.
  4. // Serve always returns a non-nil error.
  5. func (srv *Server) Serve(l net.Listener) error {
  6. defer l.Close()
  7. if fn := testHookServerServe; fn != nil {
  8. fn(srv, l)
  9. }
  10. var tempDelay time.Duration // how long to sleep on accept failure
  11. if err := srv.setupHTTP2(); err != nil {
  12. return err
  13. }
  14. for {
  15. rw, e := l.Accept()
  16. if e != nil {
  17. if ne, ok := e.(net.Error); ok && ne.Temporary() {
  18. if tempDelay == 0 {
  19. tempDelay = 5 * time.Millisecond
  20. } else {
  21. tempDelay *= 2
  22. }
  23. if max := 1 * time.Second; tempDelay > max {
  24. tempDelay = max
  25. }
  26. srv.logf("http: Accept error: %v; retrying in %v", e, tempDelay)
  27. time.Sleep(tempDelay)
  28. continue
  29. }
  30. return e
  31. }
  32. tempDelay = 0
  33. c := srv.newConn(rw)
  34. c.setState(c.rwc, StateNew) // before Serve can return
  35. go c.serve()
  36. }
  37. }

关键代码:

  1. c := srv.newConn(rw)
  2. c.setState(c.rwc, StateNew) // before Serve can return
  3. go c.serve()

newConn

  1. // Create new connection from rwc.
  2. func (srv *Server) newConn(rwc net.Conn) *conn {
  3. c := &conn{
  4. server: srv,
  5. rwc: rwc,
  6. }
  7. if debugServerConnections {
  8. c.rwc = newLoggingConn("server", c.rwc)
  9. }
  10. return c
  11. }

2.5. 如何分配handler

conn先解析request:c.readRequest(),获取相应的handler:handler:=c.server.Handler,即ListenAndServe的第二个参数,因为值为nil,所以默认handler=DefaultServeMux。该变量是一个路由器,用来匹配url跳转到其相应的handle函数。其中http.HandleFunc(“/“,sayhelloName)即注册了请求“/”的路由规则,当uri为“/”时,路由跳转到函数sayhelloName。DefaultServeMux会调用ServeHTTP方法,这个方法内部调用sayhelloName本身,最后写入response的信息反馈给客户端。

2.5.1. c.serve()

  1. // Serve a new connection.
  2. func (c *conn) serve() {
  3. ...
  4. for {
  5. w, err := c.readRequest()
  6. ...
  7. serverHandler{c.server}.ServeHTTP(w, w.req)
  8. ..
  9. }
  10. }

2.5.2. c.readRequest()

  1. // Read next request from connection.
  2. func (c *conn) readRequest() (w *response, err error) {
  3. if c.hijacked() {
  4. return nil, ErrHijacked
  5. }
  6. if d := c.server.ReadTimeout; d != 0 {
  7. c.rwc.SetReadDeadline(time.Now().Add(d))
  8. }
  9. if d := c.server.WriteTimeout; d != 0 {
  10. defer func() {
  11. c.rwc.SetWriteDeadline(time.Now().Add(d))
  12. }()
  13. }
  14. c.r.setReadLimit(c.server.initialReadLimitSize())
  15. c.mu.Lock() // while using bufr
  16. if c.lastMethod == "POST" {
  17. // RFC 2616 section 4.1 tolerance for old buggy clients.
  18. peek, _ := c.bufr.Peek(4) // ReadRequest will get err below
  19. c.bufr.Discard(numLeadingCRorLF(peek))
  20. }
  21. req, err := readRequest(c.bufr, keepHostHeader)
  22. c.mu.Unlock()
  23. if err != nil {
  24. if c.r.hitReadLimit() {
  25. return nil, errTooLarge
  26. }
  27. return nil, err
  28. }
  29. c.lastMethod = req.Method
  30. c.r.setInfiniteReadLimit()
  31. hosts, haveHost := req.Header["Host"]
  32. if req.ProtoAtLeast(1, 1) && (!haveHost || len(hosts) == 0) {
  33. return nil, badRequestError("missing required Host header")
  34. }
  35. if len(hosts) > 1 {
  36. return nil, badRequestError("too many Host headers")
  37. }
  38. if len(hosts) == 1 && !validHostHeader(hosts[0]) {
  39. return nil, badRequestError("malformed Host header")
  40. }
  41. for k, vv := range req.Header {
  42. if !validHeaderName(k) {
  43. return nil, badRequestError("invalid header name")
  44. }
  45. for _, v := range vv {
  46. if !validHeaderValue(v) {
  47. return nil, badRequestError("invalid header value")
  48. }
  49. }
  50. }
  51. delete(req.Header, "Host")
  52. req.RemoteAddr = c.remoteAddr
  53. req.TLS = c.tlsState
  54. if body, ok := req.Body.(*body); ok {
  55. body.doEarlyClose = true
  56. }
  57. w = &response{
  58. conn: c,
  59. req: req,
  60. reqBody: req.Body,
  61. handlerHeader: make(Header),
  62. contentLength: -1,
  63. }
  64. w.cw.res = w
  65. w.w = newBufioWriterSize(&w.cw, bufferBeforeChunkingSize)
  66. return w, nil
  67. }

2.5.3. ServeHTTP(w, w.req)

  1. func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {
  2. handler := sh.srv.Handler
  3. if handler == nil {
  4. handler = DefaultServeMux
  5. }
  6. if req.RequestURI == "*" && req.Method == "OPTIONS" {
  7. handler = globalOptionsHandler{}
  8. }
  9. handler.ServeHTTP(rw, req)
  10. }

2.5.4. DefaultServeMux

  1. type ServeMux struct {
  2. mu sync.RWMutex
  3. m map[string]muxEntry
  4. hosts bool // whether any patterns contain hostnames
  5. }
  6. type muxEntry struct {
  7. explicit bool
  8. h Handler
  9. pattern string
  10. }
  11. // NewServeMux allocates and returns a new ServeMux.
  12. func NewServeMux() *ServeMux { return &ServeMux{m: make(map[string]muxEntry)} }
  13. // DefaultServeMux is the default ServeMux used by Serve.
  14. var DefaultServeMux = NewServeMux()

handler接口的定义

  1. type Handler interface {
  2. ServeHTTP(ResponseWriter, *Request)
  3. }

2.5.5. ServeMux.ServeHTTP

  1. // ServeHTTP dispatches the request to the handler whose
  2. // pattern most closely matches the request URL.
  3. func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
  4. if r.RequestURI == "*" {
  5. if r.ProtoAtLeast(1, 1) {
  6. w.Header().Set("Connection", "close")
  7. }
  8. w.WriteHeader(StatusBadRequest)
  9. return
  10. }
  11. h, _ := mux.Handler(r)
  12. h.ServeHTTP(w, r)
  13. }

mux.Handler(r)

  1. // Handler returns the handler to use for the given request,
  2. // consulting r.Method, r.Host, and r.URL.Path. It always returns
  3. // a non-nil handler. If the path is not in its canonical form, the
  4. // handler will be an internally-generated handler that redirects
  5. // to the canonical path.
  6. //
  7. // Handler also returns the registered pattern that matches the
  8. // request or, in the case of internally-generated redirects,
  9. // the pattern that will match after following the redirect.
  10. //
  11. // If there is no registered handler that applies to the request,
  12. // Handler returns a ``page not found'' handler and an empty pattern.
  13. func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
  14. if r.Method != "CONNECT" {
  15. if p := cleanPath(r.URL.Path); p != r.URL.Path {
  16. _, pattern = mux.handler(r.Host, p)
  17. url := *r.URL
  18. url.Path = p
  19. return RedirectHandler(url.String(), StatusMovedPermanently), pattern
  20. }
  21. }
  22. return mux.handler(r.Host, r.URL.Path)
  23. }
  24. // handler is the main implementation of Handler.
  25. // The path is known to be in canonical form, except for CONNECT methods.
  26. func (mux *ServeMux) handler(host, path string) (h Handler, pattern string) {
  27. mux.mu.RLock()
  28. defer mux.mu.RUnlock()
  29. // Host-specific pattern takes precedence over generic ones
  30. if mux.hosts {
  31. h, pattern = mux.match(host + path)
  32. }
  33. if h == nil {
  34. h, pattern = mux.match(path)
  35. }
  36. if h == nil {
  37. h, pattern = NotFoundHandler(), ""
  38. }
  39. return
  40. }

2.6. http连接处理流程图

image2017-3-5 23-50-6

3. http的执行流程总结

1、首先调用Http.HandleFunc,按如下顺序执行:

  1. 调用了DefaultServerMux的HandleFunc。
  2. 调用了DefaultServerMux的Handle。
  3. 往DefaultServerMux的map[string] muxEntry中增加对应的handler和路由规则。

2、调用http.ListenAndServe(“:9090”,nil),按如下顺序执行:

  1. 实例化Server。
  2. 调用Server的ListenAndServe()。
  3. 调用net.Listen(“tcp”,addr)监听端口。
  4. 启动一个for循环,在循环体中Accept请求。
  5. 对每个请求实例化一个Conn,并且开启一个goroutine为这个请求进行服务go c.serve()。
  6. 读取每个请求的内容w,err:=c.readRequest()。
  7. 判断handler是否为空,如果没有设置handler,handler默认设置为DefaultServeMux。
  8. 调用handler的ServeHttp。
  9. 根据request选择handler,并且进入到这个handler的ServeHTTP,
    mux.handler(r).ServeHTTP(w,r)
  10. 选择handler
  • 判断是否有路由能满足这个request(循环遍历ServeMux的muxEntry)。
  • 如果有路由满足,调用这个路由handler的ServeHttp。
  • 如果没有路由满足,调用NotFoundHandler的ServeHttp。

4. 自定义路由

Go支持外部实现路由器,ListenAndServe的第二个参数就是配置外部路由器,它是一个Handler接口。即外部路由器实现Hanlder接口。

Handler接口:

  1. type Handler interface {
  2. ServeHTTP(ResponseWriter, *Request)
  3. }

自定义路由

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. type MyMux struct{
  7. }
  8. func (p *MyMux) ServeHTTP(w http.ResponseWriter,r *http.Request){
  9. if r.URL.Path=="/"{
  10. sayhelloName(w,r)
  11. return
  12. }
  13. http.NotFound(w,r)
  14. return
  15. }
  16. func sayhelloName(w http.ResponseWriter,r *http.Request){
  17. fmt.Fprintln(w,"Hello myroute")
  18. }
  19. func main() {
  20. mux:=&MyMux{}
  21. http.ListenAndServe(":9090",mux)
  22. }

文章参考:

《Go web编程》