Skip to content

Commit 7d72f80

Browse files
committed
优化:Cookie 管理器过期数据自动回收
1 parent 9e368b2 commit 7d72f80

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

src/YurunHttp/Cookie/CookieManager.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public function getRequestCookies($uri)
149149
$uriDomain = Uri::getDomain($uri);
150150
$uriPath = $uri->getPath();
151151
$cookieList = &$this->cookieList;
152+
$time = time();
152153
foreach ($this->relationMap as $relationDomain => $list1)
153154
{
154155
if ('' === $relationDomain || $this->checkDomain($uriDomain, $relationDomain))
@@ -160,7 +161,7 @@ public function getRequestCookies($uri)
160161
foreach ($idList as $id)
161162
{
162163
$cookieItem = $cookieList[$id];
163-
if ((0 === $cookieItem->expires || $cookieItem->expires > time()) && (!$cookieItem->secure || 'https' === $uri->getScheme() || 'wss' === $uri->getScheme()))
164+
if ((0 === $cookieItem->expires || $cookieItem->expires > $time) && (!$cookieItem->secure || 'https' === $uri->getScheme() || 'wss' === $uri->getScheme()))
164165
{
165166
$result[$cookieItem->name] = $cookieItem->value;
166167
}
@@ -212,6 +213,30 @@ public function getCookieItem($name, $domain = '', $path = '/')
212213
return null;
213214
}
214215

216+
/**
217+
* 自动回收过期 Cookie 占用的空间.
218+
*
219+
* @return void
220+
*/
221+
public function gc()
222+
{
223+
if ($this->cookieList)
224+
{
225+
$time = time();
226+
foreach ($this->cookieList as $id => $item)
227+
{
228+
if ($item->expires > 0 && $time >= $item->expires)
229+
{
230+
unset($this->cookieList[$id]);
231+
if (isset($this->relationMap[$item->domain][$item->path][$item->name]) && $id === $this->relationMap[$item->domain][$item->path][$item->name])
232+
{
233+
unset($this->relationMap[$item->domain][$item->path][$item->name]);
234+
}
235+
}
236+
}
237+
}
238+
}
239+
215240
/**
216241
* 检查 uri 域名和 cookie 域名.
217242
*

src/YurunHttp/Handler/Curl.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public function send(&$request)
219219
->withError(sprintf('Maximum (%s) redirects followed', $maxRedirects));
220220
}
221221
}
222+
$this->cookieManager->gc();
222223
break;
223224
} while (true);
224225
// 关闭保存至文件的句柄
@@ -772,6 +773,7 @@ public function coBatch($requests, $timeout = null)
772773
}
773774
$result[$k] = $response;
774775
}
776+
$this->cookieManager->gc();
775777
}
776778
finally
777779
{

src/YurunHttp/Handler/Swoole.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ public function recvDefer($request, $timeout = null)
433433
{
434434
$result = $result->withSavedFileName($savedFileName);
435435
}
436+
$this->cookieManager->gc();
436437

437438
return $result;
438439
}

tests/server/Http/server.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@
4646
'Yurun-Http' => 'one suo',
4747
]);
4848
$response->cookie('a', '1');
49-
$response->cookie('b', '2', 1);
50-
$response->cookie('c', '3', 0, '/');
51-
$response->cookie('d', '4', 0, '/a');
52-
$response->cookie('e', '5', 0, '/', 'localhost');
53-
$response->cookie('f', '6', 0, '/', '', true);
54-
$response->cookie('g', '7', 0, '/', '', true, true);
49+
$response->cookie('b', '2', 0);
50+
$response->cookie('c', '3', null, '/');
51+
$response->cookie('d', '4', null, '/a');
52+
$response->cookie('e', '5', null, '/', 'localhost');
53+
$response->cookie('f', '6', null, '/', '', true);
54+
$response->cookie('g', '7', null, '/', '', true, true);
5555
$connection->send($response);
5656
break;
5757
case 'redirect301':
@@ -84,6 +84,13 @@
8484
'Location' => $request->get('url', '/'),
8585
], 'test'));
8686
break;
87+
case 'redirectCookie':
88+
$response = new Response(301, [
89+
'Location' => '/?a=info',
90+
]);
91+
$response->cookie('redirectCookie', '1');
92+
$connection->send($response);
93+
break;
8794
case 'download1':
8895
if ('nb' === $request->post('yurunhttp') && 'POST' === $request->method())
8996
{

tests/unit/HttpRequestTest/HttpRequestTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public function testCookieManager()
202202

203203
static $compareCookie = [
204204
'a' => '1',
205+
'c' => '3',
205206
];
206207

207208
$data = null;
@@ -226,6 +227,8 @@ public function testCookieManager()
226227
$this->assertNotNull($cookieItem);
227228
$this->assertFalse($cookieItem->httpOnly);
228229

230+
$this->assertNull($cookieManager->getCookieItem('b'));
231+
229232
$cookieItem = $cookieManager->getCookieItem('g');
230233
$this->assertNotNull($cookieItem);
231234
$this->assertTrue($cookieItem->httpOnly);
@@ -311,6 +314,23 @@ public function testRedirectOther()
311314
});
312315
}
313316

317+
/**
318+
* @return void
319+
*/
320+
public function testRedirectCookie()
321+
{
322+
$this->call(function () {
323+
$http = new HttpRequest();
324+
325+
$response = $http->post($this->host . '?a=redirectCookie');
326+
$data = $response->json(true);
327+
$this->assertEquals('1', $data['cookie']['redirectCookie'] ?? null);
328+
$cookieItem = $http->getHandler()->getCookieManager()->getCookieItem('redirectCookie');
329+
$this->assertNotNull($cookieItem);
330+
$this->assertEquals('1', $cookieItem->value);
331+
});
332+
}
333+
314334
/**
315335
* Upload single file.
316336
*

0 commit comments

Comments
 (0)