mirror of
https://github.com/MercuryWorkshop/epoxy-tls.git
synced 2025-05-12 22:10:01 -04:00
ws close code and reason, ws headers
This commit is contained in:
parent
cef912f86b
commit
0aec6e563c
6 changed files with 23 additions and 10 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "epoxy-client"
|
name = "epoxy-client"
|
||||||
version = "2.0.0"
|
version = "2.0.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
shopt -s inherit_errexit
|
shopt -s inherit_errexit
|
||||||
|
|
||||||
mkdir out/
|
mkdir out/ || true
|
||||||
rm -r pkg/ || true
|
rm -r pkg/ || true
|
||||||
mkdir pkg/
|
mkdir pkg/
|
||||||
|
|
||||||
|
|
|
@ -182,8 +182,9 @@ onmessage = async (msg) => {
|
||||||
);
|
);
|
||||||
let ws = await epoxy_client.connect_websocket(
|
let ws = await epoxy_client.connect_websocket(
|
||||||
handlers,
|
handlers,
|
||||||
"wss://echo.websocket.events",
|
"ws://localhost:5000",
|
||||||
[],
|
[],
|
||||||
|
{ "x-header": "abc" },
|
||||||
);
|
);
|
||||||
while (true) {
|
while (true) {
|
||||||
log("sending `data`");
|
log("sending `data`");
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@mercuryworkshop/epoxy-tls",
|
"name": "@mercuryworkshop/epoxy-tls",
|
||||||
"version": "2.0.0-3",
|
"version": "2.0.1-1",
|
||||||
"description": "A wasm library for using raw encrypted tls/ssl/https/websocket streams on the browser",
|
"description": "A wasm library for using raw encrypted tls/ssl/https/websocket streams on the browser",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "./build.sh"
|
"build": "./build.sh"
|
||||||
|
|
|
@ -245,8 +245,9 @@ impl EpoxyClient {
|
||||||
handlers: EpoxyHandlers,
|
handlers: EpoxyHandlers,
|
||||||
url: String,
|
url: String,
|
||||||
protocols: Vec<String>,
|
protocols: Vec<String>,
|
||||||
|
headers: JsValue,
|
||||||
) -> Result<EpoxyWebSocket, EpoxyError> {
|
) -> Result<EpoxyWebSocket, EpoxyError> {
|
||||||
EpoxyWebSocket::connect(self, handlers, url, protocols).await
|
EpoxyWebSocket::connect(self, handlers, url, protocols, headers).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_tcp(
|
pub async fn connect_tcp(
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::{str::from_utf8, sync::Arc};
|
||||||
use base64::{prelude::BASE64_STANDARD, Engine};
|
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use fastwebsockets::{
|
use fastwebsockets::{
|
||||||
CloseCode, FragmentCollectorRead, Frame, OpCode, Payload, Role, WebSocket, WebSocketWrite,
|
FragmentCollectorRead, Frame, OpCode, Payload, Role, WebSocket, WebSocketWrite,
|
||||||
};
|
};
|
||||||
use futures_util::lock::Mutex;
|
use futures_util::lock::Mutex;
|
||||||
use getrandom::getrandom;
|
use getrandom::getrandom;
|
||||||
|
@ -17,12 +17,12 @@ use hyper::{
|
||||||
body::Incoming,
|
body::Incoming,
|
||||||
upgrade::{self, Upgraded},
|
upgrade::{self, Upgraded},
|
||||||
};
|
};
|
||||||
use js_sys::{ArrayBuffer, Function, Uint8Array};
|
use js_sys::{ArrayBuffer, Function, Object, Uint8Array};
|
||||||
use tokio::io::WriteHalf;
|
use tokio::io::WriteHalf;
|
||||||
use wasm_bindgen::{prelude::*, JsError, JsValue};
|
use wasm_bindgen::{prelude::*, JsError, JsValue};
|
||||||
use wasm_bindgen_futures::spawn_local;
|
use wasm_bindgen_futures::spawn_local;
|
||||||
|
|
||||||
use crate::{tokioio::TokioIo, EpoxyClient, EpoxyError, EpoxyHandlers, HttpBody};
|
use crate::{tokioio::TokioIo, utils::entries_of_object, EpoxyClient, EpoxyError, EpoxyHandlers, HttpBody};
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub struct EpoxyWebSocket {
|
pub struct EpoxyWebSocket {
|
||||||
|
@ -37,6 +37,7 @@ impl EpoxyWebSocket {
|
||||||
handlers: EpoxyHandlers,
|
handlers: EpoxyHandlers,
|
||||||
url: String,
|
url: String,
|
||||||
protocols: Vec<String>,
|
protocols: Vec<String>,
|
||||||
|
headers: JsValue,
|
||||||
) -> Result<Self, EpoxyError> {
|
) -> Result<Self, EpoxyError> {
|
||||||
let EpoxyHandlers {
|
let EpoxyHandlers {
|
||||||
onopen,
|
onopen,
|
||||||
|
@ -66,6 +67,16 @@ impl EpoxyWebSocket {
|
||||||
request = request.header(SEC_WEBSOCKET_PROTOCOL, protocols.join(","));
|
request = request.header(SEC_WEBSOCKET_PROTOCOL, protocols.join(","));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if web_sys::Headers::instanceof(&headers) && let Ok(entries) = Object::from_entries(&headers) {
|
||||||
|
for header in entries_of_object(&entries) {
|
||||||
|
request = request.header(&header[0], &header[1]);
|
||||||
|
}
|
||||||
|
} else if headers.is_truthy() {
|
||||||
|
for header in entries_of_object(&headers.into()) {
|
||||||
|
request = request.header(&header[0], &header[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let request = request.body(HttpBody::new(Bytes::new()))?;
|
let request = request.body(HttpBody::new(Bytes::new()))?;
|
||||||
|
|
||||||
let mut response = client.client.request(request).await?;
|
let mut response = client.client.request(request).await?;
|
||||||
|
@ -170,12 +181,12 @@ impl EpoxyWebSocket {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn close(&self) -> Result<(), EpoxyError> {
|
pub async fn close(&self, code: u16, reason: String) -> Result<(), EpoxyError> {
|
||||||
let ret = self
|
let ret = self
|
||||||
.tx
|
.tx
|
||||||
.lock()
|
.lock()
|
||||||
.await
|
.await
|
||||||
.write_frame(Frame::close(CloseCode::Normal.into(), b""))
|
.write_frame(Frame::close(code, reason.as_bytes()))
|
||||||
.await;
|
.await;
|
||||||
match ret {
|
match ret {
|
||||||
Ok(ok) => Ok(ok),
|
Ok(ok) => Ok(ok),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue