Commit bde99eae authored by horo@chromium.org's avatar horo@chromium.org

[ServiceWorker] Send the body of Request in fetch() API.


TEST=LayoutTests/http/tests/serviceworker/resources/request-worker.js
BUG=402387

Review URL: https://codereview.chromium.org/533123002

git-svn-id: svn://svn.chromium.org/blink/trunk@181436 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 4ad85680
...@@ -44,10 +44,30 @@ if (isset($_COOKIE['cookie'])) { ...@@ -44,10 +44,30 @@ if (isset($_COOKIE['cookie'])) {
$cookie = $_COOKIE['cookie']; $cookie = $_COOKIE['cookie'];
} }
$files = array();
foreach ($_FILES as $key => $file) {
$content = '';
$fp = fopen($file['tmp_name'], 'r');
if ($fp) {
$content = $file['size'] > 0 ? fread($fp, $file['size']) : '';
fclose($fp);
}
$files[] = array('key' => $key,
'name' => $file['name'],
'type' => $file['type'],
'error' => $file['error'],
'size' => $file['size'],
'content' => $content);
}
header('Content-Type: application/json'); header('Content-Type: application/json');
$arr = array('jsonpResult' => 'success', $arr = array('jsonpResult' => 'success',
'method' => $_SERVER['REQUEST_METHOD'], 'method' => $_SERVER['REQUEST_METHOD'],
'headers' => getallheaders(), 'headers' => getallheaders(),
'body' => file_get_contents('php://input'),
'files' => $files,
'get' => $_GET,
'post' => $_POST,
'username' => $username, 'username' => $username,
'password' => $password, 'password' => $password,
'cookie' => $cookie); 'cookie' => $cookie);
......
...@@ -32,6 +32,113 @@ async_test(function(t) { ...@@ -32,6 +32,113 @@ async_test(function(t) {
.catch(unreached_rejection(t)); .catch(unreached_rejection(t));
}, 'Fetch result of 404 response in ServiceWorkerGlobalScope'); }, 'Fetch result of 404 response in ServiceWorkerGlobalScope');
function evalJsonp(text) {
return new Promise(function(resolve) {
var report = resolve;
// text must contain report() call.
eval(text);
});
}
async_test(function(t) {
var request =
new Request('fetch-access-control.php',
{
method: 'POST',
body: new Blob(['Test Blob'], {type: 'test/type'})
});
fetch(request)
.then(function(response) { return response.body.asText(); })
.then(evalJsonp)
.then(function(result) {
assert_equals(result.method, 'POST');
assert_equals(result.body, 'Test Blob');
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch with Blob body test in ServiceWorkerGlobalScope');
async_test(function(t) {
var request = new Request('fetch-access-control.php',
{method: 'POST', body: 'Test String'});
fetch(request)
.then(function(response) { return response.body.asText(); })
.then(evalJsonp)
.then(function(result) {
assert_equals(result.method, 'POST');
assert_equals(result.body, 'Test String');
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch with string body test in ServiceWorkerGlobalScope');
async_test(function(t) {
var text = "Test ArrayBuffer";
var array = new Uint8Array(text.length);
for (var i = 0; i < text.length; ++i)
array[i] = text.charCodeAt(i);
var request = new Request('fetch-access-control.php',
{method: 'POST', body: array.buffer});
fetch(request)
.then(function(response) { return response.body.asText(); })
.then(evalJsonp)
.then(function(result) {
assert_equals(result.method, 'POST');
assert_equals(result.body, 'Test ArrayBuffer');
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch with ArrayBuffer body test in ServiceWorkerGlobalScope');
async_test(function(t) {
var text = "Test ArrayBufferView";
var array = new Uint8Array(text.length);
for (var i = 0; i < text.length; ++i)
array[i] = text.charCodeAt(i);
var request = new Request('fetch-access-control.php',
{method: 'POST', body: array});
fetch(request)
.then(function(response) { return response.body.asText(); })
.then(evalJsonp)
.then(function(result) {
assert_equals(result.method, 'POST');
assert_equals(result.body, 'Test ArrayBufferView');
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch with ArrayBufferView body test in ServiceWorkerGlobalScope');
async_test(function(t) {
var formData = new FormData();
formData.append('StringKey1', '1234567890');
formData.append('StringKey2', 'ABCDEFGHIJ');
formData.append('BlobKey', new Blob(['blob content']));
formData.append('FileKey',
new File(['file content'], 'file.dat'));
var request = new Request('fetch-access-control.php',
{method: 'POST', body: formData});
fetch(request)
.then(function(response) { return response.body.asText(); })
.then(evalJsonp)
.then(function(result) {
assert_equals(result.method, 'POST');
assert_equals(result.post['StringKey1'], '1234567890');
assert_equals(result.post['StringKey2'], 'ABCDEFGHIJ');
var files = [];
for (var i = 0; i < result.files.length; ++i) {
files[result.files[i].key] = result.files[i];
}
assert_equals(files['BlobKey'].content, 'blob content');
assert_equals(files['BlobKey'].name, 'blob');
assert_equals(files['BlobKey'].size, 12);
assert_equals(files['FileKey'].content, 'file content');
assert_equals(files['FileKey'].name, 'file.dat');
assert_equals(files['FileKey'].size, 12);
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch with FormData body test in ServiceWorkerGlobalScope');
test(function(t) { test(function(t) {
function runInfiniteFetchLoop() { function runInfiniteFetchLoop() {
fetch('dummy.html') fetch('dummy.html')
......
...@@ -258,6 +258,7 @@ void FetchManager::Loader::performNetworkError() ...@@ -258,6 +258,7 @@ void FetchManager::Loader::performNetworkError()
void FetchManager::Loader::performHTTPFetch() void FetchManager::Loader::performHTTPFetch()
{ {
ASSERT(m_request->url().protocolIsInHTTPFamily());
// CORS preflight fetch procedure is implemented inside DocumentThreadableLoader. // CORS preflight fetch procedure is implemented inside DocumentThreadableLoader.
// "1. Let |HTTPRequest| be a copy of |request|, except that |HTTPRequest|'s // "1. Let |HTTPRequest| be a copy of |request|, except that |HTTPRequest|'s
...@@ -273,6 +274,15 @@ void FetchManager::Loader::performHTTPFetch() ...@@ -273,6 +274,15 @@ void FetchManager::Loader::performHTTPFetch()
request.addHTTPHeaderField(AtomicString(list[i]->first), AtomicString(list[i]->second)); request.addHTTPHeaderField(AtomicString(list[i]->first), AtomicString(list[i]->second));
} }
if (m_request->method() != "GET" && m_request->method() != "HEAD") {
RefPtr<BlobDataHandle> blobDataHandle = m_request->blobDataHandle();
if (blobDataHandle.get()) {
RefPtr<FormData> httpBody(FormData::create());
httpBody->appendBlob(blobDataHandle->uuid(), blobDataHandle);
request.setHTTPBody(httpBody);
}
}
// "2. Append `Referer`/empty byte sequence, if |HTTPRequest|'s |referrer| // "2. Append `Referer`/empty byte sequence, if |HTTPRequest|'s |referrer|
// is none, and `Referer`/|HTTPRequest|'s referrer, serialized and utf-8 // is none, and `Referer`/|HTTPRequest|'s referrer, serialized and utf-8
// encoded, otherwise, to HTTPRequest's header list. // encoded, otherwise, to HTTPRequest's header list.
......
...@@ -55,7 +55,7 @@ FetchRequestData* FetchRequestData::createRestrictedCopy(ExecutionContext* conte ...@@ -55,7 +55,7 @@ FetchRequestData* FetchRequestData::createRestrictedCopy(ExecutionContext* conte
request->m_url = m_url; request->m_url = m_url;
request->m_method = m_method; request->m_method = m_method;
request->m_headerList = m_headerList->createCopy(); request->m_headerList = m_headerList->createCopy();
// FIXME: Support body. request->m_blobDataHandle = m_blobDataHandle;
request->m_origin = origin; request->m_origin = origin;
if (context->isDocument()) if (context->isDocument())
request->m_referrer.setClient(blink::Referrer(context->url().strippedForUseAsReferrer(), toDocument(context)->referrerPolicy())); request->m_referrer.setClient(blink::Referrer(context->url().strippedForUseAsReferrer(), toDocument(context)->referrerPolicy()));
...@@ -75,7 +75,7 @@ FetchRequestData* FetchRequestData::createCopy() const ...@@ -75,7 +75,7 @@ FetchRequestData* FetchRequestData::createCopy() const
request->m_method = m_method; request->m_method = m_method;
request->m_headerList = m_headerList->createCopy(); request->m_headerList = m_headerList->createCopy();
request->m_unsafeRequestFlag = m_unsafeRequestFlag; request->m_unsafeRequestFlag = m_unsafeRequestFlag;
// FIXME: Support body. request->m_blobDataHandle = m_blobDataHandle;
request->m_origin = m_origin; request->m_origin = m_origin;
request->m_sameOriginDataURLFlag = m_sameOriginDataURLFlag; request->m_sameOriginDataURLFlag = m_sameOriginDataURLFlag;
request->m_context = m_context; request->m_context = m_context;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment