This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
requests.inc
111 lines (91 loc) · 4.34 KB
/
requests.inc
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
<?php
require_once("utils.inc");
require_once("dbapi.inc");
// $aResources is the 'entries' property from a HAR file:
// log->entries
function resourcesFromHar($aHarResources, $pageref=null) {
global $ghReqHeaders, $ghRespHeaders;
// HAR (JSON) is heirarchical. MySQL is flat.
// We have to flatten the HAR object into a hash.
// This code is brittle.
$aResources = array(); // I found I had to return a NEW array
foreach($aHarResources as $resource) {
if ( $pageref && $pageref != $resource['pageref'] ) {
// In some cases the HAR file may contain MULTIPLE pages in which case
// the $pageRef parameter identifies the resources we want.
continue;
}
// copy things up to the top
$req = $resource['request'];
$resource['method'] = $req['method'];
$resource['reqHttpVersion'] = $req['httpVersion'];
$resource['url'] = $req['url'];
if ( array_key_exists('headersSize', $req) && 0 < $req['headersSize'] ) $resource['reqHeadersSize'] = $req['headersSize'];
if ( array_key_exists('bodySize', $req) && 0 < $req['bodySize'] ) $resource['reqBodySize'] = $req['bodySize'];
flattenHeaders($resource, $req['headers'], "req", $ghReqHeaders);
$resp = $resource['response'];
$resource['status'] = $resp['status'];
$resource['respHttpVersion'] = $resp['httpVersion'];
if ( array_key_exists('url', $resp) ) $resource['redirectUrl'] = $resp['url'];
if ( array_key_exists('headersSize', $resp) && 0 < $resp['headersSize'] ) $resource['respHeaderSize'] = $resp['headersSize'];
if ( array_key_exists('bodySize', $resp) && 0 < $resp['bodySize'] ) $resource['respBodySize'] = $resp['bodySize'];
if ( array_key_exists('_cdn_provider', $resp) ) $resource['_cdn_provider'] = $resp['_cdn_provider'];
if ( array_key_exists('_gzip_save', $resp) ) $resource['_gzip_save'] = $resp['_gzip_save'];
$content = $resp['content'];
$resource['respSize'] = $content['size'];
$resource['mimeType'] = $content['mimeType'];
flattenHeaders($resource, $resp['headers'], "resp", $ghRespHeaders);
array_push($aResources, $resource);
}
return $aResources;
}
// The HAR file has request & response headers, some of which have the same name.
// To put them in a single hash (assoc array) we have to use different names.
// Plus we like to change the names a bit, and remove/merge other headers.
// $resource is the main hash where we're putting the flattened headers
// $aHeaders is the 'headers' property from the HAR file
// $prefix is either "req" or "resp" and is used to prefix the CookieLen and OtherHeaders keys
function flattenHeaders(&$resource, $aHeaders, $prefix, $mapping) {
foreach($aHeaders as $header) {
$name = $header['name'];
$lcname = strtolower($name); // normalize camel case in header names
$value = $header['value'];
if ( array_key_exists($lcname, $mapping) ) {
// This is one of the standard headers we want to save.
$flatname = $mapping[$lcname];
$resource[$flatname] = ( array_key_exists($flatname, $resource) ? $resource[$flatname] . " " : "" ) . $value; // append if already exists
// TODO - these are going to generate a warning if they exceed the DB column size
}
else if ( "cookie" == $lcname || "set-cookie" == $lcname ) {
// We don't save the cookie headers, just the size.
$key = $prefix . "CookieLen";
$resource[$key] = ( array_key_exists($key, $resource) ? $resource[$key] : 0 ) + strlen($value); // increment if already exists
}
else {
// All other headers are lumped together.
$key = $prefix . "OtherHeaders";
$resource[$key] = ( array_key_exists($key, $resource) ? $resource[$key] . ", " : "" ) . "$name = $value"; // append if already exists
}
}
}
// Return TRUE or FALSE indicating if the page's resources are in the requests table.
function resourcesAvailableFromTable($pageid, $tablename=null) {
global $gRequestsTable;
$num = doSimpleQuery("select count(*) from " . ( $tablename ? $tablename : $gRequestsTable ) . " where pageid = '$pageid';");
return $num;
}
// We should NOT be accessing the requests table from the production website.
function getRequestsFromTableDEPRECATED($pageid) {
global $gRequestsTable;
$query = "select * from $gRequestsTable where pageid = '$pageid';";
$result = doQuery($query);
$aReqs = array();
if ( $result ) {
while ( $req = mysqli_fetch_assoc($result) ) {
array_push($aReqs, $req);
}
}
mysqli_free_result($result);
return $aReqs;
}
?>