OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/loader/BeaconLoader.h" |
| 7 |
| 8 #include "FetchInitiatorTypeNames.h" |
| 9 #include "core/dom/Document.h" |
| 10 #include "core/fetch/CrossOriginAccessControl.h" |
| 11 #include "core/fetch/FetchContext.h" |
| 12 #include "core/fileapi/File.h" |
| 13 #include "core/frame/LocalFrame.h" |
| 14 #include "core/html/DOMFormData.h" |
| 15 #include "platform/network/FormData.h" |
| 16 #include "platform/network/ParsedContentType.h" |
| 17 #include "platform/network/ResourceRequest.h" |
| 18 #include "wtf/ArrayBufferView.h" |
| 19 |
| 20 namespace WebCore { |
| 21 |
| 22 void BeaconLoader::prepareRequest(LocalFrame* frame, ResourceRequest& request) |
| 23 { |
| 24 request.setTargetType(ResourceRequest::TargetIsBeacon); |
| 25 request.setHTTPMethod("POST"); |
| 26 request.setHTTPHeaderField("Cache-Control", "max-age=0"); |
| 27 request.setAllowStoredCredentials(true); |
| 28 frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document()
, request, FetchSubresource); |
| 29 |
| 30 // Origin: is unconditionally added for POSTs by addAdditionalRequestHeaders
(); |
| 31 // limit ourselves to cross-origin requests. |
| 32 SecurityOrigin* securityOrigin = frame->document()->securityOrigin(); |
| 33 if (securityOrigin->canRequest(request.url())) |
| 34 request.clearHTTPOrigin(); |
| 35 } |
| 36 |
| 37 void BeaconLoader::issueRequest(LocalFrame* frame, ResourceRequest& request) |
| 38 { |
| 39 FetchInitiatorInfo initiatorInfo; |
| 40 initiatorInfo.name = FetchInitiatorTypeNames::beacon; |
| 41 |
| 42 SimplexLoader::start(frame, request, initiatorInfo); |
| 43 } |
| 44 |
| 45 void BeaconLoader::sendBeacon(LocalFrame* frame, const KURL& beaconURL, const St
ring& data) |
| 46 { |
| 47 ResourceRequest request(beaconURL); |
| 48 prepareRequest(frame, request); |
| 49 |
| 50 request.setHTTPContentType("text/plain;charset=UTF-8"); |
| 51 request.setHTTPBody(FormData::create(data.utf8())); |
| 52 |
| 53 issueRequest(frame, request); |
| 54 } |
| 55 |
| 56 void BeaconLoader::sendBeacon(LocalFrame* frame, const KURL& beaconURL, PassRefP
tr<ArrayBufferView>& data) |
| 57 { |
| 58 ResourceRequest request(beaconURL); |
| 59 prepareRequest(frame, request); |
| 60 |
| 61 RefPtr<FormData> entityBody = FormData::create(data->baseAddress(), data->by
teLength()); |
| 62 // Q: not in the spec; should there be some default? |
| 63 AtomicString contentType = AtomicString("application/octet-stream"); |
| 64 |
| 65 request.setHTTPContentType(contentType); |
| 66 request.setHTTPBody(entityBody.release()); |
| 67 |
| 68 issueRequest(frame, request); |
| 69 } |
| 70 |
| 71 void BeaconLoader::sendBeacon(LocalFrame* frame, const KURL& beaconURL, PassRefP
trWillBeRawPtr<Blob>& data) |
| 72 { |
| 73 ResourceRequest request(beaconURL); |
| 74 prepareRequest(frame, request); |
| 75 |
| 76 RefPtr<FormData> entityBody = FormData::create(); |
| 77 if (data->hasBackingFile()) |
| 78 entityBody->appendFile(toFile(data.get())->path()); |
| 79 else |
| 80 entityBody->appendBlob(data->uuid(), data->blobDataHandle()); |
| 81 |
| 82 AtomicString contentType; |
| 83 const String& blobType = data->type(); |
| 84 if (!blobType.isEmpty() && isValidContentType(blobType)) |
| 85 request.setHTTPContentType(AtomicString(contentType)); |
| 86 |
| 87 request.setHTTPBody(entityBody.release()); |
| 88 |
| 89 issueRequest(frame, request); |
| 90 } |
| 91 |
| 92 void BeaconLoader::sendBeacon(LocalFrame* frame, const KURL& beaconURL, PassRefP
trWillBeRawPtr<DOMFormData>& data) |
| 93 { |
| 94 ResourceRequest request(beaconURL); |
| 95 prepareRequest(frame, request); |
| 96 |
| 97 RefPtr<FormData> entityBody = data->createMultiPartFormData(data->encoding()
); |
| 98 AtomicString contentType = AtomicString("multipart/form-data; boundary=", At
omicString::ConstructFromLiteral) + entityBody->boundary().data(); |
| 99 |
| 100 request.setHTTPContentType(contentType); |
| 101 request.setHTTPBody(entityBody.release()); |
| 102 |
| 103 issueRequest(frame, request); |
| 104 } |
| 105 |
| 106 } // namespace WebCore |
OLD | NEW |