summaryrefslogtreecommitdiffstats
path: root/lib/python/web_support.py
blob: c320ff033c48d86fceb426c4ae5c826ca331f5c5 (plain) (blame)
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
# web_support.py -- simple HTTP generation framework
# Copyright (C) 2005 Florian Weimer <fw@deneb.enyo.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import cgi
import cStringIO
import os
import re
import socket
import struct
import sys
import grp
import traceback
import types
import urllib
import threading
import SocketServer
import BaseHTTPServer

class ServinvokeError(Exception):
    pass

class Service:
    """A class for service objects.

    Service objects are contacted by the program servinvoke and
    process HTTP requests in a serialized fashion.  (Only the data
    transfer from and to the client happens in parallel, and this is
    handled by the servinvoke program.)

    If the newly created socket is owned by the www-data group, it is
    automatically made readable by that group.
    """

    def __init__(self, socket_name):
        self.socket_name = socket_name
        self._unlinkSocket()
        self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
        self.socket.bind(self.socket_name)
        self.socket.listen(5)
        self._chmod()

    def __del__(self):
        self._unlinkSocket()

    def _unlinkSocket(self):
        try:
            os.unlink(self.socket_name)
        except OSError:
            pass

    def _chmod(self):
        gid = os.stat(self.socket_name).st_gid
        grpent = grp.getgrgid(gid)
        if grpent[0] == 'www-data':
            os.chmod(self.socket_name, 0660)

    def log(self, msg, *args):
        sys.stderr.write((msg % args) + "\n")

    def run(self):
        while 1:
            (client, addr) = self.socket.accept()

            def read(count):
                data = ''
                cnt = 0
                while cnt != count:
                    d = client.recv(count - cnt)
                    if d:
                        data += d
                        cnt = len(data)
                    else:
                        self.log("unexpected end of data from servinvoke")
                        raise ServinvokeError()

                return data

            try:
                header = read(24)
                (magic, version, cli_size, cli_count, env_size, env_count) = \
                        struct.unpack("!6I", header)
                if magic != 0x15fd34df:
                    sys.log("unknown magic number %08X", magic)
                if version != 1:
                    sys.log("unknown version %08X", magic)
                cli = read(cli_size).split('\0')[:-1]
                env = {}
                for x in read(env_size).split('\0')[:-1]:
                    (key, value) = x.split('=', 1)
                    env[key] = value
                data = []
                while 1:
                    d = client.recv(4096)
                    if d:
                        data.append(d)
                    else:
                        break
                data = ''.join(data)
                result = cStringIO.StringIO()
                self.handle(cli, env, data, result)
                client.sendall(result.getvalue())
                client.close()

            except ServinvokeError:
                client.close()
                pass
            except KeyboardInterrupt:
                client.close()
                raise
            except:
                client.close()
                target = cStringIO.StringIO()
                traceback.print_exc(None, target)
                self.log("%s", target.getvalue())

        def handle(args, environ, data):
            """Invoke by run to handle a single request.  Should
            return the data to be sent back to the client."""
            return ""

class URL:
    """A simple wrapper class for strings which are interpreted as URLs."""
    def __init__(self, url):
        self.__url = url
    def __str__(self):
        return self.__url
    def __repr__(self):
        return "URL(%s)" % `self.__url`

