Skip to content

Commit 3ad7fce

Browse files
committed
fixes
1 parent 7094fd6 commit 3ad7fce

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

src/main/java/com/browserstack/local/Local.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ public void stop() throws Exception {
109109
public void stop(Map<String, String> options) throws Exception {
110110
LocalBinary lb;
111111
if (options.get("binarypath") != null) {
112-
lb = new LocalBinary(options.get("binarypath"));
112+
lb = new LocalBinary(options.get("binarypath"), options.get("key"));
113113
} else {
114-
lb = new LocalBinary("");
114+
lb = new LocalBinary("", options.get("key"));
115115
}
116116
binaryPath = lb.getBinaryPath();
117117
makeCommand(options, "stop");

src/main/java/com/browserstack/local/LocalBinary.java

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ private void downloadAndVerifyBinary(String path) throws LocalException {
6060
checkBinary();
6161
} catch (Throwable e) {
6262
if (fallbackEnabled) throw e;
63+
File binary_file = new File(binaryPath);
64+
if (binary_file.exists()) {
65+
binary_file.delete();
66+
}
6367
fallbackEnabled = true;
6468
downloadFailureThrowable = e;
6569
downloadAndVerifyBinary(path);
@@ -191,39 +195,46 @@ private boolean makePath(String path) {
191195
}
192196
}
193197

194-
private void fetchSourceUrl() {
195-
if ((!fallbackEnabled && sourceUrl) || (fallbackEnabled && !downloadFailureThrowable)) {
198+
private void fetchSourceUrl() throws LocalException {
199+
if ((!fallbackEnabled && sourceUrl != null) || (fallbackEnabled && downloadFailureThrowable == null)) {
196200
/* Retry because binary (from any of the endpoints) validation failed */
197201
return;
198202
}
199203

200-
URL url = new URL("https://local.browserstack.com/binary/api/v1/endpoint");
201-
URLConnection connection = url.openConnection();
202-
203-
connection.setDoOutput(true);
204-
connection.setRequestProperty("Content-Type", "application/json");
205-
connection.setRequestProperty("User-Agent", "browserstack-local-java/" + Local.getPackageVersion());
206-
connection.setRequestProperty("Accept", "application/json");
207-
if (fallbackEnabled) connection.setRequestProperty("X-Local-Fallback-Cloudflare", "true");
208-
209-
String jsonInput = "{\"auth_token\": " + key + (fallbackEnabled ? (", \"error_message\": " + downloadFailureThrowable.getMessage()) : "") + "}";
210-
211-
try (OutputStream os = connection.getOutputStream()) {
212-
byte[] input = jsonInput.getBytes("utf-8");
213-
os.write(input, 0, input.length);
214-
}
215-
216-
try (InputStream is = connection.getInputStream();
217-
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"))) {
218-
StringBuilder response = new StringBuilder();
219-
String line;
220-
while ((line = reader.readLine()) != null) {
221-
response.append(line.trim());
222-
}
223-
String responseBody = response.toString();
224-
JSONObject json = new JSONObject(responseBody);
225-
this.sourceUrl = json.getJSONObject("data").getString("endpoint");
226-
if(fallbackEnabled) downloadFailureThrowable = null;
204+
try {
205+
URL url = new URL("https://local.browserstack.com/binary/api/v1/endpoint");
206+
URLConnection connection = url.openConnection();
207+
208+
connection.setDoOutput(true);
209+
connection.setRequestProperty("Content-Type", "application/json");
210+
connection.setRequestProperty("User-Agent", "browserstack-local-java/" + Local.getPackageVersion());
211+
connection.setRequestProperty("Accept", "application/json");
212+
if (fallbackEnabled) connection.setRequestProperty("X-Local-Fallback-Cloudflare", "true");
213+
214+
String jsonInput = "{\"auth_token\": \"" + key + (fallbackEnabled ? ("\", \"error_message\": \"" + downloadFailureThrowable.getMessage()) + "\"" : "\"") + "}";
215+
216+
try (OutputStream os = connection.getOutputStream()) {
217+
byte[] input = jsonInput.getBytes("utf-8");
218+
os.write(input, 0, input.length);
219+
}
220+
221+
try (InputStream is = connection.getInputStream();
222+
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"))) {
223+
StringBuilder response = new StringBuilder();
224+
String line;
225+
while ((line = reader.readLine()) != null) {
226+
response.append(line.trim());
227+
}
228+
String responseBody = response.toString();
229+
JSONObject json = new JSONObject(responseBody);
230+
if (json.has("error")) {
231+
throw new Exception(json.getString("error"));
232+
}
233+
this.sourceUrl = json.getJSONObject("data").getString("endpoint");
234+
if(fallbackEnabled) downloadFailureThrowable = null;
235+
}
236+
} catch (Throwable e) {
237+
throw new LocalException("Error trying to fetch the source URL: " + e.getMessage());
227238
}
228239
}
229240

0 commit comments

Comments
 (0)