Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guide/client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@ When the connection fails, or stays disconnected for two seconds, the session re

The timeout bounds the requests as a whole, not each read, so a server that sends its answer a byte at a time cannot stretch it. A request still out when the session times out or is cancelled is cut off at once; one still waiting for the head of its answer only from Java 16 on, and before that at its timeout. Once the server has named the session, in the head of its answer, it is deleted all the same; a session the server created for an answer that had not come yet is left for the server to time out.

Failures and reports name the URL of an endpoint or session by its scheme, host and port only, since the rest may be a secret: services take stream keys and signatures in the query, and some, such as Cloudflare Stream, in the path. A failure can go to a log as it is, apart from what the endpoint itself says in an error, which it passes on unchanged.

`session.close()` deletes the session on the server, which frees what the server holds for it, and closes the peer. Both start at once, so a slow server does not keep the peer open, and the deletion takes at most the session's timeout. The tracks a WHIP session published stay open. `session.peer()` is a regular peer, for stats and anything else. A deletion the server refuses or that fails goes to the runtime's error handler, and also fails the future of `closeAsync()`, which completes once both are done; a peer that did not close fails it too.

`session.extensions()` gives the protocol extensions the endpoint offers for the session (RFC 9725, 4.9): the URL of each by its relation type, such as `urn:ietf:params:whip:ext:example:server-sent-events` (`urn:ietf:params:whep:ext:...` for WHEP), as the endpoint named them in `Link` header fields of its answer, resolved against it. Of a relation type named twice, the first counts. Links of other relation types are ignored, as WHIP and WHEP ask, apart from `ice-server`. The session uses none of the extensions itself; an application uses those it knows, and the map is empty if the endpoint named none.
Expand Down
8 changes: 6 additions & 2 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCIceServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ public boolean equals(Object obj) {
Objects.equals(username, other.username);
}

