Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
45dbbc39
Commit
45dbbc39
authored
Aug 16, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Additional fix to #4728.
parent
109da0b7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
13 deletions
+18
-13
caching-http.md
docs/guide/caching-http.md
+3
-5
HttpCache.php
framework/filters/HttpCache.php
+7
-4
HttpCacheTest.php
tests/unit/framework/filters/HttpCacheTest.php
+8
-4
No files found.
docs/guide/caching-http.md
View file @
45dbbc39
...
@@ -107,12 +107,10 @@ Expensive ETag generation may defeat the purpose of using `HttpCache` and introd
...
@@ -107,12 +107,10 @@ Expensive ETag generation may defeat the purpose of using `HttpCache` and introd
since they need to be re-evaluated on every request. Try to find a simple expression that invalidates
since they need to be re-evaluated on every request. Try to find a simple expression that invalidates
the cache if the page content has been modified.
the cache if the page content has been modified.
> Note: In compliance to [RFC 7232](http://tools.ietf.org/html/rfc7232#section-2.4),
> Note: In compliance to [RFC 7232, section 2.4](http://tools.ietf.org/html/rfc7232#section-2.4),
`HttpCache`
will send out both
`ETag`
and
`Last-Modified`
headers if they are both configured.
`HttpCache`
will send out both
`ETag`
and
`Last-Modified`
headers if they are both configured.
However, in order to satisfy
[
section 3.3
](
http://tools.ietf.org/html/rfc7232#section-3.3
)
the
And if the client sends both of the
`If-None-Match`
header and the
`If-Modified-Since`
header, only the former
`If-None-Match`
client header will always take precedence over
`If-Modified-Since`
during validation; meaning
will be respected.
latter one is going to be ignored if a
`If-None-Match`
header is present in the request.
## `Cache-Control` Header <a name="cache-control"></a>
## `Cache-Control` Header <a name="cache-control"></a>
...
...
framework/filters/HttpCache.php
View file @
45dbbc39
...
@@ -130,7 +130,6 @@ class HttpCache extends ActionFilter
...
@@ -130,7 +130,6 @@ class HttpCache extends ActionFilter
if
(
$this
->
validateCache
(
$lastModified
,
$etag
))
{
if
(
$this
->
validateCache
(
$lastModified
,
$etag
))
{
$response
->
setStatusCode
(
304
);
$response
->
setStatusCode
(
304
);
return
false
;
return
false
;
}
}
...
@@ -150,10 +149,14 @@ class HttpCache extends ActionFilter
...
@@ -150,10 +149,14 @@ class HttpCache extends ActionFilter
*/
*/
protected
function
validateCache
(
$lastModified
,
$etag
)
protected
function
validateCache
(
$lastModified
,
$etag
)
{
{
if
(
$etag
!==
null
&&
in_array
(
$etag
,
Yii
::
$app
->
request
->
getEtags
(),
true
))
{
if
(
isset
(
$_SERVER
[
'HTTP_IF_NONE_MATCH'
]))
{
return
true
;
// HTTP_IF_NONE_MATCH takes precedence over HTTP_IF_MODIFIED_SINCE
// http://tools.ietf.org/html/rfc7232#section-3.3
return
$etag
===
null
||
in_array
(
$etag
,
Yii
::
$app
->
request
->
getEtags
(),
true
);
}
elseif
(
isset
(
$_SERVER
[
'HTTP_IF_MODIFIED_SINCE'
]))
{
return
$lastModified
===
null
||
@
strtotime
(
$_SERVER
[
'HTTP_IF_MODIFIED_SINCE'
])
>=
$lastModified
;
}
else
{
}
else
{
return
$
lastModified
!==
null
&&
isset
(
$_SERVER
[
'HTTP_IF_MODIFIED_SINCE'
])
&&
@
strtotime
(
$_SERVER
[
'HTTP_IF_MODIFIED_SINCE'
])
>=
$lastModified
;
return
$
etag
===
null
&&
$lastModified
===
null
;
}
}
}
}
...
...
tests/unit/framework/filters/HttpCacheTest.php
View file @
45dbbc39
...
@@ -37,7 +37,10 @@ class HttpCacheTest extends \yiiunit\TestCase
...
@@ -37,7 +37,10 @@ class HttpCacheTest extends \yiiunit\TestCase
$method
=
new
\ReflectionMethod
(
$httpCache
,
'validateCache'
);
$method
=
new
\ReflectionMethod
(
$httpCache
,
'validateCache'
);
$method
->
setAccessible
(
true
);
$method
->
setAccessible
(
true
);
$this
->
assertFalse
(
$method
->
invoke
(
$httpCache
,
null
,
null
));
unset
(
$_SERVER
[
'HTTP_IF_MODIFIED_SINCE'
],
$_SERVER
[
'HTTP_IF_NONE_MATCH'
]);
$this
->
assertTrue
(
$method
->
invoke
(
$httpCache
,
null
,
null
));
$this
->
assertFalse
(
$method
->
invoke
(
$httpCache
,
0
,
null
));
$this
->
assertFalse
(
$method
->
invoke
(
$httpCache
,
0
,
'"foo"'
));
$_SERVER
[
'HTTP_IF_MODIFIED_SINCE'
]
=
'Thu, 01 Jan 1970 00:00:00 GMT'
;
$_SERVER
[
'HTTP_IF_MODIFIED_SINCE'
]
=
'Thu, 01 Jan 1970 00:00:00 GMT'
;
$this
->
assertTrue
(
$method
->
invoke
(
$httpCache
,
0
,
null
));
$this
->
assertTrue
(
$method
->
invoke
(
$httpCache
,
0
,
null
));
...
@@ -45,8 +48,10 @@ class HttpCacheTest extends \yiiunit\TestCase
...
@@ -45,8 +48,10 @@ class HttpCacheTest extends \yiiunit\TestCase
$_SERVER
[
'HTTP_IF_NONE_MATCH'
]
=
'"foo"'
;
$_SERVER
[
'HTTP_IF_NONE_MATCH'
]
=
'"foo"'
;
$this
->
assertTrue
(
$method
->
invoke
(
$httpCache
,
0
,
'"foo"'
));
$this
->
assertTrue
(
$method
->
invoke
(
$httpCache
,
0
,
'"foo"'
));
$this
->
assertFalse
(
$method
->
invoke
(
$httpCache
,
0
,
'"foos"'
));
$this
->
assertTrue
(
$method
->
invoke
(
$httpCache
,
1
,
'"foo"'
));
$this
->
assertTrue
(
$method
->
invoke
(
$httpCache
,
1
,
'"foo"'
));
$this
->
assertFalse
(
$method
->
invoke
(
$httpCache
,
null
,
null
));
$this
->
assertFalse
(
$method
->
invoke
(
$httpCache
,
1
,
'"foos"'
));
$this
->
assertTrue
(
$method
->
invoke
(
$httpCache
,
null
,
null
));
}
}
/**
/**
...
@@ -67,4 +72,4 @@ class HttpCacheTest extends \yiiunit\TestCase
...
@@ -67,4 +72,4 @@ class HttpCacheTest extends \yiiunit\TestCase
$this
->
assertStringStartsWith
(
'"'
,
$etag
);
$this
->
assertStringStartsWith
(
'"'
,
$etag
);
$this
->
assertStringEndsWith
(
'"'
,
$etag
);
$this
->
assertStringEndsWith
(
'"'
,
$etag
);
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment