Play Framework:从coffeescript传递特殊字符时的URISyntaxException

时间:2014-06-05 05:53:38

标签: scala playframework anorm

目前,我正在使用Play Framework在项目中执行搜索框。我做了一个简单的代码,在输入""中传递search_box值。并从服务器查询搜索结果。

$(".search_box").on "input", (event) ->
LoadResult("/route_to_fetch_result/" + $(".search_box").val())
//LoadResult is a function that fetch json result returned by the search query.

当用户输入任何这些特殊字符时会出现问题(' [',']','#','% ',' /',' \'或' |')到搜索框。由于函数返回内部服务器错误。

即使我放了一个" try-catch"该错误仍然存​​在。阻止查询和控制器中没有触发的功能。

def getSearch_result(keyword: String) = Action { implicit request =>
    try{
        val searchList = Model.getSearch_result(keyword)
        Ok(Json.toJson(searchList))         
    }
    catch{ case _ => /*do return empty*/ }
}

模型中的查询功能

def getSearch_result(keyword: String): List[Search_result] = DB.withConnection{ implicit c =>
    try{
        SQL("""query LIKE {keyword}""").on('keyword -> ("%"+keyword+"%")).as(search_resultParser *)
    }
    catch{ case _ => /*do return empty*/ }
}

在浏览器上直接打开具有相同参数的路径返回了URISyntaxException。

URISyntaxException: Illegal character in path at index 22: /route_to_fetch_result/1[
java.net.URISyntaxException: Illegal character in path at index 22: /route_to_fetch_result/1[
 java.net.URI$Parser.fail(Unknown Source)
 java.net.URI$Parser.checkChars(Unknown Source)
 java.net.URI$Parser.parseHierarchical(Unknown Source)
 java.net.URI$Parser.parse(Unknown Source)
 java.net.URI.<init>(Unknown Source)
 play.core.server.netty.PlayDefaultUpstreamHandler$$anon$1.path(PlayDefaultUpstreamHandler.scala:98)
 play.docs.DocumentationHandler.maybeHandleDocRequest(DocumentationHandler.scala:58)
 play.docs.DocumentationHandler.maybeHandleDocRequest(DocumentationHandler.scala:28)
 play.core.ReloadableApplication.handleWebCommand(ApplicationProvider.scala:171)
 play.core.server.Server$$anonfun$getHandlerFor$1.apply(Server.scala:79)
 play.core.server.Server$$anonfun$getHandlerFor$1.apply(Server.scala:79)
 scala.util.control.Exception$Catch$$anonfun$either$1.apply(Exception.scala:124)
 scala.util.control.Exception$Catch$$anonfun$either$1.apply(Exception.scala:124)
 scala.util.control.Exception$Catch.apply(Exception.scala:102)
 scala.util.control.Exception$Catch.either(Exception.scala:124)
 play.core.server.Server$class.getHandlerFor(Server.scala:79)
 play.core.server.NettyServer.getHandlerFor(NettyServer.scala:35)
 play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$8.apply(PlayDefaultUpstreamHandler.scala:117)
 play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$8.apply(PlayDefaultUpstreamHandler.scala:117)
 scala.util.Either.fold(Either.scala:98)
 play.core.server.netty.PlayDefaultUpstreamHandler.messageReceived(PlayDefaultUpstreamHandler.scala:111)
 org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
 org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
 org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
 com.typesafe.netty.http.pipelining.HttpPipeliningHandler.messageReceived(HttpPipeliningHandler.java:62)
 org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88)
 org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
 org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
 org.jboss.netty.handler.codec.http.HttpContentDecoder.messageReceived(HttpContentDecoder.java:108)
 org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
 org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
 org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
 org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
 org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:459)
 org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:536)
 org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:435)
 org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
 org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
 org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
 org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
 org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
 org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)
 org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:109)
 org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:312)
 org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:90)
 org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
 org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
 org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
 java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 java.lang.Thread.run(Unknown Source)

目前,我阻止用户通过按键事件输入特殊字符。 有更好的解决方案吗?

1 个答案:

答案 0 :(得分:0)

特殊字符在URL中无效。你需要对它们进行编码:

encodeURIComponent($(".search_box").val())

如果您有这样的路线,也会发生同样的事情:

GET    /search/:keyword                  controllers.Page.search(keyword: String)

您手动访问:

/search/bad[url

结果:

java.net.URISyntaxException: Illegal character in path