Partial responses inline, HTH. On Thu, Apr 29, 2021, 6:09 AM Amit Saha <amitsaha...@gmail.com> wrote:
> Hi all, when an incoming request comes in, does the ListenAndServe() > function read the first line (as explained in > https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages) to figure > out whether there is a handler registered or not for the path and then > simply hands it over to the handler (if there is one)? In the http package, that role is played by the ServeMux <https://golang.org/pkg/net/http/#ServeMux>. > It seems like it is also reading the headers, and then populating the > http.Request object with them. But, I am curious as to why does it do > so? Is there any specific header that the server must look for? > HTTP headers arrive on the wire before the body, so they have to be consumed prior to the body. The HTTP package parses them and makes them available so the handler and middleware can make use of them as needed to do header-related things like authentication. > As far as the Body is concerned, that is left to the handler to > decide, is that correct? > I guess you mean that the handler gets to decide for itself whether to read the body or not. AFAIK, that's correct. One benefit of parsing the headers into a map is that it makes HTTP middleware more composeable and easier to write. Parsing the headers first means different middlewares can examine the same headers. The same is not true of the body, which can only be consumed once unless you add special handling to store it in memory for later. > Thanks, > Amit. > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/CANODV3kSLPyV0snkPOr8Q_j%2BR%3DGOfVie2FNxEA1B-8%2BFvQxMEA%40mail.gmail.com > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CALJzkY8qgh-GKPK_vKC-%3Dz1gGfZoNHT62KfWtMh%3D0bbv-R5afw%40mail.gmail.com.