Skip to content

Instantly share code, notes, and snippets.

@flano-yuki
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flano-yuki/679da913507919760a19 to your computer and use it in GitHub Desktop.
Save flano-yuki/679da913507919760a19 to your computer and use it in GitHub Desktop.
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, "192.168.0.161");
socket_connect($sock, '106.186.112.116', 80);
function create_settings_frame($ack){
$ack_flag = $ack ? 0x01 : 0x00;
$frame = "\0"."\0"."\0". //length
"\4". //type
"$ack_flag".
"\0"."\0"."\0"."\0"; //stream id
return $frame;
}
function length_to_3bytes($length){
return chr( $length >> 16).
chr(($length >> 8 ) & 255).
chr( $length & 255);
}
function encode_headers($headers){
$encoded_headers = "";
foreach($headers as $key => $value){
$prefix = "\0"; //whithout indexing
$encoded_key = chr(strlen($key)).$key;
$encoded_value = chr(strlen($value)).$value;
$encoded_headers .= $prefix. $encoded_key. $encoded_value;
}
return $encoded_headers;
}
function create_headers_frame($headers) {
$encoded_headers = encode_headers($headers);
$frame = length_to_3bytes(strlen($encoded_headers)).
"\1". //type
"\5". //flag
"\0"."\0"."\0"."\1"; //stream id
return $frame. $encoded_headers;
}
$request = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
socket_write($sock, $request);
socket_write($sock, create_settings_frame(false));
$buf = "";
while(true){
if(strlen($buf) >= 9){ //can parse frame header
$length = (ord($buf[0]) << 16 ) +
(ord($buf[1]) << 8 ) +
ord($buf[2]);
if(strlen($buf) < $length + 9){
continue;
}
}
else{
$buf .= socket_read($sock, 10000);
continue;
}
$type = ord($buf[3]);
$flags = ord($buf[4]);
if($type == 0x00){
print("Recieve DATA\n");
print(substr($buf , 9, 100));
}
elseif($type == 0x01){
print("Recieve HEADER\n");
}
elseif($type == 0x04){
if(($flags & 1) == 0){
print("Recieve SETTINGS\n");
print("SEND ACK\n");
socket_write($sock, create_settings_frame(true));
}
if(($flags & 1) == 1){
print("Receive SETTINGS Ack\n");
print("Send HEADERS\n");
$headers = [
":method" => "GET",
":path" => "/",
":scheme" => "http",
":authority" => "nghttp2.org"
];
socket_write($sock, create_headers_frame($headers));
}
}
elseif($type == 0x0a){
print("Recieve ALTSVC\n");
}
$buf = substr($buf,$length + 9);
sleep(1);
}
sleep(1);
$buf += @socket_read($sock, 100);
socket_close($sock);
?>
Recieve SETTINGS
SEND ACK
Recieve ALTSVC
Receive SETTINGS Ack
Send HEADERS
Recieve HEADER
Recieve DATA
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html cRecieve DATA
ent program to output HTTP/2 transaction in well-known
<a href="https://dvcs.w3.org/hg/webperf/raw-f...(ry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment