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
1f3f6612
Commit
1f3f6612
authored
May 16, 2013
by
Ragazzo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed range requests, tests improved
parent
71c60b63
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
8 deletions
+35
-8
ResponseTest.php
tests/unit/framework/web/ResponseTest.php
+28
-5
Response.php
yii/web/Response.php
+7
-3
No files found.
tests/unit/framework/web/ResponseTest.php
View file @
1f3f6612
...
@@ -41,7 +41,7 @@ class ResponseTest extends \yiiunit\TestCase
...
@@ -41,7 +41,7 @@ class ResponseTest extends \yiiunit\TestCase
static
::
$httpResponseCode
=
200
;
static
::
$httpResponseCode
=
200
;
}
}
public
function
ranges
()
public
function
r
ightR
anges
()
{
{
// TODO test more cases for range requests and check for rfc compatibility
// TODO test more cases for range requests and check for rfc compatibility
// http://www.w3.org/Protocols/rfc2616/rfc2616.txt
// http://www.w3.org/Protocols/rfc2616/rfc2616.txt
...
@@ -53,14 +53,14 @@ class ResponseTest extends \yiiunit\TestCase
...
@@ -53,14 +53,14 @@ class ResponseTest extends \yiiunit\TestCase
}
}
/**
/**
* @dataProvider ranges
* @dataProvider r
ightR
anges
*/
*/
public
function
testSendFileRanges
(
$rangeHeader
,
$expectedHeader
,
$length
,
$expectedFile
)
public
function
testSendFileRanges
(
$rangeHeader
,
$expectedHeader
,
$length
,
$expectedFile
)
{
{
$content
=
$this
->
generateTestFileContent
();
$content
=
$this
->
generateTestFileContent
();
$_SERVER
[
'HTTP_RANGE'
]
=
'bytes='
.
$rangeHeader
;
$_SERVER
[
'HTTP_RANGE'
]
=
'bytes='
.
$rangeHeader
;
$sent
=
$this
->
runSendFile
(
'testFile.txt'
,
$content
,
null
);
$sent
=
$this
->
runSendFile
(
'testFile.txt'
,
$content
,
null
);
$this
->
assertEquals
(
$expectedFile
,
$sent
);
$this
->
assertEquals
(
$expectedFile
,
$sent
);
$this
->
assertTrue
(
in_array
(
'HTTP/1.1 206 Partial Content'
,
static
::
$headers
));
$this
->
assertTrue
(
in_array
(
'HTTP/1.1 206 Partial Content'
,
static
::
$headers
));
$this
->
assertTrue
(
in_array
(
'Accept-Ranges: bytes'
,
static
::
$headers
));
$this
->
assertTrue
(
in_array
(
'Accept-Ranges: bytes'
,
static
::
$headers
));
...
@@ -69,6 +69,30 @@ class ResponseTest extends \yiiunit\TestCase
...
@@ -69,6 +69,30 @@ class ResponseTest extends \yiiunit\TestCase
$this
->
assertTrue
(
in_array
(
'Content-Length: '
.
$length
,
static
::
$headers
));
$this
->
assertTrue
(
in_array
(
'Content-Length: '
.
$length
,
static
::
$headers
));
}
}
public
function
wrongRanges
()
{
// TODO test more cases for range requests and check for rfc compatibility
// http://www.w3.org/Protocols/rfc2616/rfc2616.txt
return
array
(
array
(
'1-2,3-5,6-10'
),
// multiple range request not supported
array
(
'5-1'
),
// last-byte-pos value is less than its first-byte-pos value
array
(
'-100000'
),
// last-byte-pos bigger then content length
array
(
'10000-'
),
// first-byte-pos bigger then content length
);
}
/**
* @dataProvider wrongRanges
*/
public
function
testSendFileWrongRanges
(
$rangeHeader
)
{
$this
->
setExpectedException
(
'yii\base\HttpException'
,
'Requested Range Not Satisfiable'
);
$content
=
$this
->
generateTestFileContent
();
$_SERVER
[
'HTTP_RANGE'
]
=
'bytes='
.
$rangeHeader
;
$this
->
runSendFile
(
'testFile.txt'
,
$content
,
null
);
}
protected
function
generateTestFileContent
()
protected
function
generateTestFileContent
()
{
{
return
'12ёжик3456798áèabcdefghijklmnopqrstuvwxyz!"§$%&/(ёжик)=?'
;
return
'12ёжик3456798áèabcdefghijklmnopqrstuvwxyz!"§$%&/(ёжик)=?'
;
...
@@ -83,4 +107,4 @@ class ResponseTest extends \yiiunit\TestCase
...
@@ -83,4 +107,4 @@ class ResponseTest extends \yiiunit\TestCase
$file
=
ob_get_clean
();
$file
=
ob_get_clean
();
return
$file
;
return
$file
;
}
}
}
}
\ No newline at end of file
yii/web/Response.php
View file @
1f3f6612
...
@@ -50,7 +50,7 @@ class Response extends \yii\base\Response
...
@@ -50,7 +50,7 @@ class Response extends \yii\base\Response
if
(
isset
(
$_SERVER
[
'HTTP_RANGE'
]))
{
if
(
isset
(
$_SERVER
[
'HTTP_RANGE'
]))
{
// client sent us a multibyte range, can not hold this one for now
// client sent us a multibyte range, can not hold this one for now
if
(
strpos
(
','
,
$_SERVER
[
'HTTP_RANGE'
]
)
!==
false
)
{
if
(
strpos
(
$_SERVER
[
'HTTP_RANGE'
],
','
)
!==
false
)
{
header
(
"Content-Range: bytes
$contentStart
-
$contentEnd
/
$fileSize
"
);
header
(
"Content-Range: bytes
$contentStart
-
$contentEnd
/
$fileSize
"
);
throw
new
HttpException
(
416
,
'Requested Range Not Satisfiable'
);
throw
new
HttpException
(
416
,
'Requested Range Not Satisfiable'
);
}
}
...
@@ -63,14 +63,18 @@ class Response extends \yii\base\Response
...
@@ -63,14 +63,18 @@ class Response extends \yii\base\Response
}
else
{
}
else
{
$range
=
explode
(
'-'
,
$range
);
$range
=
explode
(
'-'
,
$range
);
$contentStart
=
$range
[
0
];
$contentStart
=
$range
[
0
];
$contentEnd
=
(
isset
(
$range
[
1
])
&&
is_numeric
(
$range
[
1
]))
?
$range
[
1
]
:
$fileSize
-
1
;
// check if the last-byte-pos presents in header
if
((
isset
(
$range
[
1
])
&&
is_numeric
(
$range
[
1
])))
{
$contentEnd
=
$range
[
1
];
}
}
}
/* Check the range and make sure it's treated according to the specs.
/* Check the range and make sure it's treated according to the specs.
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
*/
*/
// End bytes can not be larger than $end.
// End bytes can not be larger than $end.
$contentEnd
=
(
$contentEnd
>
$fileSize
)
?
$fileSize
:
$contentEnd
;
$contentEnd
=
(
$contentEnd
>
$fileSize
)
?
$fileSize
-
1
:
$contentEnd
;
// Validate the requested range and return an error if it's not correct.
// Validate the requested range and return an error if it's not correct.
$wrongContentStart
=
(
$contentStart
>
$contentEnd
||
$contentStart
>
$fileSize
-
1
||
$contentStart
<
0
);
$wrongContentStart
=
(
$contentStart
>
$contentEnd
||
$contentStart
>
$fileSize
-
1
||
$contentStart
<
0
);
...
...
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