class URLFactory:
    """Creates URL objects.

    This factory class handles the case where a script wants to
    generate URLs which reference to itself (see scriptRelative)."""

    def __init__(self, server_name, script_name, path_info='',
                 params={}, secure=False):
        self.server_name = server_name or 'localhost'
        script_name = self._stripSlashes(script_name or '')
        if script_name[-1:] == '/' or script_name == '':
            self.script_name = script_name
        else:
            self.script_name = script_name + '/'
        self.path_info = self._stripSlashes(path_info)
        self.params = params
        self.secure = secure

    def _convertArgs(self, args):
        arglist = []
        for (key, value) in args.items():
            if value is None:
                continue
            if type(value) not in (types.ListType, types.TupleType):
                value = (value,)
            for v in value:
                arglist.append("%s=%s" % (urllib.quote(key),
                                          urllib.quote(v)))
        if arglist:
            return "?" + '&'.join(arglist)
        else:
            return ""
    def _stripSlashes(self, arg):
        while arg[:1] == '/':
            arg = arg[1:]
        return arg

    def absolute(self, url, **args):
        """Creates an absolute URL, with optional arguments to pass."""

        return URL(url + self._convertArgs(args))

    def absoluteDict(self, url, args):
        """Creates an absolute URL, with arguments to pass."""

        return URL(url + self._convertArgs(args))

    def scriptRelative(self, path, **args):
        """Returns a URL which references to the path relative to the
        current script.  Optionally, arguments to pass can be included."""

        return URL("/%s%s%s" % (self.script_name,
                                self._stripSlashes(path),
                                self._convertArgs(args)))

    def scriptRelativeFull(self, path, **args):
        """Like scriptRelative, but returns an absolute URL, including
        the http:// prefix."""
        if self.secure:
            schema = "https"
        else:
            schema = "http"
        return URL("%s://%s/%s%s%s" % (schema,
                                       self.server_name, self.script_name,
                                       self._stripSlashes(path),
                                       self._convertArgs(args)))

    def updateParamsDict(self, args):
        new_args = {}
        for (key, value) in self.params.items():
            new_args[key] = value
        for (key, value) in args.items():
            new_args[key] = value
        return URL("/%s%s%s" % (self.script_name, self.path_info,
                                self._convertArgs(new_args)))

    def updateParams(self, **args):
        self.updateParamsDict(args)

charToHTML = map(chr, range(256))
charToHTMLattr = map(chr, range(256))
def _initStringToHTML(s):
    for (ch, repl) in (('<', '&lt;'),
                       ('>', '&gt;'),
                       ('&', '&amp;')):
        s[ord(ch)] = repl
_initStringToHTML(charToHTML)
_initStringToHTML(charToHTMLattr)
charToHTMLattr[ord('"')] = '&34;'
del _initStringToHTML

def escapeHTML(str):
    '''Replaces the characters <>&" in the passed strings with their
    HTML entities.'''

    result = []
    append = result.append
    for ch in str:
        append(charToHTML[ord(ch)])
    return ''.join(result)

class HTMLBase:
    def flatten(self, write):
        """Invokes write repeatedly, for the tag and its contents.

        Note that typically, a lot of very short strings a written, so
        it's better to add some buffering before sending the strings
        elsewhere."""
        pass

    def toString(self):
        """Invokes flatten to create a new string object."""
        r = cStringIO.StringIO()
        self.flatten(r.write)
        return r.getvalue()

    def toHTML(self):
        return VerbatimHTML(self.toString())

class VerbatimHTML(HTMLBase):
    """Creates verbatim HTML from a string object.  Mainly used for
    optimizing recurring HTML snippets."""

    def __init__(self, contents):
        self.__contents = contents

    def flatten(self, write):
        write(self.__contents)

_string_types = (types.StringType, types.UnicodeType)

class Compose(HTMLBase):
    """Glues a sequence of HTML snippets together, without enclosing it in
    a tag."""
    def __init__(self, contents):
        self.__contents = contents

    def flatten(self, write):
        for x in self.__contents:
            if type(x) in _string_types:
                write(escapeHTML(x))
            else:
                x.flatten(write)

def compose(*contents):
    """Concatenates several HTML objects."""
    return Compose(contents)


