-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
executable file
·199 lines (160 loc) · 7.13 KB
/
main.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
""" DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2016 Arti Zirk <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
"""
from pprint import pprint
import json
import random
import base64
from html import escape as html_escape
from collections import deque
from urllib.parse import parse_qs
max_nr_messages = 25
html = """<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Message Board</title>
<meta name="description" content="A simple message board written in python">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<style>
body {{
margin: 10px auto;
max-width: 650px;
line-height: 1.6;
font-size: 18px;
color: #444;
padding: 0 10px
}}
h1,h2,h3 {{
line-height: 1.2
}}
hr {{
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}}
.error {{
background: #f55;
color: #000;
}}
</style>
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<h1>Message Board</h1>
<p>Last <span id=msg_count>{msg_count}</span> messages left here</p>
<span id=content>
{content}
</span>
<form action="/submit" method="post">
Add a message: 
<input type="text" name="message" value="" required autofocus>
<input type="submit" value="Submit">
</form>
<hr>
<small><a href="https://github.com/artizirk/message-board" style="color:black;">Source code</a></small>, <small>Good luck!</small>
<script src="/app.js"></script>
</body>
</html>
"""
favicon = b'AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//wAA/4cAAP+7AAD/uwAA/7sAAP+HAAD/uwAAvrsAAL67AAC+hwAAvv8AALb/AACi/wAAiP8AAJz/AAD//wAA'
appjs = """
let interval_id = window.setInterval(function(){
var el_msg_count = document.getElementById("msg_count");
var el_content = document.getElementById("content");
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function(){
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
el_content.innerHTML = httpRequest.responseText;
el_msg_count.innerHTML = el_content.childNodes[0].childElementCount;
} else {
window.clearInterval(interval_id);
el_content.innerHTML += '<p class=error>There was a problem with the chat update, reload the page to try again.</p>';
}
}
};
httpRequest.open('GET', '/htmlfragment?' + (new Date()).getTime());
httpRequest.send();
}, 1000);
"""
default = [
"divide-by-zero error",
"not enough memory, go get system upgrade",
"need to wrap system in aluminum foil to fix problem",
"Typo in the code"
]
default = ["BOFH excuse #{}: {}".format(i, x) for i, x in enumerate(default)]
messages = deque(default, maxlen=max_nr_messages)
def application(env, start_response):
if env["REQUEST_METHOD"] == "POST":
try:
l = int(env.get('CONTENT_LENGTH', 0))
except KeyError:
start_response('411 Length Required', [('Content-Type', 'application/json')])
return [json.dumps({"error":{"code":411, "message": "Content-Length header missing"}}).encode()]
if l > 150:
start_response('413 Payload Too Large', [('Content-Type', 'application/json')])
return [json.dumps({"error":{"code":413, "message": "Payload Too Large"}}).encode()]
message = env['wsgi.input'].read(l).decode()
if not message:
start_response('400 Bad Request', [('Content-Type', 'application/json')])
return [json.dumps({"error":{"code":400, "message": "message not provided"}}).encode()]
try:
message = json.loads(message)["message"]
except json.decoder.JSONDecodeError:
pass
except KeyError:
start_response('400 Bad Request', [('Content-Type', 'application/json')])
return [json.dumps({"error":{"code":400, "message": "message key missing from json payload data"}}).encode()]
if message.startswith("message="):
message = parse_qs(message)["message"][0]
messages.appendleft(html_escape(message.strip()))
#start_response("200 OK", [('Content-Type', 'application/json')])
if env["PATH_INFO"] == "/submit":
start_response("303 See Other", [('Content-Type', 'application/json'),
('Location', '/')])
return [b"OK"]
if env["PATH_INFO"] == "/clear":
messages.clear()
messages.extendleft(default)
start_response("303 See Other", [('Content-Type', 'application/json'),
('Location', '/')])
return [b"OK"]
if env["PATH_INFO"] == "/favicon.ico":
start_response('200 OK', [('Content-Type', 'image/x-icon')])
return [base64.b64decode(favicon)]
if env["PATH_INFO"] == "/app.js":
start_response('200 OK', [('Content-Type', 'text/javascript')])
return[appjs.encode()]
if env["PATH_INFO"] == "/json" or 'HTTP_USER_AGENT' in env and env['HTTP_USER_AGENT'].startswith(('python-requests', 'HTTPie', 'curl', 'Wget')):
start_response("200 OK", [('Content-Type', 'application/json')])
return [json.dumps(list(messages), indent=4).encode()]
start_response('200 OK', [('Content-Type', 'text/html')])
content = []
for message in list(messages):
content.append(" <li>{}</li>\n".format(message))
content = "<ol>\n{} </ol>".format("".join(content))
if env["PATH_INFO"] == "/htmlfragment":
return [content.encode()]
return [html.format(msg_count=len(messages), content=content).encode()]
if __name__ == "__main__":
from wsgiref.simple_server import make_server
httpd = make_server('0.0.0.0', 8080, application)
print("Serving on http://0.0.0.0:8080/")
httpd.serve_forever()