|
@@ -1,7 +1,53 @@
|
1
|
|
-io.stdin:read(1)
|
|
1
|
+http = {
|
|
2
|
+ ERRORS = {
|
|
3
|
+ [400] = "Bad Request",
|
|
4
|
+ [500] = "Server Error",
|
|
5
|
+ }
|
|
6
|
+}
|
2
|
7
|
|
3
|
|
-print('HTTP/1.1 200 OK
|
|
8
|
+NL = '
|
|
9
|
+'
|
|
10
|
+CR = chr(13)
|
|
11
|
+
|
|
12
|
+func get_request()
|
|
13
|
+ result = {}
|
|
14
|
+
|
|
15
|
+ httpline = io.stdin:read(io.LINE)
|
|
16
|
+ httpline = httpline:split(NL)[0]
|
|
17
|
+ httpline = httpline:split(CR)[0]
|
|
18
|
+ parts = httpline:split(" ")
|
|
19
|
+
|
|
20
|
+ if (3 != (#parts)) || (parts[2] != "HTTP/1.1") then
|
|
21
|
+ error(400)
|
|
22
|
+ end
|
|
23
|
+
|
|
24
|
+ result.method = parts[0]
|
|
25
|
+ result.path = parts[1]
|
|
26
|
+ result.protocol = parts[2]
|
|
27
|
+
|
|
28
|
+ return result
|
|
29
|
+end
|
|
30
|
+
|
|
31
|
+out = try(get_request)
|
|
32
|
+success = out[0]
|
|
33
|
+value = out[1]
|
|
34
|
+
|
|
35
|
+if success then
|
|
36
|
+ print('HTTP/1.1 200 OK
|
|
37
|
+Content-type: text/plain
|
|
38
|
+
|
|
39
|
+Hello from Sol! You sent in these values:', value)
|
|
40
|
+else
|
|
41
|
+ if type(value) == "int" then
|
|
42
|
+ print('HTTP/1.1 ' + tostring(value) + ' ' + (http.ERRORS[value]) + '
|
|
43
|
+Content-type: text/plain
|
|
44
|
+
|
|
45
|
+Error ' + tostring(value) + ': ' + (http.ERRORS[value]))
|
|
46
|
+ else
|
|
47
|
+ print('HTTP/1.1 500 Server Error
|
4
|
48
|
Content-type: text/plain
|
5
|
49
|
|
6
|
|
-Hello from Sol!')
|
|
50
|
+Internal error: ' + tostring(value))
|
|
51
|
+ end
|
|
52
|
+end
|
7
|
53
|
|