class Tag(HTMLBase):
    """Base class for HTML tags."""

    re_name = re.compile(r'\A_?[a-zA-Z][a-zA-Z0-9]*\Z')

    def __init__(self, name, contents, attribs={}):
        self._check(name)
        self.__name = name
        attrs = []
        append = attrs.append
        for (key, value) in attribs.items():
            if value is None:
                continue
            self._check(key)
            append(' ')
            if key[0] == '_':
                append(key[1:])
            else:
                append(key)
            append('="')
            for ch in str(value):
                append(charToHTMLattr[ord(ch)])
            append('"')
        self.__attribs = ''.join(attrs)
        self.contents = contents

    def _check(self, name):
        if self.re_name.match(name):
            return
        else:
            raise ValueError("invalid name: " + repr(name))

    def flatten(self, write):
        if self.contents:
            write("<%s%s>" % (self.__name, self.__attribs))
            closing = "</%s>" % self.__name
            try:
                for x in self.contents:
                    if type(x) in _string_types:
                        write(escapeHTML(x))
                    else:
                        x.flatten(write)
            except:
                # If we encountered any exception, try to write the
                # closing tag nevertheless.  This increases our
                # chances that we produce valid XML.
                try:
                    write(closing)
                except:
                    pass
                raise
            write(closing)

        else:
            write("<%s%s/>" % (self.__name, self.__attribs))

    def __repr__(self):
        return "<websupport.Tag instance, name=%s>" % `self.__name`

    def toString(self):
        r = cStringIO.StringIO()
        self.flatten(r.write)
        return r.getvalue()

def tag(__name, __contents, **__attribs):
    """Creates a new tag object.

    name - name of the tag
    contents - a sequence objet (or iterator) for the enclosed contents
    attribs - keyword arguments forming attributes
    """
    return Tag(__name, __contents, __attribs)

def emptyTag(__name, **__attribs):
    """A tag without contents.

    name - name of the tag
    attribs - keyword arguments forming attributes
    """
    return Tag(__name, None, __attribs)

def A(url, text=None):
    if text is None:
        text = url
    return tag('a', text, href=str(url))
def STYLE(contents, type='text/css'):
    return tag('style', contents, type=type)
def SCRIPT(contents, type="text/javascript", src=""):
    return tag('script', contents, type=type, src=src)
def LINK(contents, type="text/css", rel="stylesheet", href=""):
    return tag('link', contents, type=type, rel=rel, href=href)
def TITLE(contents):
    return tag('title', contents)
def HTML(head, body):
    return tag('html', (HEAD(head), BODY(body)))
def HEAD(contents):
    return tag('head', contents)
def BODY(contents, onload=None):
    return tag('body', contents, onload=onload)
def H1(contents):
    return tag('h1', contents)
def H2(contents):
    return tag('h2', contents)
def H3(contents):
    return tag('h3', contents)
def P(*contents):
    return Tag('p', contents)
def SPAN(*__contents, **__attribs):
    return Tag('span', __contents, __attribs)
def HR():
    return tag('hr', ())
def BR():
    return tag('br', ())
def CODE(*contents):
    return tag('code', contents)
def EM(*contents):
    return tag('em', contents)
def B(contents):
    return tag('b', contents)
def TABLE(contents):
    return tag('table', contents)
def TR(*contents):
    return tag('tr', contents)
def TH(*contents):
    return tag('th', contents)
def TD(*contents):
    return tag('td', contents)
def FORM(*__contents, **__attribs):
    return Tag('form', __contents, __attribs)
def LABEL(*__contents, **__attribs):
    return Tag('label', __contents, __attribs)
def INPUT(*__contents, **__attribs):
    return Tag('input', __contents, __attribs)
def UL(contents):
    return tag('ul', contents)
def LI(*__contents, **__attribs):
    return Tag('li', __contents, __attribs)
def HEADER(*__contents, **__attribs):
    return Tag('header', __contents, __attribs)
def FOOTER(*__contents, **__attribs):
    return Tag('footer', __contents, __attribs)
def NAV(*__contents, **__attribs):
    return Tag('nav', __contents, __attribs)

