Compare commits

...

3 Commits

Author SHA1 Message Date
bf1c5ad92f feat: c.IP add use-proxy-header arg 2024-07-04 17:37:27 +08:00
9b7c8d9d24 fix: writer 2024-06-25 16:36:43 +08:00
c13263fe0d fix: logger missing status while 404 2024-06-17 14:31:49 +08:00
3 changed files with 36 additions and 19 deletions

12
app.go
View File

@ -202,17 +202,19 @@ func (a *App) handleHTTPRequest(c *Ctx) {
}
if len(allowed) > 0 {
c.handlers = a.combineHandlers()
c.handlers = a.combineHandlers(a.config.MethodNotAllowedHandler)
serveError(c, a.config.MethodNotAllowedHandler)
_ = c.Next()
return
}
}
c.handlers = a.combineHandlers()
c.handlers = a.combineHandlers(a.config.NotFoundHandler)
serveError(c, a.config.NotFoundHandler)
_ = c.Next()
return
}
func errorHandler(c *Ctx) error {
@ -271,6 +273,6 @@ func redirectRequest(c *Ctx) {
//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()
}

36
ctx.go
View File

@ -14,10 +14,14 @@ import (
"sync"
)
var (
forwardHeaders = []string{"CF-Connecting-IP", "X-Forwarded-For", "X-Real-Ip"}
)
type Ctx struct {
lock sync.Mutex
writermem responseWriter
writer http.ResponseWriter
Writer ResponseWriter
Request *http.Request
path string
method string
@ -39,7 +43,6 @@ func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ct
ctx := &Ctx{
lock: sync.Mutex{},
writer: writer,
Request: request,
path: request.URL.Path,
method: request.Method,
@ -54,11 +57,13 @@ func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ct
}
ctx.writermem = responseWriter{
ResponseWriter: ctx.writer,
ResponseWriter: writer,
size: -1,
status: 0,
}
ctx.Writer = &ctx.writermem
return ctx
}
@ -190,11 +195,24 @@ func (c *Ctx) Get(key string, defaultValue ...string) string {
return value
}
func (c *Ctx) IP() string {
func (c *Ctx) IP(useProxyHeader ...bool) string {
ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr))
if err != nil {
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
}
@ -303,11 +321,11 @@ func (c *Ctx) SSEvent(event string, data interface{}) error {
c.Set("Cache-Control", "no-cache")
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 {
if f, ok := c.writer.(http.Flusher); ok {
if f, ok := c.Writer.(http.Flusher); ok {
f.Flush()
return nil
}
@ -315,13 +333,9 @@ func (c *Ctx) Flush() error {
return errors.New("http.Flusher is not implemented")
}
func (c *Ctx) RawWriter() http.ResponseWriter {
return c.writer
}
func (c *Ctx) HTML(html string) error {
c.SetHeader("Content-Type", "text/html")
_, err := c.writer.Write([]byte(html))
_, err := c.Write([]byte(html))
return err
}

View File

@ -42,8 +42,9 @@ type ResponseWriter interface {
type responseWriter struct {
http.ResponseWriter
size int
status int
written bool
size int
status int
}
var _ ResponseWriter = (*responseWriter)(nil)
@ -103,7 +104,7 @@ func (w *responseWriter) Size() int {
}
func (w *responseWriter) Written() bool {
return w.size != noWritten
return w.size != noWritten || w.written
}
// Hijack implements the http.Hijacker interface.