Skip to content

Commit

Permalink
Change inferred media type of scala.xml.Elem to application/xml.
Browse files Browse the repository at this point in the history
This makes XML literals as HTML a Bad Idea, so they are ported to
Twirl in the example.

Related to http4s/http4s#153.
  • Loading branch information
rossabaker committed Jan 11, 2015
1 parent 16d934f commit 49412e7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
46 changes: 6 additions & 40 deletions examples/src/main/scala/com/example/http4s/ExampleService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,12 @@ object ExampleService {
def service1(implicit executionContext: ExecutionContext) = HttpService {

case req @ GET -> Root =>
// Supports Play Framework template -- see src/main/twirl.
Ok(html.index())

case GET -> Root / "ping" =>
// EntityEncoder allows for easy conversion of types to a response body
Ok(
<html>
<body>
<h1>Welcome to http4s.</h1>

<p>Some examples:</p>

<ul>
<li><a href="/http4s/ping">Ping route</a></li>
<li><a href="/http4s/future">A asynchronous result</a></li>
<li><a href="/http4s/streaming">A streaming result</a></li>
<li><a href="/http4s/ip">Get your IP address</a></li>
<li><a href="/http4s/redirect">A redirect url</a></li>
<li><a href="/http4s/content-change">A HTML result written as a String</a></li>

<li><a href="/http4s/echo">Echo some form encoded data</a></li>
<li><a href="/http4s/echo2">Echo some form encoded data minus a few chars</a></li>
<li><a href="/http4s/sum">Calculate the sum of the submitted numbers</a></li>
<li><a href="/http4s/short-sum">Try to calculate a sum, but the body will be to large</a></li>

<li><a href="/http4s/form-encoded">A submission form</a></li>
<li><a href="/http4s/push">Server push</a></li>
</ul>
</body>
</html>
)

case GET -> Root / "ping" => Ok("pong")
Ok("pong")

case GET -> Root / "future" =>
// EntityEncoder allows rendering asynchronous results as well
Expand Down Expand Up @@ -92,7 +69,6 @@ object ExampleService {
.withHeaders(`Content-Type`(`text/plain`), `Transfer-Encoding`(TransferCoding.chunked))

case req @ GET -> Root / "echo" =>
// submissionForm is a Play Framework template -- see src/main/twirl.
Ok(html.submissionForm("echo data"))

case req @ POST -> Root / "echo2" =>
Expand Down Expand Up @@ -123,17 +99,7 @@ object ExampleService {
///////////////////////////////////////////////////////////////
//////////////// Form encoding example ////////////////////////
case req @ GET -> Root / "form-encoded" =>
val html =
<html><body>
<p>Submit something.</p>
<form name="input" method="post">
<p>First name: <input type="text" name="firstname"/></p>
<p>Last name: <input type="text" name="lastname"/></p>
<p><input type="submit" value="Submit"/></p>
</form>
</body></html>

Ok(html)
Ok(html.formEncoded())

case req @ POST -> Root / "form-encoded" =>
// EntityDecoders return a Task[A] which is easy to sequence
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html><body>
<p>Submit something.</p>
<form name="input" method="post">
<p>First name: <input type="text" name="firstname"/></p>
<p>Last name: <input type="text" name="lastname"/></p>
<p><input type="submit" value="Submit"/></p>
</form>
</body></html>
24 changes: 24 additions & 0 deletions examples/src/main/twirl/com/example/http4s/index.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<body>
<h1>Welcome to http4s.</h1>

<p>Some examples:</p>

<ul>
<li><a href="ping">Ping route</a></li>
<li><a href="future">A asynchronous result</a></li>
<li><a href="streaming">A streaming result</a></li>
<li><a href="ip">Get your IP address</a></li>
<li><a href="redirect">A redirect url</a></li>
<li><a href="content-change">A HTML result written as a String</a></li>

<li><a href="echo">Echo some form encoded data</a></li>
<li><a href="echo2">Echo some form encoded data minus a few chars</a></li>
<li><a href="sum">Calculate the sum of the submitted numbers</a></li>
<li><a href="short-sum">Try to calculate a sum, but the body will be to large</a></li>

<li><a href="form-encoded">A submission form</a></li>
<li><a href="push">Server push</a></li>
</ul>
</body>
</html>

0 comments on commit 49412e7

Please sign in to comment.