def _linkify(match):
    extra = match.group(2)
    if extra is None:
        extra = ""
    link  = match.group(1)
    return "%s%s" % (A(link).toString(), extra)

def linkify(contents):
    contents = re.sub(r'(httpS?://[\w.-]+/.*?)([,\s]|$)', _linkify, contents)
    return contents

def make_table(contents, title=None, caption=None, replacement=None, introduction=None):
    rows = []
    for row in contents:
        cols = []
        if caption and not rows:
            for col in caption:
                cols.append(TH(col))
            rows.append(Tag('tr', cols))
            cols = []

        for col in row:
            cols.append(TD(col))
        rows.append(Tag('tr', cols))
    if rows:
	if not introduction:
            introduction=''
        if not title:
            title=''
        return compose(title, introduction, TABLE(rows))
    else:
        return compose()

def make_pre(lines):
    """Creates a pre-formatted text area."""
    pre = []
    append = pre.append
    for line in lines:
        # turn https:// and http:// into links
        results=re.search("(.*)(?P<url>https?://[^\s]+)(.*)", line)
        if results:
            for group in results.groups():
                if group.startswith('http://') or group.startswith('https://'):
                    append(A(group))
                else:
                    append(group)
        else:
            append(tag("SPAN",line))
        append(BR())
    return tag('pre', pre)

def make_menu(convert, *entries):
    """Creates an unnumbered list of hyperlinks.
    Each entry can be:

    - a pair (URL, LABEL).
      convert(URL) is used as the link, and LABEL as the link text.
    - some non-tuple value.
      This is added as an individual item.
    """
    ul = []
    append = ul.append
    for e in entries:
        if type(e) == types.TupleType:
            (relurl, label) = e
            append(LI(A(convert(relurl), label)))
        else:
            append(LI(e))
    return tag('ul', ul)

def make_numbered_list(entries):
    """Creates a numbered list.
    ENTRIES should be a sequence of P objects."""
    ol = []
    append = ol.append
    for e in entries:
        append(LI(e))
    return tag('ol', ol)

def make_list(lst, separator=", "):
    """Creates a list of HTML elements."""
    assert type(lst) != types.StringType
    c = []
    if lst:
        append = c.append
        for e in lst[:-1]:
            append(e)
            append(separator)
        append(lst[-1])
    return Compose(c)

class InvalidPath(Exception):
    """An unknown path was submitted to PathRouter.get"""

class PathRouter:
    """This class maps paths to registered values."""

    def __init__(self):
        self.__map = {}

    def register(self, path, value):
        """Registers the indicated value for the path.

        Path may end with '*' or '**', indicating single-level
        wildcards or multi-level wildcards."""

        m = self.__map
        p = path.split('/')
        if p and not p[0]:
            del p[0]
        for x in range(len(p)):
            element = p[x]
            if element:
                if element in m:
                    m = m[element]
                else:
                    if element == '*':
                        if x + 1 != len(p):
                            raise ValueError('wildcard * in the middle of path')
                        m['*'] = value
                        return
                    if element == '**':
                        if x + 1 != len(p):
                            raise ValueError(
                                  'wildcard ** in the middle of path')
                        m['**'] = value
                        return

                    m_new = {}
                    m[element] = m_new
                    m = m_new
            else:
                raise ValueError("path contains empty element")
        m[''] = value

    def get(self, path):
        """Returns a tuple (VALUE, REMAINING-PATH), for the
        most-specific path matching the given path."""

        m = self.__map
        p = path.split('/')
        while p and not p[-1]:
            del p[-1]
        l = len(p)
        for x in range(l):
            element = p[x]

            # Ignore empty path elements (leadings slash, duplicated
            # slashes).
            if element:
                try:
                    m = m[element]
                except KeyError:
                    if x + 1 == l and '*' in m:
                        # Use '*' only if the remaining path is empty.
                        return (m['*'], tuple(p[x:]))
                    if '**' in m:
                        return (m['**'], tuple(p[x:]))
                    raise InvalidPath()
        try:
            result = m['']
        except KeyError:
            if '*' in m:
                result = m['*']
            elif '**' in m:
                result = m['**']
            else:
                raise InvalidPath()
        return (result, ())

