-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathadvancedhttpserver.py
More file actions
executable file
·2216 lines (1967 loc) · 75.2 KB
/
advancedhttpserver.py
File metadata and controls
executable file
·2216 lines (1967 loc) · 75.2 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# advancedhttpserver.py
#
# 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 project 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
# OWNER OR CONTRIBUTORS 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.
#
# pylint: disable=too-many-lines
# Homepage: https://github.com/zeroSteiner/AdvancedHTTPServer
# Author: Spencer McIntyre (zeroSteiner)
# Config file example
FILE_CONFIG = """
[server]
ip = 0.0.0.0
port = 8080
web_root = /var/www/html
list_directories = True
# Set an ssl_cert to enable SSL
# ssl_cert = /path/to/cert.pem
# ssl_key = /path/to/cert.key
# ssl_version = TLSv1
"""
# The AdvancedHTTPServer systemd service unit file
# Quick how to:
# 1. Copy this file to /etc/systemd/system/pyhttpd.service
# 2. Edit the run parameters appropriately in the ExecStart option
# 3. Set configuration settings in /etc/pyhttpd.conf
# 4. Run "systemctl daemon-reload"
FILE_SYSTEMD_SERVICE_UNIT = """
[Unit]
Description=Python Advanced HTTP Server
After=network.target
[Service]
Type=simple
ExecStart=/sbin/runuser -l nobody -c "/usr/bin/python -m advancedhttpserver -c /etc/pyhttpd.conf"
ExecStop=/bin/kill -INT $MAINPID
[Install]
WantedBy=multi-user.target
"""
__version__ = '2.2.1'
__all__ = (
'AdvancedHTTPServer',
'RegisterPath',
'RequestHandler',
'RPCClient',
'RPCClientCached',
'RPCError',
'RPCConnectionError',
'ServerTestCase',
'WebSocketHandler',
'build_server_from_argparser',
'build_server_from_config'
)
import base64
import binascii
import collections
import datetime
import hashlib
import io
import json
import logging
import logging.handlers
import mimetypes
import os
import posixpath
import random
import re
import select
import shutil
import socket
import sqlite3
import ssl
import string
import struct
import sys
import threading
import time
import traceback
import unittest
import urllib
import weakref
import zlib
if sys.version_info[0] < 3:
import BaseHTTPServer
import cgi as html
import Cookie
import httplib
import Queue as queue
import SocketServer as socketserver
import urlparse
http = type('http', (), {'client': httplib, 'cookies': Cookie, 'server': BaseHTTPServer})
urllib.parse = urlparse
urllib.parse.quote = urllib.quote
urllib.parse.unquote = urllib.unquote
urllib.parse.urlencode = urllib.urlencode
from ConfigParser import ConfigParser
else:
import html
import http.client
import http.cookies
import http.server
import queue
import socketserver
import urllib.parse
from configparser import ConfigParser
g_handler_map = {}
g_serializer_drivers = {}
"""Dictionary of available drivers for serialization."""
g_ssl_has_server_sni = (getattr(ssl, 'HAS_SNI', False) and sys.version_info >= ((2, 7, 9) if sys.version_info[0] < 3 else (3, 4)))
"""An indication of if the environment offers server side SNI support."""
def _serialize_ext_dump(obj):
if obj.__class__ == datetime.date:
return 'datetime.date', obj.isoformat()
elif obj.__class__ == datetime.datetime:
return 'datetime.datetime', obj.isoformat()
elif obj.__class__ == datetime.time:
return 'datetime.time', obj.isoformat()
raise TypeError('Unknown type: ' + repr(obj))
def _serialize_ext_load(obj_type, obj_value, default):
if obj_type == 'datetime.date':
return datetime.datetime.strptime(obj_value, '%Y-%m-%d').date()
elif obj_type == 'datetime.datetime':
return datetime.datetime.strptime(obj_value, '%Y-%m-%dT%H:%M:%S' + ('.%f' if '.' in obj_value else ''))
elif obj_type == 'datetime.time':
return datetime.datetime.strptime(obj_value, '%H:%M:%S' + ('.%f' if '.' in obj_value else '')).time()
return default
def _json_default(obj):
obj_type, obj_value = _serialize_ext_dump(obj)
return {'__complex_type__': obj_type, 'value': obj_value}
def _json_object_hook(obj):
return _serialize_ext_load(obj.get('__complex_type__'), obj.get('value'), obj)
g_serializer_drivers['application/json'] = {
'dumps': lambda d: json.dumps(d, default=_json_default),
'loads': lambda d, e: json.loads(d, object_hook=_json_object_hook)
}
try:
import msgpack
except ImportError:
has_msgpack = False
else:
has_msgpack = True
_MSGPACK_EXT_TYPES = {10: 'datetime.datetime', 11: 'datetime.date', 12: 'datetime.time'}
def _msgpack_default(obj):
obj_type, obj_value = _serialize_ext_dump(obj)
obj_type = next(i[0] for i in _MSGPACK_EXT_TYPES.items() if i[1] == obj_type)
if sys.version_info[0] == 3:
obj_value = obj_value.encode('utf-8')
return msgpack.ExtType(obj_type, obj_value)
def _msgpack_ext_hook(code, obj_value):
default = msgpack.ExtType(code, obj_value)
if sys.version_info[0] == 3:
obj_value = obj_value.decode('utf-8')
obj_type = _MSGPACK_EXT_TYPES.get(code)
return _serialize_ext_load(obj_type, obj_value, default)
g_serializer_drivers['binary/message-pack'] = {
'dumps': lambda d: msgpack.dumps(d, default=_msgpack_default),
'loads': lambda d, e: msgpack.loads(d, encoding=e, ext_hook=_msgpack_ext_hook)
}
if hasattr(logging, 'NullHandler'):
logging.getLogger('AdvancedHTTPServer').addHandler(logging.NullHandler())
def random_string(size):
"""
Generate a random string of *size* length consisting of both letters
and numbers. This function is not meant for cryptographic purposes
and should not be used to generate security tokens.
:param int size: The length of the string to return.
:return: A string consisting of random characters.
:rtype: str
"""
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(size))
def resolve_ssl_protocol_version(version=None):
"""
Look up an SSL protocol version by name. If *version* is not specified, then
the strongest protocol available will be returned.
:param str version: The name of the version to look up.
:return: A protocol constant from the :py:mod:`ssl` module.
:rtype: int
"""
if version is None:
protocol_preference = ('TLSv1_2', 'TLSv1_1', 'TLSv1', 'SSLv3', 'SSLv23', 'SSLv2')
for protocol in protocol_preference:
if hasattr(ssl, 'PROTOCOL_' + protocol):
return getattr(ssl, 'PROTOCOL_' + protocol)
raise RuntimeError('could not find a suitable ssl PROTOCOL_ version constant')
elif isinstance(version, str):
if not hasattr(ssl, 'PROTOCOL_' + version):
raise ValueError('invalid ssl protocol version: ' + version)
return getattr(ssl, 'PROTOCOL_' + version)
raise TypeError("ssl_version() argument 1 must be str, not {0}".format(type(version).__name__))
def build_server_from_argparser(description=None, server_klass=None, handler_klass=None):
"""
Build a server from command line arguments. If a ServerClass or
HandlerClass is specified, then the object must inherit from the
corresponding AdvancedHTTPServer base class.
:param str description: Description string to be passed to the argument parser.
:param server_klass: Alternative server class to use.
:type server_klass: :py:class:`.AdvancedHTTPServer`
:param handler_klass: Alternative handler class to use.
:type handler_klass: :py:class:`.RequestHandler`
:return: A configured server instance.
:rtype: :py:class:`.AdvancedHTTPServer`
"""
import argparse
def _argp_dir_type(arg):
if not os.path.isdir(arg):
raise argparse.ArgumentTypeError("{0} is not a valid directory".format(repr(arg)))
return arg
def _argp_port_type(arg):
if not arg.isdigit():
raise argparse.ArgumentTypeError("{0} is not a valid port".format(repr(arg)))
arg = int(arg)
if arg < 0 or arg > 65535:
raise argparse.ArgumentTypeError("{0} is not a valid port".format(repr(arg)))
return arg
description = (description or 'HTTP Server')
server_klass = (server_klass or AdvancedHTTPServer)
handler_klass = (handler_klass or RequestHandler)
parser = argparse.ArgumentParser(conflict_handler='resolve', description=description, fromfile_prefix_chars='@')
parser.epilog = 'When a config file is specified with --config only the --log, --log-file and --password options will be used.'
parser.add_argument('-c', '--conf', dest='config', type=argparse.FileType('r'), help='read settings from a config file')
parser.add_argument('-i', '--ip', dest='ip', default='0.0.0.0', help='the ip address to serve on')
parser.add_argument('-L', '--log', dest='loglvl', choices=('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'), default='INFO', help='set the logging level')
parser.add_argument('-p', '--port', dest='port', default=8080, type=_argp_port_type, help='port to serve on')
parser.add_argument('-v', '--version', action='version', version=parser.prog + ' Version: ' + __version__)
parser.add_argument('-w', '--web-root', dest='web_root', default='.', type=_argp_dir_type, help='path to the web root directory')
parser.add_argument('--log-file', dest='log_file', help='log information to a file')
parser.add_argument('--no-threads', dest='use_threads', action='store_false', default=True, help='disable threading')
parser.add_argument('--password', dest='password', help='password to use for basic authentication')
ssl_group = parser.add_argument_group('ssl options')
ssl_group.add_argument('--ssl-cert', dest='ssl_cert', help='the ssl cert to use')
ssl_group.add_argument('--ssl-key', dest='ssl_key', help='the ssl key to use')
ssl_group.add_argument('--ssl-version', dest='ssl_version', choices=[p[9:] for p in dir(ssl) if p.startswith('PROTOCOL_')], help='the version of ssl to use')
arguments = parser.parse_args()
logging.getLogger('').setLevel(logging.DEBUG)
console_log_handler = logging.StreamHandler()
console_log_handler.setLevel(getattr(logging, arguments.loglvl))
console_log_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-8s %(message)s"))
logging.getLogger('').addHandler(console_log_handler)
if arguments.log_file:
main_file_handler = logging.handlers.RotatingFileHandler(arguments.log_file, maxBytes=262144, backupCount=5)
main_file_handler.setLevel(logging.DEBUG)
main_file_handler.setFormatter(logging.Formatter("%(asctime)s %(name)-30s %(levelname)-10s %(message)s"))
logging.getLogger('').setLevel(logging.DEBUG)
logging.getLogger('').addHandler(main_file_handler)
if arguments.config:
config = ConfigParser()
config.readfp(arguments.config)
server = build_server_from_config(
config,
'server',
server_klass=server_klass,
handler_klass=handler_klass
)
else:
server = server_klass(
handler_klass,
address=(arguments.ip, arguments.port),
use_threads=arguments.use_threads,
ssl_certfile=arguments.ssl_cert,
ssl_keyfile=arguments.ssl_key,
ssl_version=arguments.ssl_version
)
server.serve_files_root = arguments.web_root
if arguments.password:
server.auth_add_creds('', arguments.password)
return server
def build_server_from_config(config, section_name, server_klass=None, handler_klass=None):
"""
Build a server from a provided :py:class:`configparser.ConfigParser`
instance. If a ServerClass or HandlerClass is specified, then the
object must inherit from the corresponding AdvancedHTTPServer base
class.
:param config: Configuration to retrieve settings from.
:type config: :py:class:`configparser.ConfigParser`
:param str section_name: The section name of the configuration to use.
:param server_klass: Alternative server class to use.
:type server_klass: :py:class:`.AdvancedHTTPServer`
:param handler_klass: Alternative handler class to use.
:type handler_klass: :py:class:`.RequestHandler`
:return: A configured server instance.
:rtype: :py:class:`.AdvancedHTTPServer`
"""
server_klass = (server_klass or AdvancedHTTPServer)
handler_klass = (handler_klass or RequestHandler)
port = config.getint(section_name, 'port')
web_root = None
if config.has_option(section_name, 'web_root'):
web_root = config.get(section_name, 'web_root')
if config.has_option(section_name, 'ip'):
ip = config.get(section_name, 'ip')
else:
ip = '0.0.0.0'
ssl_certfile = None
if config.has_option(section_name, 'ssl_cert'):
ssl_certfile = config.get(section_name, 'ssl_cert')
ssl_keyfile = None
if config.has_option(section_name, 'ssl_key'):
ssl_keyfile = config.get(section_name, 'ssl_key')
ssl_version = None
if config.has_option(section_name, 'ssl_version'):
ssl_version = config.get(section_name, 'ssl_version')
server = server_klass(
handler_klass,
address=(ip, port),
ssl_certfile=ssl_certfile,
ssl_keyfile=ssl_keyfile,
ssl_version=ssl_version
)
if config.has_option(section_name, 'password_type'):
password_type = config.get(section_name, 'password_type')
else:
password_type = 'md5'
if config.has_option(section_name, 'password'):
password = config.get(section_name, 'password')
if config.has_option(section_name, 'username'):
username = config.get(section_name, 'username')
else:
username = ''
server.auth_add_creds(username, password, pwtype=password_type)
cred_idx = 0
while config.has_option(section_name, 'password' + str(cred_idx)):
password = config.get(section_name, 'password' + str(cred_idx))
if not config.has_option(section_name, 'username' + str(cred_idx)):
break
username = config.get(section_name, 'username' + str(cred_idx))
server.auth_add_creds(username, password, pwtype=password_type)
cred_idx += 1
if web_root is None:
server.serve_files = False
else:
server.serve_files = True
server.serve_files_root = web_root
if config.has_option(section_name, 'list_directories'):
server.serve_files_list_directories = config.getboolean(section_name, 'list_directories')
return server
class _RequestEmbryo(object):
__slots__ = ('server', 'socket', 'address', 'created')
def __init__(self, server, client_socket, address, created=None):
server.request_embryos.append(self)
self.server = weakref.ref(server)
self.socket = client_socket
self.address = address
self.created = created or time.time()
def fileno(self):
return self.socket.fileno()
def serve_ready(self):
server = self.server() # server is a weakref
if not server:
return False
try:
self.socket.do_handshake()
except ssl.SSLWantReadError:
return False
except (socket.error, OSError, ValueError):
self.socket.close()
server.request_embryos.remove(self)
return False
self.socket.settimeout(None)
server.request_embryos.remove(self)
server.request_queue.put((self.socket, self.address))
server.handle_request()
return True
class RegisterPath(object):
"""
Register a path and handler with the global handler map. This can be
used as a decorator. If no handler is specified then the path and
function will be registered with all :py:class:`.RequestHandler`
instances.
.. code-block:: python
@RegisterPath('^test$')
def handle_test(handler, query):
pass
"""
def __init__(self, path, handler=None, is_rpc=False):
"""
:param str path: The path regex to register the function to.
:param str handler: A specific :py:class:`.RequestHandler` class to register the handler with.
:param bool is_rpc: Whether the handler is an RPC handler or not.
"""
self.path = path
self.is_rpc = is_rpc
if handler is None or isinstance(handler, str):
self.handler = handler
elif hasattr(handler, '__name__'):
self.handler = handler.__name__
elif hasattr(handler, '__class__'):
self.handler = handler.__class__.__name__
else:
raise ValueError('unknown handler: ' + repr(handler))
def __call__(self, function):
handler_map = g_handler_map.get(self.handler, {})
handler_map[self.path] = (function, self.is_rpc)
g_handler_map[self.handler] = handler_map
return function
class RPCError(Exception):
"""
This class represents an RPC error either local or remote. Any errors
in routines executed on the server will raise this error.
"""
def __init__(self, message, status=None, remote_exception=None):
super(RPCError, self).__init__()
self.message = message
self.status = status
self.remote_exception = remote_exception
def __repr__(self):
return "{0}(message='{1}', status={2}, remote_exception={3})".format(self.__class__.__name__, self.message, self.status, self.is_remote_exception)
def __str__(self):
if self.is_remote_exception:
return 'a remote exception occurred'
return "the server responded with {0} '{1}'".format(self.status, self.message)
@property
def is_remote_exception(self):
"""
This is true if the represented error resulted from an exception on the
remote server.
:type: bool
"""
return bool(self.remote_exception is not None)
class RPCConnectionError(RPCError):
"""
An exception raised when there is a connection-related error encountered by
the RPC client.
.. versionadded:: 2.1.0
"""
pass
class RPCClient(object):
"""
This object facilitates communication with remote RPC methods as
provided by a :py:class:`.RequestHandler` instance.
Once created this object can be called directly, doing so is the same
as using the call method.
This object uses locks internally to be thread safe. Only one thread
can execute a function at a time.
"""
def __init__(self, address, use_ssl=False, username=None, password=None, uri_base='/', ssl_context=None):
"""
:param tuple address: The address of the server to connect to as (host, port).
:param bool use_ssl: Whether to connect with SSL or not.
:param str username: The username to authenticate with.
:param str password: The password to authenticate with.
:param str uri_base: An optional prefix for all methods.
:param ssl_context: An optional SSL context to use for SSL related options.
"""
self.host = str(address[0])
self.port = int(address[1])
if not hasattr(self, 'logger'):
self.logger = logging.getLogger('AdvancedHTTPServer.RPCClient')
self.headers = None
"""An optional dictionary of headers to include with each RPC request."""
self.use_ssl = bool(use_ssl)
self.ssl_context = ssl_context
self.uri_base = str(uri_base)
self.username = (None if username is None else str(username))
self.password = (None if password is None else str(password))
self.lock = threading.Lock()
"""A :py:class:`threading.Lock` instance used to synchronize operations."""
self.serializer = None
"""The :py:class:`.Serializer` instance to use for encoding RPC data to the server."""
self.set_serializer('application/json')
self.reconnect()
def __del__(self):
self.client.close()
def __reduce__(self):
address = (self.host, self.port)
return (self.__class__, (address, self.use_ssl, self.username, self.password, self.uri_base))
def set_serializer(self, serializer_name, compression=None):
"""
Configure the serializer to use for communication with the server.
The serializer specified must be valid and in the
:py:data:`.g_serializer_drivers` map.
:param str serializer_name: The name of the serializer to use.
:param str compression: The name of a compression library to use.
"""
self.serializer = Serializer(serializer_name, charset='UTF-8', compression=compression)
self.logger.debug('using serializer: ' + serializer_name)
def __call__(self, *args, **kwargs):
return self.call(*args, **kwargs)
def encode(self, data):
"""Encode data with the configured serializer."""
return self.serializer.dumps(data)
def decode(self, data):
"""Decode data with the configured serializer."""
return self.serializer.loads(data)
def reconnect(self):
"""Reconnect to the remote server."""
self.lock.acquire()
if self.use_ssl:
self.client = http.client.HTTPSConnection(self.host, self.port, context=self.ssl_context)
else:
self.client = http.client.HTTPConnection(self.host, self.port)
self.lock.release()
def call(self, method, *args, **kwargs):
"""
Issue a call to the remote end point to execute the specified
procedure.
:param str method: The name of the remote procedure to execute.
:return: The return value from the remote function.
"""
if kwargs:
options = self.encode(dict(args=args, kwargs=kwargs))
else:
options = self.encode(args)
headers = {}
if self.headers:
headers.update(self.headers)
headers['Content-Type'] = self.serializer.content_type
headers['Content-Length'] = str(len(options))
headers['Connection'] = 'close'
if self.username is not None and self.password is not None:
headers['Authorization'] = 'Basic ' + base64.b64encode((self.username + ':' + self.password).encode('UTF-8')).decode('UTF-8')
method = os.path.join(self.uri_base, method)
self.logger.debug('calling RPC method: ' + method[1:])
try:
with self.lock:
self.client.request('RPC', method, options, headers)
resp = self.client.getresponse()
except http.client.ImproperConnectionState:
raise RPCConnectionError('improper connection state')
if resp.status != 200:
raise RPCError(resp.reason, resp.status)
resp_data = resp.read()
resp_data = self.decode(resp_data)
if not ('exception_occurred' in resp_data and 'result' in resp_data):
raise RPCError('missing response information', resp.status)
if resp_data['exception_occurred']:
raise RPCError('remote method incurred an exception', resp.status, remote_exception=resp_data['exception'])
return resp_data['result']
class RPCClientCached(RPCClient):
"""
This object builds upon :py:class:`.RPCClient` and
provides additional methods for cacheing results in memory.
"""
def __init__(self, *args, **kwargs):
cache_db = kwargs.pop('cache_db', ':memory:')
super(RPCClientCached, self).__init__(*args, **kwargs)
self.cache_db = sqlite3.connect(cache_db, check_same_thread=False)
cursor = self.cache_db.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS cache (method TEXT NOT NULL, options_hash BLOB NOT NULL, return_value BLOB NOT NULL)')
self.cache_db.commit()
self.cache_lock = threading.Lock()
def cache_call(self, method, *options):
"""
Call a remote method and store the result locally. Subsequent
calls to the same method with the same arguments will return the
cached result without invoking the remote procedure. Cached results are
kept indefinitely and must be manually refreshed with a call to
:py:meth:`.cache_call_refresh`.
:param str method: The name of the remote procedure to execute.
:return: The return value from the remote function.
"""
options_hash = self.encode(options)
if len(options_hash) > 20:
options_hash = hashlib.new('sha1', options_hash).digest()
options_hash = sqlite3.Binary(options_hash)
with self.cache_lock:
cursor = self.cache_db.cursor()
cursor.execute('SELECT return_value FROM cache WHERE method = ? AND options_hash = ?', (method, options_hash))
return_value = cursor.fetchone()
if return_value:
return_value = bytes(return_value[0])
return self.decode(return_value)
return_value = self.call(method, *options)
store_return_value = sqlite3.Binary(self.encode(return_value))
with self.cache_lock:
cursor = self.cache_db.cursor()
cursor.execute('INSERT INTO cache (method, options_hash, return_value) VALUES (?, ?, ?)', (method, options_hash, store_return_value))
self.cache_db.commit()
return return_value
def cache_call_refresh(self, method, *options):
"""
Call a remote method and update the local cache with the result
if it already existed.
:param str method: The name of the remote procedure to execute.
:return: The return value from the remote function.
"""
options_hash = self.encode(options)
if len(options_hash) > 20:
options_hash = hashlib.new('sha1', options).digest()
options_hash = sqlite3.Binary(options_hash)
with self.cache_lock:
cursor = self.cache_db.cursor()
cursor.execute('DELETE FROM cache WHERE method = ? AND options_hash = ?', (method, options_hash))
return_value = self.call(method, *options)
store_return_value = sqlite3.Binary(self.encode(return_value))
with self.cache_lock:
cursor = self.cache_db.cursor()
cursor.execute('INSERT INTO cache (method, options_hash, return_value) VALUES (?, ?, ?)', (method, options_hash, store_return_value))
self.cache_db.commit()
return return_value
def cache_clear(self):
"""Purge the local store of all cached function information."""
with self.cache_lock:
cursor = self.cache_db.cursor()
cursor.execute('DELETE FROM cache')
self.cache_db.commit()
self.logger.info('the RPC cache has been purged')
return
class ServerNonThreaded(http.server.HTTPServer, object):
"""
This class is used internally by :py:class:`.AdvancedHTTPServer` and
is not intended for use by other classes or functions. It is responsible for
listening on a single address, TCP port and SSL combination.
"""
def __init__(self, *args, **kwargs):
self.__config = kwargs.pop('config')
if not hasattr(self, 'logger'):
self.logger = logging.getLogger('AdvancedHTTPServer')
self.allow_reuse_address = True
self.request_queue = queue.Queue()
self.request_embryos = []
self.using_ssl = False
super(ServerNonThreaded, self).__init__(*args, **kwargs)
def __repr__(self):
address = self.server_address[0]
if self.socket.family == socket.AF_INET:
address += ':' + str(self.server_address[1])
elif self.socket.family == socket.AF_INET6:
address = '[' + address + ']:' + str(self.server_address[1])
return "<{0} address: {1} ssl: {2!r}>".format(self.__class__.__name__, address, self.using_ssl)
@property
def read_checkable_fds(self):
return [self] + self.request_embryos
def get_config(self):
return self.__config
def get_request(self):
return self.request_queue.get(block=True, timeout=None)
def handle_request(self):
timeout = self.socket.gettimeout()
if timeout is None:
timeout = self.timeout
elif self.timeout is not None:
timeout = min(timeout, self.timeout)
try:
request, client_address = self.request_queue.get(block=True, timeout=timeout)
except queue.Empty:
return self.handle_timeout()
except OSError:
return None
if self.verify_request(request, client_address):
try:
self.process_request(request, client_address)
except Exception:
self.handle_error(request, client_address)
self.shutdown_request(request)
except:
self.shutdown_request(request)
raise
else:
self.shutdown_request(request)
return None
def finish_request(self, request, client_address):
try:
super(ServerNonThreaded, self).finish_request(request, client_address)
except IOError:
self.logger.warning('IOError encountered in finish_request')
except KeyboardInterrupt:
self.logger.warning('KeyboardInterrupt encountered in finish_request')
self.shutdown()
def serve_ready(self):
client_socket, address = self.socket.accept()
if self.using_ssl:
client_socket.settimeout(0)
embryo = _RequestEmbryo(self, client_socket, address)
embryo.serve_ready()
else:
client_socket.settimeout(None)
self.request_queue.put((client_socket, address))
self.handle_request()
def server_bind(self, *args, **kwargs):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
super(ServerNonThreaded, self).server_bind(*args, **kwargs)
def shutdown(self, *args, **kwargs):
try:
self.socket.shutdown(socket.SHUT_RDWR)
except socket.error:
pass
self.socket.close()
class ServerThreaded(socketserver.ThreadingMixIn, ServerNonThreaded):
"""
This class is used internally by :py:class:`.AdvancedHTTPServer` and
is not intended for use by other classes or functions. It is responsible for
listening on a single address, TCP port and SSL combination.
"""
daemon_threads = True
class RequestHandler(http.server.BaseHTTPRequestHandler, object):
"""
This is the primary http request handler class of the
AdvancedHTTPServer framework. Custom request handlers must inherit
from this object to be compatible. Instances of this class are created
automatically. This class will handle standard HTTP GET, HEAD, OPTIONS,
and POST requests. Callback functions called handlers can be registered
to resource paths using regular expressions in the *handler_map*
attribute for GET HEAD and POST requests and *rpc_handler_map* for RPC
requests. Non-RPC handler functions that are not class methods of
the request handler instance will be passed the instance of the
request handler as the first argument.
"""
if not mimetypes.inited:
mimetypes.init() # try to read system mime.types
extensions_map = mimetypes.types_map.copy()
extensions_map.update({
'': 'application/octet-stream', # Default
'.py': 'text/plain',
'.rb': 'text/plain',
'.c': 'text/plain',
'.h': 'text/plain',
})
protocol_version = 'HTTP/1.1'
wbufsize = 4096
web_socket_handler = None
"""An optional class to handle Web Sockets. This class must be derived from :py:class:`.WebSocketHandler`."""
def __init__(self, *args, **kwargs):
self.cookies = None
self.path = None
self.wfile = None
self._wfile = None
self.server = args[2]
self.headers_active = False
"""Whether or not the request is in the sending headers phase."""
self.handler_map = {}
"""The dictionary object which maps regular expressions of resources to the functions which should handle them."""
self.rpc_handler_map = {}
"""The dictionary object which maps regular expressions of RPC functions to their handlers."""
for map_name in (None, self.__class__.__name__):
handler_map = g_handler_map.get(map_name, {})
for path, function_info in handler_map.items():
function, function_is_rpc = function_info
if function_is_rpc:
self.rpc_handler_map[path] = function
else:
self.handler_map[path] = function
self.basic_auth_user = None
"""The name of the user if the current request is using basic authentication."""
self.query_data = None
"""The parameter data that has been passed to the server parsed as a dict."""
self.raw_query_data = None
"""The raw data that was parsed into the :py:attr:`.query_data` attribute."""
self.__config = self.server.get_config()
"""A reference to the configuration provided by the server."""
self.on_init()
super(RequestHandler, self).__init__(*args, **kwargs)
def setup(self, *args, **kwargs):
ret = super(RequestHandler, self).setup(*args, **kwargs)
self._wfile = self.wfile
return ret
def on_init(self):
"""
This method is meant to be over ridden by custom classes. It is
called as part of the __init__ method and provides an opportunity
for the handler maps to be populated with entries or the config to be
customized.
"""
pass # over ride me
def __get_handler(self, is_rpc=False):
handler = None
handler_map = (self.rpc_handler_map if is_rpc else self.handler_map)
for (path_regex, handler) in handler_map.items():
if re.match(path_regex, self.path):
break
else:
return (None, None)
is_method = False
self_handler = None
if hasattr(handler, '__name__'):
self_handler = getattr(self, handler.__name__, None)
if self_handler is not None and (handler == self_handler.__func__ or handler == self_handler):
is_method = True
return (handler, is_method)
def version_string(self):
return self.__config['server_version']
def respond_file(self, file_path, attachment=False, query=None):
"""
Respond to the client by serving a file, either directly or as
an attachment.
:param str file_path: The path to the file to serve, this does not need to be in the web root.
:param bool attachment: Whether to serve the file as a download by setting the Content-Disposition header.
"""
del query
file_path = os.path.abspath(file_path)
try:
file_obj = open(file_path, 'rb')
except IOError:
self.respond_not_found()
return
self.send_response(200)
self.send_header('Content-Type', self.guess_mime_type(file_path))
fs = os.fstat(file_obj.fileno())
self.send_header('Content-Length', str(fs[6]))
if attachment:
file_name = os.path.basename(file_path)
self.send_header('Content-Disposition', 'attachment; filename=' + file_name)
self.send_header('Last-Modified', self.date_time_string(fs.st_mtime))
self.end_headers()
shutil.copyfileobj(file_obj, self.wfile)
file_obj.close()
return
def respond_list_directory(self, dir_path, query=None):
"""
Respond to the client with an HTML page listing the contents of
the specified directory.
:param str dir_path: The path of the directory to list the contents of.
"""
del query
try:
dir_contents = os.listdir(dir_path)
except os.error:
self.respond_not_found()
return
if os.path.normpath(dir_path) != self.__config['serve_files_root']:
dir_contents.append('..')
dir_contents.sort(key=lambda a: a.lower())
displaypath = html.escape(urllib.parse.unquote(self.path), quote=True)
f = io.BytesIO()
encoding = sys.getfilesystemencoding()
f.write(b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n')
f.write(b'<html>\n<title>Directory listing for ' + displaypath.encode(encoding) + b'</title>\n')
f.write(b'<body>\n<h2>Directory listing for ' + displaypath.encode(encoding) + b'</h2>\n')
f.write(b'<hr>\n<ul>\n')
for name in dir_contents:
fullname = os.path.join(dir_path, name)
displayname = linkname = name
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
if os.path.islink(fullname):
displayname = name + "@"
# Note: a link to a directory displays with @ and links with /
f.write(('<li><a href="' + urllib.parse.quote(linkname) + '">' + html.escape(displayname, quote=True) + '</a>\n').encode(encoding))
f.write(b'</ul>\n<hr>\n</body>\n</html>\n')
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header('Content-Type', 'text/html; charset=' + encoding)
self.send_header('Content-Length', length)
self.end_headers()
shutil.copyfileobj(f, self.wfile)
f.close()
return
def respond_not_found(self):
"""Respond to the client with a default 404 message."""
self.send_response_full(b'Resource Not Found\n', status=404)
return
def respond_redirect(self, location='/'):
"""
Respond to the client with a 301 message and redirect them with
a Location header.
:param str location: The new location to redirect the client to.
"""
self.send_response(301)
self.send_header('Content-Length', 0)
self.send_header('Location', location)
self.end_headers()
return
def respond_server_error(self, status=None, status_line=None, message=None):
"""
Handle an internal server error, logging a traceback if executed
within an exception handler.
:param int status: The status code to respond to the client with.
:param str status_line: The status message to respond to the client with.
:param str message: The body of the response that is sent to the client.
"""
(ex_type, ex_value, ex_traceback) = sys.exc_info()
if ex_type:
(ex_file_name, ex_line, _, _) = traceback.extract_tb(ex_traceback)[-1]
line_info = "{0}:{1}".format(ex_file_name, ex_line)
log_msg = "encountered {0} in {1}".format(repr(ex_value), line_info)
self.server.logger.error(log_msg, exc_info=True)
status = (status or 500)