public
Created

  • Download Gist
project/build.properties
INI
1 2 3 4 5 6 7 8
#Project properties
#Mon Mar 28 17:17:14 CEST 2011
project.organization=vv
project.name=ParserTest
sbt.version=0.7.5.RC1
project.version=1.0
build.scala.versions=2.8.1
project.initialize=false
src/main/scala/Test.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
trait SsdpType
case object Notify extends SsdpType
case object Search extends SsdpType
case object OK extends SsdpType
 
import scala.util.parsing.combinator._
 
case class SsdpMessage(header: SsdpType, values: List[(String, String)])
 
object SsdpParser extends RegexParsers {
 
 
def fulldoc: Parser[SsdpMessage] = header ~ nameValuePairs <~ "\r\n" ^^ {
case header ~ values => SsdpMessage(header, values)
}
 
 
 
def nameValuePairs:Parser[List[(String, String)]] = repsep(nameValuePair, "\r\n")
 
 
 
def nameValuePair:Parser[(String, String)] = (
//name
("""[-a-zA-Z0-9.]*""".r <~ ":")
//value
~ """[a-zA-Z0-9\:/,_; \[\].\-\"\'\?=]*""".r ) ^^ {
case name ~ value => (name, value)
}
 
 
 
def header: Parser[SsdpType] = okLine//notifyLine | okLine | mSearch
def notifyLine:Parser[SsdpType] = "NOTIFY * HTTP/1.1\r\n" ^^ (x =>Notify)
def mSearch:Parser[SsdpType] = "M-SEARCH * HTTP/1.1\r\n" ^^ (x=>Search)
def okLine:Parser[SsdpType] = "HTTP/1.1 200 OK\r\n" ^^ (x=>OK)
 
override def skipWhitespace = false
 
 
val input = """HTTP/1.1 200 OK
Cache-Control: max-age=300
Date: Mon, 28 Mar 2011 06:37:31 GMT
Ext:
Location: http://192.168.1.1:1780/InternetGatewayDevice.xml
Server: POSIX UPnP/1.0 DD-WRT Linux/V24
ST: urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1
USN: uuid:01A0DDA7-7404-815B-63C4-539B920D5E56::urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1
"""
def main(args: Array[String]) {
println(phrase(fulldoc)(new scala.util.parsing.input.CharArrayReader(input.toCharArray)))
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.