Compare commits

...

5 Commits

  1. 12
      Cargo.toml
  2. 28
      README.md
  3. 43
      src/handshake/server.rs

@ -1,14 +1,14 @@
[package]
name = "tungstenite"
description = "Lightweight stream-based WebSocket implementation"
categories = ["web-programming::websocket", "network-programming"]
name = "ng-tungstenite"
description = "fork of tungstenite for Nextgraph.org"
categories = []
keywords = ["websocket", "io", "web"]
authors = ["Alexey Galakhov", "Daniel Abramov"]
authors = ["Alexey Galakhov", "Daniel Abramov", "Niko PLP <niko@nextgraph.org>"]
license = "MIT OR Apache-2.0"
readme = "README.md"
homepage = "https://github.com/snapview/tungstenite-rs"
homepage = "https://git.nextgraph.org/NextGraph/tungstenite-rs"
documentation = "https://docs.rs/tungstenite/0.19.0"
repository = "https://github.com/snapview/tungstenite-rs"
repository = "https://git.nextgraph.org/NextGraph/tungstenite-rs"
version = "0.19.0"
edition = "2018"
rust-version = "1.51"

@ -1,4 +1,6 @@
# Tungstenite
# ng-tungstenite
fork of https://github.com/snapview/tungstenite-rs for the needs of NextGraph.org
Lightweight stream-based WebSocket implementation for [Rust](https://www.rust-lang.org/).
@ -40,8 +42,8 @@ take a look at [`tokio-tungstenite`](https://github.com/snapview/tokio-tungsteni
[Documentation](https://docs.rs/tungstenite)
Introduction
------------
## Introduction
This library provides an implementation of WebSockets,
[RFC6455](https://tools.ietf.org/html/rfc6455). It allows for both synchronous (like TcpStream)
and asynchronous usage and is easy to integrate into any third-party event loops including
@ -49,23 +51,21 @@ and asynchronous usage and is easy to integrate into any third-party event loops
WebSocket protocol but still makes them accessible for those who wants full control over the
network.
Why Tungstenite?
----------------
## Why Tungstenite?
It's formerly WS2, the 2nd implementation of WS. WS2 is the chemical formula of
tungsten disulfide, the tungstenite mineral.
Features
--------
## Features
Tungstenite provides a complete implementation of the WebSocket specification.
TLS is supported on all platforms using `native-tls` or `rustls`. The following
features are available:
* `native-tls`
* `native-tls-vendored`
* `rustls-tls-native-roots`
* `rustls-tls-webpki-roots`
- `native-tls`
- `native-tls-vendored`
- `rustls-tls-native-roots`
- `rustls-tls-webpki-roots`
Choose the one that is appropriate for your needs.
@ -74,13 +74,11 @@ otherwise you won't be able to communicate with the TLS endpoints.
There is no support for permessage-deflate at the moment, but the PRs are welcome :wink:
Testing
-------
## Testing
Tungstenite is thoroughly tested and passes the [Autobahn Test Suite](https://crossbar.io/autobahn/) for
WebSockets. It is also covered by internal unit tests as well as possible.
Contributing
------------
## Contributing
Please report bugs and make feature requests [here](https://github.com/snapview/tungstenite-rs/issues).

@ -30,7 +30,7 @@ pub type Request = HttpRequest<()>;
pub type Response = HttpResponse<()>;
/// Server error response type.
pub type ErrorResponse = HttpResponse<Option<String>>;
pub type ErrorResponse = HttpResponse<Option<Vec<u8>>>;
fn create_parts<T>(request: &HttpRequest<T>) -> Result<Builder> {
if request.method() != http::Method::GET {
@ -156,23 +156,15 @@ pub trait Callback: Sized {
/// Called whenever the server read the request from the client and is ready to reply to it.
/// May return additional reply headers.
/// Returning an error resulting in rejecting the incoming connection.
fn on_request(
self,
request: &Request,
response: Response,
) -> StdResult<Response, ErrorResponse>;
fn on_request(self, request: &Request) -> StdResult<(), ErrorResponse>;
}
impl<F> Callback for F
where
F: FnOnce(&Request, Response) -> StdResult<Response, ErrorResponse>,
F: FnOnce(&Request) -> StdResult<(), ErrorResponse>,
{
fn on_request(
self,
request: &Request,
response: Response,
) -> StdResult<Response, ErrorResponse> {
self(request, response)
fn on_request(self, request: &Request) -> StdResult<(), ErrorResponse> {
self(request)
}
}
@ -181,12 +173,8 @@ where
pub struct NoCallback;
impl Callback for NoCallback {
fn on_request(
self,
_request: &Request,
response: Response,
) -> StdResult<Response, ErrorResponse> {
Ok(response)
fn on_request(self, _request: &Request) -> StdResult<(), ErrorResponse> {
Ok(())
}
}
@ -240,24 +228,24 @@ impl<S: Read + Write, C: Callback> HandshakeRole for ServerHandshake<S, C> {
return Err(Error::Protocol(ProtocolError::JunkAfterRequest));
}
let response = create_response(&result)?;
let callback_result = if let Some(callback) = self.callback.take() {
callback.on_request(&result, response)
callback.on_request(&result)
} else {
Ok(response)
Ok(())
};
match callback_result {
Ok(response) => {
Ok(_) => {
let response = create_response(&result)?;
let mut output = vec![];
write_response(&mut output, &response)?;
ProcessingResult::Continue(HandshakeMachine::start_write(stream, output))
}
Err(resp) => {
if resp.status().is_success() {
return Err(Error::Protocol(ProtocolError::CustomResponseSuccessful));
}
// if resp.status().is_success() {
// return Err(Error::Protocol(ProtocolError::CustomResponseSuccessful));
// }
self.error_response = Some(resp);
let resp = self.error_response.as_ref().unwrap();
@ -266,7 +254,7 @@ impl<S: Read + Write, C: Callback> HandshakeRole for ServerHandshake<S, C> {
write_response(&mut output, resp)?;
if let Some(body) = resp.body() {
output.extend_from_slice(body.as_bytes());
output.extend(body);
}
ProcessingResult::Continue(HandshakeMachine::start_write(stream, output))
@ -279,7 +267,6 @@ impl<S: Read + Write, C: Callback> HandshakeRole for ServerHandshake<S, C> {
debug!("Server handshake failed.");
let (parts, body) = err.into_parts();
let body = body.map(|b| b.as_bytes().to_vec());
return Err(Error::Http(http::Response::from_parts(parts, body)));
} else {
debug!("Server handshake done.");

Loading…
Cancel
Save