@@ -46,13 +46,6 @@ class Http
46
46
*/
47
47
protected static $ _uploadTmpDir = '' ;
48
48
49
- /**
50
- * Cache.
51
- *
52
- * @var array
53
- */
54
- protected static $ _cache = array ();
55
-
56
49
/**
57
50
* Open cache.
58
51
*
@@ -107,12 +100,15 @@ public static function enableCache($value)
107
100
*/
108
101
public static function input ($ recv_buffer , TcpConnection $ connection )
109
102
{
103
+ static $ input = array ();
104
+ if (!isset ($ recv_buffer [512 ]) && isset ($ input [$ recv_buffer ])) {
105
+ return $ input [$ recv_buffer ];
106
+ }
110
107
$ crlf_pos = \strpos ($ recv_buffer , "\r\n\r\n" );
111
108
if (false === $ crlf_pos ) {
112
109
// Judge whether the package length exceeds the limit.
113
110
if ($ recv_len = \strlen ($ recv_buffer ) >= 16384 ) {
114
- $ connection ->send ("HTTP/1.1 413 Request Entity Too Large \r\n\r\n" );
115
- $ connection ->consumeRecvBuffer ($ recv_len );
111
+ $ connection ->close ("HTTP/1.1 413 Request Entity Too Large \r\n\r\n" );
116
112
return 0 ;
117
113
}
118
114
return 0 ;
@@ -121,22 +117,39 @@ public static function input($recv_buffer, TcpConnection $connection)
121
117
$ head_len = $ crlf_pos + 4 ;
122
118
$ method = \strstr ($ recv_buffer , ' ' , true );
123
119
124
- if ($ method === 'GET ' || $ method === 'OPTIONS ' || $ method === 'HEAD ' ) {
120
+ if ($ method === 'GET ' || $ method === 'OPTIONS ' || $ method === 'HEAD ' || $ method === 'DELETE ' ) {
121
+ if (!isset ($ recv_buffer [512 ])) {
122
+ $ input [$ recv_buffer ] = $ head_len ;
123
+ if (\count ($ input ) > 512 ) {
124
+ unset($ input [key ($ input )]);
125
+ }
126
+ }
125
127
return $ head_len ;
126
- } else if ($ method !== 'POST ' && $ method !== 'PUT ' && $ method !== 'DELETE ' ) {
127
- $ connection ->send ("HTTP/1.1 400 Bad Request \r\n\r\n" , true );
128
- $ connection ->consumeRecvBuffer (\strlen ($ recv_buffer ));
128
+ } else if ($ method !== 'POST ' && $ method !== 'PUT ' ) {
129
+ $ connection ->close ("HTTP/1.1 400 Bad Request \r\n\r\n" , true );
129
130
return 0 ;
130
131
}
131
132
132
133
$ header = \substr ($ recv_buffer , 0 , $ crlf_pos );
134
+ $ length = false ;
133
135
if ($ pos = \strpos ($ header , "\r\nContent-Length: " )) {
134
- return $ head_len + (int )\substr ($ header , $ pos + 18 , 10 );
136
+ $ length = $ head_len + (int )\substr ($ header , $ pos + 18 , 10 );
135
137
} else if (\preg_match ("/ \r\ncontent-length: ?(\d+)/i " , $ header , $ match )) {
136
- return $ head_len + $ match [1 ];
138
+ $ length = $ head_len + $ match [1 ];
139
+ }
140
+
141
+ if ($ length !== false ) {
142
+ if (!isset ($ recv_buffer [512 ])) {
143
+ $ input [$ recv_buffer ] = $ length ;
144
+ if (\count ($ input ) > 512 ) {
145
+ unset($ input [key ($ input )]);
146
+ }
147
+ }
148
+ return $ length ;
137
149
}
138
150
139
- return $ method === 'DELETE ' ? $ head_len : 0 ;
151
+ $ connection ->close ("HTTP/1.1 400 Bad Request \r\n\r\n" , true );
152
+ return 0 ;
140
153
}
141
154
142
155
/**
@@ -148,9 +161,10 @@ public static function input($recv_buffer, TcpConnection $connection)
148
161
*/
149
162
public static function decode ($ recv_buffer , TcpConnection $ connection )
150
163
{
164
+ static $ requests = array ();
151
165
$ cacheable = static ::$ _enableCache && !isset ($ recv_buffer [512 ]);
152
- if (true === $ cacheable && isset (static :: $ _cache [$ recv_buffer ])) {
153
- $ request = static :: $ _cache [$ recv_buffer ];
166
+ if (true === $ cacheable && isset ($ requests [$ recv_buffer ])) {
167
+ $ request = $ requests [$ recv_buffer ];
154
168
$ request ->connection = $ connection ;
155
169
$ connection ->__request = $ request ;
156
170
$ request ->properties = array ();
@@ -160,9 +174,9 @@ public static function decode($recv_buffer, TcpConnection $connection)
160
174
$ request ->connection = $ connection ;
161
175
$ connection ->__request = $ request ;
162
176
if (true === $ cacheable ) {
163
- static :: $ _cache [$ recv_buffer ] = $ request ;
164
- if (\count (static :: $ _cache ) > 512 ) {
165
- unset(static :: $ _cache [key (static :: $ _cache )]);
177
+ $ requests [$ recv_buffer ] = $ request ;
178
+ if (\count ($ requests ) > 512 ) {
179
+ unset($ requests [key ($ requests )]);
166
180
}
167
181
}
168
182
return $ request ;
0 commit comments