From 4660eff3a89264a2199fe8780bb34c576f17b1e8 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Thu, 14 Jul 2016 17:33:09 -0700 Subject: [PATCH] Add HTTPS as default scheme --- .../java/com/microsoft/rest/BaseUrlHandler.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java b/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java index 46eacbd472e23..6d596c092e141 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java +++ b/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java @@ -7,13 +7,13 @@ package com.microsoft.rest; -import java.io.IOException; - import okhttp3.HttpUrl; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; +import java.io.IOException; + /** * Handles dynamic replacements on base URL. The arguments must be in pairs * with the string in raw URL to replace as replacements[i] and the dynamic @@ -34,6 +34,7 @@ public Response intercept(Chain chain) throws IOException { for (int i = 0; i < replacements.length; i += 2) { baseUrl = baseUrl.replaceAll("(?i)\\Q" + replacements[i] + "\\E", replacements[i + 1]); } + baseUrl = removeRedundantProtocol(baseUrl); HttpUrl baseHttpUrl = HttpUrl.parse(baseUrl); request = request.newBuilder() .url(baseHttpUrl) @@ -42,4 +43,12 @@ public Response intercept(Chain chain) throws IOException { } return chain.proceed(request); } + + private String removeRedundantProtocol(String url) { + int last = url.lastIndexOf("://") - 1; + while (last >= 0 && Character.isLetter(url.charAt(last))) { + --last; + } + return url.substring(last + 1); + } }