Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
bf1c5ad92f | |||
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()
|
||||||
}
|
}
|
||||||
|
36
ctx.go
36
ctx.go
@ -14,10 +14,14 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
forwardHeaders = []string{"CF-Connecting-IP", "X-Forwarded-For", "X-Real-Ip"}
|
||||||
|
)
|
||||||
|
|
||||||
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 +43,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 +57,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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,11 +195,24 @@ func (c *Ctx) Get(key string, defaultValue ...string) string {
|
|||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) IP() string {
|
func (c *Ctx) IP(useProxyHeader ...bool) string {
|
||||||
ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr))
|
ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(useProxyHeader) > 0 && useProxyHeader[0] {
|
||||||
|
for _, h := range forwardHeaders {
|
||||||
|
for _, rip := range strings.Split(c.Request.Header.Get(h), ",") {
|
||||||
|
realIP := net.ParseIP(strings.Replace(rip, " ", "", -1))
|
||||||
|
if check := net.ParseIP(realIP.String()); check != nil {
|
||||||
|
ip = realIP.String()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ip
|
return ip
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -303,11 +321,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 +333,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.
|
||||||
|
Reference in New Issue
Block a user