@@ -3,26 +3,27 @@ use crate::{
3
3
get_blob, Address , LazyLoadBlob as KiBlob , Message , Request as KiRequest ,
4
4
Response as KiResponse ,
5
5
} ;
6
- use http:: { HeaderMap , HeaderName , HeaderValue , StatusCode } ;
6
+ pub use http:: StatusCode ;
7
+ use http:: { HeaderMap , HeaderName , HeaderValue } ;
7
8
use serde:: { Deserialize , Serialize } ;
8
9
use std:: collections:: { HashMap , HashSet } ;
9
10
use thiserror:: Error ;
10
11
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
12
13
/// result of either an HTTP or WebSocket binding, created via [`HttpServerAction`].
13
14
#[ derive( Clone , Debug , Serialize , Deserialize ) ]
14
15
pub enum HttpServerRequest {
15
16
Http ( IncomingHttpRequest ) ,
16
17
/// 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`]
18
19
/// containing a [`HttpServerAction::WebSocketClose`] message and this channel ID.
19
20
WebSocketOpen {
20
21
path : String ,
21
22
channel_id : u32 ,
22
23
} ,
23
- /// Processes can both SEND and RECEIVE this kind of request
24
+ /// Processes can both SEND and RECEIVE this kind of [`crate::Request`]
24
25
/// (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`] .
26
27
WebSocketPush {
27
28
channel_id : u32 ,
28
29
message_type : WsMessageType ,
@@ -33,7 +34,7 @@ pub enum HttpServerRequest {
33
34
}
34
35
35
36
impl HttpServerRequest {
36
- /// Parse a byte slice into an HttpServerRequest.
37
+ /// Parse a byte slice into an [` HttpServerRequest`] .
37
38
pub fn from_bytes ( bytes : & [ u8 ] ) -> serde_json:: Result < Self > {
38
39
serde_json:: from_slice ( bytes)
39
40
}
@@ -132,9 +133,9 @@ impl IncomingHttpRequest {
132
133
133
134
/// The possible message types for [`HttpServerRequest::WebSocketPush`].
134
135
/// 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
136
137
/// 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.
138
139
#[ derive( Clone , Copy , Debug , PartialEq , Serialize , Deserialize ) ]
139
140
pub enum WsMessageType {
140
141
Text ,
@@ -144,26 +145,26 @@ pub enum WsMessageType {
144
145
Close ,
145
146
}
146
147
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.
148
149
///
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`]
150
151
/// with the shape `Result<(), HttpServerActionError>` serialized to JSON.
151
152
#[ derive( Clone , Debug , Serialize , Deserialize ) ]
152
153
pub 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.
155
156
Bind {
156
157
path : String ,
157
158
/// Set whether the HTTP request needs a valid login cookie, AKA, whether
158
159
/// the user needs to be logged in to access this path.
159
160
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.
161
162
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.
164
165
cache : bool ,
165
166
} ,
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
167
168
/// be the static file to serve at this path.
168
169
///
169
170
/// SecureBind is the same as Bind, except that it forces requests to be made from
@@ -174,8 +175,8 @@ pub enum HttpServerAction {
174
175
/// will require the user to be logged in separately to the general domain authentication.
175
176
SecureBind {
176
177
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.
179
180
cache : bool ,
180
181
} ,
181
182
/// Unbind a previously-bound HTTP path
@@ -199,26 +200,26 @@ pub enum HttpServerAction {
199
200
} ,
200
201
/// Unbind a previously-bound WebSocket path
201
202
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.
203
204
WebSocketPush {
204
205
channel_id : u32 ,
205
206
message_type : WsMessageType ,
206
207
} ,
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`.
210
211
WebSocketExtPushOutgoing {
211
212
channel_id : u32 ,
212
213
message_type : WsMessageType ,
213
214
desired_reply_type : MessageType ,
214
215
} ,
215
216
/// 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`] .
217
218
/// Upon receiving reply with this type from ext, http_server parses, setting:
218
219
/// * 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.
222
223
WebSocketExtPushData {
223
224
id : u64 ,
224
225
kinode_message_type : MessageType ,
@@ -230,11 +231,12 @@ pub enum HttpServerAction {
230
231
231
232
/// HTTP Response type that can be shared over Wasm boundary to apps.
232
233
/// Respond to [`IncomingHttpRequest`] with this type.
234
+ ///
235
+ /// BODY is stored in the [`crate::LazyLoadBlob`] as bytes
233
236
#[ derive( Clone , Debug , Serialize , Deserialize ) ]
234
237
pub struct HttpResponse {
235
238
pub status : u16 ,
236
239
pub headers : HashMap < String , String > ,
237
- // BODY is stored in the lazy_load_blob, as bytes
238
240
}
239
241
240
242
impl HttpResponse {
@@ -268,7 +270,7 @@ impl HttpResponse {
268
270
}
269
271
}
270
272
271
- /// Part of the Response type issued by http_server
273
+ /// Part of the [`crate:: Response`] type issued by http_server
272
274
#[ derive( Clone , Debug , Error , Serialize , Deserialize ) ]
273
275
pub enum HttpServerError {
274
276
#[ error( "request could not be parsed to HttpServerAction: {req}." ) ]
@@ -287,7 +289,7 @@ pub enum HttpServerError {
287
289
UnexpectedResponse ,
288
290
}
289
291
290
- /// Whether the WebSocketPush is a request or a response .
292
+ /// Whether the [`HttpServerAction:: WebSocketPush`] is [`crate::Request`] or [`crate::Response`] .
291
293
#[ derive( Clone , Copy , Debug , Serialize , Deserialize ) ]
292
294
pub enum MessageType {
293
295
Request ,
@@ -1040,11 +1042,11 @@ impl HttpServer {
1040
1042
1041
1043
/// Push a WebSocket message to all channels on a given path.
1042
1044
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 ( )
1048
1050
}
1049
1051
}
1050
1052
@@ -1078,6 +1080,19 @@ pub fn send_ws_push(channel_id: u32, message_type: WsMessageType, blob: KiBlob)
1078
1080
. unwrap ( )
1079
1081
}
1080
1082
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
+
1081
1096
/// Guess the MIME type of a file from its extension.
1082
1097
pub fn get_mime_type ( filename : & str ) -> String {
1083
1098
let file_path = std:: path:: Path :: new ( filename) ;
0 commit comments