fix: writer
This commit is contained in:
parent
c13263fe0d
commit
9b7c8d9d24
2
app.go
2
app.go
@ -273,6 +273,6 @@ func redirectRequest(c *Ctx) {
|
|||||||
|
|
||||||
//debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)
|
//debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)
|
||||||
|
|
||||||
http.Redirect(c.writer, req, rURL, code)
|
http.Redirect(c.Writer, req, rURL, code)
|
||||||
c.writermem.WriteHeaderNow()
|
c.writermem.WriteHeaderNow()
|
||||||
}
|
}
|
||||||
|
17
ctx.go
17
ctx.go
@ -17,7 +17,7 @@ import (
|
|||||||
type Ctx struct {
|
type Ctx struct {
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
writermem responseWriter
|
writermem responseWriter
|
||||||
writer http.ResponseWriter
|
Writer ResponseWriter
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
path string
|
path string
|
||||||
method string
|
method string
|
||||||
@ -39,7 +39,6 @@ func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ct
|
|||||||
|
|
||||||
ctx := &Ctx{
|
ctx := &Ctx{
|
||||||
lock: sync.Mutex{},
|
lock: sync.Mutex{},
|
||||||
writer: writer,
|
|
||||||
Request: request,
|
Request: request,
|
||||||
path: request.URL.Path,
|
path: request.URL.Path,
|
||||||
method: request.Method,
|
method: request.Method,
|
||||||
@ -54,11 +53,13 @@ func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ct
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.writermem = responseWriter{
|
ctx.writermem = responseWriter{
|
||||||
ResponseWriter: ctx.writer,
|
ResponseWriter: writer,
|
||||||
size: -1,
|
size: -1,
|
||||||
status: 0,
|
status: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx.Writer = &ctx.writermem
|
||||||
|
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -303,11 +304,11 @@ func (c *Ctx) SSEvent(event string, data interface{}) error {
|
|||||||
c.Set("Cache-Control", "no-cache")
|
c.Set("Cache-Control", "no-cache")
|
||||||
c.Set("Transfer-Encoding", "chunked")
|
c.Set("Transfer-Encoding", "chunked")
|
||||||
|
|
||||||
return sse.Encode(c.writer, sse.Event{Event: event, Data: data})
|
return sse.Encode(c.Writer, sse.Event{Event: event, Data: data})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) Flush() error {
|
func (c *Ctx) Flush() error {
|
||||||
if f, ok := c.writer.(http.Flusher); ok {
|
if f, ok := c.Writer.(http.Flusher); ok {
|
||||||
f.Flush()
|
f.Flush()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -315,13 +316,9 @@ func (c *Ctx) Flush() error {
|
|||||||
return errors.New("http.Flusher is not implemented")
|
return errors.New("http.Flusher is not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) RawWriter() http.ResponseWriter {
|
|
||||||
return c.writer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Ctx) HTML(html string) error {
|
func (c *Ctx) HTML(html string) error {
|
||||||
c.SetHeader("Content-Type", "text/html")
|
c.SetHeader("Content-Type", "text/html")
|
||||||
_, err := c.writer.Write([]byte(html))
|
_, err := c.Write([]byte(html))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,6 +42,7 @@ type ResponseWriter interface {
|
|||||||
|
|
||||||
type responseWriter struct {
|
type responseWriter struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
|
written bool
|
||||||
size int
|
size int
|
||||||
status int
|
status int
|
||||||
}
|
}
|
||||||
@ -103,7 +104,7 @@ func (w *responseWriter) Size() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *responseWriter) Written() bool {
|
func (w *responseWriter) Written() bool {
|
||||||
return w.size != noWritten
|
return w.size != noWritten || w.written
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hijack implements the http.Hijacker interface.
|
// Hijack implements the http.Hijacker interface.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user