Skip to content

Commit

Permalink
Put markers where work needs to be done
Browse files Browse the repository at this point in the history
  • Loading branch information
asergeyev committed Dec 14, 2017
1 parent 9f7a6a7 commit 25cb52c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
17 changes: 16 additions & 1 deletion libwebsocketd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package libwebsocketd
import (
"errors"
"fmt"
"golang.org/x/net/websocket"
"net"
"net/http"
"net/http/cgi"
Expand All @@ -19,6 +18,8 @@ import (
"path/filepath"
"regexp"
"strings"

"golang.org/x/net/websocket"
)

var ForkNotAllowedError = errors.New("too many forks active")
Expand All @@ -44,6 +45,10 @@ func NewWebsocketdServer(config *Config, log *LogScope, maxforks int) *Websocket

// wshandshake returns closure to verify websocket origin header according to configured rules
func (h *WebsocketdServer) wshandshake(log *LogScope) func(*websocket.Config, *http.Request) error {
// CONVERT GORILLA
// this is going away mostly, except that feature of passing pre-configured headers to client
// should be implemented before gorilla's upgrader takes the control

return func(wsconf *websocket.Config, req *http.Request) error {
if len(h.Config.Headers)+len(h.Config.HeadersWs) > 0 {
if wsconf.Header == nil {
Expand Down Expand Up @@ -79,6 +84,10 @@ func pushHeaders(h http.Header, hdrs []string) {

// ServeHTTP muxes between WebSocket handler, CGI handler, DevConsole, Static HTML or 404.
func (h *WebsocketdServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// CONVERT GORILLA
// this is main HTTP response handler it's core that initiates websocket connection upgrade
// and input/output handling needs to be replaced with gorilla-websocket read loop...
// connection should be passed to libwebsocketd handler (see handler.go file, wshandler method)

log := h.Log.NewLevel(h.Log.LogFunc)
log.Associate("url", h.TellURL("http", req.Host, req.RequestURI))
Expand Down Expand Up @@ -226,6 +235,12 @@ func (h *WebsocketdServer) noteForkCompled() {
}

func checkOrigin(wsconf *websocket.Config, req *http.Request, config *Config, log *LogScope) (err error) {
// CONVERT GORILLA:
// this is origin checking function, it's called from wshandshake which is from ServeHTTP main handler
// should be trivial to reuse in gorilla's upgrader.CheckOrigin function.
// Only difference is to parse request and fetching passed Origin header out of it instead of using
// pre-parsed wsconf.Origin

// check for origin to be correct in future
// handshaker triggers answering with 403 if error was returned
// We keep behavior of original handshaker that populates this field
Expand Down
7 changes: 6 additions & 1 deletion libwebsocketd/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"bufio"
"crypto/tls"
"fmt"
"golang.org/x/net/websocket"
"net/http"
"strings"
"testing"

"golang.org/x/net/websocket"
)

var tellHostPortTests = []struct {
Expand Down Expand Up @@ -76,6 +77,10 @@ var CheckOriginTests = []struct {
{"server.example.com", ReqHTTP, "", OriginCouldDiffer, NoOriginList, ReturnsPass, "any origin allowed, even empty"},
}

// CONVERT GORILLA
// as method for origin checking changes to handle things without websocket.Config the test
// should be altered too

func TestCheckOrigin(t *testing.T) {
for _, testcase := range CheckOriginTests {
br := bufio.NewReader(strings.NewReader(fmt.Sprintf(`GET /chat HTTP/1.1
Expand Down
4 changes: 4 additions & 0 deletions libwebsocketd/websocket_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"golang.org/x/net/websocket"
)

// CONVERT GORILLA
// This file should be altered to use gorilla's websocket connection type and proper
// message dispatching methods

type WebSocketEndpoint struct {
ws *websocket.Conn
output chan []byte
Expand Down

0 comments on commit 25cb52c

Please sign in to comment.