ref 2
This commit is contained in:
parent
3bc0920f09
commit
9295e5445e
|
@ -53,7 +53,7 @@ func (ctx *context) Next() error {
|
|||
// Redirects the client to a different location with the specified status code.
|
||||
func (ctx *context) Redirect(status int, location string) error {
|
||||
ctx.response.SetStatus(status)
|
||||
ctx.response.SetHeader("Location", location)
|
||||
ctx.response.SetHeader(HeaderLocation, location)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
57
header.go
57
header.go
|
@ -1,6 +1,63 @@
|
|||
package web
|
||||
|
||||
// Header represents an HTTP header with key and value
|
||||
type Header struct {
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
|
||||
// Common HTTP header keys
|
||||
const (
|
||||
HeaderContentType = "Content-Type"
|
||||
HeaderContentLength = "Content-Length"
|
||||
HeaderHost = "Host"
|
||||
HeaderAccept = "Accept"
|
||||
HeaderUserAgent = "User-Agent"
|
||||
HeaderAcceptEncoding = "Accept-Encoding"
|
||||
HeaderAcceptLanguage = "Accept-Language"
|
||||
HeaderConnection = "Connection"
|
||||
HeaderCookie = "Cookie"
|
||||
HeaderSetCookie = "Set-Cookie"
|
||||
HeaderLocation = "Location"
|
||||
HeaderAuthorization = "Authorization"
|
||||
HeaderCacheControl = "Cache-Control"
|
||||
HeaderOrigin = "Origin"
|
||||
HeaderReferer = "Referer"
|
||||
HeaderTransferEncoding = "Transfer-Encoding"
|
||||
)
|
||||
|
||||
// Pre-allocated common headers
|
||||
var (
|
||||
// Content type headers
|
||||
HeaderContentTypeJSON = Header{Key: HeaderContentType, Value: "application/json"}
|
||||
HeaderContentTypeHTML = Header{Key: HeaderContentType, Value: "text/html"}
|
||||
HeaderContentTypePlain = Header{Key: HeaderContentType, Value: "text/plain"}
|
||||
HeaderContentTypeXML = Header{Key: HeaderContentType, Value: "application/xml"}
|
||||
HeaderContentTypeForm = Header{Key: HeaderContentType, Value: "application/x-www-form-urlencoded"}
|
||||
HeaderContentTypeMultipart = Header{Key: HeaderContentType, Value: "multipart/form-data"}
|
||||
|
||||
// Connection headers
|
||||
HeaderConnectionClose = Header{Key: HeaderConnection, Value: "close"}
|
||||
HeaderConnectionKeepAlive = Header{Key: HeaderConnection, Value: "keep-alive"}
|
||||
)
|
||||
|
||||
// FindHeader looks for a header by key in a slice of headers
|
||||
func FindHeader(headers []Header, key string) (string, bool) {
|
||||
for _, h := range headers {
|
||||
if h.Key == key {
|
||||
return h.Value, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// SetHeader sets a header value in a slice of headers
|
||||
func SetHeader(headers *[]Header, key string, value string) {
|
||||
for i, h := range *headers {
|
||||
if h.Key == key {
|
||||
(*headers)[i].Value = value
|
||||
return
|
||||
}
|
||||
}
|
||||
*headers = append(*headers, Header{Key: key, Value: value})
|
||||
}
|
||||
|
|
|
@ -32,13 +32,8 @@ type request struct {
|
|||
|
||||
// Returns the header value for the given key.
|
||||
func (req *request) Header(key string) string {
|
||||
for _, header := range req.headers {
|
||||
if header.Key == key {
|
||||
return header.Value
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
value, _ := FindHeader(req.headers, key)
|
||||
return value
|
||||
}
|
||||
|
||||
// Returns the requested host.
|
||||
|
|
18
response.go
18
response.go
|
@ -28,25 +28,13 @@ func (res *response) Body() []byte {
|
|||
|
||||
// Returns the header value for the given key.
|
||||
func (res *response) Header(key string) string {
|
||||
for _, header := range res.headers {
|
||||
if header.Key == key {
|
||||
return header.Value
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
value, _ := FindHeader(res.headers, key)
|
||||
return value
|
||||
}
|
||||
|
||||
// Sets the header value for the given key.
|
||||
func (res *response) SetHeader(key string, value string) {
|
||||
for i, header := range res.headers {
|
||||
if header.Key == key {
|
||||
res.headers[i].Value = value
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
res.headers = append(res.headers, Header{Key: key, Value: value})
|
||||
SetHeader(&res.headers, key, value)
|
||||
}
|
||||
|
||||
// Replaces the response body with the new contents.
|
||||
|
|
|
@ -210,7 +210,7 @@ func (s *server) handleConnection(conn net.Conn) {
|
|||
}
|
||||
|
||||
// Read the body, if any
|
||||
if contentLength := ctx.request.Header("Content-Length"); contentLength != "" {
|
||||
if contentLength := ctx.request.Header(HeaderContentLength); contentLength != "" {
|
||||
length, _ := strconv.Atoi(contentLength)
|
||||
ctx.request.body = make([]byte, length)
|
||||
ctx.reader.Read(ctx.request.body)
|
||||
|
@ -244,7 +244,7 @@ func (s *server) handleRequest(ctx *context, method string, url string, writer i
|
|||
tmp := bytes.Buffer{}
|
||||
tmp.WriteString("HTTP/1.1 ")
|
||||
tmp.WriteString(strconv.Itoa(int(ctx.status)))
|
||||
tmp.WriteString("\r\nContent-Length: ")
|
||||
tmp.WriteString("\r\n" + HeaderContentLength + ": ")
|
||||
tmp.WriteString(strconv.Itoa(len(ctx.response.body)))
|
||||
tmp.WriteString("\r\n")
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user