diff --git a/build.gradle b/build.gradle index e9a0f54a..d3698700 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ buildscript { allprojects { group = 'com.github.niqdev' - version = '1.3.2' + version = '1.3.3' repositories { jcenter() diff --git a/mjpeg-view/build.gradle b/mjpeg-view/build.gradle index 6e3facc5..906bac6b 100644 --- a/mjpeg-view/build.gradle +++ b/mjpeg-view/build.gradle @@ -42,6 +42,7 @@ dependencies { compile 'io.reactivex:rxjava:1.3.2' compile 'io.reactivex:rxandroid:1.2.1' + compile 'com.albroco:bare-bones-digest:1.0.1' } apply from: '../bintray.gradle' diff --git a/mjpeg-view/src/main/java/com/github/niqdev/mjpeg/Mjpeg.java b/mjpeg-view/src/main/java/com/github/niqdev/mjpeg/Mjpeg.java index 5c08cbe8..f883f3e9 100644 --- a/mjpeg-view/src/main/java/com/github/niqdev/mjpeg/Mjpeg.java +++ b/mjpeg-view/src/main/java/com/github/niqdev/mjpeg/Mjpeg.java @@ -4,6 +4,9 @@ import android.text.TextUtils; import android.util.Log; +import com.albroco.barebonesdigest.DigestAuthentication; +import com.albroco.barebonesdigest.DigestChallengeResponse; + import java.io.IOException; import java.io.InputStream; import java.net.Authenticator; @@ -94,15 +97,25 @@ public Mjpeg sendConnectionCloseHeader() { } @NonNull - private Observable connect(String url) { + private Observable connect(String url, String username, String password) { return Observable.defer(() -> { try { HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection(); urlConnection.setRequestProperty("Cache-Control", "no-cache"); + if (sendConnectionCloseHeader) { urlConnection.setRequestProperty("Connection", "close"); } + if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED && !TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) { + DigestAuthentication auth = DigestAuthentication.fromResponse(urlConnection); + auth.username(username).password(password); + + urlConnection = (HttpURLConnection) new URL(url).openConnection(); + String authentication = auth.getAuthorizationForRequest("GET", urlConnection.getURL().getPath()); + urlConnection.setRequestProperty(DigestChallengeResponse.HTTP_HEADER_AUTHORIZATION, authentication); + } + InputStream inputStream = urlConnection.getInputStream(); switch (type) { // handle multiple implementations @@ -126,7 +139,7 @@ private Observable connect(String url) { * @return Observable Mjpeg stream */ public Observable open(String url) { - return connect(url) + return connect(url, null, null) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); } @@ -139,10 +152,29 @@ public Observable open(String url) { * @return Observable Mjpeg stream */ public Observable open(String url, int timeout) { - return connect(url) + return connect(url, null, null) .timeout(timeout, TimeUnit.SECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); } + /** + * Connect to a Mjpeg stream. + * + * @param url source + * @param timeout in seconds + * @param userDigestAuth in seconds + * @param passDigestAuth in seconds + * @return Observable Mjpeg stream + */ + public Observable openWithDigestAuth(String url, + int timeout, + String userDigestAuth, + String passDigestAuth) { + return connect(url, userDigestAuth, passDigestAuth) + .timeout(timeout, TimeUnit.SECONDS) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()); + } + }