-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.php
209 lines (129 loc) · 5.32 KB
/
requests.php
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
<?php
/*html parser for parsing html in php*/
include('html_parser.php');
class Requests {
public $totalNumberOfRequests = 0;
public $totalDownloadSize = 0;
/*
method to get the request of url and print it
@method($param) @param = $url
*/
public function getRequests($url){
try{
list($totalNumberOfRequests, $totalDownloadSize) = $this->calculateTotalRequests($url, $this->totalNumberOfRequests, $this->totalDownloadSize);
echo nl2br("Total Number of HTTP requests: $this->totalNumberOfRequests\nTotal Download size for all requests: $this->totalDownloadSize");
}catch (Exception $e){
echo 'Sorry Please Try Again Later!';
}
}
/*
method to calculate total requests & download size of remote url
@method($param1, $param2, $param3)
@param 1 = $url
@param 2 = $totalNumberOfRequests
@param 3 = $totalDownloadSize
*/
public function calculateTotalRequests($url, $totalNumberOfRequests, $totalDownloadSize){
try{
/*If the passed url is not html execute here*/
if (!$this->checkForHtmlUrl($url))
{
$this->totalNumberOfRequests += 1;
$this->totalDownloadSize = $this->getRemoteFileSize($url);
return;
}
/*Is passed url is html continue here*/
$html = file_get_html($url);
/*Checks for all images in html*/
foreach($html->find('img') as $element)
{
$size = $this->getRemoteFileSize($element->src);
$this->totalNumberOfRequests += 1;
$this->totalDownloadSize = $this->totalDownloadSize + $size;
}
/*Checks for all css in html*/
foreach($html->find('link') as $element)
{
if (strpos($element->href,'.css') !== false) {
$size = $this->getRemoteFileSize($element->href);
$this->totalNumberOfRequests += 1;
$this->totalDownloadSize = $this->totalDownloadSize + $size;
}
}
/*Checks for all scripts in html*/
foreach($html->find('script') as $element)
{
if (strpos($element->src,'.js') !== false) {
$size = $this->getRemoteFileSize($element->src);
$this->totalNumberOfRequests += 1;
$this->totalDownloadSize = $this->totalDownloadSize + $size;
}
}
/*Checks for all iframes in html*/
/*
As iframe is used to display a web page within a web page.
we can't count count iframe as single html document, it may contain multiple css,js,images.
have to make use of recursive function to get total number of requests and download size of iframe
*/
foreach($html->find('iframe') as $element)
{
/*recursive function call*/
list($totalNumberOfRequests, $totalDownloadSize) = $this->calculateTotalRequests($element->src, $this->totalNumberOfRequests, $this->totalDownloadSize);
}
return array($this->totalNumberOfRequests, $this->totalDownloadSize) ;
}catch (Exception $e){
echo 'Error In calculating Requests!';
}
}
/*
method to get the remote file size of url
@method($param) @param = $url
*/
public function getRemoteFileSize($url) {
/*Get Headers Approach for finding remote file size*/
$headers = @get_headers($url, true);
if (isset($headers['Content-Length']))
{
return $headers['Content-Length'];
}
if (isset($headers['Content-length']))
{
return $headers['Content-length'];
}
/*cURL Approach for finding remote file size*/
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3'),
));
curl_exec($curl);
$size = curl_getinfo($curl, CURLINFO_SIZE_DOWNLOAD);
return $size;
curl_close($curl);
}
/*
method to check if the passed url is html
@method($param) @param = $url
*/
public function checkForHtmlUrl($url){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_NOBODY, TRUE);
$data = curl_exec($curl);
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE );
curl_close($curl);
/*if content Type is text/html*/
if (strpos($contentType,'text/html') !== false)
{
return TRUE;
}else{
return FALSE;
}
}
}
$url = $argv[1];
$requests = new Requests();
$requests->getRequests($url);
?>