-
Notifications
You must be signed in to change notification settings - Fork 2
/
system.py
1232 lines (1024 loc) · 51.5 KB
/
system.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
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/python
# -*- coding: utf-8 -*-
# Hive Colony Framework
# Copyright (c) 2008-2018 Hive Solutions Lda.
#
# This file is part of Hive Colony Framework.
#
# Hive Colony Framework is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Colony Framework 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
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Colony Framework. If not, see <http://www.apache.org/licenses/>.
__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """
__version__ = "1.0.0"
""" The version of the module """
__revision__ = "$LastChangedRevision$"
""" The revision number of the module """
__date__ = "$LastChangedDate$"
""" The last change date of the module """
__copyright__ = "Copyright (c) 2008-2018 Hive Solutions Lda."
""" The copyright for the module """
__license__ = "Apache License, Version 2.0"
""" The license for the module """
import datetime
import threading
import traceback
import colony
from . import exceptions
POWERED_BY_STRING = "colony/%s (%s)"
""" The string to be used in the powered by HTTP
header to be sent to the end used as a sign of
the underlying infra-structure of WSGI """
IDENTIFIER_STRING = "Colony Framework / %s (%s)"
""" The template to be used for the string to be
returned in diagnostic messages like errors """
ALLOW_ORIGIN = "*"
""" The default value to be used in the "Access-Control-Allow-Origin"
header value, this should not be too restrictive """
ALLOW_HEADERS = "*, X-Requested-With"
""" The default value to be used in the "Access-Control-Allow-Headers"
header value, this should not be too restrictive """
ALLOW_METHODS = "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS"
""" The default value to be used in the "Access-Control-Allow-Methods"
header value, this should not be too restrictive """
CONTENT_SECURITY = "default-src * ws://* wss://* data: blob:; script-src * 'unsafe-inline' 'unsafe-eval'; style-src * 'unsafe-inline';"
""" The default value to be used in the "Content-Security-Policy"
header value, this should not be too restrictive """
FRAME_OPTIONS = "SAMEORIGIN"
""" The value to be as the default/original for the "X-Frame-Options"
header, this should ensure that the same origin is always used when
trying to embed a dynamic content into a web page """
XSS_PROTECTION = "1; mode=block"
""" Value to be used as the original one for the "X-XSS-Protection"
header value, should provide a way of preventing XSS attach under the
internet explorer browser """
CONTENT_OPTIONS = "nosniff"
""" Default "X-Content-Type-Options" header value to be used to prevent
the sniffing of content type values, ensuring that the browser sticks to
value of content type provided by the server """
PATH_INFO_PREFIX = "/dynamic/rest"
""" The prefix to be used at the start of the
path info so that every request uri is inserted
within this context (this way the uri is shorter) """
CHUNK_SIZE = 4096
""" The size (in bytes) of the message piece
to be retrieved from the message provider from
each iteration, if this value is too small the
overhead for sending may be a problem """
DEFAULT_CHARSET = "utf-8"
""" The default charset to be used by the request
this value is defined in such a way that all the
knows characters are able to be encoded """
class Wsgi(colony.System):
"""
The WSGI class, responsible for the implementation
of the colony side of the WSGI specification.
:see: http://www.python.org/dev/peps/pep-0333/
"""
def handle(
self,
environ,
start_response,
prefix = None,
alias = None,
rewrite = None
):
# retrieves some of the configuration values that are
# going to control the request handling behaviour
secure_headers = colony.conf("WSGI_SECURE_HEADERS", True, cast = bool)
allow_origin = colony.conf("WSGI_CORS", ALLOW_ORIGIN)
allow_origin = colony.conf("WSGI_ALLOW_ORIGIN", allow_origin)
allow_headers = colony.conf("WSGI_ALLOW_HEADERS", ALLOW_HEADERS)
allow_methods = colony.conf("WSGI_ALLOW_METHODS", ALLOW_METHODS)
content_security = colony.conf("WSGI_CONTENT_SECURITY", CONTENT_SECURITY)
frame_options = colony.conf("WSGI_FRAME_OPTIONS", FRAME_OPTIONS)
xss_protection = colony.conf("WSGI_XSS_PROTECTION", XSS_PROTECTION)
content_options = colony.conf("WSGI_CONTENT_OPTIONS", CONTENT_OPTIONS)
# retrieves the reference to the currently executing
# plugin manager to be used further ahead
plugin_manager = self.plugin.manager
# retrieves the reference to the rest plugin from the
# upper level WSGI plugin, this value may not be set
# in case there was a problem in the manager loading
rest_plugin = self.plugin.rest_plugin
# retrieves the string based version of the currently
# executing plugin manager, this value is going to be
# used in the formating of the powered by string
manager_version = plugin_manager.get_version()
manager_environment = plugin_manager.get_environment()
# sets the default status code value as success,
# all the request are considered to be successful
# unless otherwise is state (exception raised) note
# that the status string is also defined as valid
code = 200
status = "OK"
# creates a new WSGI request object with the provided
# environment map (this object should be able to "emulate"
# the default request) then provides the rest plugin
# with the request for handling, handling the resulting
# data or setting the exception values
request = WsgiRequest(
self,
environ,
prefix = prefix,
alias = alias,
rewrite = rewrite
)
try: rest_plugin.handle_request(request); request.finish()
except BaseException as exception:
has_code = hasattr(exception, "status_code")
code = exception.status_code if has_code else 500
try: code = int(code)
except: code = 500
status = "Not OK"
message = self.error_message(exception, code = code)
is_unicode = colony.legacy.is_unicode(message)
if is_unicode: message = message.encode(request.content_type_charset)
content = [message]
headers_out_l = []
else:
code = request.status_code
content = request.message_buffer
headers_out = request.headers_out
headers_out_l = colony.legacy.items(headers_out)
# verifies if the current content to be handled is generator based
# if that's the case special handling is required meaning for example
# that the complete set of contents is considered to be the generator
is_generator = content and colony.legacy.is_generator(content[0])
if is_generator: content = content[0]
# sets the content type to be returned as the one provided
# by the request or defaults to the basic one, then tries
# to calculate the content length based on the size of the
# various items present in the content sequence (list)
content_type = request.content_type or "text/plain"
if is_generator: content_length = -1
else: content_length = sum([len(value) for value in content])
# in case the request is mediated additional operations may
# be taken to provide the compatibility layer, the content
# sequence must be a generator function and the size must
# be pre calculated
if request.is_mediated():
content_length = request.mediated_handler.get_size()
content = request.mediate()
# update the status line with the provided code value and then
# creates the initial/static response headers list with the
# created values to be used as part of the initial response
status = "%d %s" % (code, status)
response_headers = [
("Content-Type", content_type),
("X-Powered-By", POWERED_BY_STRING % (manager_version, manager_environment))
]
# verifies if the provided content length value is considered
# valid and if that's the case adds it's value as an header
if not content_length == -1: response_headers.append(
("Content-Length", str(content_length))
)
# in case the secure headers flag is set, runs the setting operation
# over the complete set of secure headers
if secure_headers and allow_origin:
response_headers.append(("Access-Control-Allow-Origin", allow_origin))
if secure_headers and allow_headers:
response_headers.append(("Access-Control-Allow-Headers", allow_headers))
if secure_headers and allow_methods:
response_headers.append(("Access-Control-Allow-Methods", allow_methods))
if secure_headers and content_security:
response_headers.append(("Content-Security-Policy", content_security))
if secure_headers and frame_options:
response_headers.append(("X-Frame-Options", frame_options))
if secure_headers and xss_protection:
response_headers.append(("X-XSS-Protection", xss_protection))
if secure_headers and content_options:
response_headers.append(("X-Content-Type-Options", content_options))
# extends the current list of response headers with the list
# retrieved from the request (headers out)
response_headers.extend(headers_out_l)
# runs the initial start response method, part of the WSGI spec
# so that the proper initialization of the status code and the
# headers is provided and the handling of the request started
start_response(status, response_headers)
# returns the content sequence to the caller method so that is
# possible to render the appropriate message to the client
return content
def error_message(self, error, code = 500):
"""
Formats the error as message and returns it so it can be
used to notify the end user.
The retrieved message is set as an undefined encoding string
and may be used with care to avoid encoding problems.
:type error: Exception
:param error: The exception object to be used in the creation
of the message string to be returned.
:type code: int
:param code: The HTTP status code that should be included in
the created message default to internal error (500).
:rtype: String
:return: The constructed error message as a string that represents
the error that was passed as an argument
"""
# retrieves the plugin manager for the current system instance
# and uses it to retrieve the current version and environment
# to be used in the construction of the identifier string
plugin_manager = self.plugin.manager
version = plugin_manager.get_version()
manager_environment = plugin_manager.get_environment()
identifier_s = IDENTIFIER_STRING % (version, manager_environment)
# verifies if the current (plugin) manager instance is
# running under the development mode
is_development = plugin_manager.is_development()
# converts the error into a string bases and then creates the
# complete error message string from the various components,
# returning the resulting value as the result
error_s = str(error)
message = "[%d] %s\n%s\n" % (code, error_s, identifier_s)
if not is_development: return message
trace = "\n".join(self.stacktrace_message())
message += "\n%s\n" % trace
return message
def stacktrace_message(self):
"""
Retrieves/yields the contents of the stack trace associated with
an exception raised under the current context.
This method returns a generator and proper usage methods should
be used to avoid miss-usage.
:rtype: Generator
:return: The sequence containing the the various values/lines of
the stack trace associated with the current context.
"""
# retrieves the complete set of lines for the exception stack trace
# and iterates over them to yield the value of them to the caller
# method (a lazy loaded approach is used to reduce usage)
lines = traceback.format_exc().splitlines()
for line in lines: yield line
class WsgiRequest(object):
"""
Represents an HTTP request to be handled
in the WSGI context, this value may be
used as a compatibility mock to the internal
HTTP request object and as such must comply
with the same interface (protocol).
"""
service = None
""" The service instance associated with the request
this should be the owner of this request and any
external object access should be done through this """
environ = {}
""" The map containing the various environment
variables defined in the WSGI specification as
the input for processing a certain request """
status_code = 200
""" The status code for the HTTP request, this
is considered to be the valid (ok) status code
by default (optimistic approach) """
content_type = None
""" The content type as a string that defined
the content (response) to be returned from this
request """
operation_type = None
""" The type of operation (HTTP method) for the
current request, this value should be capitalized
so that an uniform version is used """
path = None
""" The (url) path created by joining the joining
the prefix value and the provided path info, this
is the virtual value to be used by colony """
original_path = None
""" The original path, without unquoting resulting
from the joining from both info part of the path
and the query string, this path should reflect the
value provided by the server and not the virtual one """
uri = None
""" The "partial" domain name relative part of
the url that reference the resource """
query_string = None
""" The string containing the query part of the url
that may be parsed for arguments """
arguments = None
""" The arguments part of the query or post data if
it's an url encoded value """
multipart = None
""" The multipart string value (contents) of a message
that is meant to parsed in multiple contexts (parts) """
attributes_map = {}
""" The map containing the various attributes resulting
from the parsing of the url encoded part of the get parameters
or from the content of a post message """
headers_out = {}
""" The map that hold the set of headers to be sent
to the client (output headers) indexed by name and
associated with the value for them """
received_message = None
""" The complete linear buffer containing the set of data
sent by the client as the payload of the request """
mediated = False
""" Flag indicating if the current response is
meant to be mediated (generator based) or is a
single content buffer is used (default) """
content_type_charset = None
""" The content type charset that is going to be
used to encode the underlying message buffer, this
must contain a charset that is able to encode all
the provided data otherwise exception will be raised
by the writing methods """
max_age = None
""" The maximum age value in seconds to be used to
control the client side cache of the returned resource,
use this value carefully to avoid mismatches in cache """
etag = None
""" The etag representing the file in an unique
way to, usually through an hash function """
expiration_timestamp = None
""" The expiration timestamp for the current request
to be send for the client side cache control """
last_modified_timestamp = None
""" The last modified timestamp for the current request
to be send for the client side cache control """
message_buffer = []
""" The list containing the message buffer to
be returned as the contents for the response
associated with this request, this value is not
used in case the response is mediated"""
def __init__(
self,
service,
environ,
content_type_charset = DEFAULT_CHARSET,
prefix = None,
alias = None,
rewrite = None
):
# sets the current "owner" service of the request
# in the current request, this is going to be used
# to access external resources
self.service = service
# retrieves the "base" value from the environment
# map so that the basic request values may be constructed
# the value are used with default values
request_method = environ.get("REQUEST_METHOD", "")
script_name = environ.get("SCRIPT_NAME", "")
path_info = environ.get("PATH_INFO", "")
query_string = environ.get("QUERY_STRING", "")
content_type = environ.get("CONTENT_TYPE", "")
content_length = int(environ.get("CONTENT_LENGTH", "") or 0)
input = environ.get("wsgi.input", None)
# resolves the provided path information so that if any alias
# value matches the start of the path info it's replaced by
# the correct matching value
path_info_r = self._resolve_path(path_info, alias)
# creates the "final" path info (resolved) value by adding
# the "static" path info prefix to it, so that smaller
# uri's may be used in WSGI, in case the "extra" prefix variable
# is set an "extra" prefix is prepended to the path info
if prefix: path_info_r = PATH_INFO_PREFIX + prefix + path_info_r
else: path_info_r = PATH_INFO_PREFIX + path_info_r
# creates the complete "original" path info value by adding
# the script name (routing base value) to the path info, this
# value may be used internally as the original (path) value
path_info_o = script_name + path_info if script_name else path_info
# runs the shorten operation on the original so that the stored
# value may be shorter than the original allowing proper absolute
# resolution even under complex proxy based configurations
path_info_o = self._shorten_path(path_info_o, rewrite)
# sets the various default request values using the "calculated"
# WSGI based values as reference
self.environ = environ
self.content_type_charset = content_type_charset
self.operation_type = request_method
self.uri = path_info_r
self.path = path_info_r + "?" + query_string if query_string else path_info_r
self.original_path = path_info_o + "?" + query_string if query_string else path_info_o
# starts the map that will hold the various attributes
# resulting from the parsing of the request
self.attributes_map = colony.OrderedMap(True)
# creates the map that will hold the various headers to
# sent to the client (output headers)
self.headers_out = {}
# starts the "static" message buffer list as an empty
# list, so that further values are appended
self.message_buffer = []
# in case the input object is defined reads the complete
# set of contents from it and sets it as the received
# message (eager loading of the contents)
self.received_message = input and input.read(content_length)
# parses the get attributes so that the corresponding map
# is populated with the arguments, preserving their order
self.__parse_get_attributes__()
# in case the content type of the request is form urlencoded
# must parse and process the post attributes
if content_type.startswith("application/x-www-form-urlencoded"):
self.parse_post_attributes()
# in case the content type of the request is multipart form data
# must parse and process the post multipart
elif content_type.startswith("multipart/form-data"):
self.parse_post_multipart()
def __getattributes__(self, attribute_name, default = None):
"""
Retrieves the attribute from the attributes map.
:type attribute_name: String
:param attribute_name: The name of the attribute to retrieve.
:type default: Object
:param default: The default object value to be returned in
case the attribute is not found.
:rtype: Object
:return: The retrieved attribute.
"""
return self.attributes_map.get(attribute_name, default)
def __setattributes__(self, attribute_name, attribute_value):
"""
Sets the given attribute in the request. The referenced
attribute is the HTTP request attribute and the setting takes
into account a possible duplication of the values.
:type attribute_name: String
:param attribute_name: The name of the attribute to be set.
:type attribute_value: Object
:param attribute_value: The value of the attribute to be set.
"""
# in case the attribute name is already defined
# in the attributes map (duplicated reference), it
# requires a list structure to be used
if attribute_name in self.attributes_map:
# retrieves the attribute value reference from the attributes map
attribute_value_reference = self.attributes_map[attribute_name]
# retrieves the attribute value reference type
attribute_value_reference_type = type(attribute_value_reference)
# in case the attribute value reference type is (already)
# a list
if attribute_value_reference_type == list:
# adds the attribute value to the attribute value reference
attribute_value_reference.append(attribute_value)
# otherwise the attributes is not a list and it must be created
# for the first time
else:
# sets the list with the previously defined attribute reference
# and the attribute value
self.attributes_map[attribute_name] = [
attribute_value_reference,
attribute_value
]
# otherwise the attribute is not defined and a normal
# set must be performed (single and simple set)
else:
# sets the attribute value in the attributes map
self.attributes_map[attribute_name] = attribute_value
def __parse_get_attributes__(self):
# splits the (original) path to get the attributes path of
# the request and retrieves the length of this result
path_splitted = self.original_path.split("?")
path_splitted_length = len(path_splitted)
# in case there are no arguments to be parsed
if path_splitted_length < 2: return
# retrieves the query string from the path splitted
# and sets the arguments values as the query string
# then uses these arguments for parsing
self.query_string = path_splitted[1]
self.arguments = self.query_string
self.parse_arguments()
def finish(self):
"""
Finishes the current request running the final set
of validations in the request so that it remains
valid for the current set of standards.
This is also the method that sets the default values
in the request in case they were not set during the
normal handling workflow.
"""
if not "Cache-Control" in self.headers_out:
self.headers_out["Cache-Control"] = "no-cache, must-revalidate"
def parse_post_attributes(self):
"""
Parses the post attributes from the standard post
syntax. This call should only be made in case the
received message contains an urlencoded value.
"""
# sets the arguments as the received message
# and then uses this attribute to parse them
self.arguments = colony.legacy.str(self.received_message)
self.parse_arguments()
def parse_post_multipart(self):
"""
Parses the post multipart from the standard post
syntax. This call should only be made in case the
received message contains an multipart value.
"""
# sets the multipart as the received message
# and then uses this attribute to parse them
self.multipart = self.received_message
self.parse_multipart()
def parse_arguments(self):
"""
Parses the arguments, using the currently defined
arguments string (in the request).
The parsing of the arguments is based in the default get
arguments parsing.
"""
# retrieves the attribute fields list
attribute_fields_list = self.arguments.split("&")
# iterates over all the attribute fields
for attribute_field in attribute_fields_list:
# splits the attribute field in the equals operator
# and retrieves the length of the result for processing
attribute_field_splitted = attribute_field.split("=", 1)
attribute_field_splitted_length = len(attribute_field_splitted)
# in case the attribute field splitted length is invalid,
# must continue the loop (invalid value)
if attribute_field_splitted_length == 0 or attribute_field_splitted_length > 2:
continue
# in case the attribute field splitted length is two, this
# refers a valid (normal) key and value attribute
if attribute_field_splitted_length == 2:
# retrieves the attribute name and the attribute value,
# from the attribute field splitted, then "unquotes" the
# attribute value from the url encoding
attribute_name, attribute_value = attribute_field_splitted
attribute_value = colony.unquote_plus(attribute_value)
# in case the attribute field splitted length is one, this refers
# a valid single key attribute (with an unset value)
elif attribute_field_splitted_length == 1:
# retrieves the attribute name, from the attribute field splitted
# and sets the value as invalid (not set)
attribute_name, = attribute_field_splitted
attribute_value = None
# "unquotes" the attribute name from the url encoding and sets
# the attribute for the current name in the current instance
attribute_name = colony.unquote_plus(attribute_name)
self.__setattributes__(attribute_name, attribute_value)
def parse_multipart(self):
"""
Parses the multipart using the currently defined multipart value.
The processing of multipart is done according the standard
specifications and RFQs.
:see: http://en.wikipedia.org/wiki/MIME
"""
# retrieves the content type header
content_type = self.get_header("Content-Type")
# in case no content type is defined
if not content_type:
# raises the HTTP invalid multipart request exception
raise exceptions.WsgiRuntimeException(
"no content type defined"
)
# splits the content type and then strips the first
# value of it from any "extra" character
content_type_splitted = content_type.split(";")
content_type_value = content_type_splitted[0].strip()
# in case the content type value is not valid raises an
# exception indicating the error
if not content_type_value == "multipart/form-data":
raise exceptions.WsgiRuntimeException(
"invalid content type defined: " + content_type_value
)
# retrieves the boundary value and then splits it
# into the appropriate values (around the separator
# token for the boundary key and value)
boundary = content_type_splitted[1].strip()
boundary_splitted = boundary.split("=")
# in case the length of the boundary is not two (invalid)
# this is considered invalid and an exception is raised
if not len(boundary_splitted) == 2:
raise exceptions.WsgiRuntimeException(
"invalid boundary value: " + boundary
)
# retrieves (unpacks) the boundary reference and the boundary value
# and retrieves the length of the boundary value
_boundary, boundary_value = boundary_splitted
boundary_value_length = len(boundary_value)
# ensures that the boundary value is properly converted into the
# base bytes based representation (required for search)
boundary_value = colony.legacy.bytes(boundary_value)
# sets the initial index as the as the boundary value length
# plus the base boundary value of two (equivalent to: --)
current_index = boundary_value_length + 2
# iterates indefinitely for the parsing of the various content
# parts, and setting the attributes in the current request
while True:
# retrieves the end index (boundary start index)
end_index = self.multipart.find(boundary_value, current_index)
# in case the end index is invalid (end of multipart)
# must break the loop
if end_index == -1: break
# parses the multipart part retrieving the headers map and the contents
# the sent indexes avoid the extra newline values incrementing and decrementing
# the value of two at the end and start
headers_map, contents = self._parse_multipart_part(current_index + 2, end_index - 2)
# parses the content disposition header retrieving the content
# disposition map and list (with the attributes order) then
# sets the contents in the content disposition map and retrieves
# the name from the content disposition map
content_disposition_map = self._parse_content_disposition(headers_map)
content_disposition_map["contents"] = contents
name = content_disposition_map["name"]
# sets the attribute attribute in the current request and
# then sets the current index as the end index
self.__setattributes__(name, content_disposition_map)
current_index = end_index + boundary_value_length
def get_header(self, header_name):
# normalizes the header name into the uppercase
# and underscore based version and then adds
# the HTTP prefix so that it becomes standard to
# the WSGI specification, then uses it to retrieve
# the value from the environment map
header_name = header_name.upper()
header_name = header_name.replace("-", "_")
header_name_b = "HTTP_" + header_name
header = self.environ.get(header_name, None)
return self.environ.get(header_name_b, header)
def set_header(self, header_name, header_value, encode = True):
"""
Set a response header value on the request.
The header that is set is sent to the client after
the request handling.
:type header_name: String
:param header_name: The name of the header to be set.
:type header_value: Object
:param header_value: The value of the header to be sent
in the response.
:type encode: bool
:param encode: If the header value should be encoded in
case the type is unicode.
"""
# retrieves the header value type
header_value_type = type(header_value)
# in case the header value type is unicode
# and the encode flag is set must encode the
# header value using the current encoding
if header_value_type == colony.legacy.UNICODE and encode:
header_value = header_value.encode(self.content_type_charset)
# converts the header value string into a compatible
# string (according to all versions of python) so that
# an unicode is used instead for python 3
header_value = colony.legacy.str(header_value)
# sets the header value in the headers map so that
# any further access to the map will reflect the change
self.headers_out[header_name] = header_value
def append_header(self, header_name, header_value):
"""
Appends an header value to a response header.
This method calls the set header method in case the
header is not yet defined.
:type header_name: String
:param header_name: The name of the header to be appended
with the value.
:type header_value: Object
:param header_value: The value of the header to be appended
in the response.
"""
# in case the header is already defined
if header_name in self.headers_out:
# retrieves the current header value and then creates the
# final header value as the appending of both the current
# and the concatenation value
current_header_value = self.headers_out[header_name]
final_header_value = current_header_value + header_value
else:
# sets the final header value as the header value
final_header_value = header_value
# sets the final header value
self.set_header(header_name, final_header_value)
def read(self):
"""
Reads the complete set of data present in the current
current request, this call may bloc the control (in a
blocking service).
:rtype: String
:return: The buffer containing the full data (message body)
for the current request.
"""
return self.received_message
def write(self, message, flush = 1, encode = True):
# retrieves the message type and in case it's unicode
# it must be encoded using the currently set encoding
# then adds the resulting message into the message
# buffer to be flushed into the connection
message_type = type(message)
if message_type == colony.legacy.UNICODE and encode:
message = message.encode(self.content_type_charset)
self.message_buffer.append(message)
def execute_background(self, callable, retries = 0, timeout = 0.0, timestamp = None):
"""
Executes the given callable object in a background
thread, avoiding the blocking of the current thread.
This method is useful for avoid blocking the request
handling method in non critic tasks.
:type callable: Callable
:param callable: The callable to be called in background.
:type retries: int
:param retries: The number of times to retry executing the
callable in case exception is raised.
:type timeout: float
:param timeout: The time to be set in between calls of the
callable, used together with the retry value.
:type timestamp: float
:param timestamp: The unix second based timestamp for the
first execution of the callable.
"""
self._execute_background_thread(
callable,
retries = retries,
timeout = timeout,
timestamp = timestamp
)
def flush(self):
pass
def allow_cookies(self):
self.allow_cookies = True
def deny_cookies(self):
self.allow_cookies = False
def is_mediated(self):
return self.mediated
def is_chunked_encoded(self):
return self.chunked_encoding
def is_secure(self):
"""
Checks if the current request is being transmitted over a secure
channel, the verification is made at a connection abstraction
level (down socket verification).
:rtype: bool
:return: If the current request is being transmitted over a secure
channel (secure request).
"""
# retrieves the url scheme for the current request and returns
# the result of the comparison of it against the secure scheme
url_scheme = self.environ.get("wsgi.url_scheme", None)
return url_scheme == "https"
def get_attributes_list(self):
"""
Retrieves the list of attribute names in the
current attributes map.
:rtype: List
:return: The list of attribute names in the
current attributes map.
"""
return colony.legacy.keys(self.attributes_map)
def get_attribute(self, attribute_name, default = None):
return self.__getattributes__(attribute_name, default)
def set_attribute(self, attribute_name, attribute_value):
self.__setattributes__(attribute_name, attribute_value)
def get_operation_type(self):
return self.operation_type
def set_operation_type(self, operation_type):
self.operation_type = operation_type
def get_method(self):
"""
Retrieves the method used in the current request
object for the current request.
This method is an alias to the retrieval of the
operation type.
:rtype: String
:return: The method used in the current request
context.
"""
return self.get_operation_type()
def get_max_age(self):
return self.max_age
def set_max_age(self, max_age):
self.max_age = max_age
self.headers_out["Cache-Control"] = "max-age=%d" % max_age
def get_etag(self):
return self.etag
def set_etag(self, etag):
self.etag = etag
self.headers_out["ETag"] = etag
def get_expiration_timestamp(self):
return self.expiration_timestamp
def set_expiration_timestamp(self, expiration_timestamp):
self.expiration_timestamp = expiration_timestamp
def get_last_modified_timestamp(self):
return self.last_modified_timestamp
def set_last_modified_timestamp(self, last_modified_timestamp):
self.last_modified_timestamp = last_modified_timestamp
last_modified = datetime.datetime.fromtimestamp(self.last_modified_timestamp)
last_modified_f = last_modified.strftime("%a, %d %b %Y %H:%M:%S GMT")
self.headers_out["Last-Modified"] = last_modified_f
def verify_resource_modification(self, modified_timestamp = None, etag_value = None):
# retrieves the if modified header value and in case the
# modified timestamp and if modified header are defined
# the date time base modification check must be run
if_modified_header = self.get_header("If-Modified-Since")
if modified_timestamp and if_modified_header:
try:
# converts the if modified header value to date time and then
# converts the modified timestamp to date time
if_modified_header_data_time = datetime.datetime.strptime(
if_modified_header,
"%a, %d %b %Y %H:%M:%S GMT"
)
modified_date_time = datetime.datetime.fromtimestamp(modified_timestamp)
# in case the modified date time is less or the same
# as the if modified header date time (no modification)
# must return false as there was no modification
if modified_date_time <= if_modified_header_data_time: return False
except:
# prints a warning for not being able to check the modification date
self.service.plugin.warning("Problem while checking modification date")
# retrieves the if none match value and in case it is
# defined together with the etag value the etag based
# checking must be performed
if_none_match_header = self.get_header("If-None-Match")
if etag_value and if_none_match_header:
# in case the value of the if none match header is the same
# as the etag value of the file (no modification) must