-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpAssignment.php
67 lines (53 loc) · 1.68 KB
/
httpAssignment.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
<?PHP
error_reporting(E_ALL);
$host = $argv[1];
/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname($host);
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " .
socket_strerror(socket_last_error()) . "\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...\n";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " .
socket_strerror(socket_last_error($socket)) . "\n";
}
$in = "";
$in .= "HEAD / HTTP/1.1\r\n";
$in .= "Host: $host\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';
$response = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
$response .= $out;
}
$seperated_response = explode(" ",$response,2);
$html_version = $seperated_response[0];
$rest = $seperated_response[1];
$seperated_rest = explode("\n",$rest,2);
$status_code = $seperated_rest[0];
$response_message = explode("\n",$seperated_rest[1]);
$response = array();
echo "\n\n$html_version\n $status_code\n\n";
foreach ($response_message as $res) {
$arr = explode(":",$res,2);
if(sizeof($arr)>=2){
$response[$arr[0]] = $arr[1];
}
}
foreach($response as $key => $value){
if($key!=NULL || $value!=NULL){
echo "$key : $value\n";
}
}
socket_close($socket);
?>