class Result(object):
    """Base class for result objects."""

    def __init__(self):
        self.status = 500
        self.headers = {}

    def flatten(self, write):
        for k, v in self.headers.items():
            write("%s: %s\n" % (k, v))
        write("\n")

    def flatten_later(self):
        """Flattens this result.

        Returns a closure which sends the result using a
        BaseHTTPRequestHandler object passed as argument."""
        def later(req):
            req.send_response(self.status)
            for k, v in self.headers.items():
                req.send_header(k, v)
            req.end_headers()
        return later

class RedirectResult(Result):
    """Permanently redirects the browser to a new URL."""
    def __init__(self, url, permanent=True):
        super(RedirectResult, self).__init__()
        if permanent:
            self.status = 301
        else:
            self.status = 302
        self.headers['Location'] = str(url)

class HTMLResult(Result):
    """An object of this class combines a status code with HTML contents."""
    def __init__(self, contents, doctype='', status=200):
        super(HTMLResult, self).__init__()
        self.contents = contents
        self.status = status
        self.doctype = doctype
        self.headers['Content-Type'] = 'text/html; charset=UTF-8'

    def flatten(self, write):
        """Invokes write for the response header and all HTML data.
        Includes the doctype declaration."""
        super(HTMLResult, self).flatten(write)
        write("%s\n" % self.doctype)
        self.contents.flatten(write)

    def flatten_later(self):
        headers_later = super(HTMLResult, self).flatten_later()
        buf = cStringIO.StringIO()
        buf.write(self.doctype)
        buf.write('\n')
        self.contents.flatten(buf.write)
        buf = buf.getvalue()
        self.headers['Content-Length'] = str(len(buf))
        def later(req):
            headers_later(req)
            if req.command != 'HEAD':
                req.wfile.write(buf)
        return later

class BinaryResult(Result):
    """An object of this class combines a status code with HTML contents."""
    def __init__(self, contents,
                 mimetype='application/octet-stream', status=200):
        super(BinaryResult, self).__init__()
        self.contents = contents
        self.status = status
        self.headers['Content-Type'] = mimetype
        self.headers['Content-Length'] = str(len(self.contents))

    def flatten(self, write):
        """Invokes write for the response header and the binary data."""
        super(BinaryResult, self).flatten(write)
        write(self.contents)

    def flatten_later(self):
        headers_later = super(BinaryResult, self).flatten_later()
        def later(req):
            headers_later(req)
            if req.command != 'HEAD':
                req.wfile.write(self.contents)
        return later

class WebServiceBase:
    def __init__(self):
        self.router = PathRouter()

    def register(self, path, method):
        """Requests that method is invoked if path is encountered.

        The path has the syntax required by PathRouter.register.  The
        method should be a function taking several arguments

        - the remaining path
        - a dictionary for the request parameters
        - a URLFactory object

        The method is expected to return a HTMLResult object.
        """
        self.router.register(path, method)

    def html_dtd(self):
        """Returns the DOCTYPE declaration to be used for HTML documents.
        Can be overridden."""
        return '<!DOCTYPE html>'

    def add_title(self, title, body, head_contents=None, body_attribs={}):
        """Takes a sequence of HTML objects and wraps them in 'body'
        and 'html' tags.  Puts title in front of it, and optionally
        includes the head_contents material.  The attributes of the
        body element are taken from the body_attribs dictionary."""
        t = TITLE(title)
        if head_contents is None:
            head_list = [t]
        else:
            if isinstance(head_contents, HTMLBase):
                head_list = [head_contents]
            else:
                head_list = list(head_contents)
            head_list.append(t)
        if isinstance(body, HTMLBase):
            body_list = [body]
        else:
            body_list = list(body)
        body_list[:0] = (HEADER(H1(title)),)

        return tag('html',
                   (HEAD(head_list), Tag('body', body_list, body_attribs)))

    def pre_dispatch(self, url):
        """Invoked by handle prior to calling the registered handler."""
        pass

