@@ -3,26 +3,27 @@ use crate::{
33 get_blob, Address , LazyLoadBlob as KiBlob , Message , Request as KiRequest ,
44 Response as KiResponse ,
55} ;
6- use http:: { HeaderMap , HeaderName , HeaderValue , StatusCode } ;
6+ pub use http:: StatusCode ;
7+ use http:: { HeaderMap , HeaderName , HeaderValue } ;
78use serde:: { Deserialize , Serialize } ;
89use std:: collections:: { HashMap , HashSet } ;
910use thiserror:: Error ;
1011
11- /// HTTP Request received from the `http_server:distro:sys` service as a
12+ /// [`crate:: Request`] received from the `http_server:distro:sys` service as a
1213/// result of either an HTTP or WebSocket binding, created via [`HttpServerAction`].
1314#[ derive( Clone , Debug , Serialize , Deserialize ) ]
1415pub enum HttpServerRequest {
1516 Http ( IncomingHttpRequest ) ,
1617 /// Processes will receive this kind of request when a client connects to them.
17- /// If a process does not want this websocket open, they should issue a *request*
18+ /// If a process does not want this websocket open, they should issue a [`crate::Request`]
1819 /// containing a [`HttpServerAction::WebSocketClose`] message and this channel ID.
1920 WebSocketOpen {
2021 path : String ,
2122 channel_id : u32 ,
2223 } ,
23- /// Processes can both SEND and RECEIVE this kind of request
24+ /// Processes can both SEND and RECEIVE this kind of [`crate::Request`]
2425 /// (send as [`HttpServerAction::WebSocketPush`]).
25- /// When received, will contain the message bytes as lazy_load_blob .
26+ /// When received, will contain the message bytes as [`crate::LazyLoadBlob`] .
2627 WebSocketPush {
2728 channel_id : u32 ,
2829 message_type : WsMessageType ,
@@ -33,7 +34,7 @@ pub enum HttpServerRequest {
3334}
3435
3536impl HttpServerRequest {
36- /// Parse a byte slice into an HttpServerRequest.
37+ /// Parse a byte slice into an [` HttpServerRequest`] .
3738 pub fn from_bytes ( bytes : & [ u8 ] ) -> serde_json:: Result < Self > {
3839 serde_json:: from_slice ( bytes)
3940 }
@@ -132,9 +133,9 @@ impl IncomingHttpRequest {
132133
133134/// The possible message types for [`HttpServerRequest::WebSocketPush`].
134135/// Ping and Pong are limited to 125 bytes by the WebSockets protocol.
135- /// Text will be sent as a Text frame, with the lazy_load_blob bytes
136+ /// Text will be sent as a Text frame, with the [`crate::LazyLoadBlob`] bytes
136137/// being the UTF-8 encoding of the string. Binary will be sent as a
137- /// Binary frame containing the unmodified lazy_load_blob bytes.
138+ /// Binary frame containing the unmodified [`crate::LazyLoadBlob`] bytes.
138139#[ derive( Clone , Copy , Debug , PartialEq , Serialize , Deserialize ) ]
139140pub enum WsMessageType {
140141 Text ,
@@ -144,26 +145,26 @@ pub enum WsMessageType {
144145 Close ,
145146}
146147
147- /// Request type sent to `http_server:distro:sys` in order to configure it.
148+ /// [`crate:: Request`] type sent to `http_server:distro:sys` in order to configure it.
148149///
149- /// If a response is expected, all actions will return a Response
150+ /// If a [`crate::Response`] is expected, all actions will return a [`crate:: Response`]
150151/// with the shape `Result<(), HttpServerActionError>` serialized to JSON.
151152#[ derive( Clone , Debug , Serialize , Deserialize ) ]
152153pub enum HttpServerAction {
153- /// Bind expects a lazy_load_blob if and only if `cache` is TRUE. The lazy_load_blob should
154- /// be the static file to serve at this path.
154+ /// Bind expects a [`crate::LazyLoadBlob`] if and only if `cache` is TRUE.
155+ /// The [`crate::LazyLoadBlob`] should be the static file to serve at this path.
155156 Bind {
156157 path : String ,
157158 /// Set whether the HTTP request needs a valid login cookie, AKA, whether
158159 /// the user needs to be logged in to access this path.
159160 authenticated : bool ,
160- /// Set whether requests can be fielded from anywhere, or only the loopback address.
161+ /// Set whether [`crate::Request`]s can be fielded from anywhere, or only the loopback address.
161162 local_only : bool ,
162- /// Set whether to bind the lazy_load_blob statically to this path. That is, take the
163- /// lazy_load_blob bytes and serve them as the response to any request to this path.
163+ /// Set whether to bind the [`crate::LazyLoadBlob`] statically to this path. That is, take the
164+ /// [`crate::LazyLoadBlob`] bytes and serve them as the response to any request to this path.
164165 cache : bool ,
165166 } ,
166- /// SecureBind expects a lazy_load_blob if and only if `cache` is TRUE. The lazy_load_blob should
167+ /// SecureBind expects a [`crate::LazyLoadBlob`] if and only if `cache` is TRUE. The [`crate::LazyLoadBlob`] should
167168 /// be the static file to serve at this path.
168169 ///
169170 /// SecureBind is the same as Bind, except that it forces requests to be made from
@@ -174,8 +175,8 @@ pub enum HttpServerAction {
174175 /// will require the user to be logged in separately to the general domain authentication.
175176 SecureBind {
176177 path : String ,
177- /// Set whether to bind the lazy_load_blob statically to this path. That is, take the
178- /// lazy_load_blob bytes and serve them as the response to any request to this path.
178+ /// Set whether to bind the [`crate::LazyLoadBlob`] statically to this path. That is, take the
179+ /// [`crate::LazyLoadBlob`] bytes and serve them as the response to any request to this path.
179180 cache : bool ,
180181 } ,
181182 /// Unbind a previously-bound HTTP path
@@ -199,26 +200,26 @@ pub enum HttpServerAction {
199200 } ,
200201 /// Unbind a previously-bound WebSocket path
201202 WebSocketUnbind { path : String } ,
202- /// When sent, expects a lazy_load_blob containing the WebSocket message bytes to send.
203+ /// When sent, expects a [`crate::LazyLoadBlob`] containing the WebSocket message bytes to send.
203204 WebSocketPush {
204205 channel_id : u32 ,
205206 message_type : WsMessageType ,
206207 } ,
207- /// When sent, expects a `lazy_load_blob` containing the WebSocket message bytes to send.
208- /// Modifies the `lazy_load_blob` by placing into ` WebSocketExtPushData` with id taken from
209- /// this `KernelMessage` and `kinode_message_type` set to `desired_reply_type`.
208+ /// When sent, expects a [`crate::LazyLoadBlob`] containing the WebSocket message bytes to send.
209+ /// Modifies the [`crate::LazyLoadBlob`] by placing into [`HttpServerAction:: WebSocketExtPushData`] ` with id taken from
210+ /// this [ `KernelMessage`] ` and `kinode_message_type` set to `desired_reply_type`.
210211 WebSocketExtPushOutgoing {
211212 channel_id : u32 ,
212213 message_type : WsMessageType ,
213214 desired_reply_type : MessageType ,
214215 } ,
215216 /// For communicating with the ext.
216- /// Kinode's http_server sends this to the ext after receiving ` WebSocketExtPushOutgoing`.
217+ /// Kinode's http_server sends this to the ext after receiving [`HttpServerAction:: WebSocketExtPushOutgoing`] .
217218 /// Upon receiving reply with this type from ext, http_server parses, setting:
218219 /// * id as given,
219- /// * message type as given (Request or Response),
220- /// * body as HttpServerRequest::WebSocketPush,
221- /// * blob as given.
220+ /// * message type as given ([`crate:: Request`] or [`crate:: Response`] ),
221+ /// * body as [` HttpServerRequest::WebSocketPush`] ,
222+ /// * [`crate::LazyLoadBlob`] as given.
222223 WebSocketExtPushData {
223224 id : u64 ,
224225 kinode_message_type : MessageType ,
@@ -230,11 +231,12 @@ pub enum HttpServerAction {
230231
231232/// HTTP Response type that can be shared over Wasm boundary to apps.
232233/// Respond to [`IncomingHttpRequest`] with this type.
234+ ///
235+ /// BODY is stored in the [`crate::LazyLoadBlob`] as bytes
233236#[ derive( Clone , Debug , Serialize , Deserialize ) ]
234237pub struct HttpResponse {
235238 pub status : u16 ,
236239 pub headers : HashMap < String , String > ,
237- // BODY is stored in the lazy_load_blob, as bytes
238240}
239241
240242impl HttpResponse {
@@ -268,7 +270,7 @@ impl HttpResponse {
268270 }
269271}
270272
271- /// Part of the Response type issued by http_server
273+ /// Part of the [`crate:: Response`] type issued by http_server
272274#[ derive( Clone , Debug , Error , Serialize , Deserialize ) ]
273275pub enum HttpServerError {
274276 #[ error( "request could not be parsed to HttpServerAction: {req}." ) ]
@@ -287,7 +289,7 @@ pub enum HttpServerError {
287289 UnexpectedResponse ,
288290}
289291
290- /// Whether the WebSocketPush is a request or a response .
292+ /// Whether the [`HttpServerAction:: WebSocketPush`] is [`crate::Request`] or [`crate::Response`] .
291293#[ derive( Clone , Copy , Debug , Serialize , Deserialize ) ]
292294pub enum MessageType {
293295 Request ,
@@ -1040,11 +1042,11 @@ impl HttpServer {
10401042
10411043 /// Push a WebSocket message to all channels on a given path.
10421044 pub fn ws_push_all_channels ( & self , path : & str , message_type : WsMessageType , blob : KiBlob ) {
1043- if let Some ( channels ) = self . ws_channels . get ( path) {
1044- channels . iter ( ) . for_each ( |channel_id| {
1045- send_ws_push ( * channel_id , message_type , blob . clone ( ) ) ;
1046- } ) ;
1047- }
1045+ ws_push_all_channels ( & self . ws_channels , path, message_type , blob ) ;
1046+ }
1047+
1048+ pub fn get_ws_channels ( & self ) -> HashMap < String , HashSet < u32 > > {
1049+ self . ws_channels . clone ( )
10481050 }
10491051}
10501052
@@ -1078,6 +1080,19 @@ pub fn send_ws_push(channel_id: u32, message_type: WsMessageType, blob: KiBlob)
10781080 . unwrap ( )
10791081}
10801082
1083+ pub fn ws_push_all_channels (
1084+ ws_channels : & HashMap < String , HashSet < u32 > > ,
1085+ path : & str ,
1086+ message_type : WsMessageType ,
1087+ blob : KiBlob ,
1088+ ) {
1089+ if let Some ( channels) = ws_channels. get ( path) {
1090+ channels. iter ( ) . for_each ( |channel_id| {
1091+ send_ws_push ( * channel_id, message_type, blob. clone ( ) ) ;
1092+ } ) ;
1093+ }
1094+ }
1095+
10811096/// Guess the MIME type of a file from its extension.
10821097pub fn get_mime_type ( filename : & str ) -> String {
10831098 let file_path = std:: path:: Path :: new ( filename) ;
0 commit comments