-
Notifications
You must be signed in to change notification settings - Fork 735
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
Improve checkApiUrlValidity() method #251
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.text.ParseException; | ||
|
@@ -482,7 +483,34 @@ void check(String apiUrl) throws IOException { | |
* Otherwise this method throws {@link IOException} to indicate the problem. | ||
*/ | ||
public void checkApiUrlValidity() throws IOException { | ||
retrieve().to("/", GHApiInfo.class).check(apiUrl); | ||
try { | ||
retrieve().to("/", GHApiInfo.class).check(apiUrl); | ||
} catch (IOException ioe) { | ||
if (isPrivateModeEnabled()) { | ||
throw new IOException("GitHub Enterprise server (" + apiUrl + ") with private mode enabled"); | ||
} | ||
throw ioe; | ||
} | ||
} | ||
|
||
/** | ||
* Ensures if a GitHub Enterprise server is configured in private mode. | ||
* | ||
* @return {@code true} if private mode is enabled. If it tries to use this method with GitHub, returns {@code | ||
* false}. | ||
*/ | ||
private boolean isPrivateModeEnabled() { | ||
try { | ||
HttpURLConnection connect = getConnector().connect(getApiURL("/")); | ||
if (connect.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED | ||
&& connect.getHeaderField("Server") != null | ||
&& connect.getHeaderField("Server").equals("GitHub.com")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This header is suspicious for GitHub Enterprise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @oleg-nenashev No, they use this header for both. github.com
My GitHub Enterprise server
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @recena sorry but I am not comfortable with this "Server" thing as reverse proxies could remove this header. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cyrille-leclerc The goal of this PR was to provide a method to verify if a URL represents a GitHub endpoint, only that. A |
||
return true; | ||
} | ||
return false; | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,11 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws IOException { | |
@Test | ||
public void testGitHubIsApiUrlValid() throws IOException { | ||
GitHub github = GitHub.connectAnonymously(); | ||
github.checkApiUrlValidity(); | ||
//GitHub github = GitHub.connectToEnterpriseAnonymously("https://github.mycompany.com/api/v3/"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cyrille-leclerc You can use this test to play with the method and different scenarios. |
||
try { | ||
github.checkApiUrlValidity(); | ||
} catch (IOException ioe) { | ||
assertTrue(ioe.getMessage().contains("private mode enabled")); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced this check is enough. There may be other errors (e.g. network failure), but this check suppresses all other issues. Maybe you should throw a new exception with the cause and a message, which says that it maybe related to the private mode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same feeling here, I would prefer a logic based on the HTTP error code (401 for private mode, http proxy auth...) and on the Network exception (connection refused, ssl handshake rejected for negotiation failure...). I am investigating to make proposals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method basically is the same but now we have included a new case.
I tested with:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A Network exception throws a
IOException
and turns out an negative validation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev
Why? How? I'm only handling a special case. If
private mode
is not configured and there is an problem, an exception is thrown.The exception is thrown with a customized message (IMO, very clear). The doubt can be about the way used to verify the
privade mode
.