Merge: Added contributing guidelines and link from readme
[nit.git] / lib / nitcorn / http_response.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Frederic Sevillano
4 # Copyright 2013 Jean-Philippe Caissy <jpcaissy@piji.ca>
5 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18
19 # Provides the `HttpResponse` class and `http_status_codes`
20 module http_response
21
22 # A response to send over HTTP
23 class HttpResponse
24
25 # HTTP protocol version
26 var http_version = "HTTP/1.0" is writable
27
28 # Status code of this response (200, 404, etc.)
29 var status_code: Int is writable
30
31 # Return the message associated to `status_code`
32 fun status_message: nullable String do return http_status_codes[status_code]
33
34 # Headers of this response as a `Map`
35 var header = new HashMap[String, String]
36
37 # Body of this response
38 var body = "" is writable
39
40 # Files appended after `body`
41 var files = new Array[String]
42
43 # Finalize this response before sending it over HTTP
44 fun finalize
45 do
46 # Set the content length if not already set
47 if not header.keys.has("Content-Length") then
48 # Size of the body
49 var len = body.bytelen
50
51 # Size of included files
52 for path in files do
53 # TODO handle these error cases elsewhere, an error here will result in an invalid response
54 if not path.file_exists then
55 print_error "File does not exists at '{path}'"
56 continue
57 end
58
59 var stat = path.file_stat
60 if stat == null then
61 print_error "Failed to stat file at '{path}'"
62 continue
63 end
64
65 len += stat.size
66 end
67
68 # Set header
69 header["Content-Length"] = len.to_s
70 end
71
72 # Set server ID
73 if not header.keys.has("Server") then header["Server"] = "nitcorn"
74 end
75
76 # Get this reponse as a string according to HTTP protocol
77 redef fun to_s
78 do
79 finalize
80
81 var buf = new FlatBuffer
82 buf.append("{http_version} {status_code} {status_message or else ""}\r\n")
83 for key, value in header do
84 buf.append("{key}: {value}\r\n")
85 end
86 buf.append("\r\n{body}")
87 return buf.to_s
88 end
89 end
90
91 # Helper class to associate HTTP status code to their message
92 #
93 # You probably want the default instance available as the top-level method
94 # `http_status_codes`.
95 class HttpStatusCodes
96
97 # All know code and their message
98 var codes = new HashMap[Int, String]
99
100 # Init the status `codes` list.
101 protected init is old_style_init do insert_status_codes
102
103 # Get the message associated to the status `code`, return `null` in unknown
104 fun [](code: Int): nullable String
105 do
106 if codes.keys.has(code) then
107 return codes[code]
108 else return null
109 end
110
111 private fun insert_status_codes
112 do
113 codes[100] = "Continue"
114 codes[101] = "Switching Protocols"
115 codes[200] = "OK"
116 codes[201] = "Created"
117 codes[202] = "Accepted"
118 codes[203] = "Non-Authoritative Information"
119 codes[204] = "No Content"
120 codes[205] = "Reset Content"
121 codes[206] = "Partial Content"
122 codes[300] = "Multiple Choices"
123 codes[301] = "Moved Permanently"
124 codes[302] = "Found"
125 codes[303] = "See Other"
126 codes[304] = "Not Modified"
127 codes[305] = "Use Proxy"
128 codes[307] = "Temporary Redirect"
129 codes[400] = "Bad Request"
130 codes[401] = "Unauthorized"
131 codes[402] = "Payment Requred"
132 codes[403] = "Forbidden"
133 codes[404] = "Not Found"
134 codes[405] = "Method Not Allowed"
135 codes[406] = "Not Acceptable"
136 codes[407] = "Proxy Authentication Required"
137 codes[408] = "Request Timeout"
138 codes[409] = "Conflict"
139 codes[410] = "Gone"
140 codes[411] = "Length Required"
141 codes[412] = "Precondition Failed"
142 codes[413] = "Request Entity Too Large"
143 codes[414] = "Request-URI Too Long"
144 codes[415] = "Unsupported Media Type"
145 codes[416] = "Requested Range Not Satisfiable"
146 codes[417] = "Expectation Failed"
147 codes[500] = "Internal Server Error"
148 codes[501] = "Not Implemented"
149 codes[502] = "Bad Gateway"
150 codes[503] = "Service Unavailable"
151 codes[504] = "Gateway Timeout"
152 codes[505] = "HTTP Version Not Supported"
153 end
154 end
155
156 # Get the default instance of `HttpStatusCodes`
157 fun http_status_codes: HttpStatusCodes do return once new HttpStatusCodes