-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathweb_server.py
220 lines (200 loc) · 8.67 KB
/
web_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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A local HTTP server for Android OTA package generation.
Based on OTA_from_target_files.py
Usage::
python ./web_server.py [<port>]
API::
GET /check : check the status of all jobs
GET /check/<id> : check the status of the job with <id>
GET /file : fetch the target file list
GET /file/<path> : Add build file(s) in <path>, and return the target file list
GET /download/<id> : download the ota package with <id>
POST /run/<id> : submit a job with <id>,
arguments set in a json uploaded together
POST /file/<filename> : upload a target file
[TODO] POST /cancel/<id> : cancel a job with <id>
TODO:
- Avoid unintentionally path leakage
- Avoid overwriting build when uploading build with same file name
Other GET request will be redirected to the static request under 'dist' directory
"""
from http.server import BaseHTTPRequestHandler, SimpleHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
from threading import Lock
from ota_interface import ProcessesManagement
from target_lib import TargetLib
import logging
import json
import cgi
import os
import stat
import zipfile
LOCAL_ADDRESS = '0.0.0.0'
class CORSSimpleHTTPHandler(SimpleHTTPRequestHandler):
def end_headers(self):
try:
origin_address, _ = cgi.parse_header(self.headers['Origin'])
self.send_header('Access-Control-Allow-Credentials', 'true')
self.send_header('Access-Control-Allow-Origin', origin_address)
except TypeError:
pass
super().end_headers()
class RequestHandler(CORSSimpleHTTPHandler):
def _set_response(self, code=200, type='text/html'):
self.send_response(code)
self.send_header('Content-type', type)
self.end_headers()
def do_OPTIONS(self):
self.send_response(200)
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
self.send_header("Access-Control-Allow-Headers", "X-Requested-With")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
def do_GET(self):
if self.path == '/check' or self.path == '/check/':
statuses = jobs.get_status()
self._set_response(type='application/json')
self.wfile.write(
json.dumps([status.to_dict_basic()
for status in statuses]).encode()
)
elif self.path.startswith('/check/'):
id = self.path[7:]
status = jobs.get_status_by_ID(id=id)
self._set_response(type='application/json')
self.wfile.write(
json.dumps(status.to_dict_detail(target_lib)).encode()
)
elif self.path.startswith('/file') or self.path.startswith("/reconstruct_build_list"):
if self.path == '/file' or self.path == '/file/':
file_list = target_lib.get_builds()
else:
file_list = target_lib.new_build_from_dir()
builds_info = [build.to_dict() for build in file_list]
self._set_response(type='application/json')
self.wfile.write(
json.dumps(builds_info).encode()
)
logging.debug(
"GET request:\nPath:%s\nHeaders:\n%s\nBody:\n%s\n",
str(self.path), str(self.headers), file_list
)
return
elif self.path.startswith('/download'):
self.path = self.path[10:]
return CORSSimpleHTTPHandler.do_GET(self)
else:
if not os.path.exists('dist' + self.path):
logging.info('redirect to dist')
self.path = '/dist/'
else:
self.path = '/dist' + self.path
return CORSSimpleHTTPHandler.do_GET(self)
def do_POST(self):
if self.path.startswith('/run'):
content_type, _ = cgi.parse_header(self.headers['content-type'])
if content_type != 'application/json':
self.send_response(400)
self.end_headers()
return
content_length = int(self.headers['Content-Length'])
post_data = json.loads(self.rfile.read(content_length))
try:
jobs.ota_generate(post_data, id=str(self.path[5:]))
self._set_response(code=200)
self.send_header("Content-Type", 'application/json')
self.wfile.write(json.dumps(
{"success": True, "msg": "OTA Generator started running"}).encode())
except Exception as e:
logging.warning(
"Failed to run ota_from_target_files %s", e.__traceback__)
self.send_error(
400, "Failed to run ota_from_target_files", str(e))
logging.debug(
"POST request:\nPath:%s\nHeaders:\n%s\nBody:\n%s\n",
str(self.path), str(self.headers),
json.dumps(post_data)
)
elif self.path.startswith('/file'):
file_name = os.path.join('target', self.path[6:])
file_length = int(self.headers['Content-Length'])
with open(file_name, 'wb') as output_file:
# Unwrap the uploaded file first (due to the usage of FormData)
# The wrapper has a boundary line at the top and bottom
# and some file information in the beginning
# There are a file content line, a file name line, and an empty line
# The boundary line in the bottom is 4 bytes longer than the top one
# Please refer to the following links for more details:
# https://stackoverflow.com/questions/8659808/how-does-http-file-upload-work
# https://datatracker.ietf.org/doc/html/rfc1867
upper_boundary = self.rfile.readline()
file_length -= len(upper_boundary) * 2 + 4
file_length -= len(self.rfile.readline())
file_length -= len(self.rfile.readline())
file_length -= len(self.rfile.readline())
BUFFER_SIZE = 1024*1024
for offset in range(0, file_length, BUFFER_SIZE):
chunk = self.rfile.read(
min(file_length-offset, BUFFER_SIZE))
output_file.write(chunk)
target_lib.new_build(self.path[6:], file_name)
self._set_response(code=201)
self.wfile.write(
"File received, saved into {}".format(
file_name).encode('utf-8')
)
else:
self.send_error(400)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
def run_server(SeverClass=ThreadedHTTPServer, HandlerClass=RequestHandler, port=8000):
server_address = (LOCAL_ADDRESS, port)
server_instance = SeverClass(server_address, HandlerClass)
try:
logging.info(
'Server is on, address:\n %s',
'http://' + str(server_address[0]) + ':' + str(port))
server_instance.serve_forever()
except KeyboardInterrupt:
pass
server_instance.server_close()
logging.info('Server has been turned off.')
if __name__ == '__main__':
from sys import argv
print(argv)
logging.basicConfig(level=logging.INFO)
EXTRACT_DIR = None
if os.path.exists("otatools.zip"):
logging.info("Found otatools.zip, extracting...")
EXTRACT_DIR = "/tmp/otatools-" + str(os.getpid())
os.makedirs(EXTRACT_DIR, exist_ok=True)
with zipfile.ZipFile("otatools.zip", "r") as zfp:
zfp.extractall(EXTRACT_DIR)
# mark all binaries executable by owner
bin_dir = os.path.join(EXTRACT_DIR, "bin")
for filename in os.listdir(bin_dir):
os.chmod(os.path.join(bin_dir, filename), stat.S_IRWXU)
logging.info("Extracted otatools to {}".format(EXTRACT_DIR))
if not os.path.isdir('target'):
os.mkdir('target', 755)
if not os.path.isdir('output'):
os.mkdir('output', 755)
target_lib = TargetLib()
jobs = ProcessesManagement(otatools_dir=EXTRACT_DIR)
if len(argv) == 2:
run_server(port=int(argv[1]))
else:
run_server()