Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added digest auth #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {

allprojects {
group = 'com.github.niqdev'
version = '1.3.2'
version = '1.3.3'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert this, I will change it when release the lib

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

Copy link
Contributor Author

@amadeu01 amadeu01 Apr 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, It could be done by gradle task.

def getVersionCode = { ->
  try {
    def code = new ByteArrayOutputStream()
    exec {
      commandLine 'git', 'rev-list', 'HEAD', '--count'
      standardOutput = code
    }
    return Integer.parseInt(code.toString().trim())
  } catch (exception) {
    return "1";
  }
}

// Version name is Last Tag Name + No. of commits form last Tag +  short git sha
def getVersionName = { ->
  try {
    def stdout = new ByteArrayOutputStream()
    exec {
      commandLine 'git', 'describe', '--tags', '--dirty'
      standardOutput = stdout
    }
    return stdout.toString().trim()
  } catch (exception) {
    return "0.0.0.1";
  }
}

// Use
android{
  defaultConfig {
    ...
    versionCode getVersionCode()
    versionName getVersionName()
    ...
  }
}

@niqdev

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, so to use it properly i need to tag it just before release the lib i.e. ./gradlew build bintrayUpload. correct? can you add it in a different PR unless you think is going to be different in #59?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @niqdev
I will do another PR, for that change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this should be set automatically.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you go #115


repositories {
jcenter()
Expand Down
1 change: 1 addition & 0 deletions mjpeg-view/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
38 changes: 35 additions & 3 deletions mjpeg-view/src/main/java/com/github/niqdev/mjpeg/Mjpeg.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,15 +97,25 @@ public Mjpeg sendConnectionCloseHeader() {
}

@NonNull
private Observable<MjpegInputStream> connect(String url) {
private Observable<MjpegInputStream> 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)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if is worth it, maybe extract it in a method and add comment for why is needed, otherwise is fine.
The important thing is that it must be retrocompatible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I will do it later. Thanks for your feedback

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
Expand All @@ -126,7 +139,7 @@ private Observable<MjpegInputStream> connect(String url) {
* @return Observable Mjpeg stream
*/
public Observable<MjpegInputStream> open(String url) {
return connect(url)
return connect(url, null, null)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
Expand All @@ -139,10 +152,29 @@ public Observable<MjpegInputStream> open(String url) {
* @return Observable Mjpeg stream
*/
public Observable<MjpegInputStream> 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<MjpegInputStream> openWithDigestAuth(String url,
int timeout,
String userDigestAuth,
String passDigestAuth) {
return connect(url, userDigestAuth, passDigestAuth)
.timeout(timeout, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}

}