2015-03-04 00:49:45 +08:00
|
|
|
// Package websockets implements a WebSocket server by executing
|
|
|
|
// a command and piping its input and output through the WebSocket
|
|
|
|
// connection.
|
|
|
|
package websockets
|
|
|
|
|
|
|
|
import (
|
2015-03-04 08:36:18 +08:00
|
|
|
"errors"
|
2015-03-04 00:49:45 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/flynn/go-shlex"
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
2015-03-04 08:36:18 +08:00
|
|
|
type (
|
|
|
|
// WebSockets is a type that holds configuration for the
|
|
|
|
// websocket middleware generally, like a list of all the
|
|
|
|
// websocket endpoints.
|
|
|
|
WebSockets struct {
|
2015-03-20 13:52:56 +08:00
|
|
|
// Next is the next HTTP handler in the chain for when the path doesn't match
|
|
|
|
Next http.HandlerFunc
|
|
|
|
|
2015-03-04 09:39:38 +08:00
|
|
|
// Sockets holds all the web socket endpoint configurations
|
2015-03-04 08:36:18 +08:00
|
|
|
Sockets []WSConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// WSConfig holds the configuration for a single websocket
|
2015-03-20 13:52:56 +08:00
|
|
|
// endpoint which may serve multiple websocket connections.
|
2015-03-04 08:36:18 +08:00
|
|
|
WSConfig struct {
|
|
|
|
Path string
|
|
|
|
Command string
|
|
|
|
Arguments []string
|
2015-03-20 13:52:56 +08:00
|
|
|
Respawn bool // TODO: Not used, but parser supports it until we decide on it
|
2015-03-04 08:36:18 +08:00
|
|
|
}
|
|
|
|
)
|
2015-03-04 00:49:45 +08:00
|
|
|
|
2015-03-04 08:36:18 +08:00
|
|
|
// ServeHTTP converts the HTTP request to a WebSocket connection and serves it up.
|
2015-03-04 00:49:45 +08:00
|
|
|
func (ws WebSockets) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-03-04 08:36:18 +08:00
|
|
|
for _, sockconfig := range ws.Sockets {
|
|
|
|
if middleware.Path(r.URL.Path).Matches(sockconfig.Path) {
|
|
|
|
socket := WebSocket{
|
|
|
|
WSConfig: sockconfig,
|
|
|
|
Request: r,
|
|
|
|
}
|
2015-03-04 00:49:45 +08:00
|
|
|
websocket.Handler(socket.Handle).ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-03-20 13:52:56 +08:00
|
|
|
|
|
|
|
// Didn't match a websocket path, so pass-thru
|
|
|
|
ws.Next(w, r)
|
2015-03-04 00:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// New constructs and configures a new websockets middleware instance.
|
|
|
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
2015-03-04 08:36:18 +08:00
|
|
|
var websocks []WSConfig
|
2015-03-20 13:52:56 +08:00
|
|
|
var respawn bool
|
|
|
|
|
|
|
|
optionalBlock := func() (hadBlock bool, err error) {
|
|
|
|
for c.NextBlock() {
|
|
|
|
hadBlock = true
|
|
|
|
if c.Val() == "respawn" {
|
|
|
|
respawn = true
|
|
|
|
} else {
|
|
|
|
return true, c.Err("Expected websocket configuration parameter in block")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2015-03-04 00:49:45 +08:00
|
|
|
|
|
|
|
for c.Next() {
|
2015-03-04 09:39:38 +08:00
|
|
|
var val, path, command string
|
2015-03-04 00:49:45 +08:00
|
|
|
|
|
|
|
// Path or command; not sure which yet
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
val = c.Val()
|
|
|
|
|
2015-03-20 13:52:56 +08:00
|
|
|
// Extra configuration may be in a block
|
|
|
|
hadBlock, err := optionalBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-03-04 00:49:45 +08:00
|
|
|
}
|
|
|
|
|
2015-03-20 13:52:56 +08:00
|
|
|
if !hadBlock {
|
|
|
|
// The next argument on this line will be the command or an open curly brace
|
|
|
|
if c.NextArg() {
|
|
|
|
path = val
|
|
|
|
command = c.Val()
|
|
|
|
} else {
|
|
|
|
path = "/"
|
|
|
|
command = val
|
|
|
|
}
|
2015-03-04 00:49:45 +08:00
|
|
|
|
2015-03-20 13:52:56 +08:00
|
|
|
// Okay, check again for optional block
|
|
|
|
hadBlock, err = optionalBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-04 00:49:45 +08:00
|
|
|
}
|
|
|
|
|
2015-03-20 13:52:56 +08:00
|
|
|
// Split command into the actual command and its arguments
|
|
|
|
cmd, args, err := parseCommandAndArgs(command)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-03-04 00:49:45 +08:00
|
|
|
}
|
|
|
|
|
2015-03-04 08:36:18 +08:00
|
|
|
websocks = append(websocks, WSConfig{
|
2015-03-04 00:49:45 +08:00
|
|
|
Path: path,
|
|
|
|
Command: cmd,
|
|
|
|
Arguments: args,
|
2015-03-20 13:52:56 +08:00
|
|
|
Respawn: respawn,
|
2015-03-04 00:49:45 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-04 09:39:38 +08:00
|
|
|
GatewayInterface = envGatewayInterface
|
|
|
|
ServerSoftware = envServerSoftware
|
|
|
|
|
2015-03-04 00:49:45 +08:00
|
|
|
return func(next http.HandlerFunc) http.HandlerFunc {
|
2015-03-20 13:52:56 +08:00
|
|
|
return WebSockets{Next: next, Sockets: websocks}.ServeHTTP
|
2015-03-04 00:49:45 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2015-03-04 09:39:38 +08:00
|
|
|
|
2015-03-20 13:52:56 +08:00
|
|
|
// parseCommandAndArgs takes a command string and parses it
|
|
|
|
// shell-style into the command and its separate arguments.
|
|
|
|
func parseCommandAndArgs(command string) (cmd string, args []string, err error) {
|
|
|
|
parts, err := shlex.Split(command)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Error parsing command for websocket: " + err.Error())
|
|
|
|
return
|
|
|
|
} else if len(parts) == 0 {
|
|
|
|
err = errors.New("No command found for use by websocket")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = parts[0]
|
|
|
|
if len(parts) > 1 {
|
|
|
|
args = parts[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-04 09:39:38 +08:00
|
|
|
var (
|
|
|
|
// See CGI spec, 4.1.4
|
|
|
|
GatewayInterface string
|
|
|
|
|
|
|
|
// See CGI spec, 4.1.17
|
|
|
|
ServerSoftware string
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
envGatewayInterface = "caddy-CGI/1.1"
|
2015-03-20 13:52:56 +08:00
|
|
|
envServerSoftware = "caddy/?.?.?" // TODO
|
2015-03-04 09:39:38 +08:00
|
|
|
)
|