-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebparse.php
executable file
·212 lines (173 loc) · 5.83 KB
/
webparse.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
210
211
212
<?
/*********************************\
webparse.php
A minimal HTTP parsing thing
Written to interface via the web.
Written by Benjamin.Sauls
Twitch
\*********************************/
function get_heads ($host, $port, $path, $method, $version, $headers = NULL) {
// A function to get the response headers from a site.
// This currently uses a connection for each request, but I would like to work in pipelined requests in the future.
// Make sure we have the leading slash for the resource.
if($path[0] != "/") {
$path = "/".$path;
}
// Build the query.
$q = "$method $path HTTP/$version\n";
$q .= $headers;
$q .= "\n\n";
// Open the socket and/or report errors.
$sock = fsockopen($host, $port, $errno, $errmsg);
if(!$sock) { die("*** Socket Error #$errno: $errmsg.\n"); }
// Set a boolean to mark/check for the end of the headers.
$in_header = true;
$hi = 0;
// Send the request down the socket.
fwrite($sock, $q);
// Parse the response as long as we are in the headers.
while($in_header == true) {
$li = fgets($sock);
// Check for the empty line signaling the end of the headers.
if($li == "\r\n") { $in_header = false; }
else {
// Remove the \n from the lines, we can add these manually as needed.
$li = trim($li);
$lines[$hi] = $li;
}
$hi++;
}
// Close the socket.
fclose($sock);
// This leads to interesting TCP behavior.
// The script FINs and tears down the connection as soon as the headers are
// received, but the server continues to send the response in packets which
// are reset by the script. We could use a HEAD method rather than a GET,
// but some servers may not support this.
// Now, parse through the response lines.
// Set up some easier to use variables.
foreach($lines As $lin => $lv) {
if($lin == "0") {
preg_match("/(HTTP\/...) ([0-9]{3}) (.*)/", $lv, $rsline);
$header["response"] = $rsline;
} else {
preg_match("/^([A-Za-z0-9-]*):(.*)/", $lv, $hname);
$header[$hname[1]] = $hname[2];
}
}
$header["q"] = $q;
return $header;
// Return values:
// This will return an associative array using the header names for
// associative indecies.
// There is also a special array 'response' which contains the first
// line of the response. The sub elements of this are then broken further.
// ["response"]
// - [1] = "HTTP/1.1"
// - [2] = "200"
// - [3] = "OK"
}
// Build an array of methods, even though some aren't fully supported by most servers.
// This will make it easier to add/remove these as needed.
$methods = array(
"GET",
"POST",
"HEAD",
"OPTIONS",
"PUT",
"DELETE",
"TRACE",
"CONNECT"
);
// Build our request form now.
?>
<html>
<head>
<title>Request Building / Testing Engine</title>
</head>
<body style="background-color: #dedede;">
<div style="background-color: #aaa8a7;">
<form action="<?=$_SERVER["PHP_SELF"];?>" method="POST">
<table width="75%" align="center">
<tr>
<td align="center">Host:<br><input type="text" name="host" value="<?=$_POST["host"];?>"></td>
</tr>
<tr>
<td align="center">
<select name="method" size="1">
<?
foreach($methods As $m) {
echo " <option value=\"$m\"";
if($_POST["method"] == $m) { echo " SELECTED"; }
echo ">$m</option>\n";
}
?>
</select>
<input type="text" size="50" name="resource" value="<?=$_POST["resource"];?>">
<select name="version" size="1">
<option value="1.1">1.1</option>
<option value="1.0">1.0</option>
<option value="0.9">0.9</option>
</select>
</td>
</tr>
<tr>
<td align="center">
<textarea name="headers" cols="50" rows="10"><?=$_POST["headers"];?></textarea>
</td>
</tr>
<tr>
<td align="center"><input type="submit" value="Request" name="requesting"></td>
</table>
</form>
</div>
<hr>
<?
// If we have just POSTed a request, let's parse and print it.
if(isset($_POST["requesting"])) {
if($_POST["host"] == "") {
// I would like to make this fail nicer later.
die("Please set a host value.");
}
// Run our function to get the headers.
$header = get_heads($_POST["host"], '80', $_POST["resource"], $_POST["method"], $_POST["version"], $_POST["headers"]);
// Let's display the full request so we can remember what's really going on, here.
// First we need to "br" those newlines.
$newq = nl2br($header["q"]);
echo "<div style=\"background-color: #17c077;\">$newq</div>";
// Let's set an array so we can change the colors based on the type of status code.
// Nifty.
$bgs = array(
"1" => "6dafe1",
"2" => "6dafe1",
"3" => "d3b87f",
"4" => "d27777",
"5" => "fc5555"
);
echo "<div style=\"background-color: #". $bgs[$header["response"][2][0]] .";\">";
echo "<hr>";
echo "<b>";
echo $header["response"][1]." ".$header["response"][2]." ".$header["response"][3]."<p>";
echo "</b>";
// This is sort of confusing. I needed to reference the actual response line
// and wanted to be able to print out the query, as well. To do this, though
// I needed to pass them in the return value. So we have to make sure we don't
// confuse these for being headers.
//
// Excluding the query and response, let's spit out the headers.
foreach($header As $header_name => $header_value) {
if($header_name == "response" || $header_name == "q") {
} else {
echo "$header_name:$header_value<br>";
}
}
// I believe I was aiming to set variables for each cookie here, or something.
// Honestly I don't recall. I'll update if I do. =/
preg_match("/([A-Za-z0-9\=\-\:]*);/", $header["Set-Cookie"], $ck);
echo $ck[1]."\n";
$cookies = explode(":", $header["Set-Cookie"]);
}
?>
</div>
</body>
</html>