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
|
<?php declare(strict_types = 1);
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * @extends QM_DataCollector<QM_Data_Raw_Request> */ class QM_Collector_Raw_Request extends QM_DataCollector {
public $id = 'raw_request';
public function get_storage(): QM_Data { return new QM_Data_Raw_Request(); }
/** * Extracts headers from a PHP-style $_SERVER array. * * From WP_REST_Server::get_headers() * * @param array<string, string> $server Associative array similar to `$_SERVER`. * @return array<string, string> Headers extracted from the input. */ protected function get_headers( array $server ) { $headers = array();
// CONTENT_* headers are not prefixed with HTTP_. $additional = array( 'CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true, );
foreach ( $server as $key => $value ) { if ( strpos( $key, 'HTTP_' ) === 0 ) { $headers[ substr( $key, 5 ) ] = $value; } elseif ( isset( $additional[ $key ] ) ) { $headers[ $key ] = $value; } }
return $headers; }
/** * Process request and response data. * * @return void */ public function process() { $request = array( 'ip' => $_SERVER['REMOTE_ADDR'] ?? '', 'method' => strtoupper( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ), 'scheme' => is_ssl() ? 'https' : 'http', 'host' => self::get_host(), 'path' => wp_unslash( $_SERVER['REQUEST_URI'] ?? '/' ), 'query' => wp_unslash( $_SERVER['QUERY_STRING'] ?? '' ), 'headers' => $this->get_headers( wp_unslash( $_SERVER ) ), );
ksort( $request['headers'] );
$request['url'] = sprintf( '%s://%s%s', $request['scheme'], $request['host'], $request['path'] );
$this->data->request = $request;
$headers = array(); $raw_headers = headers_list(); foreach ( $raw_headers as $row ) { list( $key, $value ) = explode( ':', $row, 2 ); $headers[ trim( $key ) ] = trim( $value ); }
ksort( $headers );
$response = array( 'status' => http_response_code(), 'headers' => $headers, );
$this->data->response = $response; } }
/** * @param array<string, QM_Collector> $collectors * @param QueryMonitor $qm * @return array<string, QM_Collector> */ function register_qm_collector_raw_request( array $collectors, QueryMonitor $qm ) { $collectors['raw_request'] = new QM_Collector_Raw_Request(); return $collectors; }
add_filter( 'qm/collectors', 'register_qm_collector_raw_request', 10, 2 );
|