forked from hacktoolkit/django-htk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
241 lines (187 loc) · 6.01 KB
/
views.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
# Python Standard Library Imports
import re
# Django Imports
from django.http import (
Http404,
HttpResponse,
)
from django.shortcuts import (
get_object_or_404,
redirect,
)
from django.template import (
TemplateDoesNotExist,
loader,
)
from django.urls import reverse
# HTK Imports
from htk.compat import b64decode
from htk.utils import htk_setting
from htk.view_helpers import get_view_context
# isort: off
def health_check(request):
response = HttpResponse('200 OK', status=200)
return response
def browser_info(request, data=None, template_name=None, renderer=None):
if data is None:
from htk.view_helpers import wrap_data
data = wrap_data(request)
if template_name is None:
template_name = 'htk/fragments/browser_info.html'
if renderer is None:
from htk.view_helpers import render_custom
renderer = render_custom
from htk.utils.constants import REQUEST_HTTP_HEADERS_STANDARD
from htk.utils.request import get_custom_http_headers
data['standard_http_headers'] = REQUEST_HTTP_HEADERS_STANDARD
data['custom_http_headers'] = get_custom_http_headers(request)
response = renderer(request, template_name, data=data)
return response
def debugger(request):
import rollbar
message = request.GET.get('m')
rollbar.report_message(message, 'debug')
from htk.api.utils import json_response_okay
response = json_response_okay()
return response
def generic_template_view(
request,
template_name,
context_dict=None,
content_type='text/html',
missing_template_exception=Http404,
):
try:
from htk.utils.templates import get_template_context_generator
template = loader.get_template(template_name)
template_context = {}
template_context_generator = get_template_context_generator()
if template_context_generator:
template_context = template_context_generator(request)
if context_dict:
template_context.update(context_dict)
response = HttpResponse(
template.render(template_context), content_type=content_type
)
except TemplateDoesNotExist:
response = None
raise missing_template_exception
return response
def json_file_download(request, data, filename, filename_suffix='.json'):
"""Returns a JSON file attachment response (downloadable)
`data` must be `json.dumps()`-able
"""
response = generic_template_view(
request,
'data.json',
context_dict={'json': data},
content_type="application/json",
)
response['Content-Disposition'] = (
f'attachment; filename={filename}{filename_suffix}'
)
return response
def google_site_verification(request, code):
from htk.exceptions import MissingGoogleSiteVerificationFile
template_name = 'site_verification/google%s.html' % code
response = generic_template_view(
request,
template_name,
missing_template_exception=MissingGoogleSiteVerificationFile,
)
return response
def html_site_verification(request, code):
from htk.exceptions import MissingHtmlSiteVerificationFile
template_name = 'site_verification/%s--.html' % code
response = generic_template_view(
request,
template_name,
missing_template_exception=MissingHtmlSiteVerificationFile,
)
return response
def bing_site_auth(request):
from htk.exceptions import MissingBingSiteVerificationFile
template_name = 'site_verification/BingSiteAuth.xml'
response = generic_template_view(
request,
template_name,
content_type='text/xml',
missing_template_exception=MissingBingSiteVerificationFile,
)
return response
def brave_rewards_verification(request):
from htk.exceptions import MissingBraveRewardsVerificationFile
template_name = '.well-known/brave-rewards-verification.txt'
response = generic_template_view(
request,
template_name,
content_type='text/plain',
missing_template_exception=MissingBraveRewardsVerificationFile,
)
return response
def robots(request):
url_namespace = request.resolver_match.namespace
if url_namespace:
template_prefix = '%s/' % url_namespace
else:
template_prefix = ''
template_name = '%srobots.txt' % template_prefix
context_dict = get_view_context(request)
context_dict.update(
{
'request': {
'request': request,
'host': request.get_host(),
},
}
)
response = generic_template_view(
request,
template_name,
context_dict=context_dict,
content_type='text/plain',
)
return response
def redir(request):
response = None
encoded_url = request.GET.get('url', None)
if encoded_url:
try:
url = b64decode(str(encoded_url), url_safe=True)
if not re.match('^https?://', url):
url = 'http://%s' % url
else:
pass
response = redirect(url)
except:
# can encounter various illegal values from vulnerability scans and bots
pass
if response is None:
response = redirect('/')
return response
##################################################
# error pages
def error_view(request):
path_no_slash = request.path[1:]
url_namespace = request.resolver_match.namespace
if url_namespace:
template_prefix = '%s/' % url_namespace
else:
template_prefix = ''
template_name = '%s%s.html' % (
template_prefix,
path_no_slash,
)
error_messages = {
'400': '400 Bad Request',
'403': '403 Access Denied',
'404': '404 Not Found',
'500': '500 Internal Server Error',
}
context_dict = {
'error_message': error_messages.get(path_no_slash, 'Unknown Error')
}
response = generic_template_view(
request, template_name, context_dict=context_dict
)
return response