mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
Update to version 11.0.1. Feature request implemented: https://github.com/kataras/iris/issues/1113
Former-commit-id: 0ce38dbacc2458fe327fa4401fdde1e69c8aacb0
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/iris-contrib/go.uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -22,15 +25,24 @@ const (
|
||||
DefaultWebsocketReadBufferSize = 4096
|
||||
// DefaultWebsocketWriterBufferSize 4096
|
||||
DefaultWebsocketWriterBufferSize = 4096
|
||||
// DefaultClientSourcePath "/iris-ws.js"
|
||||
DefaultClientSourcePath = "/iris-ws.js"
|
||||
// DefaultEvtMessageKey is the default prefix of the underline websocket events
|
||||
// that are being established under the hoods.
|
||||
//
|
||||
// Defaults to "iris-websocket-message:".
|
||||
// Last character of the prefix should be ':'.
|
||||
DefaultEvtMessageKey = "iris-websocket-message:"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultIDGenerator returns the result of 64
|
||||
// random combined characters as the id of a new connection.
|
||||
// Used when config.IDGenerator is nil
|
||||
DefaultIDGenerator = func(context.Context) string { return randomString(64) }
|
||||
// DefaultIDGenerator returns a random unique for a new connection.
|
||||
// Used when config.IDGenerator is nil.
|
||||
DefaultIDGenerator = func(context.Context) string {
|
||||
id, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return randomString(64)
|
||||
}
|
||||
return id.String()
|
||||
}
|
||||
)
|
||||
|
||||
// Config the websocket server configuration
|
||||
@@ -38,9 +50,16 @@ var (
|
||||
type Config struct {
|
||||
// IDGenerator used to create (and later on, set)
|
||||
// an ID for each incoming websocket connections (clients).
|
||||
// The request is an argument which you can use to generate the ID (from headers for example).
|
||||
// The request is an input parameter which you can use to generate the ID (from headers for example).
|
||||
// If empty then the ID is generated by DefaultIDGenerator: randomString(64)
|
||||
IDGenerator func(ctx context.Context) string
|
||||
// EvtMessagePrefix is the prefix of the underline websocket events that are being established under the hoods.
|
||||
// This prefix is visible only to the javascript side (code) and it has nothing to do
|
||||
// with the message that the end-user receives.
|
||||
// Do not change it unless it is absolutely necessary.
|
||||
//
|
||||
// If empty then defaults to []byte("iris-websocket-message:").
|
||||
EvtMessagePrefix []byte
|
||||
// Error is the function that will be fired if any client couldn't upgrade the HTTP connection
|
||||
// to a websocket connection, a handshake error.
|
||||
Error func(w http.ResponseWriter, r *http.Request, status int, reason error)
|
||||
@@ -135,9 +154,47 @@ func (c Config) Validate() Config {
|
||||
}
|
||||
}
|
||||
|
||||
if len(c.EvtMessagePrefix) == 0 {
|
||||
c.EvtMessagePrefix = []byte(DefaultEvtMessageKey)
|
||||
}
|
||||
|
||||
if c.IDGenerator == nil {
|
||||
c.IDGenerator = DefaultIDGenerator
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
const (
|
||||
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
letterIdxBits = 6 // 6 bits to represent a letter index
|
||||
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
|
||||
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
|
||||
)
|
||||
|
||||
var src = rand.NewSource(time.Now().UnixNano())
|
||||
|
||||
// random takes a parameter (int) and returns random slice of byte
|
||||
// ex: var randomstrbytes []byte; randomstrbytes = utils.Random(32)
|
||||
func random(n int) []byte {
|
||||
b := make([]byte, n)
|
||||
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
||||
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
|
||||
if remain == 0 {
|
||||
cache, remain = src.Int63(), letterIdxMax
|
||||
}
|
||||
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
|
||||
b[i] = letterBytes[idx]
|
||||
i--
|
||||
}
|
||||
cache >>= letterIdxBits
|
||||
remain--
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
// randomString accepts a number(10 for example) and returns a random string using simple but fairly safe random algorithm
|
||||
func randomString(n int) string {
|
||||
return string(random(n))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user