summaryrefslogtreecommitdiffstats
path: root/third_party/python/aiohttp/examples/legacy/srv.py
blob: 628b6f332f1eeef602438a9f6df48aac3865b372 (plain)
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
#!/usr/bin/env python3
"""Simple server written using an event loop."""

import argparse
import asyncio
import logging
import os
import sys

import aiohttp
import aiohttp.server

try:
    import ssl
except ImportError:  # pragma: no cover
    ssl = None


class HttpRequestHandler(aiohttp.server.ServerHttpProtocol):
    async def handle_request(self, message, payload):
        print(
            "method = {!r}; path = {!r}; version = {!r}".format(
                message.method, message.path, message.version
            )
        )

        path = message.path

        if not (path.isprintable() and path.startswith("/")) or "/." in path:
            print("bad path", repr(path))
            path = None
        else:
            path = "." + path
            if not os.path.exists(path):
                print("no file", repr(path))
                path = None
            else:
                isdir = os.path.isdir(path)

        if not path:
            raise aiohttp.HttpProcessingError(code=404)

        for hdr, val in message.headers.items():
            print(hdr, val)

        if isdir and not path.endswith("/"):
            path = path + "/"
            raise aiohttp.HttpProcessingError(
                code=302, headers=(("URI", path), ("Location", path))
            )

        response = aiohttp.Response(self.writer, 200, http_version=message.version)
        response.add_header("Transfer-Encoding", "chunked")

        # content encoding
        accept_encoding = message.headers.get("accept-encoding", "").lower()
        if "deflate" in accept_encoding:
            response.add_header("Content-Encoding", "deflate")
            response.add_compression_filter("deflate")
        elif "gzip" in accept_encoding:
            response.add_header("Content-Encoding", "gzip")
            response.add_compression_filter("gzip")

        response.add_chunking_filter(1025)

        if isdir:
            response.add_header("Content-type", "text/html")
            response.send_headers()

            response.write(b"<ul>\r\n")
            for name in sorted(os.listdir(path)):
                if name.isprintable() and not name.startswith("."):
                    try:
                        bname = name.encode("ascii")
                    except UnicodeError:
                        pass
                    else:
                        if os.path.isdir(os.path.join(path, name)):
                            response.write(
                                b'<li><a href="'
                                + bname
                                + b'/">'
                                + bname
                                + b"/</a></li>\r\n"
                            )
                        else:
                            response.write(
                                b'<li><a href="'
                                + bname
                                + b'">'
                                + bname
                                + b"</a></li>\r\n"
                            )
            response.write(b"</ul>")
        else:
            response.add_header("Content-type", "text/plain")
            response.send_headers()

            try:
                with open(path, "rb") as fp:
                    chunk = fp.read(8192)
                    while chunk:
                        response.write(chunk)
                        chunk = fp.read(8192)
            except OSError:
                response.write(b"Cannot open")

        await response.write_eof()
        if response.keep_alive():
            self.keep_alive(True)


ARGS = argparse.ArgumentParser(description="Run simple HTTP server.")
ARGS.add_argument(
    "--host", action="store", dest="host", default="127.0.0.1", help="Host name"
)
ARGS.add_argument(
    "--port", action="store", dest="port", default=8080, type=int, help="Port number"
)
# make iocp and ssl mutually exclusive because ProactorEventLoop is
# incompatible with SSL
group = ARGS.add_mutually_exclusive_group()
group.add_argument(
    "--iocp", action="store_true", dest="iocp", help="Windows IOCP event loop"
)
group.add_argument("--ssl", action="store_true", dest="ssl", help="Run ssl mode.")
ARGS.add_argument("--sslcert", action="store", dest="certfile", help="SSL cert file.")
ARGS.add_argument("--sslkey", action="store", dest="keyfile", help="SSL key file.")


def main():
    args = ARGS.parse_args()

    if ":" in args.host:
        args.host, port = args.host.split(":", 1)
        args.port = int(port)

    if args.iocp:
        from asyncio import windows_events

        sys.argv.remove("--iocp")
        logging.info("using iocp")
        el = windows_events.ProactorEventLoop()
        asyncio.set_event_loop(el)

    if args.ssl:
        here = os.path.join(os.path.dirname(__file__), "tests")

        if args.certfile:
            certfile = args.certfile or os.path.join(here, "sample.crt")
            keyfile = args.keyfile or os.path.join(here, "sample.key")
        else:
            certfile = os.path.join(here, "sample.crt")
            keyfile = os.path.join(here, "sample.key")

        sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        sslcontext.load_cert_chain(certfile, keyfile)
    else:
        sslcontext = None

    loop = asyncio.get_event_loop()
    f = loop.create_server(
        lambda: HttpRequestHandler(debug=True, keep_alive=75),
        args.host,
        args.port,
        ssl=sslcontext,
    )
    svr = loop.run_until_complete(f)
    socks = svr.sockets
    print("serving on", socks[0].getsockname())
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass


if __name__ == "__main__":
    main()