weinig@apple.com | 81ed449 | 2008-04-18 00:56:23 +0000 | [diff] [blame] | 1 | /* |
mark.lam@apple.com | 17ae490 | 2021-02-19 15:51:15 +0000 | [diff] [blame^] | 2 | * Copyright (C) 2008-2021 Apple Inc. All rights reserved. |
weinig@apple.com | 81ed449 | 2008-04-18 00:56:23 +0000 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
weinig@apple.com | 81ed449 | 2008-04-18 00:56:23 +0000 | [diff] [blame] | 14 | * its contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include "config.h" |
| 30 | #include "JSXMLHttpRequest.h" |
| 31 | |
jianli@chromium.org | ec0183f | 2010-01-20 23:53:27 +0000 | [diff] [blame] | 32 | #include "JSBlob.h" |
commit-queue@webkit.org | e196714 | 2017-06-15 17:49:50 +0000 | [diff] [blame] | 33 | #include "JSDOMConvertBufferSource.h" |
| 34 | #include "JSDOMConvertInterface.h" |
| 35 | #include "JSDOMConvertJSON.h" |
commit-queue@webkit.org | 425d3aa | 2017-07-18 03:52:23 +0000 | [diff] [blame] | 36 | #include "JSDOMConvertNullable.h" |
| 37 | #include "JSDOMConvertStrings.h" |
weinig@apple.com | 81ed449 | 2008-04-18 00:56:23 +0000 | [diff] [blame] | 38 | #include "JSDocument.h" |
weinig@apple.com | 81ed449 | 2008-04-18 00:56:23 +0000 | [diff] [blame] | 39 | |
weinig@apple.com | 81ed449 | 2008-04-18 00:56:23 +0000 | [diff] [blame] | 40 | |
| 41 | namespace WebCore { |
keith_miller@apple.com | ce64b73 | 2017-10-17 07:10:58 +0000 | [diff] [blame] | 42 | using namespace JSC; |
weinig@apple.com | 81ed449 | 2008-04-18 00:56:23 +0000 | [diff] [blame] | 43 | |
mark.lam@apple.com | 17ae490 | 2021-02-19 15:51:15 +0000 | [diff] [blame^] | 44 | template<typename Visitor> |
| 45 | void JSXMLHttpRequest::visitAdditionalChildren(Visitor& visitor) |
mhahnenberg@apple.com | 982c9ea | 2011-09-23 19:40:09 +0000 | [diff] [blame] | 46 | { |
commit-queue@webkit.org | c02d703 | 2018-05-05 23:45:15 +0000 | [diff] [blame] | 47 | if (auto* upload = wrapped().optionalUpload()) |
ggaren@apple.com | 4e21a1e | 2011-04-25 00:47:17 +0000 | [diff] [blame] | 48 | visitor.addOpaqueRoot(upload); |
weinig@apple.com | a244b91 | 2008-07-30 02:39:48 +0000 | [diff] [blame] | 49 | |
commit-queue@webkit.org | c02d703 | 2018-05-05 23:45:15 +0000 | [diff] [blame] | 50 | if (auto* responseDocument = wrapped().optionalResponseXML()) |
ggaren@apple.com | 4e21a1e | 2011-04-25 00:47:17 +0000 | [diff] [blame] | 51 | visitor.addOpaqueRoot(responseDocument); |
weinig@apple.com | 81ed449 | 2008-04-18 00:56:23 +0000 | [diff] [blame] | 52 | } |
| 53 | |
mark.lam@apple.com | 17ae490 | 2021-02-19 15:51:15 +0000 | [diff] [blame^] | 54 | DEFINE_VISIT_ADDITIONAL_CHILDREN(JSXMLHttpRequest); |
| 55 | |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 56 | JSValue JSXMLHttpRequest::response(JSGlobalObject& lexicalGlobalObject) const |
crogers@google.com | 01f283a | 2010-11-23 21:51:00 +0000 | [diff] [blame] | 57 | { |
commit-queue@webkit.org | c50546c | 2017-07-22 03:54:16 +0000 | [diff] [blame] | 58 | auto cacheResult = [&] (JSValue value) -> JSValue { |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 59 | m_response.set(lexicalGlobalObject.vm(), this, value); |
commit-queue@webkit.org | c50546c | 2017-07-22 03:54:16 +0000 | [diff] [blame] | 60 | return value; |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | if (wrapped().responseCacheIsValid()) |
| 65 | return m_response.get(); |
| 66 | |
darin@apple.com | 0739f81 | 2016-05-01 04:50:10 +0000 | [diff] [blame] | 67 | auto type = wrapped().responseType(); |
| 68 | |
| 69 | switch (type) { |
darin@apple.com | 53438e9 | 2016-05-03 05:47:34 +0000 | [diff] [blame] | 70 | case XMLHttpRequest::ResponseType::EmptyString: |
commit-queue@webkit.org | 425d3aa | 2017-07-18 03:52:23 +0000 | [diff] [blame] | 71 | case XMLHttpRequest::ResponseType::Text: { |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 72 | auto scope = DECLARE_THROW_SCOPE(lexicalGlobalObject.vm()); |
| 73 | return cacheResult(toJS<IDLNullable<IDLUSVString>>(lexicalGlobalObject, scope, wrapped().responseText())); |
commit-queue@webkit.org | 425d3aa | 2017-07-18 03:52:23 +0000 | [diff] [blame] | 74 | } |
darin@apple.com | 0739f81 | 2016-05-01 04:50:10 +0000 | [diff] [blame] | 75 | default: |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | if (!wrapped().doneWithoutErrors()) |
commit-queue@webkit.org | c50546c | 2017-07-22 03:54:16 +0000 | [diff] [blame] | 80 | return cacheResult(jsNull()); |
commit-queue@webkit.org | cae42a4 | 2014-02-06 09:42:26 +0000 | [diff] [blame] | 81 | |
commit-queue@webkit.org | d2c5b6e | 2016-07-21 06:51:05 +0000 | [diff] [blame] | 82 | JSValue value; |
darin@apple.com | 0739f81 | 2016-05-01 04:50:10 +0000 | [diff] [blame] | 83 | switch (type) { |
darin@apple.com | 53438e9 | 2016-05-03 05:47:34 +0000 | [diff] [blame] | 84 | case XMLHttpRequest::ResponseType::EmptyString: |
| 85 | case XMLHttpRequest::ResponseType::Text: |
darin@apple.com | 0739f81 | 2016-05-01 04:50:10 +0000 | [diff] [blame] | 86 | ASSERT_NOT_REACHED(); |
commit-queue@webkit.org | d2c5b6e | 2016-07-21 06:51:05 +0000 | [diff] [blame] | 87 | return jsUndefined(); |
crogers@google.com | 01f283a | 2010-11-23 21:51:00 +0000 | [diff] [blame] | 88 | |
darin@apple.com | 53438e9 | 2016-05-03 05:47:34 +0000 | [diff] [blame] | 89 | case XMLHttpRequest::ResponseType::Json: |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 90 | value = toJS<IDLJSON>(*globalObject(), wrapped().responseTextIgnoringResponseType()); |
commit-queue@webkit.org | d2c5b6e | 2016-07-21 06:51:05 +0000 | [diff] [blame] | 91 | if (!value) |
| 92 | value = jsNull(); |
| 93 | break; |
rniwa@webkit.org | 89ac896 | 2013-09-03 18:45:51 +0000 | [diff] [blame] | 94 | |
commit-queue@webkit.org | d2c5b6e | 2016-07-21 06:51:05 +0000 | [diff] [blame] | 95 | case XMLHttpRequest::ResponseType::Document: { |
darin@apple.com | 8570494 | 2016-10-08 23:01:27 +0000 | [diff] [blame] | 96 | auto document = wrapped().responseXML(); |
| 97 | ASSERT(!document.hasException()); |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 98 | value = toJS<IDLInterface<Document>>(lexicalGlobalObject, *globalObject(), document.releaseReturnValue()); |
commit-queue@webkit.org | d2c5b6e | 2016-07-21 06:51:05 +0000 | [diff] [blame] | 99 | break; |
| 100 | } |
darin@apple.com | 8570494 | 2016-10-08 23:01:27 +0000 | [diff] [blame] | 101 | |
darin@apple.com | 53438e9 | 2016-05-03 05:47:34 +0000 | [diff] [blame] | 102 | case XMLHttpRequest::ResponseType::Blob: |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 103 | value = toJSNewlyCreated<IDLInterface<Blob>>(lexicalGlobalObject, *globalObject(), wrapped().createResponseBlob()); |
commit-queue@webkit.org | d2c5b6e | 2016-07-21 06:51:05 +0000 | [diff] [blame] | 104 | break; |
crogers@google.com | 01f283a | 2010-11-23 21:51:00 +0000 | [diff] [blame] | 105 | |
darin@apple.com | 53438e9 | 2016-05-03 05:47:34 +0000 | [diff] [blame] | 106 | case XMLHttpRequest::ResponseType::Arraybuffer: |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 107 | value = toJS<IDLInterface<ArrayBuffer>>(lexicalGlobalObject, *globalObject(), wrapped().createResponseArrayBuffer()); |
commit-queue@webkit.org | d2c5b6e | 2016-07-21 06:51:05 +0000 | [diff] [blame] | 108 | break; |
crogers@google.com | 01f283a | 2010-11-23 21:51:00 +0000 | [diff] [blame] | 109 | } |
darin@apple.com | 8570494 | 2016-10-08 23:01:27 +0000 | [diff] [blame] | 110 | |
commit-queue@webkit.org | d2c5b6e | 2016-07-21 06:51:05 +0000 | [diff] [blame] | 111 | wrapped().didCacheResponse(); |
commit-queue@webkit.org | c50546c | 2017-07-22 03:54:16 +0000 | [diff] [blame] | 112 | return cacheResult(value); |
crogers@google.com | 01f283a | 2010-11-23 21:51:00 +0000 | [diff] [blame] | 113 | } |
| 114 | |
weinig@apple.com | 81ed449 | 2008-04-18 00:56:23 +0000 | [diff] [blame] | 115 | } // namespace WebCore |