Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #26 from twitter/param_fix

CgiRequestHandler: Handle multiple params correctly
  • Loading branch information...
commit 1592d7b1e6f86042874a4d1ec48052567be09327 2 parents 7242855 + 3e87bf6
Robey Pointer authored
View
12 src/main/scala/com/twitter/ostrich/admin/AdminHttpService.scala
@@ -18,7 +18,7 @@ package com.twitter.ostrich
package admin
import java.io.{InputStream, OutputStream}
-import java.net.{InetSocketAddress, Socket}
+import java.net.{InetSocketAddress, Socket, URI}
import scala.io.Source
import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer}
import com.twitter.conversions.time._
@@ -118,8 +118,14 @@ class FolderResourceHandler(staticPath: String) extends CustomHttpHandler {
}
object CgiRequestHandler {
- def exchangeToParameters(exchange: HttpExchange): List[(String, String)] = {
- Option(exchange.getRequestURI.getQuery).toList.map { param =>
+ def exchangeToParameters(exchange: HttpExchange): List[(String, String)] =
+ Option(exchange.getRequestURI) match {
+ case Some(uri) => uriToParameters(uri)
+ case None => Nil
+ }
+
+ def uriToParameters(uri: URI): List[(String, String)] = {
+ Option(uri.getQuery).getOrElse("").split("&").toList.filter { _.contains("=") }.map { param =>
param.split("=", 2).toList match {
case k :: v :: Nil => (k, v)
case k :: Nil => (k, "")
View
18 src/test/scala/com/twitter/ostrich/admin/AdminHttpServiceSpec.scala
@@ -17,14 +17,15 @@
package com.twitter.ostrich
package admin
-import java.net.{Socket, SocketException, URL}
+import java.net.{Socket, SocketException, URI, URL}
import scala.io.Source
import com.twitter.json.Json
import com.twitter.logging.{Level, Logger}
import org.specs.Specification
+import org.specs.util.DataTables
import stats.Stats
-object AdminHttpServiceSpec extends Specification {
+object AdminHttpServiceSpec extends Specification with DataTables {
val PORT = 9996
val BACKLOG = 20
@@ -283,5 +284,18 @@ object AdminHttpServiceSpec extends Specification {
get("/stats.txt") must beMatching(" kangaroos: 1")
}
}
+
+ "parse parameters" in {
+ "uri" | "result" |>
+ "/p" ! Nil |
+ "/p?a=b" ! ("a", "b") :: Nil |
+ "/p?a=b&c=d" ! ("a", "b") :: ("c", "d") :: Nil |
+ "/p?" ! Nil |
+ "/p?invalid" ! Nil |
+ "/p?a=" ! ("a", "") :: Nil |
+ "/p?=b" ! ("", "b") :: Nil | { (uriStr, result) =>
+ CgiRequestHandler.uriToParameters(new URI(uriStr)) mustEqual result
+ }
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.