Skip to content
Closed
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
23 changes: 19 additions & 4 deletions core/src/main/scala/org/apache/spark/TestUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import java.util.Arrays
import java.util.concurrent.{CountDownLatch, TimeUnit}
import java.util.jar.{JarEntry, JarOutputStream}
import javax.net.ssl._
import javax.servlet.http.HttpServletResponse
import javax.tools.{JavaFileObject, SimpleJavaFileObject, ToolProvider}

import scala.collection.JavaConverters._
Expand Down Expand Up @@ -186,12 +187,12 @@ private[spark] object TestUtils {
}

/**
* Returns the response code from an HTTP(S) URL.
* Returns the response code and url (if redirected) from an HTTP(S) URL.
*/
def httpResponseCode(
def httpResponseCodeAndURL(
url: URL,
method: String = "GET",
headers: Seq[(String, String)] = Nil): Int = {
headers: Seq[(String, String)] = Nil): (Int, Option[String]) = {
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
connection.setRequestMethod(method)
headers.foreach { case (k, v) => connection.setRequestProperty(k, v) }
Expand All @@ -210,16 +211,30 @@ private[spark] object TestUtils {
sslCtx.init(null, Array(trustManager), new SecureRandom())
connection.asInstanceOf[HttpsURLConnection].setSSLSocketFactory(sslCtx.getSocketFactory())
connection.asInstanceOf[HttpsURLConnection].setHostnameVerifier(verifier)
connection.setInstanceFollowRedirects(false)
}

try {
connection.connect()
connection.getResponseCode()
if (connection.getResponseCode == HttpServletResponse.SC_FOUND) {
(connection.getResponseCode, Option(connection.getHeaderField("Location")))
} else {
(connection.getResponseCode(), None)
}
} finally {
connection.disconnect()
}
}

/**
* Returns the response code from an HTTP(S) URL.
*/
def httpResponseCode(
url: URL,
method: String = "GET",
headers: Seq[(String, String)] = Nil): Int = {
httpResponseCodeAndURL(url, method, headers)._1
}
}


Expand Down
10 changes: 6 additions & 4 deletions core/src/main/scala/org/apache/spark/ui/JettyUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private[spark] object JettyUtils extends Logging {

// redirect the HTTP requests to HTTPS port
httpConnector.setName(REDIRECT_CONNECTOR_NAME)
collection.addHandler(createRedirectHttpsHandler(securePort, scheme))
collection.addHandler(createRedirectHttpsHandler(connector, scheme))
Copy link
Contributor

Choose a reason for hiding this comment

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

So is there a reason why you didn't make this a single line change to pass the correct port?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vanzin , AFAIK in this code, at that time when we create a redirect handler, the https ServerConnector hasn't yet started, so we couldn't get the real port from it, it will return -1 as we call getLocalPort.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, good point. I guess I was thinking about the code in master which starts the connector right away.

Some(connector)

case None =>
Expand Down Expand Up @@ -378,7 +378,9 @@ private[spark] object JettyUtils extends Logging {
server.getHandler().asInstanceOf[ContextHandlerCollection])
}

private def createRedirectHttpsHandler(securePort: Int, scheme: String): ContextHandler = {
private def createRedirectHttpsHandler(
httpsConnector: ServerConnector,
scheme: String): ContextHandler = {
val redirectHandler: ContextHandler = new ContextHandler
redirectHandler.setContextPath("/")
redirectHandler.setVirtualHosts(Array("@" + REDIRECT_CONNECTOR_NAME))
Expand All @@ -391,8 +393,8 @@ private[spark] object JettyUtils extends Logging {
if (baseRequest.isSecure) {
return
}
val httpsURI = createRedirectURI(scheme, baseRequest.getServerName, securePort,
baseRequest.getRequestURI, baseRequest.getQueryString)
val httpsURI = createRedirectURI(scheme, baseRequest.getServerName,
httpsConnector.getLocalPort, baseRequest.getRequestURI, baseRequest.getQueryString)
response.setContentLength(0)
response.encodeRedirectURL(httpsURI)
response.sendRedirect(httpsURI)
Expand Down
6 changes: 5 additions & 1 deletion core/src/test/scala/org/apache/spark/ui/UISuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,12 @@ class UISuite extends SparkFunSuite {
s"$scheme://localhost:$port/test1/root",
s"$scheme://localhost:$port/test2/root")
urls.foreach { url =>
val rc = TestUtils.httpResponseCode(new URL(url))
val (rc, redirectUrl) = TestUtils.httpResponseCodeAndURL(new URL(url))
assert(rc === expected, s"Unexpected status $rc for $url")
if (rc == HttpServletResponse.SC_FOUND) {
assert(
TestUtils.httpResponseCode(new URL(redirectUrl.get)) === HttpServletResponse.SC_OK)
}
}
}
} finally {
Expand Down