1. Golang-web Working mechanism
1.web Mode of operation:
Browser url–> Request DNS server resolve domain name to corresponding ip–> establish TCP protocol –> send Http request–>web server processing Request –>http Response return client –> render response Body –> presented to the user UI interface
2.Http Request
GET request: http://xx.xx.com?name=test1&id=123//use? to separate the URL and transfer data between parameters with the & connection URL has a length limit, clear text transmission, parameters in the URL.
3.http response
Status code: 1XX: Request received, continue processing 2XX: Success, request has been successfully processed 3XX: REDIRECT 4XX: Client error 5XX: server-side error
4.http Package Run Process:
(1.) Create a listen Socket listener on the specified port, waiting for the client's request to arrive.
(2.) Listensocket receives the client's request, obtains the clients socket, communicates with the client through the clients socket.
(3) Processing the client's request, first read the HTTP request from the client socket protocol header, if it is a POST request, also need to read the client submitted data, and then to the corresponding handler processing request, handler processing, the data back to the client.
(4) Specific implementation:
Initializes a server that calls net. Listen ("tcp", addr) monitors ports, srv. Serve (NET. Listener) receives request information from the client. Create a connection, execute gorouting
(5.) Two core features of Go HTTP, Conn and servmux, for high concurrency and high performance.
Srv.netconn (rw)//Each request will create a connection that holds the information for this request and passes that information to the header of the corresponding Handler,handler read response. This preserves the independence of each request.
Go C.serve ()
Type Servemux struct {
Mu sync. Rwmutex//Lock, due to request design to concurrent processing, therefore need to lock
M map[string]muxenty//Routing rule, a string (route expression) corresponds to a MUX entity {explicit BOOL//whether the exact match H Handler//This route expression corresponds to which Handler pattern s Tring//Match String}
hosts bool//whether in arbitrary rules with host information
}
Type Handler interface {
Servehttp (respnsewriter,*request)//Routing Actuator
}
Type handlerfunc func (Responsewriter, *request)
Servehttp calls F (W, R).
Func (f Handlerfunc) servehttp (w responsewriter, R *request) {
F (W, R)
}
Func (Mux *servemux) servehttp(w responsewriter, R *request) {
if R.requesturi = = "*" {
W.header (). Set ("Connection", "close")
W.writeheader (Statusbadrequest)
Return
}
H, _: = Mux. Handler (R)
H.servehttp (W, R)
}
Func (Mux *servemux) Handler (R *request) (H Handler, pattern string) {
If R.method! = "CONNECT" {
If p: = CleanPath (R.url. Path); P! = R.url. Path {
_, Pattern = Mux.handler (R.host, p)
Return Redirecthandler (P, statusmovedpermanently), pattern
}
}
Return Mux.handler (R.host, R.url. Path)
}
Func (Mux *servemux) handler (host, path string) (H handler, pattern string) {
Mux.mu.RLock ()
Defer Mux.mu.RUnlock ()
Host-specific pattern takes precedence over generic ones
If mux.hosts {
H, pattern = mux.match (host + path)
}
if H = = Nil {
H, pattern = mux.match (path)
}
if H = = Nil {
H, pattern = Notfoundhandler (), ""
}
Return
}
5. Execution Process:
First Call Http.handlefunc
In order to do a few things:
1 called the Handlefunc of Defaultservemux.
2 called the handle of Defaultservemux.
3 Add the corresponding handler and routing rules to the map[string]muxentry in Defaultservemux
Next, call HTTP. Listenandserve (": 9090", nil)
In order to do a few things:
1 instantiating the server
2 calling the server's Listenandserve ()
3 Call net. Listen ("tcp", addr) Listening port
4 Start A For loop, accept requests in the loop body
5 instantiate a conn for each request and open a Goroutine to service the request Go C.serve ()
6 read the contents of each request W, err: = C.readrequest ()
7 Determine if handler is empty, if not set handler (this example is not set handler), handler is set to Defaultservemux
8 Calling Handler's Servehttp
9 In this example, the following goes into the Defaultservemux.servehttp
10 Select handler according to request and enter into this handler servehttp
Mux.handler (R). Servehttp (W, R)
11 Select handler:
A determine if there is a route to satisfy this request (looping through Servermux's muxentry)
B If there is a route to satisfy, call this route handler Servehttp
C If no route is met, call Notfoundhandler's servehttp