Objective:
There are several ways that Golang handles HTTP in general:
Use NET packages directly (this is a very low-level package, which is the TCP hierarchy and needs to be encapsulated yourself).
Use the HTTP package (which is already encapsulated and can be used directly).
Finally, the use of third-party open source packages (not very recommended, usually using the official HTTP package, easy to upgrade and migration).
The HTTP packet contains the implementation of the HTTP request's client and server-side processing process. These methods implement HTTP processes in a functional manner, such as: Get,head,post, and Postform. Golang HTTP processing is similar but different from the rest of the Web language, and the rest of the language, such as PHP, uses processes to process each request, while the Golang is implemented in the underlying package to hold each HTTP connection, which is the biggest difference, This allows the Golang to use its HTTP package directly to implement the backend server functionality without using traditional servers such as Nginx or tomact to help hold the HTTP connection. Of course, for load balancing functions can also be the use of nginx function. (Although Golang is using a co-process to operate the network IO, the underlying is the same as using a network model such as Epoll)
To implement a simple Web server:
A simple HTTP server, Curl 127.0.0.1:3000/foo access
HTTP server-side processing functions:
The Func Handle (pattern string, handler handler) is assigned a corresponding processing logic method according to the match.
The Func handlefunc (pattern string, handler func (Responsewriter, *request)), is used primarily for the presentation of dynamic file content.
Func listenandserve (addr string, handler handler) error listens for network requests and then calls handler with serve to process connection requests. Normally handler is nil, Use the default Defaultservemux.
Func listenandservetls (addr string, CertFile string, keyfile string, handler handler) error The function is basically the same as the Listenandserve function, and neither The same is true if the function requires an HTTPS connection. That is, you must give the service serve a file that contains an integer key, and if the certificate is signed by a certificate authority, then the certificate file must be the service certificate followed by the CA certificate.
Func Servefile (w responsewriter, R *request, name string) responds to the request with the contents of the specified file or directory.
Func Setcookie (w responsewriter, Cookie *cookie) sets a cookie for W.
The Func statustext (code int) string returns a text representation of the HTTP status code, and returns an empty string if the code is unknown.
Func Maxbytesreader (w responsewriter, R io. Readcloser, n Int64) io. Readcloser the function is similar to Io.limitreader but the function is used to limit the size of the request body. Unlike Io.limitreader, the function returns a readercloser that returns a non-eof when read exceeds the limit. And when the Close method is called, close the underlying reader. The function organizes clients to maliciously send a large number of requests, wasting server resources.
Func parsehttpversion (vers string) (major, minor int, ok bool) resolves the HTTP string version, and "http/1.0" returns (1, 0, true).
Func Proxyurl (Fixedurl *url. URL) func (*request) (*url. URL, error) returns a proxy function for transport that always returns the same URL.
Func Redirect (w responsewriter, R *request, urlstr string, code int) returns a redirect URL to the specified request, which may be a relative path to a relative request path.
Func Serve (L net. Listener, Handler handler) Error This function accepts an incoming HTTP connection for Listener L, creating a new service coprocessor for each connection, This service takes the request and then calls handler to respond to them. Handler is generally nil, so the default Defaultservemux is used.
HTTP Client request function:
The client has Do,get,head,post and Postform methods. The Do method can set the request to a series of settings, while others have fewer request settings. If the client uses the default client, the Get,head,post and Postform methods are equivalent to the default http.get,http. Post,http. Head and Http.postform functions.
Func (c *client) do (req *request) (resp *response, err Error) does send an HTTP request and returns an HTTP response that adheres to the Client's policy, such as redirection, Cookies and auth. Errors are often caused by a policy, and when err is nil, resp always contains a non-nil resp.body. When the caller finishes reading the resp.body, it should close it, if the resp.body is not closed, The client underlying roundtripper will not be able to reuse the existing TCP connection to serve the next request, and if resp.body is non-nil, it must be closed. Often, get,post, or postform, is used to replace do.
Func (c *client) get (URL string) (Resp *response, err Error) uses the Get method to request the specified URL. The GET request specifies the page information and returns the entity principal.
The Func (c *client) head (URL string) (Resp *response, err Error) uses the Head method to request that the specified Url,head return only the header of the page.
Func (c *client) Post (URL string, bodyType string, body io. Reader) (Resp *response, err Error) uses the Post method to request the specified URL if the body is also an IO. Closer, then close it after the request
Func (c *client) postform (URL string, data URL. Values) (Resp *response, err Error) uses the Post method to request the specified URL, using the key and value of data as the request body.
The Do method provides the flexibility to configure the request and then make requests. Take advantage of HTTP. Client and Http.newrequest to simulate the request. Simulates a request with a cookie.
Func (c *cookie) string () string This function returns the serialized result of the Cookie. If only the name and value fields are set, the serialization results can be used with the cookie header of the HTTP request or the Set-cookie header of the HTTP reply, and if other fields are set, the serialization result can only be used for the Set-cookie header of the HTTP reply.
The Func (d Dir) open (name string) (File, error) type Filefile is returned by the open method of filesystem and can be implemented by Fileserver. This method and *os. File behaves the same way.
Func fileserver (Root FileSystem) Handlerfileserver returns an HTTP processor that provides the file access service using the FileSystem interface. You can use Httpdir to use the FileSystem interface implementation of the operating system. It is mainly used for the display of static files.
Func Notfoundhandler () handler returns a simple request processor that returns "404 Page Not Found" for any request
Func redirecthandler (URL string, code int) handler redirects any request it receives to the given URL using the given status code
Func stripprefix (prefix string, h Handler) Handler removes the specified prefix from the request Url.path, and then gives the saved request to the Handler h for processing, for those path requests that do not start with the specified prefix, The function returns an HTTP 404 Not Found error.
Func Timeouthandler (H Handler, dt time. Duration, MSG String) handler has a time-out limit of handler, the function returns a new handler call to Serverhttp in H to process each request, but if a call exceeds the time limit, A 503 Service Request unreachable message is returned to the requestor, and a time-out error is returned in Responsewriter.
Among them, fileserver is often used together with Stripprefix to achieve static file display, for example as follows:
The type Handlerfunchandlerfunc type is an adapter that we can use as an HTTP processor using a generic function. If f is a function with the appropriate signature, Handlerfunc (f) implements the handler interface by calling F.
typehijackerinterface{//Hijack let the caller take over the connection, after calling hijack (), the HTTP Server library will no longer process the connection, and the management and shutdown responsibilities for that connection will be taken over by the caller. Hijack () (NET. Conn, *bufio. Readwriter, error)//conn represents the Connection object, BUFRW represents the read-write cache object for the connection.
Func Newrequest (method, urlstr string, body io. Reader) (*request, error) returns a new request with the specified Method,url and optional body. If the body parameter implements the Io.closer interface, the body field of the request return value is set to body, The Do, post, and Postform methods of the client type and the Transport.roundtrip method are closed.
Func readrequest (b *bufio. Reader) (req *request, err Error) reads and resolves a request from B.
Func (R *request) Addcookie (c *cookie) adds a Cookie to the request by adding a cookie,addcookie to it. According to the RFC 6265 section 5.4 rules, Addcookie does not add more than one cookie header field. This means that all cookies are written on the same line, separated by semicolons (comma-separated attributes within the cookie)
The Func (r *request) cookie (name string) (*cookie, error) returns the Cookie for the name specified in Request, and returns Errnocookie if no discovery is found
Func (R *request) cookies () []*cookie returns all cookies for the request
The Func (R *request) Setbasicauth (username, password string) provides a header with certain permissions to HTTP basic permissions with the provided user name and password. When using HTTP basic authorization, the user name and password are not encrypted
Func (R *request) useragent () string if sent in Request, the function returns the user-agent of the client.
Func (R *request) formfile (key string) (multipart. File, *multipart. Fileheader, error) returns the first file that meets the criteria for Key,formfile of the specified format, and calls Parsemultipartform and parseform if necessary.
The Func (R *request) Formvalue (key String) string returns the first value in the queue that key gets. The topic parameter in post and put takes precedence over value in the URL during the query. To access multiple values of the same key, call Parseform and check requestform directly.
Func (R *request) Multipartreader () (*multipart. Reader, error) If this is a multipart POST request, the function will return a MIME multi-part reader, otherwise it will return a nil and error. Use this function instead of Parsemultipartform to handle the request body as a stream stream.
The Func (R *request) parseform () error resolves the query string in the URL and updates the parsing result to R. The form field. For a post or put request, parseform also resolves the body as a form and updates the results to R. Postform is also updated to R.form. In the parsing result, the post or put request body takes precedence over the URL query string (the variable with the same name, with the value of the principal in front of the value of the query string). If the size of the requested body is not limited by the Maxbytesreader function, its size is limited to the beginning 10MB by default. Parsemultipartform will automatically call Parseform. It is meaningless to call this method repeatedly.
The Func (R *request) parsemultipartform (maxmemory Int64) Errorparsemultipartform parses the requested body as Multipart/form-data. The entire body of the request is parsed, the resulting file record up to Maxmemery bytes is saved in memory, and the remainder is stored in the temp file of the hard drive. If necessary, Parsemultipartform will call Parseform itself. It is meaningless to call this method repeatedly.
The Func (R *request) Postformvalue (key String) string returns the first value of the specified element in the post or put request body, where the parameters in the URL are ignored.
Func (R *request) Protoatleast (major, minor int) bool detects if the HTTP protocol used in the Request is at least major.minor
Func (R *request) Referer () string if the Request has refer, then refer returns the corresponding URL. Referer is misspelled in the request, and this error has existed since the beginning of HTTP. This value can also be obtained from the headermap using header["Referer"), and the advantage of using Referer this method instead of map in the process is that the error of the method can be checked during the compilation process and cannot be checked for key in the map.
Func (R *request) Write (w io. Writer) Errorwrite method writes the http/1.1 request to W (for writing the request to the downlevel tcpconn, and so on) in a wired format. This method takes into account the following fields of the request: Host URL Method (defaults to "GET") Header contentlength transferencoding Body if body,contentlength field exists <= 0 and the TransferEncoding field is not explicitly set to the [identity]],write method will explicitly add "transfer-encoding:chunked" to the requested header field. The body field is closed after the request is sent.
Func (R *request) writeproxy (w io). Writer) Error This function is similar to the Write method, but the request written by this method is written in the format of the HTTP proxy. In particular, according to RFC 2616 section 5.1.2,writeproxy, the 1th line of the request (Request-uri line) is initialized with an absolute URI, including the protocol and host name. In either case, the Writeproxy will use R.host or R.url.host to set the host header.
Type response refers to the response to an HTTP request response
The func get (URL string) (Resp *response, err Error) uses the Get method to request a specified URL, and if Response is a code in the following redirect, the redirect content is called after get, up to 10 redirects.
301 (Permanent Redirect, tells the client that it should be accessed from the new address later).
302 (transient redirection, as the standard of HTTP1.0, formerly known as moved temporarily, is now called found. Now used only for compatibility processing, including PHP's default location redirection used to also 302), note: 303 and 307 is actually the refinement of 302.
303 (for a POST request, it indicates that the request has been processed and the client can then use the Get method to request the URL in the location).
307 (temporary redirection, for a POST request, indicates that the request has not been processed and the client should re-initiate the POST request to the URL in the location). If there are too many redirects or an HTTP protocol error will cause an error. When err is nil, resp always contains a non-nil resp.body,get that is a wrapper on Defaultclient.get.
Func Head (URL string) (Resp *response, err Error) This function features the function of the head method in net. This method is consistent with the head method in the default defaultclient.
Func Post (URL string, bodyType string, body io. Reader) (Resp *response, err Error) This method is consistent with the Post method in the default defaultclient.
Func postform (URL string, data URL. Values) (Resp *response, err Error) This method is consistent with the Postform method in the default defaultclient.
The path ID gets:
The pre-Golang version of the official is to provide the function to get the number of the association, and later to prevent the process is abused, the method is blocked, but does provide a new way---is to get through the stack, but this way can not be used in production environment to avoid impact performance (this design thinking could be borrowed from)
Call this method to get it.