forked from bottlepy/bottle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bottle.py
1342 lines (1118 loc) · 44.3 KB
/
bottle.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
# -*- coding: utf-8 -*-
"""
Bottle is a fast and simple micro-framework for small web applications. It
offers request dispatching (Routes) with url parameter support, templates,
a built-in HTTP Server and adapters for many third party WSGI/HTTP-server and
template engines - all in a single file and with no dependencies other than the
Python Standard Library.
Homepage and documentation: http://wiki.github.com/defnull/bottle
Licence (MIT)
-------------
Copyright (c) 2009, Marcel Hellkamp.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Example
-------
from bottle import route, run, request, response, send_file, abort
@route('/')
def hello_world():
return 'Hello World!'
@route('/hello/:name')
def hello_name(name):
return 'Hello %s!' % name
@route('/hello', method='POST')
def hello_post():
name = request.POST['name']
return 'Hello %s!' % name
@route('/static/:filename#.*#')
def static_file(filename):
send_file(filename, root='/path/to/static/files/')
run(host='localhost', port=8080)
Code Comments
-------------
#BC: Backward compatibility. This piece of code makes no sense but is here to
maintain compatibility to previous releases.
#TODO: Please help to improve this piece of code. There is something ugly
or missing here.
#REF: A reference to a bug report, article, api doc or howto. Follow the link
to read more.
"""
__author__ = 'Marcel Hellkamp'
__version__ = '0.6.4'
__license__ = 'MIT'
import types
import sys
import cgi
import mimetypes
import os
import os.path
import traceback
import re
import random
import threading
import time
import warnings
import email.utils
from wsgiref.headers import Headers as HeaderWrapper
from Cookie import SimpleCookie
import subprocess
import thread
import tempfile
import hmac
import base64
if sys.version_info >= (3,0,0):
# See Request.POST
from io import BytesIO
from io import TextIOWrapper
else:
from StringIO import StringIO as BytesIO
TextIOWrapper = None
try:
from urlparse import parse_qs
except ImportError: # pragma: no cover
from cgi import parse_qs
try:
import cPickle as pickle
except ImportError: # pragma: no cover
import pickle as pickle
try:
try:
from json import dumps as json_dumps
except ImportError: # pragma: no cover
from simplejson import dumps as json_dumps
except ImportError: # pragma: no cover
json_dumps = None
try:
from functools import partial
except ImportError: # pragma: no cover
partial = None
# Exceptions and Events
class BottleException(Exception):
""" A base class for exceptions used by bottle. """
pass
class HTTPResponse(BottleException):
""" Used to break execution and imediately finish the response """
def __init__(self, status=200, output=''):
super(BottleException, self).__init__(status, output)
self.status = int(status)
self.output = output
class HTTPError(HTTPResponse):
""" Used to generate an error page """
def __init__(self, code=500, message='Unknown Error', exception=None):
super(HTTPError, self).__init__(code, message)
self.exception = exception
def __str__(self):
return ERROR_PAGE_TEMPLATE % {
'status' : self.status,
'url' : request.path,
'error_name' : HTTP_CODES.get(self.status, 'Unknown').title(),
'error_message' : repr(self.output)
}
# Routing
# Bottle uses routes to map URLs to handler callbacks.
class RouteError(BottleException):
""" This is a base class for all routing related exceptions """
class RouteSyntaxError(RouteError):
""" The route parser found something not supported by this router """
class RouteBuildError(RouteError):
""" The route could not been build """
class Router(object):
''' A route associates a string (e.g. URL) with an object (e.g. function)
Dynamic routes use regular expressions to describe all matching strings.
Some dynamic routes may extract parts of the string and provide them as
data. This router matches a string against multiple routes and returns
the associated object along with the extracted data.
'''
syntax = re.compile(r'(?P<pre>.*?)'
r'(?P<escape>\\)?'
r':(?P<name>[a-zA-Z_]+)?'
r'(#(?P<rex>.*?)#)?')
def __init__(self):
self.static = dict()
self.dynamic = []
self.splits = dict()
def is_dynamic(self, route):
''' Returns True if the route contains dynamic syntax '''
for text, name, rex in self.itersplit(route):
if name or rex:
return True
return False
def split(self, route):
''' Splits a route into (prefix, parameter name, pattern) triples.
The prefix may be empty. The other two may be None. '''
return list(self.itersplit(route))
def itersplit(self, route):
''' Same as Router.split() but returns an iterator. '''
match = None
for match in self.syntax.finditer(route):
pre = match.group('pre')
name = match.group('name')
rex = match.group('rex')
if match.group('escape'):
yield match.group(0).replace('\\:',':',1), None, None
continue
if rex:
rex = re.sub(r'\(\?P<[^>]*>', '(?:', rex)
rex = re.sub(r'\((?!\?)', '(?:', rex)
try:
rex = re.compile(rex)
except re.error, e:
raise RouteSyntaxError("Syntax error in '%s' offset %d: %s"
% (route, match.start('rex'), repr(e)))
if rex.groups > 1: # Should not be possible.
raise RouteSyntaxError("Groups in route '%s'." % (route))
yield pre, name, rex
if not match:
yield route, None, None
elif match.end() < len(route):
yield route[match.end():], None, None
def parse(self, route):
''' Parses a route and returns a tuple. The first element is a
RegexObject with named groups. The second is a non-grouping version
of that RegexObject. '''
rexp = ''
fexp = ''
isdyn = False
for text, name, rex in self.itersplit(route):
rexp += re.escape(text)
fexp += re.escape(text)
if name and rex:
rexp += '(?P<%s>%s)' % (name, rex.pattern)
fexp += '(?:%s)' % rex.pattern
elif name:
rexp += '(?P<%s>[^/]+)' % name
fexp += '[^/]+'
elif rex:
rexp += '(?:%s)' % rex.pattern
fexp += '(?:%s)' % rex.pattern
return re.compile('%s' % rexp), re.compile('%s' % fexp)
def add(self, route, data, static=False, name=None):
''' Adds a route to the router. Syntax:
`:name` matches everything up to the next slash.
`:name#regexp#` matches a regular expression.
`:#regexp#` creates an anonymous match.
A backslash can be used to escape the `:` character.
'''
if not self.is_dynamic(route) or static:
self.static[route] = data
return
rexp, fexp = self.parse(route)
rexp = re.compile('^(%s)$' % rexp.pattern)
if not rexp.groupindex:
rexp = None # No named groups -> Nothing to extract
if fexp.groups: # Should not be possible.
raise RouteSyntaxError("Route contains groups '%s'." % (route))
try:
big_re, subroutes = self.dynamic[-1]
big_re = '%s|(^%s$)' % (big_re.pattern, fexp.pattern)
big_re = re.compile(big_re)
subroutes.append((data, rexp))
self.dynamic[-1] = (big_re, subroutes)
except (AssertionError, IndexError), e: # AssertionError: To many groups
self.dynamic.append((re.compile('(^%s$)' % fexp.pattern),
[(data, rexp)]))
if name:
self.splits[name] = self.split(route)
def match(self, uri):
''' Matches an URL and returns a (handler, data) tuple '''
if uri in self.static:
return self.static[uri], None
for big_re, subroutes in self.dynamic:
match = big_re.match(uri)
if match:
data, group_re = subroutes[match.lastindex - 1]
if not group_re:
return data, None
group_match = group_re.match(uri)
if not group_match:
return None, None
return data, group_match.groupdict()
return None, None
def build(self, route_name, **args):
''' Builds an URL out of a named route and some parameters.'''
if route_name not in self.splits:
raise RouteBuildError("No route found with name '%s'." % route_name)
out = []
for text, key, rex in self.splits[route_name]:
out.append(text)
if key and key not in args:
raise RouteBuildError("Missing parameter '%s' in route '%s'"
% (key, route_name))
if rex and not key:
raise RouteBuildError("Anonymous pattern found. Can't "
"generate the route '%s'." % route_name)
#TODO: Do this in add()
if rex and not re.match('^%s$' % rex.pattern, args[key]):
raise RouteBuildError("Parameter '%s' does not match pattern "
"for route '%s': '%s'" % (key, route_name, rex.pattern))
if key:
out.append(args[key])
return ''.join(out)
# WSGI abstraction: Request and response management
class Bottle(object):
def __init__(self, catchall=True, autojson=True, path = ''):
self.routes = dict()
self.default_route = None
self.error_handler = {}
if autojson and json_dumps:
self.jsondump = json_dumps
else:
self.jsondump = False
self.catchall = catchall
self.config = dict()
self.serve = True
self.rootpath = path
def match_url(self, path, method='GET'):
""" Find a callback bound to a path and a specific method.
Return (callback, param) tuple or (None, None).
method=HEAD falls back to GET. method=GET fall back to ALL.
"""
if not path.startswith(self.rootpath):
return None, None
path = path[len(self.rootpath):].strip().lstrip('/')
method = method.upper()
handler, param = self.routes.setdefault(method, Router()).match(path)
if not handler and method == 'HEAD':
handler, param = self.routes.setdefault('GET', Router()).match(path)
if not handler:
handler, param = self.routes.setdefault('ALL', Router()).match(path)
if not handler:
handler, param = self.default_route, None
return handler, param
def route(self, url, method='GET', **kargs):
"""
Decorator for request handler.
"""
path = url.strip().lstrip('/')
method = method.upper()
def wrapper(handler):
self.routes.setdefault(method, Router())\
.add(path, handler, **kargs)
return handler
return wrapper
def default(self):
""" Decorator for request handler. Same as add_defroute( handler )."""
def wrapper(handler):
self.default_route = handler
return handler
return wrapper
def error(self, code=500):
"""
Decorator for error handler.
"""
def wrapper(handler):
self.error_handler[int(code)] = handler
return handler
return wrapper
def handle(self, url, method, catchall=True):
""" Run the matching handler. Return handler output, HTTPResponse or
HTTPError. If catchall is true, all exceptions thrown within a
handler function are converted to HTTPError(500). """
if not self.serve:
return HTTPError(503, "Server stopped")
handler, args = self.match_url(request.path, request.method)
if not handler:
return HTTPError(404, "Not found")
if not args:
args = dict()
try:
return handler(**args)
except HTTPResponse, e:
return e
except (KeyboardInterrupt, SystemExit, MemoryError):
raise
except Exception, e:
if not self.catchall:
raise
err = "Unhandled Exception: %s\n" % (repr(e))
if DEBUG:
err += '\n\nTraceback:\n' + traceback.format_exc(10)
request.environ['wsgi.errors'].write(err)
return HTTPError(500, err, e)
def cast(self, out):
""" Try to cast the input into something WSGI compatible. Correct
HTTP headers and status codes when possible. Empty output on HEAD
requests.
Support: False, str, unicode, list(unicode), file, dict, list(dict),
HTTPResponse and HTTPError
"""
if isinstance(out, HTTPResponse):
response.status = out.status
if isinstance(out, HTTPError):
out = self.error_handler.get(out.status, str)(out)
else:
out = out.output
if not out:
response.header['Content-Length'] = '0'
return []
if isinstance(out, types.StringType):
out = [out]
elif isinstance(out, unicode):
out = [out.encode(response.charset)]
elif isinstance(out, list) and isinstance(out[0], unicode):
out = map(lambda x: x.encode(response.charset), out)
elif hasattr(out, 'read'):
out = request.environ.get('wsgi.file_wrapper',
lambda x: iter(lambda: x.read(8192), ''))(out)
elif self.jsondump and isinstance(out, dict)\
or self.jsondump and isinstance(out, list) and isinstance(out[0], dict):
out = [self.jsondump(out)]
response.content_type = 'application/json'
if isinstance(out, list) and len(out) == 1:
response.header['Content-Length'] = str(len(out[0]))
if response.status in (100, 101, 204, 304) or request.method == 'HEAD':
out = [] # rfc2616 section 4.3
if not hasattr(out, '__iter__'):
raise TypeError('Request handler for route "%s" returned [%s] '
'which is not iterable.' % (request.path, type(out).__name__))
return out
def __call__(self, environ, start_response):
""" The bottle WSGI-interface. """
try:
request.bind(environ, self)
response.bind(self)
out = self.handle(request.path, request.method)
out = self.cast(out)
status = '%d %s' % (response.status, HTTP_CODES[response.status])
start_response(status, response.wsgiheaders())
return out
except (KeyboardInterrupt, SystemExit, MemoryError):
raise
except Exception, e:
if not self.catchall:
raise
err = "<h1>Critial error while processing request: %s</h1>" %\
environ.get('PATH_INFO', '/')
if DEBUG:
err += '<h2>Error:</h2>\n<pre>%s</pre>\n'\
'<h2>Traceback:</h2>\n<pre>%s</pre>\n' %\
(repr(e), traceback.format_exc(10))
environ['wsgi.errors'].write(err)
start_response('500 INTERNAL SERVER ERROR', [])
return [err]
class Request(threading.local):
""" Represents a single request using thread-local namespace. """
def bind(self, environ, app):
"""
Binds the enviroment of the current request to this request handler
"""
self.environ = environ
self._GET = None
self._POST = None
self._GETPOST = None
self._COOKIES = None
self._body = None
self.path = self.environ.get('PATH_INFO', '/').strip()
self.app = app
if not self.path.startswith('/'):
self.path = '/' + self.path
@property
def method(self):
""" Get the request method (GET,POST,PUT,DELETE,...) """
return self.environ.get('REQUEST_METHOD', 'GET').upper()
@property
def query_string(self):
""" Get content of QUERY_STRING """
return self.environ.get('QUERY_STRING', '')
@property
def input_length(self):
""" Get content of CONTENT_LENGTH """
try:
return max(0,int(self.environ.get('CONTENT_LENGTH', '0')))
except ValueError:
return 0
@property
def GET(self):
""" Get a dict with GET parameters. """
if self._GET is None:
data = parse_qs(self.query_string, keep_blank_values=True)
self._GET = {}
for key, value in data.iteritems():
if len(value) == 1:
self._GET[key] = value[0]
else:
self._GET[key] = value
return self._GET
@property
def POST(self):
""" Get a dict with parsed POST or PUT data. """
if self._POST is None:
qs_backup = self.environ.get('QUERY_STRING','')
self.environ['QUERY_STRING'] = ''
if TextIOWrapper:
fb = TextIOWrapper(self.body, encoding='ISO-8859-1')
else:
fb = self.body
data = cgi.FieldStorage(fp=fb,
environ=self.environ, keep_blank_values=True)
self.environ['QUERY_STRING'] = qs_backup
self._POST = {}
for item in data.list:
name = item.name
if not item.filename:
item = item.value
self._POST.setdefault(name, []).append(item)
for key in self._POST:
if len(self._POST[key]) == 1:
self._POST[key] = self._POST[key][0]
return self._POST
@property
def params(self):
""" Returns a mix of GET and POST data. POST overwrites GET """
if self._GETPOST is None:
self._GETPOST = dict(self.GET)
self._GETPOST.update(dict(self.POST))
return self._GETPOST
@property
def body(self):
if not self._body:
maxread = self.input_length
if maxread < 1024*100: #TODO Should not be hard coded...
self._body = BytesIO()
else:
self._body = tempfile.TemporaryFile(mode='w+b')
while maxread > 0:
part = self.environ['wsgi.input'].read(min(maxread, 8192))
if not part: #TODO: Wrong content_length. Error? Do nothing?
break
self._body.write(part)
maxread -= len(part)
self.environ['wsgi.input'] = self._body
self._body.seek(0)
return self._body
@property
def auth(self): #TODO: Tests and docs. Add support for digest. namedtuple?
return parse_auth(self.environ.get('HTTP_AUTHORIZATION'))
@property
def COOKIES(self):
""" Returns a dict with COOKIES. """
if self._COOKIES is None:
raw_dict = SimpleCookie(self.environ.get('HTTP_COOKIE',''))
self._COOKIES = {}
for cookie in raw_dict.itervalues():
self._COOKIES[cookie.key] = cookie.value
return self._COOKIES
def get_cookie(self, *args):
value = self.COOKIES.get(*args)
sec = self.app.config['securecookie.key']
dec = cookie_decode(value, sec)
return dec or value
class Response(threading.local):
""" Represents a single response using thread-local namespace. """
def bind(self, app):
""" Clears old data and creates a brand new Response object """
self._COOKIES = None
self.status = 200
self.header_list = []
self.header = HeaderWrapper(self.header_list)
self.charset = 'UTF-8'
self.content_type = 'text/html; charset=UTF-8'
self.error = None
self.app = app
def add_header(self, key, value):
self.header.add_header(key.title(), str(value))
def wsgiheaders(self):
''' Returns a wsgi conform list of header/value pairs '''
for c in self.COOKIES.values():
self.add_header('Set-Cookie', c.OutputString())
return self.header_list
@property
def COOKIES(self):
if not self._COOKIES:
self._COOKIES = SimpleCookie()
return self._COOKIES
def set_cookie(self, key, value, **kargs):
"""
Sets a Cookie. Optional settings:
expires, path, comment, domain, max-age, secure, version, httponly
"""
if not isinstance(value, basestring):
sec = self.app.config['securecookie.key']
value = cookie_encode(value, sec)
self.COOKIES[key] = value
for k, v in kargs.iteritems():
self.COOKIES[key][k] = v
def get_content_type(self):
""" Get the current 'Content-Type' header. """
return self.header['Content-Type']
def set_content_type(self, value):
if 'charset=' in value:
self.charset = value.split('charset=')[-1].split(';')[0].strip()
self.header['Content-Type'] = value
content_type = property(get_content_type, set_content_type, None,
get_content_type.__doc__)
class BaseController(object):
_singleton = None
def __new__(cls, *a, **k):
if not cls._singleton:
cls._singleton = object.__new__(cls, *a, **k)
return cls._singleton
_default_app = Bottle()
def app(newapp = None):
"""
Returns the current default app or sets a new one.
Defaults to an instance of Bottle
"""
global _default_app
if newapp:
_default_app = newapp
return _default_app
default_app = app # BC: 0.6.4
def abort(code=500, text='Unknown Error: Appliction stopped.'):
""" Aborts execution and causes a HTTP error. """
raise HTTPError(code, text)
def redirect(url, code=307):
""" Aborts execution and causes a 307 redirect """
response.header['Location'] = url
raise HTTPError(code, "")
def send_file(*a, **k): #BC 0.6.4
""" Raises the output of static_file() """
raise static_file(*a, **k)
def static_file(filename, root, guessmime = True, mimetype = None, download = False):
""" Opens a file in a save way and returns a HTTPError object with status
code 200, 305, 401 or 404. Sets Content-Type, Content-Length and
Last-Modified header. Obeys If-Modified-Since header and HEAD requests.
"""
root = os.path.abspath(root) + os.sep
filename = os.path.abspath(os.path.join(root, filename.strip('/\\')))
if not filename.startswith(root):
return HTTPError(401, "Access denied.")
if not os.path.exists(filename) or not os.path.isfile(filename):
return HTTPError(404, "File does not exist.")
if not os.access(filename, os.R_OK):
return HTTPError(401, "You do not have permission to access this file.")
if guessmime and not mimetype:
mimetype = mimetypes.guess_type(filename)[0]
if not mimetype: mimetype = 'text/plain'
response.content_type = mimetype
if download == True:
request.header['Content-Disposition'] = 'attachment; filename=%s' %\
os.path.basename(filename)
elif download:
request.header['Content-Disposition'] = 'attachment; filename=%s' %\
download
stats = os.stat(filename)
if 'Last-Modified' not in response.header:
lm = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(stats.st_mtime))
response.header['Last-Modified'] = lm
if 'HTTP_IF_MODIFIED_SINCE' in request.environ:
ims = request.environ['HTTP_IF_MODIFIED_SINCE']
# IE sends "<date>; length=146"
ims = ims.split(";")[0].strip()
ims = parse_date(ims)
if ims is not None and ims >= stats.st_mtime:
return HTTPError(304, "Not modified")
if 'Content-Length' not in response.header:
response.header['Content-Length'] = str(stats.st_size)
if request.method == 'HEAD':
return HTTPResponse(200, '')
else:
return HTTPResponse(200, open(filename, 'rb'))
def parse_date(ims):
"""
Parses date strings usually found in HTTP header and returns UTC epoch.
Understands rfc1123, rfc850 and asctime.
"""
try:
ts = email.utils.parsedate_tz(ims)
if ts is not None:
if ts[9] is None:
return time.mktime(ts[:8] + (0,)) - time.timezone
else:
return time.mktime(ts[:8] + (0,)) - ts[9] - time.timezone
except (ValueError, IndexError):
return None
def parse_auth(header):
try:
method, data = header.split(None, 1)
if method.lower() == 'basic':
name, pwd = base64.b64decode(data).split(':', 1)
return name, pwd
except (KeyError, ValueError, TypeError), a:
return None
def cookie_encode(data, key):
''' Encode and sign a pickle-able object. Return a string '''
msg = base64.b64encode(pickle.dumps(data, -1))
sig = base64.b64encode(hmac.new(key, msg).digest())
return '!%s?%s' % (sig, msg)
def cookie_decode(data, key):
''' Verify and decode an encoded string. Return an object or None'''
if cookie_is_encoded(data):
sig, msg = data.split('?',1)
if sig[1:] == base64.b64encode(hmac.new(key, msg).digest()):
return pickle.loads(base64.b64decode(msg))
return None
def cookie_is_encoded(data):
''' Verify and decode an encoded string. Return an object or None'''
return bool(data.startswith('!') and '?' in data)
# Decorators
def validate(**vkargs):
"""
Validates and manipulates keyword arguments by user defined callables.
Handles ValueError and missing arguments by raising HTTPError(403).
"""
def decorator(func):
def wrapper(**kargs):
for key, value in vkargs.iteritems():
if key not in kargs:
abort(403, 'Missing parameter: %s' % key)
try:
kargs[key] = value(kargs[key])
except ValueError, e:
abort(403, 'Wrong parameter format for: %s' % key)
return func(**kargs)
return wrapper
return decorator
def route(url, **kargs):
"""
Decorator for request handler. Same as add_route(url, handler, **kargs).
"""
return default_app().route(url, **kargs)
if partial:
get = partial(route, method='GET')
head = partial(route, method='HEAD')
post = partial(route, method='POST')
put = partial(route, method='PUT')
delete = partial(route, method='DELETE')
route_all = partial(route, method='ALL')
def default():
"""
Decorator for request handler. Same as set_default(handler).
"""
return default_app().default()
def error(code=500):
"""
Decorator for error handler. Same as set_error_handler(code, handler).
"""
return default_app().error(code)
# Server adapter
class WSGIAdapter(object):
def run(self, handler): # pragma: no cover
pass
def __repr__(self):
return "%s()" % (self.__class__.__name__)
class CGIServer(WSGIAdapter):
def run(self, handler):
from wsgiref.handlers import CGIHandler
CGIHandler().run(handler)
class ServerAdapter(WSGIAdapter):
def __init__(self, host='127.0.0.1', port=8080, **kargs):
WSGIAdapter.__init__(self)
self.host = host
self.port = int(port)
self.options = kargs
def __repr__(self):
return "%s (%s:%d)" % (self.__class__.__name__, self.host, self.port)
class WSGIRefServer(ServerAdapter):
def run(self, handler):
from wsgiref.simple_server import make_server
srv = make_server(self.host, self.port, handler)
srv.serve_forever()
class CherryPyServer(ServerAdapter):
def run(self, handler):
from cherrypy import wsgiserver
server = wsgiserver.CherryPyWSGIServer((self.host, self.port), handler)
server.start()
class FlupServer(ServerAdapter):
def run(self, handler):
from flup.server.fcgi import WSGIServer
WSGIServer(handler, bindAddress=(self.host, self.port)).run()
class PasteServer(ServerAdapter):
def run(self, handler):
from paste import httpserver
from paste.translogger import TransLogger
app = TransLogger(handler)
httpserver.serve(app, host=self.host, port=str(self.port), **self.options)
class FapwsServer(ServerAdapter):
"""
Extremly fast webserver using libev.
See http://william-os4y.livejournal.com/
Experimental ...
"""
def run(self, handler):
import fapws._evwsgi as evwsgi
from fapws import base
evwsgi.start(self.host, self.port)
evwsgi.set_base_module(base)
def app(environ, start_response):
environ['wsgi.multiprocess'] = False
return handler(environ, start_response)
evwsgi.wsgi_cb(('',app))
evwsgi.run()
def run(app=None, server=WSGIRefServer, host='127.0.0.1', port=8080,
interval=1, reloader=False, **kargs):
""" Runs bottle as a web server. """
if not app:
app = default_app()
quiet = bool(kargs.get('quiet', False))
# Instantiate server, if it is a class instead of an instance
if isinstance(server, type):
if issubclass(server, CGIServer):
server = server()
elif issubclass(server, ServerAdapter):
server = server(host=host, port=port, **kargs)
if not isinstance(server, WSGIAdapter):
raise RuntimeError("Server must be a subclass of WSGIAdapter")
if not quiet and isinstance(server, ServerAdapter): # pragma: no cover
if not reloader or os.environ.get('BOTTLE_CHILD') == 'true':
print "Bottle server starting up (using %s)..." % repr(server)
print "Listening on http://%s:%d/" % (server.host, server.port)
print "Use Ctrl-C to quit."
print
else:
print "Bottle auto reloader starting up..."
try:
if reloader and interval:
reloader_run(server, app, interval)
else:
server.run(app)
except KeyboardInterrupt:
if not quiet: # pragma: no cover
print "Shutting Down..."
#TODO: If the parent process is killed (with SIGTERM) the childs survive...
def reloader_run(server, app, interval):
if os.environ.get('BOTTLE_CHILD') == 'true':
# We are a child process
files = dict()
for module in sys.modules.values():
file_path = getattr(module, '__file__', None)
if file_path and os.path.isfile(file_path):
file_split = os.path.splitext(file_path)
if file_split[1] in ('.py', '.pyc', '.pyo'):
file_path = file_split[0] + '.py'
files[file_path] = os.stat(file_path).st_mtime
thread.start_new_thread(server.run, (app,))
while True:
time.sleep(interval)
for file_path, file_mtime in files.iteritems():
if not os.path.exists(file_path):
print "File changed: %s (deleted)" % file_path
elif os.stat(file_path).st_mtime > file_mtime:
print "File changed: %s (modified)" % file_path
else: continue
print "Restarting..."
app.serve = False
time.sleep(interval) # be nice and wait for running requests
sys.exit(3)
while True:
args = [sys.executable] + sys.argv
environ = os.environ.copy()
environ['BOTTLE_CHILD'] = 'true'
exit_status = subprocess.call(args, env=environ)
if exit_status != 3:
sys.exit(exit_status)
# Templates
class TemplateError(HTTPError):
def __init__(self, message):
HTTPError.__init__(self, 500, message)
class BaseTemplate(object):
def __init__(self, template='', name=None, filename=None, lookup=[]):
"""