This repository was archived by the owner on Jun 22, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_handler.module
More file actions
370 lines (322 loc) · 13.4 KB
/
Copy pathhttp_handler.module
File metadata and controls
370 lines (322 loc) · 13.4 KB
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
<?php
/**
* @file
* Enables the ability to return correct HTTP Status code.
*/
use Drupal\http_handler\Component\Enum\HttpStatusCodeEnum;
/**
* Implements hook_help().
*
* @see https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_help/7.x
*/
function http_handler_help($path, $arg) {
switch ($path) {
case 'admin/help#http_handler':
$output = '<pre>';
$filepath = drupal_get_path('module', 'http_handler') . DIRECTORY_SEPARATOR . 'README.md';
if (file_exists($filepath)) {
if ($readme = file_get_contents($filepath)) {
$output .= $readme;
if (module_exists('markdown')) {
$filters = module_invoke('markdown', 'filter_info');
$info = $filters['filter_markdown'];
if (function_exists($info['process callback'])) {
$output = $info['process callback']($readme, NULL);
}
}
}
}
$output .= '</pre>';
break;
default:
$output = '';
break;
}
return $output;
}
/**
* Implements hook_init().
*
* @see https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_init/7
*/
function http_handler_init() {
module_load_include('inc', 'http_handler', 'includes/http_handler.functions');
set_exception_handler('http_handler_exception_handler');
}
/**
* Implements hook_page_delivery_callback_alter().
*
* @see https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_page_delivery_callback_alter/7.x
*/
function http_handler_page_delivery_callback_alter(&$callback) {
if ($callback == 'drupal_deliver_html_page') {
$callback = 'http_handler_deliver_html_page';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @see https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_FORM_ID_alter/7.x
*/
function http_handler_form_system_site_information_settings_alter(&$form, &$form_state, $form_id) {
$clean_url = variable_get('clean_url', 0);
$default_site_403 = variable_get('site_403', '');
$form['error_page']['site_' . HttpStatusCodeEnum::HTTP_BAD_REQUEST] = array(
'#type' => 'textfield',
'#title' => t('Default @code (@text) page', array('@code' => HttpStatusCodeEnum::HTTP_BAD_REQUEST, '@text' => 'bad request')),
'#default_value' => variable_get('site_' . HttpStatusCodeEnum::HTTP_BAD_REQUEST, $default_site_403),
'#size' => 40,
'#description' => t(
'This page is displayed when the requested document is on error to the current user. Leave blank to display a generic "@text" page.',
array('@text' => 'bad request')
),
'#field_prefix' => url(NULL, array('absolute' => TRUE)) . ($clean_url ? '' : '?q='),
);
$form['error_page']['site_' . HttpStatusCodeEnum::HTTP_UNAUTHORIZED] = array(
'#type' => 'textfield',
'#title' => t('Default @code (@text) page', array('@code' => HttpStatusCodeEnum::HTTP_UNAUTHORIZED, '@text' => 'unauthorized')),
'#default_value' => variable_get('site_' . HttpStatusCodeEnum::HTTP_UNAUTHORIZED, $default_site_403),
'#size' => 40,
'#description' => t(
'This page is displayed when the requested document is on error to the current user. Leave blank to display a generic "@text" page.',
array('@text' => 'unauthorized')
),
'#field_prefix' => url(NULL, array('absolute' => TRUE)) . ($clean_url ? '' : '?q='),
);
$form['error_page']['site_' . HttpStatusCodeEnum::HTTP_METHOD_NOT_ALLOWED] = array(
'#type' => 'textfield',
'#title' => t('Default @code (@text) page', array('@code' => HttpStatusCodeEnum::HTTP_METHOD_NOT_ALLOWED, '@text' => 'method not allowed')),
'#default_value' => variable_get('site_' . HttpStatusCodeEnum::HTTP_METHOD_NOT_ALLOWED, $default_site_403),
'#size' => 40,
'#description' => t(
'This page is displayed when the requested document is on error to the current user. Leave blank to display a generic "@text" page.',
array('@text' => 'method not allowed')
),
'#field_prefix' => url(NULL, array('absolute' => TRUE)) . ($clean_url ? '' : '?q='),
);
$form['#validate'][] = 'http_handler_system_site_information_settings_validate';
}
/**
* Validates the submitted site-information form.
*
* Callback for http_handler_form_system_site_information_settings_alter().
*
* @param array $form
* Nested array of form elements that comprise the form.
* @param array $form_state
* A keyed array containing the current state of the form.
*
* @see system_site_information_settings_validate()
*/
function http_handler_system_site_information_settings_validate(array $form, array &$form_state) {
// Validate 400 error path.
if (
!empty($form_state['values']['site_' . HttpStatusCodeEnum::HTTP_BAD_REQUEST])
&& !drupal_valid_path($form_state['values']['site_' . HttpStatusCodeEnum::HTTP_BAD_REQUEST])
) {
form_set_error('site_' . HttpStatusCodeEnum::HTTP_BAD_REQUEST, t('The path "@path" is either invalid or you do not have access to it.', array('@path' => $form_state['values']['site_' . HttpStatusCodeEnum::HTTP_BAD_REQUEST])));
}
// Validate 401 error path.
if (
!empty($form_state['values']['site_' . HttpStatusCodeEnum::HTTP_UNAUTHORIZED])
&& !drupal_valid_path($form_state['values']['site_' . HttpStatusCodeEnum::HTTP_UNAUTHORIZED])
) {
form_set_error('site_' . HttpStatusCodeEnum::HTTP_UNAUTHORIZED, t('The path "@path" is either invalid or you do not have access to it.', array('@path' => $form_state['values']['site_' . HttpStatusCodeEnum::HTTP_UNAUTHORIZED])));
}
// Validate 405 error path.
if (
!empty($form_state['values']['site_' . HttpStatusCodeEnum::HTTP_METHOD_NOT_ALLOWED])
&& !drupal_valid_path($form_state['values']['site_' . HttpStatusCodeEnum::HTTP_METHOD_NOT_ALLOWED])
) {
form_set_error('site_' . HttpStatusCodeEnum::HTTP_METHOD_NOT_ALLOWED, t('The path "@path" is either invalid or you do not have access to it.', array('@path' => $form_state['values']['site_' . HttpStatusCodeEnum::HTTP_METHOD_NOT_ALLOWED])));
}
}
/**
* Change the exception handler.
*
* Callback for http_handler_init().
*
* @param \Exception $exception
* The exception raised.
*
* @todo be more specific for exception and add an alter
*/
function http_handler_exception_handler(\Exception $exception) {
watchdog_exception('http_handler', $exception);
switch (TRUE) {
case $exception instanceof \LogicException:
restore_exception_handler();
throw $exception;
case $exception instanceof \BadFunctionCallException:
// No break.
case $exception instanceof \BadMethodCallException:
drupal_method_not_allowed();
break;
case $exception instanceof \DomainException:
// No break.
case $exception instanceof \RangeException:
// No break.
case $exception instanceof \LengthException:
// No break.
case $exception instanceof \InvalidArgumentException:
// No break.
case $exception instanceof \UnexpectedValueException:
// No break.
case $exception instanceof \RuntimeException:
drupal_bad_request();
break;
case $exception instanceof \UnderflowException:
drupal_gone();
break;
case $exception instanceof \LogicException:
// No break.
case $exception instanceof \OutOfBoundsException:
// No break.
case $exception instanceof \OutOfRangeException:
// No break.
case $exception instanceof \OverflowException:
// No break.
default:
_drupal_exception_handler($exception);
break;
}
}
/**
* Packages and sends the result of a page callback to the browser as HTML.
*
* Callback for http_handler_page_delivery_callback_alter().
*
* @param mixed $page_callback_result
* The result of a page callback. Can be one of:
* - null: to indicate no content.
* - An integer menu status constant: to indicate an error condition.
* - A string of HTML content.
* - A renderable array of content.
*
* @see drupal_deliver_page()
*/
function http_handler_deliver_html_page($page_callback_result) {
// Emit the correct charset HTTP header, but not if the page callback
// result is null, since that likely indicates that it printed something
// in which case, no further headers may be sent, and not if code running
// for this page request has already set the content type header.
if (isset($page_callback_result) && is_null(drupal_get_http_header('Content-Type'))) {
drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
}
// Send appropriate HTTP-Header for browsers and search engines.
global $language;
drupal_add_http_header('Content-Language', $language->language);
// By default, do not allow the site to be rendered in an iframe on another
// domain, but provide a variable to override this. If the code running for
// this page request already set the X-Frame-Options header earlier, don't
// overwrite it here.
$frame_options = variable_get('x_frame_options', 'SAMEORIGIN');
if ($frame_options && is_null(drupal_get_http_header('X-Frame-Options'))) {
drupal_add_http_header('X-Frame-Options', $frame_options);
}
$default_site_403 = variable_get('site_403', '');
$q = $_GET['q'];
switch ($page_callback_result) {
case HttpStatusCodeEnum::HTTP_BAD_REQUEST:
drupal_add_http_header('Status', $page_callback_result . ' Bad Request');
watchdog('bad request', 'drupal_deliver_page error : @query', array('@query' => $q), WATCHDOG_WARNING);
if (!isset($_GET['destination'])) {
if (!url_is_external($q)) {
$_GET['destination'] = $q;
}
}
// Act as 400 in order to use defined page.
$path = drupal_get_normal_path(variable_get('site_' . $page_callback_result, $default_site_403));
if ($path && $path != $q) {
menu_set_active_item($path);
$return = menu_execute_active_handler($path, FALSE);
}
if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
drupal_set_title(t('Bad Request'));
$return = t('You are not authorized to access this page.');
}
echo drupal_render_page($return);
drupal_page_footer();
break;
case HttpStatusCodeEnum::HTTP_UNAUTHORIZED:
drupal_add_http_header('Status', $page_callback_result . ' Unauthorized');
watchdog('authentication required', 'drupal_deliver_page error : @query', array('@query' => $q), WATCHDOG_WARNING);
if (!isset($_GET['destination'])) {
if (!url_is_external($q)) {
$_GET['destination'] = $q;
}
}
// Act as 401 in order to use defined page.
$path = drupal_get_normal_path(variable_get('site_' . $page_callback_result, $default_site_403));
if ($path && $path != $q) {
menu_set_active_item($path);
$return = menu_execute_active_handler($path, FALSE);
}
if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
drupal_set_title(t('Access Unauthorized'));
$return = t('You are not authorized to access this page.');
}
echo drupal_render_page($return);
// Perform end-of-request tasks.
drupal_page_footer();
break;
case HttpStatusCodeEnum::HTTP_METHOD_NOT_ALLOWED:
drupal_add_http_header('Status', $page_callback_result . ' Method Not Allowed');
watchdog('method not allowed', 'drupal_deliver_page error : @query', array('@query' => $q), WATCHDOG_WARNING);
if (!isset($_GET['destination'])) {
if (!url_is_external($q)) {
$_GET['destination'] = $q;
}
}
// Act as 405 in order to use defined page.
$path = drupal_get_normal_path(variable_get('site_' . $page_callback_result, $default_site_403));
if ($path && $path != $q) {
menu_set_active_item($path);
$return = menu_execute_active_handler($path, FALSE);
}
if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
drupal_set_title(t('Method Not Allowed'));
$return = t('You are not authorized to access this page.');
}
echo drupal_render_page($return);
drupal_page_footer();
break;
case HttpStatusCodeEnum::HTTP_GONE:
drupal_add_http_header('Status', $page_callback_result . ' Gone');
watchdog('gone', 'drupal_deliver_page error : @query', array('@query' => $q), WATCHDOG_WARNING);
if (!isset($_GET['destination'])) {
if (!url_is_external($q)) {
$_GET['destination'] = $q;
}
}
// Act as 410 in order to use defined page.
$path = drupal_get_normal_path(variable_get('site_' . $page_callback_result, $default_site_403));
if ($path && $path != $q) {
menu_set_active_item($path);
$return = menu_execute_active_handler($path, FALSE);
}
if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
drupal_set_title(t('Resource Gone'));
$return = t('The resource is no longer available and no redirect address is known..');
}
echo drupal_render_page($return);
drupal_page_footer();
break;
case HttpStatusCodeEnum::HTTP_NOT_FOUND:
drupal_deliver_html_page($page_callback_result);
break;
case HttpStatusCodeEnum::HTTP_FORBIDDEN:
// No break.
case MENU_ACCESS_DENIED:
if (user_is_anonymous()) {
http_handler_deliver_html_page(HttpStatusCodeEnum::HTTP_UNAUTHORIZED);
}
else {
drupal_deliver_html_page($page_callback_result);
}
break;
default:
drupal_deliver_html_page($page_callback_result);
break;
}
}