class WebService(Service, WebServiceBase):
    "CGI service implemented using servinvoke"
    def __init__(self, socket_name):
        Service.__init__(self, socket_name)
        WebServiceBase.__init__(self)

    def __writeError(self, result, code, msg):
        result.write('Status: %d\nContent-Type: text/plain\n\n%s\n'
                     % (code, msg))

    def handle(self, args, environment, data, result):
        params = cgi.parse(data, environment)
        path = environment.get('PATH_INFO', '')
        server_name = environment.get('SERVER_NAME', '')
        server_port = environment.get('SERVER_PORT', '')
        if server_port and server_port != 80:
            server_name = server_name + ":" + server_port
        script_name = environment.get('SCRIPT_NAME', '')

        try:
            (method, remaining) = self.router.get(path)
        except InvalidPath:
            self.__writeError(result, 404, "page not found")
            return
        self.pre_dispatch()
        url = URLFactory(server_name, script_name, path, params)
        r = method(remaining, params, url)
        assert isinstance(r, Result), `r`
        r.flatten(result.write)

class ThreadingHTTPServer(SocketServer.ThreadingMixIn,
                          BaseHTTPServer.HTTPServer):
    pass

RE_BASE_URL = re.compile(r'^(https?)://([^/]+)(.*)')

class WebServiceHTTP(WebServiceBase):
    def __init__(self, socket_name):
        WebServiceBase.__init__(self)
        (base_url, address, port) = socket_name
        self.lock = threading.Lock()

        self.__parse_base_url(base_url)

        service_self = self
        class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
            def do_GET(self):
                (method, path, remaining, params) = self.route()
                if path is None:
                    return

                url = URLFactory(service_self.server_name,
                                 service_self.script_name,
                                 path, params,
                                 secure=service_self.secure)

                service_self.lock.acquire()
                try:
                    service_self.pre_dispatch()
                    r = method(remaining, params, url)
                    assert isinstance(r, Result), `r`
                    result = r.flatten_later()
                finally:
                    service_self.lock.release()
                result(self)
            do_HEAD = do_GET

            def __parse_path(self):
                pos = self.path.find('?')
                if pos < 0:
                    return (self.path, {})
                path = self.path[:pos]
                if path[:1] != '/':
                    path = '/' + path
                params = cgi.parse_qs(self.path[pos + 1:])
                return (path, params)

            def route(self):
                (path, params) = self.__parse_path()
                prefix_len = len(service_self.script_name)
                prefix = path[0:prefix_len]
                result = None
                if prefix == service_self.script_name:
                    suffix = path[prefix_len:]
                    try:
                        (method, remaining) = \
                            service_self.router.get(suffix)
                        return (method, suffix, remaining, params)
                    except InvalidPath:
                        pass
                self.send_error(404, "page not found")
                return (None, None, None, None)

        self.server = ThreadingHTTPServer((address, port), Handler)

    def run(self):
        self.server.serve_forever()

    def __parse_base_url(self, url):
        m = RE_BASE_URL.match(url)
        if m is None:
            raise ValueError("invalid base URL: " + url)
        self.secure = m.group(1) == "https"
        self.server_name = m.group(2)
        self.script_name = m.group(3)


