-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocket.php
More file actions
96 lines (81 loc) · 2.31 KB
/
Socket.php
File metadata and controls
96 lines (81 loc) · 2.31 KB
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
<?php
namespace UltimateGuitar\SwiftSesTransport;
use UltimateGuitar\SwiftSesTransport\Exceptions\SocketInvalidOperationException;
class Socket
{
public function __construct( $socket, $host, $path, $method="POST" )
{
$this->socket = $socket;
$this->write_started = false;
$this->write_finished = false;
$this->read_started = false;
fwrite( $this->socket, "$method $path HTTP/1.1\r\n" );
$this->header( "Host", $host );
if( "POST" == $method ) {
$this->header( "Content-Type", "application/x-www-form-urlencoded" );
}
$this->header( "Connection", "close" );
$this->header( "Transfer-Encoding", "chunked" );
}
/**
* Add an HTTP header
*
* @param $header
* @param $value
*/
public function header ( $header, $value )
{
if( $this->write_started )
{
throw new SocketInvalidOperationException( "Can not write header, body writing has started." );
}
fwrite( $this->socket, "$header: $value\r\n" );
fflush( $this->socket );
}
/**
* Write a chunk of data
* @param $chunk
*/
public function write ( $chunk )
{
if( $this->write_finished )
{
throw new SocketInvalidOperationException( "Can not write, reading has started." );
}
if( ! $this->write_started )
{
fwrite( $this->socket, "\r\n" ); // Start message body
$this->write_started = true;
}
fwrite( $this->socket, sprintf( "%x\r\n", strlen( $chunk ) ) );
fwrite( $this->socket, $chunk . "\r\n" );
fflush( $this->socket );
}
/**
* Finish writing chunks and get ready to read.
*/
public function finishWrite ()
{
$this->write("");
$this->write_finished = true;
}
/**
* Read the socket for a response
*/
public function read ()
{
if( ! $this->write_finished )
{
$this->finishWrite();
}
$this->read_started = true;
$response = new AWSResponse();
while( ! feof( $this->socket ) )
{
$response->line( fgets( $this->socket ) );
}
$response->complete();
fclose( $this->socket );
return $response;
}
}