Skip to content

Commit

Permalink
Remove websocket send encoding protocols
Browse files Browse the repository at this point in the history
While convenient, automatic conversion of messages passed to
ring.websocket/send force the messages to be a type that can be coerced
into a CharSequence or ByteBuffer. This prevents sockets from being
written that can handle higher-level data structures.
  • Loading branch information
weavejester committed Oct 17, 2023
1 parent 0fcd58e commit cd2cdcb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 34 deletions.
37 changes: 4 additions & 33 deletions ring-core/src/ring/websocket.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,6 @@
(on-ping [m socket data]
(when-let [kv (find m :on-ping)] ((val kv) socket data))))

(defprotocol TextData
"A protocol for converting text data into a String."
(->char-sequence [data]
"Convert some data into a CharSequence, ready to be sent as a websocket
text message."))

(defprotocol BinaryData
"A protocol for converting binary data into a java.nio.ByteBuffer object."
(->byte-buffer [data]
"Convert some binary data into a java.nio.ByteBuffer, ready to be sent as
a websocket binary message."))

(extend-protocol TextData
CharSequence
(->char-sequence [cs] cs))

(extend-protocol BinaryData
(Class/forName "[B")
(->byte-buffer [bs] (ByteBuffer/wrap bs))
ByteBuffer
(->byte-buffer [bb] bb))

(defn- encode-message [message]
(cond
(satisfies? TextData message) (->char-sequence message)
(satisfies? BinaryData message) (->byte-buffer message)
:else (throw (ex-info "message is not a valid text or binary data type"
{:message message}))))

(defn open?
"Returns true if the Socket is open, false otherwise."
[socket]
Expand All @@ -60,9 +31,9 @@
asynchronously with callback functions. A convenient wrapper for the -send and
-send-async protocol methods."
([socket message]
(p/-send socket (encode-message message)))
(p/-send socket message))
([socket message succeed fail]
(p/-send-async socket (encode-message message) succeed fail)))
(p/-send-async socket message succeed fail)))

(defn ping
"Sends a ping message via a websocket, with an optional byte array or
Expand All @@ -71,7 +42,7 @@
([socket]
(p/-ping socket (ByteBuffer/allocate 0)))
([socket data]
(p/-ping socket (->byte-buffer data))))
(p/-ping socket data)))

(defn pong
"Sends an unsolicited pong message via a websocket, with an optional byte
Expand All @@ -80,7 +51,7 @@
([socket]
(p/-pong socket (ByteBuffer/allocate 0)))
([socket data]
(p/-pong socket (->byte-buffer data))))
(p/-pong socket data)))

(defn close
"Closes the websocket, with an optional custom integer status code and reason
Expand Down
2 changes: 1 addition & 1 deletion ring-jetty-adapter/test/ring/adapter/test/jetty.clj
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@
(reify wsp/Listener
(on-open [_ sock]
(ws/send sock "Hello")
(ws/send sock (.getBytes "World")))
(ws/send sock (ByteBuffer/wrap (.getBytes "World"))))
(on-message [_ sock msg]
(if (string? msg)
(ws/send sock (str "t: " msg))
Expand Down

0 comments on commit cd2cdcb

Please sign in to comment.