Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #169 from StounhandJ/master
Browse files Browse the repository at this point in the history
adding work with headers
  • Loading branch information
ReneWerner87 authored May 7, 2023
2 parents c7e43f8 + bc3217f commit 0db3603
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ func New(handler func(*Conn), config ...Config) fiber.Handler {
c.Context().Request.Header.VisitAllCookie(func(key, value []byte) {
conn.cookies[string(key)] = string(value)
})

// headers
c.Context().Request.Header.VisitAll(func(key, value []byte) {
conn.headers[string(key)] = string(value)
})

if err := upgrader.Upgrade(c.Context(), func(fconn *websocket.Conn) {
conn.Conn = fconn
Expand All @@ -137,6 +142,7 @@ type Conn struct {
locals map[string]interface{}
params map[string]string
cookies map[string]string
headers map[string]string
queries map[string]string
}

Expand All @@ -154,6 +160,7 @@ func acquireConn() *Conn {
conn.params = make(map[string]string)
conn.queries = make(map[string]string)
conn.cookies = make(map[string]string)
conn.headers = make(map[string]string)
return conn
}

Expand Down Expand Up @@ -202,6 +209,17 @@ func (conn *Conn) Cookies(key string, defaultValue ...string) string {
return v
}

// Headers is used for getting a header value by key
// Defaults to empty string "" if the header doesn't exist.
// If a default value is given, it will return that value if the header doesn't exist.
func (conn *Conn) Headers(key string, defaultValue ...string) string {
v, ok := conn.headers[key]
if !ok && len(defaultValue) > 0 {
return defaultValue[0]
}
return v
}

// Constants are taken from https://github.com/fasthttp/websocket/blob/master/conn.go#L43

// Close codes defined in RFC 6455, section 11.7.
Expand Down

0 comments on commit 0db3603

Please sign in to comment.