-
Notifications
You must be signed in to change notification settings - Fork 1
/
sand_server.py
161 lines (135 loc) · 5.19 KB
/
sand_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
#!/usr/bin/env python
"""
SAND HTTP Conformance Server.
This implements a conformance server for ISO/IEC 23009-5 SAND.
It validates the incoming SAND messages as well as the protocols used by
a SAND client.
Copyright (c) 2016-, TNO, Technicolor
All rights reserved.
See AUTHORS for a full list of authors.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import logging
from flask import Flask, request
from werkzeug.routing import Rule
import click
import sand.header
from sand.xml_message import XMLValidator
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.DEBUG)
APP = Flask(__name__)
APP.debug = True
APP.url_map.add(Rule('/metrics', endpoint='metrics'))
@APP.endpoint('metrics')
def metrics():
"""
Validates the reception of metrics sent by DASH clients.
"""
success = True
expected_request_method = "POST"
expected_content_type = "application/sand+xml"
# Test 1 - HTTP method test
if request.method == expected_request_method:
logging.info("[TEST][OK] HTTP method (%s)", expected_request_method)
success &= True
else:
logging.info("[TEST][OK] HTTP method (%s != %s)",
request.method,
expected_request_method)
success = False
# Test 2 - Content-Type of SAND messages
if request.headers.get("Content-Type") == expected_content_type:
logging.info("[TEST][OK] Content-Type (%s)", expected_content_type)
success &= True
else:
logging.info("[TEST][KO] Content-Type (%s != %s)",
request.headers.get("Content-Type"),
expected_content_type)
success = False
# Test 3 - Message validation
try:
validator = XMLValidator()
if validator.from_string(request.data):
logging.info("[TEST][OK] SAND message validation")
success &= True
else:
logging.info("[TEST][KO] SAND message validation")
success = False
except:
logging.error("XML SAND message parsing")
success = False
if success:
logging.info("[RESULT][OK]")
return ("[RESULT][OK]", 200)
else:
logging.info("[RESULT][KO]")
return ("[RESULT][KO]", 400)
@APP.route('/headers')
def check_headers():
"""
Check the MPEG DASH SAND header syntax.
"""
report = {}
for header_name, msg in request.headers.items():
if header_name.upper().startswith('SAND-'):
checker = sand.header.header_name_to_checker.get(
header_name.lower())
if checker:
checker.check_syntax(msg.strip())
report[header_name] = checker.errors
else:
report[header_name] = [('Header name not supported by this ' +
'version of conformance server')]
result = "Report for SAND headers conformance:\n"
if report:
for name, errors in report.items():
if errors:
result += '%s: FAILED\n' % name
for msg in errors:
result += ' %s\n' % msg
else:
result += '%s: PASSED\n' % name
else:
result += 'No SAND header found!\n'
return result, 200, {'Content-Type': 'text/plain'}
@click.group()
def cli():
"""
Click group commands.
"""
pass
@click.command()
@click.option("--port", default=5000,
help="Listening port of the SAND conformance server.")
def run(port):
"""
Run the SAND server and listen to port 'port'.
"""
print "========= SAND conformance server ============="
print "-----------------------------------------------"
import os
if os.environ.get('PORT') is not None:
port = int(os.environ['PORT'])
APP.run(port=port)
cli.add_command(run)
if __name__ == '__main__':
cli()