Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Azure/AutoRest
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Jul 18, 2016
2 parents d7987f9 + 4660eff commit ea7edb9
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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);
}
}

0 comments on commit ea7edb9

Please sign in to comment.