6
6
use Gt \Curl \CurlMulti ;
7
7
use Gt \Curl \CurlMultiInterface ;
8
8
use Gt \Http \Header \Parser ;
9
- use Gt \Http \Response ;
10
9
use Psr \Http \Message \UriInterface ;
11
10
use React \EventLoop \LoopInterface ;
12
11
use React \Promise \Deferred ;
12
+ use React \Promise \Promise ;
13
13
14
14
class RequestResolver {
15
15
/** @var LoopInterface */
16
16
protected $ loop ;
17
- /** @var CurlMultiInterface */
18
- protected $ curlMulti ;
19
17
18
+ /** @var CurlMultiInterface[] */
19
+ protected $ curlMultiList ;
20
20
/** @var CurlInterface[] */
21
21
protected $ curlList ;
22
- /** @var Deferred [] */
22
+ /** @var Promise [] */
23
23
protected $ deferredList ;
24
- /** @var Response [] */
24
+ /** @var BodyResponse [] */
25
25
protected $ responseList ;
26
26
/** @var string[] */
27
- protected $ headersList ;
27
+ protected $ headerList ;
28
28
29
- public function __construct (
30
- LoopInterface $ loop ,
31
- string $ curlMultiClass = CurlMulti::class
32
- ) {
29
+ public function __construct (LoopInterface $ loop ) {
33
30
$ this ->loop = $ loop ;
34
31
$ this ->curlList = [];
32
+ $ this ->curlMultiList = [];
35
33
$ this ->deferredList = [];
36
- $ this ->headersList = [];
37
- $ this ->curlMulti = new $ curlMultiClass ();
34
+ $ this ->headerList = [];
38
35
}
39
36
40
37
public function add (
@@ -49,78 +46,108 @@ public function add(
49
46
}
50
47
51
48
$ curl ->setOpt (CURLOPT_RETURNTRANSFER , false );
52
- $ curl ->setOpt (CURLOPT_HEADER , true );
49
+ $ curl ->setOpt (CURLOPT_HEADER , false );
50
+ $ curl ->setOpt (CURLOPT_HEADERFUNCTION , [$ this , "writeHeader " ]);
53
51
$ curl ->setOpt (CURLOPT_WRITEFUNCTION , [$ this , "write " ]);
54
52
55
- $ this ->curlMulti ->add ($ curl );
53
+ $ curlMulti = new CurlMulti ();
54
+ $ curlMulti ->add ($ curl );
55
+
56
56
$ this ->curlList []= $ curl ;
57
+ $ this ->curlMultiList []= $ curlMulti ;
57
58
$ this ->deferredList []= $ deferred ;
58
- $ this ->headersList []= "" ;
59
- $ this ->responseList []= new Response () ;
59
+ $ this ->responseList []= new BodyResponse () ;
60
+ $ this ->headerList []= "" ;
60
61
}
61
62
62
- public function write ($ chIncoming , $ content ):int {
63
- $ match = false ;
64
- foreach ($ this ->curlList as $ i => $ curl ) {
65
- if ($ chIncoming === $ curl ->getHandle ()) {
66
- $ match = true ;
67
- break ;
68
- }
69
- }
63
+ public function write ($ ch , $ content ):int {
64
+ $ i = $ this ->getIndex ($ ch );
70
65
71
- if (!$ match ) {
72
- // TODO: Throw exception.
73
- die ("NO CURL HANDLE!!!! " );
66
+ $ body = $ this ->responseList [$ i ]->getBody ();
67
+ $ body ->write ($ content );
68
+
69
+ if ($ this ->deferredList [$ i ]) {
70
+ $ this ->deferredList [$ i ]->resolve ($ this ->responseList [$ i ]);
71
+ $ this ->deferredList [$ i ] = null ;
74
72
}
75
73
76
- if (!strstr ($ this ->headersList [$ i ], "\r\n\r\n" )) {
77
- echo "1 " ;
78
- if (strstr ($ content , "\r\n\r\n" )) {
79
- $ parser = new Parser ($ this ->headersList [$ i ]);
80
- $ this ->responseList [$ i ] = $ this ->responseList [$ i ]->withProtocolVersion (
81
- $ parser ->getProtocolVersion ()
82
- );
83
- $ this ->responseList [$ i ] = $ this ->responseList [$ i ]->withStatus (
84
- $ parser ->getStatusCode ()
85
- );
86
- foreach ($ parser ->getKeyValues () as $ key => $ value ) {
87
- $ this ->responseList [$ i ] = $ this ->responseList [$ i ]->withAddedHeader (
88
- $ key ,
89
- $ value
90
- );
74
+ return strlen ($ content );
75
+ }
76
+
77
+ public function writeHeader ($ ch , string $ rawHeader ) {
78
+ $ i = $ this ->getIndex ($ ch );
79
+
80
+ $ headerLine = trim ($ rawHeader );
81
+
82
+ if ($ headerLine === "" ) {
83
+ $ parser = new Parser ($ this ->headerList [$ i ]);
84
+ $ this ->responseList [$ i ] = $ this ->responseList [$ i ]->withProtocolVersion (
85
+ $ parser ->getProtocolVersion ()
86
+ );
87
+ $ this ->responseList [$ i ] = $ this ->responseList [$ i ]->withStatus (
88
+ $ parser ->getStatusCode ()
89
+ );
90
+ foreach ($ parser ->getKeyValues () as $ key => $ value ) {
91
+ if (empty ($ key )) {
92
+ continue ;
91
93
}
94
+ $ this ->responseList [$ i ] = $ this ->responseList [$ i ]->withAddedHeader (
95
+ $ key ,
96
+ $ value
97
+ );
92
98
}
93
-
94
- $ this ->headersList [$ i ] .= $ content ;
95
- }
96
- else {
97
- echo "2 " ;
98
- $ body = $ this ->responseList [$ i ]->getBody ();
99
- $ body ->write ($ content );
100
- $ this ->deferredList [$ i ]->resolve ($ body );
101
99
}
102
100
103
- echo ". " ;
104
-
105
- return strlen ($ content );
101
+ $ this ->headerList [$ i ] .= $ rawHeader ;
102
+ return strlen ($ rawHeader );
106
103
}
107
104
108
105
public function tick ():void {
109
- $ active = 0 ;
106
+ $ totalActive = 0 ;
107
+
108
+ foreach ($ this ->curlMultiList as $ i => $ curlMulti ) {
109
+ $ active = 0 ;
110
+
111
+ do {
112
+ $ status = $ curlMulti ->exec ($ active );
113
+ }
114
+ while ($ status === CURLM_CALL_MULTI_PERFORM );
115
+
116
+ if ($ status !== CURLM_OK ) {
117
+ // TODO: Throw exception.
118
+ die ("ERROR! " );
119
+ }
120
+
121
+ $ totalActive += $ active ;
122
+
123
+ if ($ active === 0 ) {
124
+ // TODO: Resolve body's complete promise.
125
+ $ this ->responseList [$ i ]->completeResponse ();
126
+ }
110
127
111
- do {
112
- $ status = $ this ->curlMulti ->exec ($ active );
113
- }
114
- while ($ status === CURLM_CALL_MULTI_PERFORM );
115
128
116
- if ($ status !== CURLM_OK ) {
117
- // TODO: Throw exception.
118
- die ("ERROR! " );
119
129
}
120
130
121
- if ($ active === 0 ) {
131
+ if ($ totalActive === 0 ) {
122
132
$ this ->loop ->stop ();
123
133
return ;
124
134
}
125
135
}
136
+
137
+ protected function getIndex ($ chIncoming ):int {
138
+ $ match = false ;
139
+ foreach ($ this ->curlList as $ i => $ curl ) {
140
+ if ($ chIncoming === $ curl ->getHandle ()) {
141
+ $ match = true ;
142
+ break ;
143
+ }
144
+ }
145
+
146
+ if (!$ match ) {
147
+ // TODO: Throw exception.
148
+ die ("NO CURL HANDLE!!!! " );
149
+ }
150
+
151
+ return $ i ;
152
+ }
126
153
}
0 commit comments