Added request and response

This commit is contained in:
2023-07-22 12:32:52 +02:00
parent 23f5227f6c
commit b50b9e1083
4 changed files with 109 additions and 5 deletions

View File

@ -16,24 +16,26 @@ type Context interface {
Error(status int, messages ...any) error
Get(param string) string
Reader(io.Reader) error
Request() Request
Response() Response
SetStatus(status int)
String(string) error
}
// context represents a request & response context.
type context struct {
request *http.Request
response http.ResponseWriter
request request
response response
paramNames [maxParams]string
paramValues [maxParams]string
paramCount int
}
// newContext returns a new context from the pool.
func newContext(request *http.Request, response http.ResponseWriter) *context {
func newContext(req *http.Request, res http.ResponseWriter) *context {
ctx := contextPool.Get().(*context)
ctx.request = request
ctx.response = response
ctx.request.Request = req
ctx.response.ResponseWriter = res
ctx.paramCount = 0
return ctx
}
@ -78,6 +80,16 @@ func (ctx *context) Reader(reader io.Reader) error {
return err
}
// Request returns the HTTP request.
func (ctx *context) Request() Request {
return &ctx.request
}
// Response returns the HTTP response.
func (ctx *context) Response() Response {
return &ctx.response
}
// SetStatus writes the header with the given HTTP status code.
func (ctx *context) SetStatus(status int) {
ctx.response.WriteHeader(status)