-
Notifications
You must be signed in to change notification settings - Fork 859
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
Adding the provision of redirecting HTTP requests to HTTPS #675
base: tuning_20190221
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -14,17 +14,21 @@ | |
* the License. | ||
*/ | ||
|
||
import com.google.common.base.Strings; | ||
import com.linkedin.drelephant.DrElephant; | ||
import com.linkedin.drelephant.ElephantContext; | ||
import com.sun.security.sasl.util.AbstractSaslImpl; | ||
|
||
import play.Application; | ||
import play.GlobalSettings; | ||
import play.Logger; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.util.logging.Level; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import play.Application; | ||
import play.GlobalSettings; | ||
import play.Logger; | ||
import play.api.mvc.Handler; | ||
import play.mvc.Http; | ||
|
||
|
||
/** | ||
|
@@ -34,11 +38,24 @@ public class Global extends GlobalSettings { | |
|
||
DrElephant _drElephant; | ||
|
||
private final String INVALID_PORT = "-1"; | ||
private final String HTTPS_PROTOCOL = "https://"; | ||
private final String HTTP_PORT_CONFIG_NAME = "http_port"; | ||
private final String HTTPS_PORT_CONFIG_NAME = "https_port"; | ||
|
||
private String HTTP_PORT; | ||
private String HTTPS_PORT; | ||
|
||
public void onStart(Application app) { | ||
Logger.info("Starting Application..."); | ||
|
||
fixJavaKerberos(); | ||
|
||
HTTP_PORT = ElephantContext.instance().getElephnatConf() | ||
.getProperty(HTTP_PORT_CONFIG_NAME); | ||
HTTPS_PORT = ElephantContext.instance().getElephnatConf() | ||
.getProperty(HTTPS_PORT_CONFIG_NAME); | ||
|
||
try { | ||
_drElephant = new DrElephant(); | ||
_drElephant.start(); | ||
|
@@ -80,11 +97,46 @@ private static void fixJavaKerberos() { | |
|
||
} | ||
|
||
/** | ||
* This method is overridden to re-direct all the requests to HTTP endpoint of Dr.Elephant to | ||
* the respective HTTPS enabled endpoint(If such endpoints exists) | ||
* @param requestHeader Request Header | ||
* @return The Handler to appropriately handle the given request | ||
*/ | ||
@Override | ||
public Handler onRouteRequest(Http.RequestHeader requestHeader) { | ||
if (!Strings.isNullOrEmpty(HTTP_PORT) && !Strings.isNullOrEmpty(HTTPS_PORT)) { | ||
String port = INVALID_PORT; | ||
if (!Strings.isNullOrEmpty(requestHeader.host())) { | ||
port = getPortFromHostAddress(requestHeader.host()); | ||
} | ||
if (!port.equals(INVALID_PORT) && port.equals(HTTP_PORT)) { | ||
return controllers.Default.redirect(HTTPS_PROTOCOL + requestHeader.host().replace(port, HTTPS_PORT) | ||
+ requestHeader.uri()); | ||
} | ||
} | ||
return super.onRouteRequest(requestHeader); | ||
} | ||
|
||
static void setFinalStatic(Field field, Object newValue) throws Exception { | ||
field.setAccessible(true); | ||
Field modifiersField = Field.class.getDeclaredField("modifiers"); | ||
modifiersField.setAccessible(true); | ||
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); | ||
field.set(null, newValue); | ||
} | ||
|
||
/** | ||
* | ||
* @param hostAddress The hostAddress string which will be of format "elephant.abc.com:8080" | ||
* @return Return the port of the of the hostAddress if exists else return INVALID_PORT | ||
*/ | ||
private String getPortFromHostAddress(String hostAddress) { | ||
String patterForPort = "(.+):([\\d]{1,5})"; | ||
Matcher matcher = Pattern.compile(patterForPort).matcher(hostAddress); | ||
if (matcher.find()) { | ||
return matcher.group(2); | ||
} | ||
return INVALID_PORT; | ||
} | ||
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. You might want to consider leveraging Guava's 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. Big thanks Logan, I was looking for such a utility method and couldn't find it so implemented a crude one. Thanks for the review and for reporting this issue. |
||
} |
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.
s/patter/pattern/
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.
After using Guava's HostAndPort.fromString() there was no need of the method so removed the respective method.