def __test():
    assert str(URL("")) == ""
    assert str(URL("abc")) == "abc"
    assert str(URL(" ")) == " "
    assert str(URL("&")) == "&"

    u = URLFactory(server_name=None, script_name=None)
    assert str(u.absolute("http://www.enyo.de/")) == "http://www.enyo.de/"
    assert str(u.absolute("http://www.enyo.de/", t='123')) \
           == "http://www.enyo.de/?t=123"
    assert str(u.scriptRelative("/a/b", t='123')) == "/a/b?t=123"
    assert str(u.scriptRelativeFull("/a/b", t='123')) \
           == "http://localhost/a/b?t=123"

    u = URLFactory(server_name=None, script_name=None, secure=True)
    assert str(u.absolute("http://www.enyo.de/")) == "http://www.enyo.de/"
    assert str(u.absolute("http://www.enyo.de/", t='123')) \
           == "http://www.enyo.de/?t=123"
    assert str(u.scriptRelative("/a/b", t='123')) == "/a/b?t=123"
    assert str(u.scriptRelativeFull("/a/b", t='123')) \
           == "https://localhost/a/b?t=123"

    u = URLFactory(server_name='localhost.localdomain',
                   script_name='/cgi-bin/test.cgi')
    assert str(u.scriptRelative("a/b", t='123')) \
           == "/cgi-bin/test.cgi/a/b?t=123"
    assert str(u.scriptRelativeFull("a/b", t='123=')) \
           == "http://localhost.localdomain/cgi-bin/test.cgi/a/b?t=123%3D"

    assert P("").toString() == '<p></p>'
    assert P(" ").toString() == '<p> </p>'
    assert P("&").toString() == '<p>&amp;</p>'
    assert P("\"").toString() == '<p>"</p>'
    assert P("<").toString() == '<p>&lt;</p>'
    assert P(">").toString() == '<p>&gt;</p>'
    assert P(">").toHTML().toString() == '<p>&gt;</p>'
    assert FORM(method='get').toString() == '<form method="get"/>'
    assert SPAN("green", _class="red").toString() \
           == '<span class="red">green</span>'
    assert TD(A("http://www.example.net/", "example")).toString() \
           == '<td><a href="http://www.example.net/">example</a></td>'
    #assert make_pre(['a', 'b']).toString() == '<pre>a\nb\n</pre>'

    s = cStringIO.StringIO()
    RedirectResult(u.scriptRelativeFull("123")).flatten(s.write)
    assert s.getvalue() == '''Location: http://localhost.localdomain/cgi-bin/test.cgi/123

'''

    assert make_menu(u.scriptRelative,
                     ("123", "A"),
                     ("456", "B")).toString() == \
                     '<ul><li><a href="/cgi-bin/test.cgi/123">A</a></li><li><a href="/cgi-bin/test.cgi/456">B</a></li></ul>'

    pr = PathRouter()
    pr.register('', "root")
    pr.register('/*', "default")
    pr.register('/abc', "/abc")
    pr.register('/a/bc', "/a/bc")
    pr.register('ab/c', "/ab/c")
    pr.register('/a', "/a")
    pr.register('/a/**', "/a/**")
    pr.register('/a/*', "/a/*")

    assert pr.get("") == ("root", ())
    assert pr.get("/") == ("root", ())
    assert pr.get("//") == ("root", ())
    assert pr.get("/xyz") == ("default", ("xyz",))
    assert pr.get("/a//xyz/") == ("/a/*", ("xyz",))
    assert pr.get("/a//xyz/123") == ("/a/**", ("xyz", "123"))
    assert pr.get("/abc") == ("/abc", ())

    assert linkify('foo bar moo') == 'foo bar moo'
    assert linkify('http://domain.tld/foo.cgi?bar=moo&test') == A('http://domain.tld/foo.cgi?bar=moo&test').toString()
    assert linkify('as seen in http://foo.tld/bar/moo.txt, ...') == 'as seen in %s, ...' % A('http://foo.tld/bar/moo.txt').toString()

if __name__ == "__main__":
    __test()

© 2014-2024 Faster IT GmbH | imprint | privacy policy