Commit Graph

26 Commits

Author SHA1 Message Date
Sam Atkins
3b1e063d30 LibCore+Everywhere: Make Core::Stream::read() return Bytes
A mistake I've repeatedly made is along these lines:
```c++
auto nread = TRY(source_file->read(buffer));
TRY(destination_file->write(buffer));
```

It's a little clunky to have to create a Bytes or StringView from the
buffer's data pointer and the nread, and easy to forget and just use
the buffer. So, this patch changes the read() function to return a
Bytes of the data that were just read.

The other read_foo() methods will be modified in the same way in
subsequent commits.

Fixes #13687
2022-04-16 13:27:51 -04:00
Lenny Maiorani
1a59cffb00 Libraries: Use default constructors/destructors in LibWebSocket
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-13 22:34:38 +01:00
Luke Wilde
1535fe0324 LibWebSocket: Add to the total read in bytes instead of subtracting
read_length in WebSocket::read_frame is used to track how many bytes we
have read in from the network. However, we was subtracting the number
of read in bytes instead of adding, underflowing it to about the 64-bit
unsigned integer limit.

This effectively limited us to only doing one read from the network.
This was only an issue if the server stalled when sending data,
which is especially common for large payloads. This would also cause us
to go out of sync. This meant when a new frame came in, we would read
the payload data of the previous frame as if it was the frame header
and payload of the next frame.

This allows us to read in the initial payload from Discord Gateway
that describes to the client the servers we are in, the emotes the
server has, the channels it has, etc. For an account that's only in
the Serenity Discord, this was about 20 KB (compressed!)
2022-03-10 01:15:00 +01:00
DerpyCrabs
29078d4d53 LibWebSocket: Don't try to send empty payload inside of frame
According to RFC 6455 sections 5.5.2-5.5.3 Ping and Pong frames
can have empty "Application data" that means payload can be of size 0.
This change fixes failed "buffer.size()" assertion inside
of Core::Stream::write_or_error by not trying to send empty payload
in WebSocket::send_frame.
2022-02-12 11:12:43 +01:00
sin-ack
64f135d90f LibCore+Userland: Remove Core::TCPSocket :^)
This was deprecated in favor of Core::Stream::TCPSocket, and now has no
users.
2022-02-06 17:28:17 +00:00
Ali Mohammad Pur
3f614a8fca LibWebSocket: Switch to using Core::Stream
As LibTLS now supports the Core::Stream APIs, we can get rid of the
split paths for TCP/TLS and significantly simplify the code as well.
Provided to you free of charge by the Core::Stream-ification team :^)
2022-02-06 13:10:10 +01:00
Ali Mohammad Pur
aafc451016 Userland: Convert TLS::TLSv12 to a Core::Stream::Socket
This commit converts TLS::TLSv12 to a Core::Stream object, and in the
process allows TLS to now wrap other Core::Stream::Socket objects.
As a large part of LibHTTP and LibGemini depend on LibTLS's interface,
this also converts those to support Core::Stream, which leads to a
simplification of LibHTTP (as there's no need to care about the
underlying socket type anymore).
Note that RequestServer now controls the TLS socket options, which is a
better place anyway, as RS is the first receiver of the user-requested
options (though this is currently not particularly useful).
2022-02-06 13:10:10 +01:00
Sam Atkins
45cf40653a Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr
Apologies for the enormous commit, but I don't see a way to split this
up nicely. In the vast majority of cases it's a simple change. A few
extra places can use TRY instead of manual error checking though. :^)
2022-01-24 22:36:09 +01:00
Andrew Kaster
d809637023 Userland: Resolve tautological-constant-out-of-range-compare warnings
Stop comparing platform-specific sized integer types to max() values of
other interger types. Enable the warning everywhere.
2022-01-04 07:51:31 +00:00
Ben Wiederhake
b3e9a4e603 Libraries: Fix visibility of Object-derivative constructors
Derivatives of Core::Object should be constructed through
ClassName::construct(), to avoid handling ref-counted objects with
refcount zero. Fixing the visibility means that misuses like this are
more difficult.
2021-11-02 22:56:53 +01:00
Ben Wiederhake
c569ed7e8b LibWebSocket: Add missing headers to Message.h 2021-10-06 23:52:40 +01:00
Ali Mohammad Pur
436693c0c9 LibTLS: Use a setter for on_tls_ready_to_write with some more smarts
The callback should be called as soon as the connection is established,
and if we actually set the callback when it already is, we expect it to
be called immediately.
2021-09-19 21:10:23 +04:30
Andreas Kling
1735d978ed LibWebSocket: Use deferred_invoke() when discarding a connection
We don't want to destroy the WebSocketImpl while we're still using it
higher up the stack. By using deferred_invoke(), we allow the stack
to unwind before actually destroying any objects.

This fixes an issue with the WebSocket service crashing on immediate
connection failure.
2021-09-18 12:57:58 +02:00
Andreas Kling
f82c4c4137 LibWebSocket: Fix confusion about sizeof(size_t) on 64-bit platforms
sizeof(size_t) is 8 on 64-bit platforms, not 64.
2021-09-18 12:51:31 +02:00
Andreas Kling
00895a5789 LibWebSocket: Fix a handful of clang-tidy warnings in WebSocket.{cpp,h} 2021-09-18 12:48:34 +02:00
Idan Horowitz
d6cfa34667 AK: Make URL::m_port an Optional<u16>, Expose raw port getter
Our current way of signalling a missing port with m_port == 0 was
lacking, as 0 is a valid port number in URLs.
2021-09-14 00:14:45 +02:00
Ali Mohammad Pur
97e97bccab Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safe 2021-09-06 01:53:26 +02:00
sin-ack
8ea22121ac Userland: Migrate to argument-less deferred_invoke
Only one place used this argument and it was to hold on to a strong ref
for the object. Since we already do that now, there's no need to keep
this argument around since this can be easily captured.

This commit contains no changes.
2021-09-02 03:47:47 +04:30
Ali Mohammad Pur
51c2c69357 AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-06 00:27:30 +04:30
DexesTTP
812875bc89 LibWebSocket: Fixed occasional infinite loop with TLS sockets
This was caused by a double notifier on the TLS socket, which caused
the TLS code to freak out about not being able to read properly. In
addition, the existing loop inside of drain_read() has been replaced by
code that actually works, and which includes new warnings when the
drain method is called before initialization is done or after the
websocket gets closed.
2021-05-18 08:07:56 +02:00
Linus Groh
0aab774343 Everywhere: Fix a bunch of typos 2021-05-17 17:48:55 +01:00
DexesTTP
62ed26164b Services: Add a WebSocket service
The WebSocket service isolates communication with a WebSocket to its
own isolated process. Similar to other isolating services, it has its
own user and group.
2021-04-25 19:04:34 +02:00
DexesTTP
c11ca9df33 LibWebSocket+telws: Use my own copyright headers :^)
Since I guess I'll start attributing my own code, might as well change
it for the previously written WebSocket code too !
2021-04-25 19:04:34 +02:00
Andreas Kling
b91c49364d AK: Rename adopt() to adopt_ref()
This makes it more symmetrical with adopt_own() (which is used to
create a NonnullOwnPtr from the result of a naked new.)
2021-04-23 16:46:57 +02:00
Brian Gianforcaro
1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
DexesTTP
d3a89ce737 LibWebSocket: Add a new websocket library
This library currently contains a basic WebSocket client that can
handle both standard TCP websockets and TLS websockets.
2021-04-18 22:42:10 +02:00