summaryrefslogtreecommitdiff
path: root/ngus/__init__.py
blob: 2b2be7d38b2fddb701c88feabdee93e0aed56563 (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
# ngus v1.0
# Copyright (C) 2021 Simeon Simeonov

# 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 3 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, see <http://www.gnu.org/licenses/>.

import cgi
import http.server
import pathlib

__author__ = 'Simeon Simeonov'
__version__ = '1.0'
__license__ = 'GPL3'

DEFAULT_UPLOAD_PAGE = bytes('''<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
<meta name="viewport" content="width=device-width, user-scalable=no" />
<style type="text/css">
@media (prefers-color-scheme: dark) {
  body {
    background-color: #000;
    color: #fff;
  }
}
</style>
</head>
<body>
<h1>File Upload</h1>
<form method="POST" enctype="multipart/form-data">
<input name="ufile" type="file" />
<br/>
<br/>
<input type="submit" />
</form>
</body>
</html>''', 'utf-8')


class NgusBaseHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
    """
    A custom http.server.SimpleHTTPRequestHandler that handles POST uploads
    """
    def do_GET(self):
        """GET requests will load the upload page"""
        self._send_upload_page()

    def do_POST(self):
        """POST requests will be handled according to the settings"""
        if self.server.basic_auth is not None:
            if (
                'Authorization' not in self.headers
                or len(self.headers['Authorization'].split()) != 2
                or self.headers['Authorization'].split()[0].lower() != 'basic'
                or self.headers[
                    'Authorization'
                ].split()[1] != self.server.basic_auth
            ):
                self.send_response(http.HTTPStatus.UNAUTHORIZED)
                self.send_header('WWW-Authenticate',
                                 'Basic realm="ngus", charset="UTF-8"')
                self.end_headers()
                return None
        form = cgi.FieldStorage(fp=self.rfile,
                                headers=self.headers,
                                environ={'REQUEST_METHOD': 'POST'})
        if not (upload_dir := self.server.upload_dir).is_dir():
            raise ValueError('upload_dir is not a directory')
        i_name = self.server.input_name
        if i_name in form and form[i_name].file and form[i_name].filename:
            with open(
                upload_dir / pathlib.Path(form[i_name].filename).name,
                'wb'
            ) as f:
                f.write(form[i_name].file.read())
        self._send_upload_page()
        return None

    def _send_upload_page(self):
        """Renders an upload page (form)"""
        self.send_response(http.HTTPStatus.OK)
        self.send_header('Content-Type', 'text/html; charset=utf-8')
        self.send_header('Content-Length', len(self.server.upload_page))
        self.end_headers()
        self.wfile.write(self.server.upload_page)


class NgusHTTPServer(http.server.HTTPServer):
    """A custom http.server.HTTPServer"""
    def __init__(self, *arg, **kwargs):
        """
        """
        self._basic_auth = kwargs.pop('basic_auth', None)
        self._input_name = kwargs.pop('input_name', 'ufile')
        self._upload_dir = kwargs.pop('upload_dir', pathlib.Path.cwd())
        self._upload_page = kwargs.pop('upload_page', DEFAULT_UPLOAD_PAGE)
        super().__init__(*arg, **kwargs)

    @property
    def basic_auth(self):
        """basic_auth-property"""
        return self._basic_auth

    @property
    def input_name(self):
        """input_name-property"""
        return self._input_name

    @property
    def upload_dir(self):
        """upload_dir-property"""
        return self._upload_dir

    @property
    def upload_page(self):
        """upload_page-property"""
        return self._upload_page