【资料】http包接口和结构体

Handler
//86
type Handler interface {
	ServeHTTP(ResponseWriter, *Request)
}

Handler 是个接口,只要结构体有了 ServeHTTP 这个方法,都可以实现这个接口。

 

ServeMux & DefaultServerMux

//2192行
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
}
func NewServeMux() *ServeMux { return new(ServeMux) }
var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux

//结构体方法
func (mux *ServeMux) match(path string) (h Handler, pattern string) {
func (mux *ServeMux) redirectToPathSlash(host, path string, u *url.URL) (*url.URL, bool) {
func (mux *ServeMux) shouldRedirectRLocked(host, path string) bool {
func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
func (mux *ServeMux) handler(host, path string) (h Handler, pattern string) {
func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
func (mux *ServeMux) Handle(pattern string, handler Handler) {
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {

可以看到,DefaultServeMux是一个默认的ServerMux,而ServeMux的结构体中,mu sync.RWMutex是一个并发控制的锁,一个装有muxEntry结构的map,一个hosts判断是否包含主机名。muxEntry结构体,其中一个变量h Handler和另一个变量pattern string

 

总结下:DefaultServeMux的代码看完,大概清楚了。DefaultServeMux是一个ServerMux结构,这个结构里面最最主要包含一个map[string]muxEntry,map里面的每个muxEntry又包含一个string类型的变量pattern和一个接口Handler,这个接口里面的方法是ServeHTTP(ResponseWriter, *Request)。

 

HandlerFunc
//2007 行
type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
	f(w, r)
}

conn

type conn struct {
	// server is the server on which the connection arrived.
	// Immutable; never nil.
	server *Server

	// cancelCtx cancels the connection-level context.
	cancelCtx context.CancelFunc

	// rwc is the underlying network connection.
	// This is never wrapped by other types and is the value given out
	// to CloseNotifier callers. It is usually of type *net.TCPConn or
	// *tls.Conn.
	rwc net.Conn

	// remoteAddr is rwc.RemoteAddr().String(). It is not populated synchronously
	// inside the Listener's Accept goroutine, as some implementations block.
	// It is populated immediately inside the (*conn).serve goroutine.
	// This is the value of a Handler's (*Request).RemoteAddr.
	remoteAddr string

	// tlsState is the TLS connection state when using TLS.
	// nil means not TLS.
	tlsState *tls.ConnectionState

	// werr is set to the first write error to rwc.
	// It is set via checkConnErrorWriter{w}, where bufw writes.
	werr error

	// r is bufr's read source. It's a wrapper around rwc that provides
	// io.LimitedReader-style limiting (while reading request headers)
	// and functionality to support CloseNotifier. See *connReader docs.
	r *connReader

	// bufr reads from r.
	bufr *bufio.Reader

	// bufw writes to checkConnErrorWriter{c}, which populates werr on error.
	bufw *bufio.Writer

	// lastMethod is the method of the most recent request
	// on this connection, if any.
	lastMethod string

	curReq atomic.Value // of *response (which has a Request in it)

	curState struct{ atomic uint64 } // packed (unixtime<<8|uint8(ConnState))

	// mu guards hijackedv
	mu sync.Mutex

	// hijackedv is whether this connection has been hijacked
	// by a Handler with the Hijacker interface.
	// It is guarded by mu.
	hijackedv bool
}

connReader

//642
type connReader struct {
	conn *conn

	mu      sync.Mutex // guards following
	hasByte bool
	byteBuf [1]byte
	cond    *sync.Cond
	inRead  bool
	aborted bool  // set true before conn.rwc deadline is set to past
	remain  int64 // bytes remaining
}
func (cr *connReader) lock() {
func (cr *connReader) unlock() { cr.mu.Unlock() }
func (cr *connReader) startBackgroundRead() {
func (cr *connReader) backgroundRead() {
func (cr *connReader) abortPendingRead() {
func (cr *connReader) setReadLimit(remain int64) { cr.remain = remain }
func (cr *connReader) setInfiniteReadLimit()     { cr.remain = maxInt64 }
func (cr *connReader) hitReadLimit() bool        { return cr.remain <= 0 }
func (cr *connReader) handleReadError(_ error) {
func (cr *connReader) closeNotify() {
func (cr *connReader) Read(p []byte) (n int, err error) {

    

  

 

 

 

 

 

 

posted @ 2020-12-08 17:12  梁天  阅读(244)  评论(0编辑  收藏  举报