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

Commit 0db3603

Browse files
authored
Merge pull request #169 from StounhandJ/master
adding work with headers
2 parents c7e43f8 + bc3217f commit 0db3603

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

websocket.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ func New(handler func(*Conn), config ...Config) fiber.Handler {
118118
c.Context().Request.Header.VisitAllCookie(func(key, value []byte) {
119119
conn.cookies[string(key)] = string(value)
120120
})
121+
122+
// headers
123+
c.Context().Request.Header.VisitAll(func(key, value []byte) {
124+
conn.headers[string(key)] = string(value)
125+
})
121126

122127
if err := upgrader.Upgrade(c.Context(), func(fconn *websocket.Conn) {
123128
conn.Conn = fconn
@@ -137,6 +142,7 @@ type Conn struct {
137142
locals map[string]interface{}
138143
params map[string]string
139144
cookies map[string]string
145+
headers map[string]string
140146
queries map[string]string
141147
}
142148

@@ -154,6 +160,7 @@ func acquireConn() *Conn {
154160
conn.params = make(map[string]string)
155161
conn.queries = make(map[string]string)
156162
conn.cookies = make(map[string]string)
163+
conn.headers = make(map[string]string)
157164
return conn
158165
}
159166

@@ -202,6 +209,17 @@ func (conn *Conn) Cookies(key string, defaultValue ...string) string {
202209
return v
203210
}
204211

212+
// Headers is used for getting a header value by key
213+
// Defaults to empty string "" if the header doesn't exist.
214+
// If a default value is given, it will return that value if the header doesn't exist.
215+
func (conn *Conn) Headers(key string, defaultValue ...string) string {
216+
v, ok := conn.headers[key]
217+
if !ok && len(defaultValue) > 0 {
218+
return defaultValue[0]
219+
}
220+
return v
221+
}
222+
205223
// Constants are taken from https://github.com/fasthttp/websocket/blob/master/conn.go#L43
206224

207225
// Close codes defined in RFC 6455, section 11.7.

0 commit comments

Comments
 (0)