Search in sources :

Example 1 with CallingResponse

use of org.whispersystems.signalservice.api.messages.calls.CallingResponse in project Signal-Android by WhisperSystems.

the class PushServiceSocket method makeCallingRequest.

public CallingResponse makeCallingRequest(long requestId, String url, String httpMethod, List<Pair<String, String>> headers, byte[] body) {
    ConnectionHolder connectionHolder = getRandom(serviceClients, random);
    OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().followRedirects(false).connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
    RequestBody requestBody = body != null ? RequestBody.create(null, body) : null;
    Request.Builder builder = new Request.Builder().url(url).method(httpMethod, requestBody);
    if (headers != null) {
        for (Pair<String, String> header : headers) {
            builder.addHeader(header.first(), header.second());
        }
    }
    Request request = builder.build();
    for (int i = 0; i < MAX_FOLLOW_UPS; i++) {
        try (Response response = okHttpClient.newCall(request).execute()) {
            int responseStatus = response.code();
            if (responseStatus != 307) {
                return new CallingResponse.Success(requestId, responseStatus, response.body() != null ? response.body().bytes() : new byte[0]);
            }
            String location = response.header("Location");
            HttpUrl newUrl = location != null ? request.url().resolve(location) : null;
            if (newUrl != null) {
                request = request.newBuilder().url(newUrl).build();
            } else {
                return new CallingResponse.Error(requestId, new IOException("Received redirect without a valid Location header"));
            }
        } catch (IOException e) {
            Log.w(TAG, "Exception during ringrtc http call.", e);
            return new CallingResponse.Error(requestId, e);
        }
    }
    Log.w(TAG, "Calling request max redirects exceeded");
    return new CallingResponse.Error(requestId, new IOException("Redirect limit exceeded"));
}
Also used : CallingResponse(org.whispersystems.signalservice.api.messages.calls.CallingResponse) OkHttpClient(okhttp3.OkHttpClient) ReceiptCredentialRequest(org.signal.zkgroup.receipts.ReceiptCredentialRequest) Request(okhttp3.Request) ChangePhoneNumberRequest(org.whispersystems.signalservice.api.account.ChangePhoneNumberRequest) DiscoveryRequest(org.whispersystems.signalservice.internal.contacts.entities.DiscoveryRequest) KeyBackupRequest(org.whispersystems.signalservice.internal.contacts.entities.KeyBackupRequest) ProfileKeyCredentialRequest(org.signal.zkgroup.profiles.ProfileKeyCredentialRequest) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) CallingResponse(org.whispersystems.signalservice.api.messages.calls.CallingResponse) Response(okhttp3.Response) KeyBackupResponse(org.whispersystems.signalservice.internal.contacts.entities.KeyBackupResponse) ReceiptCredentialResponse(org.signal.zkgroup.receipts.ReceiptCredentialResponse) CredentialResponse(org.whispersystems.signalservice.api.groupsv2.CredentialResponse) StorageAuthResponse(org.whispersystems.signalservice.api.storage.StorageAuthResponse) VerifyDeviceResponse(org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse) DiscoveryResponse(org.whispersystems.signalservice.internal.contacts.entities.DiscoveryResponse) ProfileKeyCredentialResponse(org.signal.zkgroup.profiles.ProfileKeyCredentialResponse) TokenResponse(org.whispersystems.signalservice.internal.contacts.entities.TokenResponse) DigestingRequestBody(org.whispersystems.signalservice.internal.push.http.DigestingRequestBody) RequestBody(okhttp3.RequestBody)

Example 2 with CallingResponse

use of org.whispersystems.signalservice.api.messages.calls.CallingResponse in project Signal-Android by WhisperSystems.

the class SignalCallManager method onSendHttpRequest.

@Override
public void onSendHttpRequest(long requestId, @NonNull String url, @NonNull CallManager.HttpMethod httpMethod, @Nullable List<HttpHeader> headers, @Nullable byte[] body) {
    if (callManager == null) {
        Log.w(TAG, "Unable to send http request, call manager is not initialized");
        return;
    }
    Log.i(TAG, "onSendHttpRequest(): request_id: " + requestId);
    networkExecutor.execute(() -> {
        List<Pair<String, String>> headerPairs;
        if (headers != null) {
            headerPairs = Stream.of(headers).map(header -> new Pair<>(header.getName(), header.getValue())).toList();
        } else {
            headerPairs = Collections.emptyList();
        }
        CallingResponse response = messageSender.makeCallingRequest(requestId, url, httpMethod.name(), headerPairs, body);
        try {
            if (response instanceof CallingResponse.Success) {
                CallingResponse.Success success = (CallingResponse.Success) response;
                callManager.receivedHttpResponse(requestId, success.getResponseStatus(), success.getResponseBody());
            } else {
                callManager.httpRequestFailed(requestId);
            }
        } catch (CallException e) {
            Log.i(TAG, "Failed to process HTTP response/failure", e);
        }
    });
}
Also used : CallingResponse(org.whispersystems.signalservice.api.messages.calls.CallingResponse) CallException(org.signal.ringrtc.CallException) Pair(org.whispersystems.libsignal.util.Pair)

Aggregations

CallingResponse (org.whispersystems.signalservice.api.messages.calls.CallingResponse)2 IOException (java.io.IOException)1 HttpUrl (okhttp3.HttpUrl)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1 CallException (org.signal.ringrtc.CallException)1 ProfileKeyCredentialRequest (org.signal.zkgroup.profiles.ProfileKeyCredentialRequest)1 ProfileKeyCredentialResponse (org.signal.zkgroup.profiles.ProfileKeyCredentialResponse)1 ReceiptCredentialRequest (org.signal.zkgroup.receipts.ReceiptCredentialRequest)1 ReceiptCredentialResponse (org.signal.zkgroup.receipts.ReceiptCredentialResponse)1 Pair (org.whispersystems.libsignal.util.Pair)1 ChangePhoneNumberRequest (org.whispersystems.signalservice.api.account.ChangePhoneNumberRequest)1 CredentialResponse (org.whispersystems.signalservice.api.groupsv2.CredentialResponse)1 GroupsV2AuthorizationString (org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString)1 VerifyDeviceResponse (org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse)1 StorageAuthResponse (org.whispersystems.signalservice.api.storage.StorageAuthResponse)1 DiscoveryRequest (org.whispersystems.signalservice.internal.contacts.entities.DiscoveryRequest)1 DiscoveryResponse (org.whispersystems.signalservice.internal.contacts.entities.DiscoveryResponse)1