[Go] Http&net packages

Handler Interface

http.Handler

A Handler responds to an HTTP request.

1
2
3
type Handler interface {
ServeHTTP(ResponseWriter, *Request) // Any object that has this method will implicitly inheritance Handler interface
}

ServeHTTP should write reply headers and data to the ResponseWriter and then return. Returning signals that the request is finished; it is not valid to use the ResponseWriter or read from the Request.Body after or concurrently with the completion of the ServeHTTP call.

Cautious handlers should read the Request.Body first, and then reply.

Server

http.ListenAndServe

1
func ListenAndServe(add string, handler Handler) error

ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

The handler is typically nil, in which case the DefaultServeMux is used.

ListenAndServe always returns a non-nil error.

http.ListenAndServeTLS

(TLS is similiar to SSL)

1
func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error

ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided.

Request

http.Request

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
type Request struct {
Method string
URL *url.URL
// Header = map[string][]string{
// "Accept-Encoding": {"gzip, deflate"},
// "Accept-Language": {"en-us"},
// "Foo": {"Bar", "two"},
// }
Header Header
Body io.ReadCloser
ContentLength int64
Host string
// This field is only available after ParseForm is called.
Form url.Values
// This field is only available after ParseForm is called.
PostForm url.Values
MultipartForm *multipart.Form
// RemoteAddr allows HTTP servers and other software to record
// the network address that sent the request, usually for
// logging.
RemoteAddr string
}

Play with request

  • Retrieve URL & Form data
  • See HTTP Method
  • See URL values
  • Work with the HTTP header
1
2
3
4
5
6
7
8
9
10
11
type URL struct {
Scheme string
Opaque string // encoded opaque data
User *Userinfo // username and password information
Host string // host or host:port
Path string
RawPath string // encoded path hint (Go 1.5 and later only; see EscapedPath method)
ForceQuery bool // append a query ('?') even if RawQuery is empty
RawQuery string // encoded query values, without '?'
Fragment string // fragment for references, without '#'
}
1
type Header map[string][]string

Response

http.Response

A ResponseWriter interface is used by an HTTP handler to construct an HTTP response.

A ResponseWriter may not be used after the Handler.ServeHTTP method has returned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
type ResponseWriter interface {
// Header returns the header map that will be sent by
// WriteHeader. Changing the header after a call to
// WriteHeader (or Write) has no effect
Header() Header

// Write writes the data to the connection as part of an HTTP reply.
//
// If WriteHeader has not yet been called, Write calls
// WriteHeader(http.StatusOK) before writing the data. If the Header
// does not contain a Content-Type line, Write adds a Content-Type set
// to the result of passing the initial 512 bytes of written data to
// DetectContentType.
Write([]byte) (int, error)

// WriteHeader sends an HTTP response header with status code.
// If WriteHeader is not called explicitly, the first call to Write
// will trigger an implicit WriteHeader(http.StatusOK).
// Thus explicit calls to WriteHeader are mainly used to
// send error codes.
WriteHeader(int)
}

Http.header functions

1
2
3
4
5
6
func (h Header) Add(key, value string)
func (h Header) Del(key string)
func (h Header) Get(key string) string
func (h Header) Set(key, value string)
func (h Header) Write(w io.Writer) error
func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error

Set the response header

1
res.Header().Set("Content-Type", "text/html; charset=utf-8")