Skip to content

Commit

Permalink
Merge pull request #4373 from thc202/quickstart/scheme-add
Browse files Browse the repository at this point in the history
quickstart: add scheme to URL if not present
  • Loading branch information
kingthorin committed Jan 24, 2023
2 parents 3c4f2d8 + 6cac219 commit 10b9ba0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 32 deletions.
3 changes: 3 additions & 0 deletions addOns/quickstart/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed
- Maintenance changes.

### Fixed
- Show correct error message when unable to access the provided URL, also, add the scheme if none provided.

## [36] - 2023-01-03
### Fixed
- Correctly unload the add-on.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,7 @@ private JButton getLaunchToolbarButton() {
launchToolbarButton = new JButton();
launchToolbarButton.setToolTipText(
Constant.messages.getString("quickstart.toolbar.button.tooltip.launch"));
launchToolbarButton.addActionListener(
e ->
launchBrowser(
launchPanel.getSelectedBrowser(), launchPanel.getUrlValue()));
launchToolbarButton.addActionListener(e -> launchPanel.launchBrowser());
}
return launchToolbarButton;
}
Expand Down Expand Up @@ -222,23 +219,7 @@ protected void launchBrowser(String browserName, String url) {
if (wd != null) {
QuickStartParam params =
getExtQuickStart().getQuickStartParam();
if (url != null
&& url.length() > 0
&& !url.equals(DEFAULT_VALUE_URL_FIELD)) {
wd.get(url);
} else if (params.isLaunchZapStartPage()) {
wd.get(
API.getInstance()
.getBaseURL(
API.Format.OTHER,
QuickStartLaunchAPI.API_PREFIX,
API.RequestType.other,
QuickStartLaunchAPI
.OTHER_START_PAGE,
true));
} else if (!params.isLaunchBlankStartPage()) {
wd.get(params.getLaunchStartPage());
}
accessUrl(wd, params, url);
// Use the same browser next time, as long
// as it worked
params.setLaunchDefaultBrowser(browserName);
Expand All @@ -257,6 +238,36 @@ protected void launchBrowser(String browserName, String url) {
.start();
}

private static void accessUrl(WebDriver wd, QuickStartParam params, String userUrl) {
String url = null;
if (userUrl != null && userUrl.length() > 0 && !userUrl.equals(DEFAULT_VALUE_URL_FIELD)) {
url = userUrl;
} else if (params.isLaunchZapStartPage()) {
url =
API.getInstance()
.getBaseURL(
API.Format.OTHER,
QuickStartLaunchAPI.API_PREFIX,
API.RequestType.other,
QuickStartLaunchAPI.OTHER_START_PAGE,
true);
} else if (!params.isLaunchBlankStartPage()) {
url = params.getLaunchStartPage();
}

if (url != null) {
try {
wd.get(url);
} catch (Exception e) {
View.getSingleton()
.showWarningDialog(
Constant.messages.getString(
"quickstart.launch.start.url.access.error", url));
LOGGER.warn("Failed to access the URL {}, cause: {}", url, e.getMessage());
}
}
}

public String getDefaultLaunchContent() {
// This is no longer read from a link
return Constant.messages.getString("quickstart.launch.html");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import javax.swing.ComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
Expand All @@ -38,6 +40,7 @@
import org.parosproxy.paros.control.Control;
import org.parosproxy.paros.model.Model;
import org.parosproxy.paros.model.SiteNode;
import org.parosproxy.paros.network.HttpHeader;
import org.parosproxy.paros.view.View;
import org.zaproxy.zap.ZAP;
import org.zaproxy.zap.eventBus.Event;
Expand Down Expand Up @@ -66,6 +69,9 @@ public class LaunchPanel extends QuickStartSubPanel implements EventConsumer {
private static final String EVENT_HUD_ENABLED_FOR_DESKTOP = "desktop.enabled";
private static final String EVENT_HUD_DISABLED_FOR_DESKTOP = "desktop.disabled";

private static final Predicate<String> SCHEME_PREDICATE =
Pattern.compile("(?i)^https?://").asPredicate();

private ImageIcon icon;
private ExtensionQuickStartLaunch extLaunch;
private JXPanel contentPanel;
Expand Down Expand Up @@ -251,15 +257,28 @@ private JButton getLaunchButton() {
launchButton.setToolTipText(
Constant.messages.getString("quickstart.button.tooltip.launch"));

launchButton.addActionListener(
e -> {
getExtQuickStart().getQuickStartParam().addRecentUrl(getUrlValue());
extLaunch.launchBrowser(getSelectedBrowser(), getUrlValue());
});
launchButton.addActionListener(e -> launchBrowser(true));
}
return launchButton;
}

private void launchBrowser(boolean addToRecentList) {
String url = getUrlValue();
if (addToRecentList) {
getExtQuickStart().getQuickStartParam().addRecentUrl(url);
}
extLaunch.launchBrowser(getSelectedBrowser(), url);
}

/**
* Launches the browser with the URL specified in the panel, if any.
*
* <p>The URL is <strong>not</strong> added to the list of recent URLs.
*/
public void launchBrowser() {
launchBrowser(false);
}

public void postInit() {
// Plugable browsers (like JxBrowser) can be added after this add-ons
// options have been loaded
Expand Down Expand Up @@ -300,16 +319,16 @@ public void postInit() {
}
}

protected String getSelectedBrowser() {
private String getSelectedBrowser() {
return getBrowserComboBox().getSelectedItem().toString();
}

protected String getUrlValue() {
Object item = getUrlField().getSelectedItem();
if (item != null) {
return item.toString();
private String getUrlValue() {
String item = (String) getUrlField().getSelectedItem();
if (item != null && !SCHEME_PREDICATE.test(item)) {
item = HttpHeader.SCHEME_HTTP + item;
}
return null;
return item;
}

private JComboBox<String> getUrlField() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ quickstart.launch.start.option.label = Start Page:
quickstart.launch.start.pulldown.url = URL (specify below)
quickstart.launch.start.pulldown.zap = Default ZAP Page
quickstart.launch.start.pulldown.blank = Blank Page
quickstart.launch.start.url.access.error = Failed to access: {0}\nTry specifying the URL directly in the browser.
quickstart.launch.start.url.label = URL:
quickstart.launch.start.url.warn = You must specify a valid URL, including the initial 'http(s):'

Expand Down

0 comments on commit 10b9ba0

Please sign in to comment.