/**
* Describes the server for logs and messages, so without the password,
* and without {@link #hashCode()}, which is computed from it.
*/
@Override
public String toString() {
return String.format("%s@%d [urls=%s, username=%s, password=%s, tlsCertPolicy=%s, hostname=%s, tlsAlpnProtocols=%s, tlsEllipticCurves=%s]",
RTCIceServer.class.getSimpleName(), hashCode(),
urls, username, password, tlsCertPolicy, hostname,
RTCIceServer.class.getSimpleName(), System.identityHashCode(this),
urls, username, password != null ? "***" : null, tlsCertPolicy, hostname,
tlsAlpnProtocols, tlsEllipticCurves);
}
}
45 changes: 35 additions & 10 deletions webrtc/src/main/java/dev/onvoid/webrtc/client/HttpCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ CompletableFuture<Response> send(String contentType, byte[] content, int maxBody
void abort() {
IOException reason = remainingNanos() <= 0
? timedOut()
: new IOException("The request to " + uri + " was aborted");
: new IOException("The request to " + redacted(uri) + " was aborted");
CompletableFuture<?> exchange;
Body body;
Future<?> retry;
Expand Down Expand Up @@ -337,8 +337,9 @@ private void exchange(URI target, String contentType, byte[] content, int maxBod
request = builder.build();
}
catch (IllegalArgumentException e) {
result.completeExceptionally(new IOException("The request to " + target + " is invalid: "
+ e.getMessage(), e));
// Without the message or the exception itself, which quote the URL
// or the value of a header field, such as the Authorization.
result.completeExceptionally(new IOException("The request to " + redacted(target) + " is invalid"));
return;
}

Expand Down Expand Up @@ -374,7 +375,7 @@ private void exchange(URI target, String contentType, byte[] content, int maxBod
return;
}
if (redirects == MAX_REDIRECTS) {
result.completeExceptionally(new IOException("The request to " + uri
result.completeExceptionally(new IOException("The request to " + redacted(uri)
+ " was redirected more than " + MAX_REDIRECTS + " times"));
return;
}
Expand Down Expand Up @@ -407,7 +408,7 @@ private HttpResponse.BodySubscriber<String> head(HttpResponse.ResponseInfo info,

synchronized (this) {
if (aborted) {
body.stop(new IOException("The request to " + uri + " was aborted"));
body.stop(new IOException("The request to " + redacted(uri) + " was aborted"));
return body;
}

Expand Down Expand Up @@ -505,19 +506,19 @@ private static URI redirectTarget(URI from, String location) throws IOException
target = from.resolve(location.trim());
}
catch (IllegalArgumentException e) {
throw new IOException("The server redirected to an invalid URL: " + location);
throw new IOException("The server redirected to an invalid URL");
}

String scheme = target.getScheme();

if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
throw new IOException("The server redirected to a URL that is not http: " + target);
throw new IOException("The server redirected to a URL that is not http: " + redacted(target));
}
if ("https".equalsIgnoreCase(from.getScheme()) && !"https".equalsIgnoreCase(scheme)) {
throw new IOException("The server redirected to a URL without TLS: " + target);
throw new IOException("The server redirected to a URL without TLS: " + redacted(target));
}
if (!"https".equalsIgnoreCase(scheme) && !isLoopback(target.getHost())) {
throw new IOException("The server redirected to a URL without TLS: " + target);
throw new IOException("The server redirected to a URL without TLS: " + redacted(target));
}

return target;
Expand Down Expand Up @@ -557,12 +558,36 @@ static boolean isLoopback(String host) {
}
}

/**
* Names a URL in a message, by its scheme, host and port only, since what
* a message says ends up in logs. The rest of a URL may be a secret, and
* not only its user info and query: Cloudflare Stream, for one, has the
* key to publish with as a segment of the path.
*
* @param uri the URL.
*
* @return the URL without what may be a secret, such as {@code
* https://example.com:8443}.
*/
static String redacted(URI uri) {
String scheme = uri.getScheme() != null ? uri.getScheme() + ":" : "";
String authority = uri.getRawAuthority();

if (authority == null) {
return scheme.isEmpty() ? "a URL without a scheme" : scheme;
}

// The raw authority, since a host that is not a valid one, as with an
// underscore, leaves getHost() and getUserInfo() null.
return scheme + "//" + authority.substring(authority.lastIndexOf('@') + 1);
}

private long remainingNanos() {
return limit - (System.nanoTime() - start);
}

private HttpTimeoutException timedOut() {
return new HttpTimeoutException("The request to " + uri + " did not complete within " + timeout);
return new HttpTimeoutException("The request to " + redacted(uri) + " did not complete within " + timeout);
}

private static Throwable unwrap(Throwable error) {
Expand Down
16 changes: 9 additions & 7 deletions webrtc/src/main/java/dev/onvoid/webrtc/client/HttpIce.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package dev.onvoid.webrtc.client;

import static dev.onvoid.webrtc.client.HttpCall.redacted;

import java.net.URI;
import java.net.http.HttpClient;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -273,25 +275,25 @@ private UnaryOperator<String> answered(HttpCall request, HttpCall.Response respo
}

if (error != null) {
throw new WebRtcException("Restarting ICE of the session " + target + " failed: " + error.getMessage(),
throw new WebRtcException("Restarting ICE of the session " + redacted(target) + " failed: " + error.getMessage(),
error);
}
if (UNSUPPORTED.contains(response.status)) {
synchronized (this) {
restartsRefused = true;
}

throw response.error("The session " + target + " takes no ICE restarts: " + response.describe());
throw response.error("The session " + redacted(target) + " takes no ICE restarts: " + response.describe());
}
if (response.status / 100 != 2) {
throw response.error("The session " + target + " answered the ICE restart with " + response.describe());
throw response.error("The session " + redacted(target) + " answered the ICE restart with " + response.describe());
}

HttpSession.requireContentType(response, FRAGMENT_TYPE);
Fragment remote = Fragment.parse(response.body);

if (remote.ufrag == null || remote.pwd == null) {
throw new WebRtcException("The session " + target + " answered the ICE restart without "
throw new WebRtcException("The session " + redacted(target) + " answered the ICE restart without "
+ "its new ICE credentials");
}

Expand Down Expand Up @@ -391,10 +393,10 @@ private void flush() {
// An error status is the cause, for its status.
Throwable cause = error != null
? error
: response.error("The session " + target + " answered the candidates with "
: response.error("The session " + redacted(target) + " answered the candidates with "
+ response.describe());

rtc.reportError(new WebRtcException("Sending ICE candidates to the session " + target
rtc.reportError(new WebRtcException("Sending ICE candidates to the session " + redacted(target)
+ " failed, and no more are sent: " + (error != null
? error.getMessage()
: response.describe()), cause));
Expand Down Expand Up @@ -491,7 +493,7 @@ private void restartByItself() {
}

if (report) {
rtc.reportError(new WebRtcException("Restarting ICE of the session " + target
rtc.reportError(new WebRtcException("Restarting ICE of the session " + redacted(target)
+ " after the connection broke failed", PeerImpl.unwrap(error)));
}
});
Expand Down
45 changes: 25 additions & 20 deletions webrtc/src/main/java/dev/onvoid/webrtc/client/HttpSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dev.onvoid.webrtc.client;

import static dev.onvoid.webrtc.client.HttpCall.redacted;
import static java.util.Objects.requireNonNull;

import java.net.URI;
Expand Down Expand Up @@ -292,18 +293,18 @@ private static CompletableFuture<URI> negotiate(WebRtc rtc, PeerImpl peer, URI e
// as the timeout again.
peer.close();
result.completeExceptionally(PeerImpl.asWebRtcException(error,
"The session with " + endpoint + " could not be set up"));
"The session with " + redacted(endpoint) + " could not be set up"));

discard(rtc, resource.getAndSet(null), authorization, timeout);
};

ScheduledFuture<?> deadline = rtc.timer.schedule(() -> fail.accept(new WebRtcException(
"The session with " + endpoint + " was not connected within " + timeout)),
"The session with " + redacted(endpoint) + " was not connected within " + timeout)),
Timeouts.toMillis(timeout), TimeUnit.MILLISECONDS);

abandoned.thenRun(() -> {
deadline.cancel(false);
fail.accept(new CancellationException("The session with " + endpoint + " was abandoned"));
fail.accept(new CancellationException("The session with " + redacted(endpoint) + " was abandoned"));
});

CompletableFuture<RTCSessionDescription> initial = prepared.thenCompose(ready -> gathering == Gathering.COMPLETE
Expand Down Expand Up @@ -385,12 +386,12 @@ private static CompletableFuture<Reply> post(HttpCall request, URI endpoint, Str
throw (WebRtcException) error;
}
if (error != null) {
throw new WebRtcException("Sending the offer to " + endpoint + " failed: " + error.getMessage(),
throw new WebRtcException("Sending the offer to " + redacted(endpoint) + " failed: " + error.getMessage(),
error);
}
if (response.status == 201) {
if (resource.get() == null) {
throw new WebRtcException("The endpoint " + endpoint + " answered without a session Location");
throw new WebRtcException("The endpoint " + redacted(endpoint) + " answered without a session Location");
}
requireContentType(response, "application/sdp");
return new Reply(response.body, false, null, 0, response, protocol);
Expand All @@ -399,7 +400,7 @@ private static CompletableFuture<Reply> post(HttpCall request, URI endpoint, Str
URI session = resourceOf(response.uri, response.header("Location"));

if (session == null) {
throw new WebRtcException("The endpoint " + endpoint
throw new WebRtcException("The endpoint " + redacted(endpoint)
+ " countered the offer without naming the session");
}

Expand All @@ -408,7 +409,7 @@ private static CompletableFuture<Reply> post(HttpCall request, URI endpoint, Str
protocol);
}

throw response.error("The endpoint " + endpoint + " answered the offer with " + response.describe());
throw response.error("The endpoint " + redacted(endpoint) + " answered the offer with " + response.describe());
});
}

Expand All @@ -426,7 +427,7 @@ private static CompletableFuture<Void> answerCounterOffer(WebRtc rtc, HttpClient
return peer.manual.answerInstead(new RTCSessionDescription(RTCSdpType.OFFER, reply.sdp), Gathering.COMPLETE)
.thenCompose(answer -> {
if (System.currentTimeMillis() > reply.validUntil) {
throw new WebRtcException("The counter-offer for " + reply.session
throw new WebRtcException("The counter-offer for " + redacted(reply.session)
+ " expired before it was answered");
}

Expand All @@ -446,12 +447,12 @@ private static CompletableFuture<Void> answerCounterOffer(WebRtc rtc, HttpClient
withRtcpMuxOnly(answer.sdp).getBytes(StandardCharsets.UTF_8), 0,
head -> { }).handle((response, error) -> {
if (error != null) {
throw new WebRtcException("Sending the answer to " + reply.session + " failed: "
throw new WebRtcException("Sending the answer to " + redacted(reply.session) + " failed: "
+ error.getMessage(), error);
}
if (response.status / 100 != 2) {
throw response.error("The endpoint answered the answer for "
+ reply.session + " with " + response.describe());
+ redacted(reply.session) + " with " + response.describe());
}

ice.attach(reply.session, response.header("ETag"), answer.sdp);
Expand Down Expand Up @@ -610,14 +611,14 @@ private static CompletableFuture<WebRtcException> discard(WebRtc rtc, URI resour
Throwable failure = error;

if (failure == null && response.status / 100 != 2) {
failure = response.error("The endpoint answered the deletion of " + resource
failure = response.error("The endpoint answered the deletion of " + redacted(resource)
+ " with " + response.describe());
}
if (failure == null) {
return null;
}

WebRtcException reported = new WebRtcException("Deleting the session " + resource + " failed",
WebRtcException reported = new WebRtcException("Deleting the session " + redacted(resource) + " failed",
failure);
rtc.reportError(reported);
return reported;
Expand Down Expand Up @@ -659,7 +660,7 @@ static CompletableFuture<HttpCall.Response> head(WebRtc rtc, URI endpoint, Strin
CompletableFuture<HttpCall.Response> response = request.send(null, null, 0, head -> { })
.handle((value, error) -> {
if (error != null) {
throw new WebRtcException("Asking " + endpoint + " with HEAD failed: " + error.getMessage(),
throw new WebRtcException("Asking " + redacted(endpoint) + " with HEAD failed: " + error.getMessage(),
error);
}

Expand Down Expand Up @@ -688,7 +689,7 @@ private static WebRtcException leftOnServer(WebRtc rtc, URI resource) {
return null;
}

WebRtcException error = new WebRtcException("The session " + resource
WebRtcException error = new WebRtcException("The session " + redacted(resource)
+ " was left on the server, since the runtime is closed");
rtc.reportError(error);
return error;
Expand All @@ -710,19 +711,23 @@ static URI resourceOf(URI endpoint, String location) {
resource = endpoint.resolve(location.trim());
}
catch (IllegalArgumentException e) {
throw new WebRtcException("The endpoint " + endpoint + " named an invalid resource: " + location, e);
// Without the exception, which quotes the Location.
throw new WebRtcException("The endpoint " + redacted(endpoint) + " named an invalid resource");
}

String scheme = resource.getScheme();

if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
throw new WebRtcException("The endpoint " + endpoint + " named a resource that is not http: " + resource);
throw new WebRtcException("The endpoint " + redacted(endpoint) + " named a resource that is not http: "
+ redacted(resource));
}
if ("https".equalsIgnoreCase(endpoint.getScheme()) && !"https".equalsIgnoreCase(scheme)) {
throw new WebRtcException("The endpoint " + endpoint + " named a resource without TLS: " + resource);
throw new WebRtcException("The endpoint " + redacted(endpoint) + " named a resource without TLS: "
+ redacted(resource));
}
if (!"https".equalsIgnoreCase(scheme) && !HttpCall.isLoopback(resource.getHost())) {
throw new WebRtcException("The endpoint " + endpoint + " named a resource without TLS: " + resource);
throw new WebRtcException("The endpoint " + redacted(endpoint) + " named a resource without TLS: "
+ redacted(resource));
}

return resource;
Expand Down Expand Up @@ -782,7 +787,7 @@ static String withRtcpMuxOnly(String sdp) {
static void requireContentType(HttpCall.Response response, String expected) {
String type = response.header("Content-Type");
if (type == null || !type.split(";", 2)[0].trim().equalsIgnoreCase(expected)) {
throw new WebRtcException("The session " + response.uri + " answered with Content-Type "
throw new WebRtcException("The session " + redacted(response.uri) + " answered with Content-Type "
+ type + "; expected " + expected);
}
}
Expand Down Expand Up @@ -888,7 +893,7 @@ static URI checkEndpoint(URI endpoint) {

if (!"https".equalsIgnoreCase(scheme) && !local) {
throw new IllegalArgumentException("The endpoint must be an https URL, or an http one on this machine, "
+ "such as http://localhost, got " + endpoint);
+ "such as http://localhost, got " + redacted(endpoint));
}

return endpoint;
Expand Down
Loading
Loading