From e8c4c06420db8cb5ab1ee4bba6082a480d098e95 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 25 Mar 2016 19:04:28 -0400 Subject: Extract accept key generation to a new module. * web/socket/server.scm (handshake): Moved. (make-handshake-response): Use new make-accept-key procedure. * web/socket/utils.scm: New file. * Makefile.am (SOURCES): Add it. --- Makefile.am | 1 + web/socket/server.scm | 15 ++------------- web/socket/utils.scm | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 web/socket/utils.scm diff --git a/Makefile.am b/Makefile.am index 92457bd..44c0cbb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,6 +43,7 @@ godir=$(libdir)/guile/2.0/ccache SOURCES = \ web/socket/base64.scm \ web/socket/sha-1.scm \ + web/socket/utils.scm \ web/socket/frame.scm \ web/socket/server.scm diff --git a/web/socket/server.scm b/web/socket/server.scm index 04ab5d4..cd52220 100644 --- a/web/socket/server.scm +++ b/web/socket/server.scm @@ -30,23 +30,12 @@ #:use-module (web request) #:use-module (web response) #:use-module (web uri) - #:use-module (web socket base64) #:use-module (web socket frame) - #:use-module (web socket sha-1) + #:use-module (web socket utils) #:export (make-server-socket run-server)) ;; See section 4.2 for explanation of the handshake. -(define (handshake client-key) - "Translate the base64 encoded CLIENT-KEY string into a base64 -encoded acceptance key." - (base64-encode - (sha-1->bytevector - (sha-1 - (string->utf8 - (string-append (string-trim-both client-key) - "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")))))) - (define (read-handshake-request client-socket) "Read HTTP request from CLIENT-SOCKET that should contain the headers required for a WebSocket handshake." @@ -58,7 +47,7 @@ headers required for a WebSocket handshake." connection for the client whose key is CLIENT-KEY, a base64 encoded string." ;; See section 4.2.2. - (let ((accept-key (handshake client-key))) + (let ((accept-key (make-accept-key (string-trim-both client-key)))) (build-response #:code 101 #:headers `((upgrade . ("websocket")) (connection . (upgrade)) diff --git a/web/socket/utils.scm b/web/socket/utils.scm new file mode 100644 index 0000000..3788796 --- /dev/null +++ b/web/socket/utils.scm @@ -0,0 +1,43 @@ +;;; guile-websocket --- WebSocket client/server +;;; Copyright © 2016 David Thompson +;;; +;;; This file is part of guile-websocket. +;;; +;;; Guile-websocket is free software; you can redistribute it and/or modify +;;; it under the terms of the GNU Lesser General Public License as +;;; published by the Free Software Foundation; either version 3 of the +;;; License, or (at your option) any later version. +;;; +;;; Guile-websocket is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;; Lesser General Public License for more details. +;;; +;;; You should have received a copy of the GNU Lesser General Public +;;; License along with guile-websocket. If not, see +;;; . + +;;; Commentary: +;; +;; WebSocket utilities. +;; +;;; Code: + +(define-module (web socket utils) + #:use-module (rnrs bytevectors) + #:use-module (web socket base64) + #:use-module (web socket sha-1) + #:export (%handshake-guid + make-accept-key)) + +;; See section 1.3 - Opening Handshake +(define %handshake-guid "258EAFA5-E914-47DA-95CA-C5AB0DC85B11") + +(define (make-accept-key client-key) + "Return a WebSocket accept key based on CLIENT-KEY, a base64 encoded +string." + (base64-encode + (sha-1->bytevector + (sha-1 + (string->utf8 + (string-append client-key %handshake-guid)))))) -- cgit v1.2.3