dd50e0b1e80e2c6b1865fd96cbbd44b4b5d6e6e6
[nit.git] / lib / nitcorn / http_errors.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Offers `ErrorTemplate` to display error pages
18 module http_errors
19
20 import template
21
22 import http_response
23
24 # A basic error page for the HTTP error `code`
25 class ErrorTemplate
26 super Template
27
28 # HTTP error code
29 var code: Int is writable
30
31 # Header on this page
32 var header: nullable Streamable = null is writable
33
34 # Body to show with this page
35 var body: nullable Streamable = null is writable
36
37 redef fun rendering
38 do
39 var code_message = http_status_codes[code]
40 var message
41 if code_message != null then
42 message = "Error {code}: {code_message}"
43 else message = "Error {code}"
44
45 add """
46 <!DOCTYPE html>
47 <head>
48 <meta charset="utf-8">
49 <meta http-equiv="X-UA-Compatible" content="IE=edge">
50 <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
51 <title>"""
52 add message
53 add """
54 </title>
55 </head>
56 <body>"""
57
58 var header = header
59 if header != null then add header
60
61 add """
62 <div class="container">
63 <h1>"""
64 add message
65 add "</h1>"
66
67 var body = body
68 if body != null then add body
69
70 add """
71 </div>
72 </body>
73 </html>"""
74 end
75
76 redef fun to_s do return write_to_string
77 end