-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
executable file
·139 lines (109 loc) · 4.02 KB
/
server.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
#!/usr/bin/env python
# Copyright (c) 2014 Martin Abente Lahaye. - tch@sugarlabs.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import json
import logging
import SimpleHTTPServer
import SocketServer
from settings import Settings
def authorize(method):
""" just a basic method for authorization """
def verify(handler, *args, **kwargs):
if 'x-api-key' not in handler.headers or \
handler.headers['x-api-key'] != Settings.API_KEY:
handler.send_response(401, "unauthorized")
handler.end_headers()
return None
return method(handler, *args, **kwargs)
return verify
def check(method):
""" put things under control """
def verify(handler, *args, **kwargs):
project_id = get_project_id(handler)
if project_id and project_id.find('/') >= 0:
handler.send_response(403, 'forbidden')
handler.end_headers()
return None
if project_id and check_if_missing(method, handler):
handler.send_response(404, 'not found')
handler.end_headers()
return None
return method(handler, *args, **kwargs)
return verify
def get_project_id(handler):
return handler.path.replace('/', '')
def get_project_path(handler):
project_id = get_project_id(handler)
return os.path.join(Settings.PROJECTS, project_id)
def get_all_projects():
filenames = []
for filename in os.listdir(Settings.PROJECTS):
filenames.append(filename)
return json.dumps(filenames)
def get_one_project(handler):
path = get_project_path(handler)
with open(path, 'r') as file:
return file.read()
def check_if_missing(method, handler):
if method.__name__ == 'do_GET' and \
not os.path.isfile(get_project_path(handler)):
return True
return False
def check_projects_path():
"""Create the project folders if its didn't exists"""
if not os.path.exists(Settings.PROJECTS):
os.mkdir(Settings.PROJECTS)
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_OPTIONS(self):
logging.info(self.headers)
self.send_response(200, "ok")
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods',
'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers',
'x-project-id, x-api-key')
@authorize
@check
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
if get_project_id(self):
body = get_one_project(self)
else:
body = get_all_projects()
self.wfile.write(body)
@authorize
@check
def do_POST(self):
self.send_response(200)
self.end_headers()
content_len = int(self.headers.getheader('content-length', 0))
content = self.rfile.read(content_len)
path = get_project_path(self)
with open(path, 'w') as file:
file.write(content)
if __name__ == '__main__':
check_projects_path()
httpd = SocketServer.TCPServer((Settings.ADDRESS,
Settings.PORT),
ServerHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()