Use a Variant for FormDataElement
[WebKit-https.git] / Source / WebCore / platform / network / cf / FormDataStreamCFNet.cpp
1 /*
2  * Copyright (C) 2005-2017 Apple Inc. All rights reserved.
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. 
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
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 "FormDataStreamCFNet.h"
31
32 #include "BlobData.h"
33 #include "FileSystem.h"
34 #include "FormData.h"
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <wtf/Assertions.h>
38 #include <wtf/HashMap.h>
39 #include <wtf/MainThread.h>
40 #include <wtf/RetainPtr.h>
41 #include <wtf/SchedulePair.h>
42 #include <wtf/StdLibExtras.h>
43 #include <wtf/Threading.h>
44
45 static const SInt32 fileNotFoundError = -43;
46
47 #if PLATFORM(COCOA)
48 extern "C" void CFURLRequestSetHTTPRequestBody(CFMutableURLRequestRef mutableHTTPRequest, CFDataRef httpBody);
49 extern "C" void CFURLRequestSetHTTPHeaderFieldValue(CFMutableURLRequestRef mutableHTTPRequest, CFStringRef httpHeaderField, CFStringRef httpHeaderFieldValue);
50 extern "C" void CFURLRequestSetHTTPRequestBodyStream(CFMutableURLRequestRef req, CFReadStreamRef bodyStream);
51 #elif PLATFORM(WIN)
52 #include <CFNetwork/CFURLRequest.h>
53 #endif
54
55 typedef struct {
56     CFIndex version; /* == 1 */
57     void *(*create)(CFReadStreamRef stream, void *info);
58     void (*finalize)(CFReadStreamRef stream, void *info);
59     CFStringRef (*copyDescription)(CFReadStreamRef stream, void *info);
60     Boolean (*open)(CFReadStreamRef stream, CFStreamError *error, Boolean *openComplete, void *info);
61     Boolean (*openCompleted)(CFReadStreamRef stream, CFStreamError *error, void *info);
62     CFIndex (*read)(CFReadStreamRef stream, UInt8 *buffer, CFIndex bufferLength, CFStreamError *error, Boolean *atEOF, void *info);
63     const UInt8 *(*getBuffer)(CFReadStreamRef stream, CFIndex maxBytesToRead, CFIndex *numBytesRead, CFStreamError *error, Boolean *atEOF, void *info);
64     Boolean (*canRead)(CFReadStreamRef stream, void *info);
65     void (*close)(CFReadStreamRef stream, void *info);
66     CFTypeRef (*copyProperty)(CFReadStreamRef stream, CFStringRef propertyName, void *info);
67     Boolean (*setProperty)(CFReadStreamRef stream, CFStringRef propertyName, CFTypeRef propertyValue, void *info);
68     void (*requestEvents)(CFReadStreamRef stream, CFOptionFlags streamEvents, void *info);
69     void (*schedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
70     void (*unschedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
71 } CFReadStreamCallBacksV1;
72
73 #if PLATFORM(WIN)
74 #define EXTERN extern "C" __declspec(dllimport)
75 #else
76 #define EXTERN extern "C"
77 #endif
78
79 EXTERN void CFReadStreamSignalEvent(CFReadStreamRef stream, CFStreamEventType event, const void *error);
80 EXTERN CFReadStreamRef CFReadStreamCreate(CFAllocatorRef alloc, const void *callbacks, void *info);
81
82 namespace WebCore {
83
84 static void formEventCallback(CFReadStreamRef stream, CFStreamEventType type, void* context);
85
86 static CFStringRef formDataPointerPropertyName = CFSTR("WebKitFormDataPointer");
87
88 CFStringRef formDataStreamLengthPropertyName()
89 {
90     return CFSTR("WebKitFormDataStreamLength");
91 }
92
93 struct FormCreationContext {
94     RefPtr<FormData> formData;
95     unsigned long long streamLength;
96 };
97
98 struct FormStreamFields {
99     RefPtr<FormData> formData;
100     SchedulePairHashSet scheduledRunLoopPairs;
101     Vector<FormDataElement> remainingElements; // in reverse order
102     CFReadStreamRef currentStream;
103     long long currentStreamRangeLength;
104     MallocPtr<char> currentData;
105     CFReadStreamRef formStream;
106     unsigned long long streamLength;
107     unsigned long long bytesSent;
108     Lock streamIsBeingOpenedOrClosedLock;
109 };
110
111 static void closeCurrentStream(FormStreamFields* form)
112 {
113     ASSERT(form->streamIsBeingOpenedOrClosedLock.isHeld());
114
115     if (form->currentStream) {
116         CFReadStreamClose(form->currentStream);
117         CFReadStreamSetClient(form->currentStream, kCFStreamEventNone, 0, 0);
118         CFRelease(form->currentStream);
119         form->currentStream = 0;
120         form->currentStreamRangeLength = BlobDataItem::toEndOfFile;
121     }
122
123     form->currentData = nullptr;
124 }
125
126 // Return false if we cannot advance the stream. Currently the only possible failure is that the underlying file has been removed or changed since File.slice.
127 static bool advanceCurrentStream(FormStreamFields* form)
128 {
129     ASSERT(form->streamIsBeingOpenedOrClosedLock.isHeld());
130
131     closeCurrentStream(form);
132
133     if (form->remainingElements.isEmpty())
134         return true;
135
136     // Create the new stream.
137     FormDataElement& nextInput = form->remainingElements.last();
138
139     bool success = switchOn(nextInput.data,
140         [form] (Vector<char>& bytes) {
141             size_t size = bytes.size();
142             MallocPtr<char> data = bytes.releaseBuffer();
143             form->currentStream = CFReadStreamCreateWithBytesNoCopy(0, reinterpret_cast<const UInt8*>(data.get()), size, kCFAllocatorNull);
144             form->currentData = WTFMove(data);
145             return true;
146         }, [form] (const FormDataElement::EncodedFileData& fileData) {
147             // Check if the file has been changed or not if required.
148             if (FileSystem::isValidFileTime(fileData.expectedFileModificationTime)) {
149                 time_t fileModificationTime;
150                 if (!FileSystem::getFileModificationTime(fileData.filename, fileModificationTime) || fileModificationTime != static_cast<time_t>(fileData.expectedFileModificationTime))
151                     return false;
152             }
153             const String& path = fileData.shouldGenerateFile ? fileData.generatedFilename : fileData.filename;
154             form->currentStream = CFReadStreamCreateWithFile(0, FileSystem::pathAsURL(path).get());
155             if (!form->currentStream) {
156                 // The file must have been removed or become unreadable.
157                 return false;
158             }
159             if (fileData.fileStart > 0) {
160                 RetainPtr<CFNumberRef> position = adoptCF(CFNumberCreate(0, kCFNumberLongLongType, &fileData.fileStart));
161                 CFReadStreamSetProperty(form->currentStream, kCFStreamPropertyFileCurrentOffset, position.get());
162             }
163             form->currentStreamRangeLength = fileData.fileLength;
164             return true;
165         }, [] (const FormDataElement::EncodedBlobData&) {
166             ASSERT_NOT_REACHED();
167             return false;
168         }
169     );
170
171     if (!success)
172         return false;
173
174     form->remainingElements.removeLast();
175
176     // Set up the callback.
177     CFStreamClientContext context = { 0, form, 0, 0, 0 };
178     CFReadStreamSetClient(form->currentStream, kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered,
179         formEventCallback, &context);
180
181     // Schedule with the current set of run loops.
182     SchedulePairHashSet::iterator end = form->scheduledRunLoopPairs.end();
183     for (SchedulePairHashSet::iterator it = form->scheduledRunLoopPairs.begin(); it != end; ++it)
184         CFReadStreamScheduleWithRunLoop(form->currentStream, (*it)->runLoop(), (*it)->mode());
185
186     return true;
187 }
188
189 static bool openNextStream(FormStreamFields* form)
190 {
191     // CFReadStreamOpen() can cause this function to be re-entered from another thread before it returns.
192     // One example when this can occur is when the stream being opened has no data. See <rdar://problem/23550269>.
193     LockHolder locker(form->streamIsBeingOpenedOrClosedLock);
194
195     // Skip over any streams we can't open.
196     if (!advanceCurrentStream(form))
197         return false;
198     while (form->currentStream && !CFReadStreamOpen(form->currentStream)) {
199         if (!advanceCurrentStream(form))
200             return false;
201     }
202     return true;
203 }
204
205 static void* formCreate(CFReadStreamRef stream, void* context)
206 {
207     FormCreationContext* formContext = static_cast<FormCreationContext*>(context);
208
209     FormStreamFields* newInfo = new FormStreamFields;
210     newInfo->formData = WTFMove(formContext->formData);
211     newInfo->currentStream = 0;
212     newInfo->currentStreamRangeLength = BlobDataItem::toEndOfFile;
213     newInfo->formStream = stream; // Don't retain. That would create a reference cycle.
214     newInfo->streamLength = formContext->streamLength;
215     newInfo->bytesSent = 0;
216
217     // Append in reverse order since we remove elements from the end.
218     size_t size = newInfo->formData->elements().size();
219     newInfo->remainingElements.reserveInitialCapacity(size);
220     for (size_t i = 0; i < size; ++i)
221         newInfo->remainingElements.uncheckedAppend(newInfo->formData->elements()[size - i - 1]);
222
223     return newInfo;
224 }
225
226 static void formFinalize(CFReadStreamRef stream, void* context)
227 {
228     FormStreamFields* form = static_cast<FormStreamFields*>(context);
229     ASSERT_UNUSED(stream, form->formStream == stream);
230
231     callOnMainThread([form] {
232         {
233             LockHolder locker(form->streamIsBeingOpenedOrClosedLock);
234             closeCurrentStream(form);
235         }
236         delete form;
237     });
238 }
239
240 static Boolean formOpen(CFReadStreamRef, CFStreamError* error, Boolean* openComplete, void* context)
241 {
242     FormStreamFields* form = static_cast<FormStreamFields*>(context);
243
244     bool opened = openNextStream(form);
245
246     *openComplete = opened;
247     error->error = opened ? 0 :
248 #if PLATFORM(WIN)
249         ENOENT;
250 #else
251         fileNotFoundError;
252 #endif
253     return opened;
254 }
255
256 static CFIndex formRead(CFReadStreamRef, UInt8* buffer, CFIndex bufferLength, CFStreamError* error, Boolean* atEOF, void* context)
257 {
258     FormStreamFields* form = static_cast<FormStreamFields*>(context);
259
260     while (form->currentStream) {
261         CFIndex bytesToRead = bufferLength;
262         if (form->currentStreamRangeLength != BlobDataItem::toEndOfFile && form->currentStreamRangeLength < bytesToRead)
263             bytesToRead = static_cast<CFIndex>(form->currentStreamRangeLength);
264         CFIndex bytesRead = CFReadStreamRead(form->currentStream, buffer, bytesToRead);
265         if (bytesRead < 0) {
266             *error = CFReadStreamGetError(form->currentStream);
267             return -1;
268         }
269         if (bytesRead > 0) {
270             error->error = 0;
271             *atEOF = FALSE;
272             form->bytesSent += bytesRead;
273             if (form->currentStreamRangeLength != BlobDataItem::toEndOfFile)
274                 form->currentStreamRangeLength -= bytesRead;
275
276             return bytesRead;
277         }
278         openNextStream(form);
279     }
280
281     error->error = 0;
282     *atEOF = TRUE;
283     return 0;
284 }
285
286 static Boolean formCanRead(CFReadStreamRef stream, void* context)
287 {
288     FormStreamFields* form = static_cast<FormStreamFields*>(context);
289
290     while (form->currentStream && CFReadStreamGetStatus(form->currentStream) == kCFStreamStatusAtEnd)
291         openNextStream(form);
292
293     if (!form->currentStream) {
294         CFReadStreamSignalEvent(stream, kCFStreamEventEndEncountered, 0);
295         return FALSE;
296     }
297     return CFReadStreamHasBytesAvailable(form->currentStream);
298 }
299
300 static void formClose(CFReadStreamRef, void* context)
301 {
302     FormStreamFields* form = static_cast<FormStreamFields*>(context);
303     LockHolder locker(form->streamIsBeingOpenedOrClosedLock);
304
305     closeCurrentStream(form);
306 }
307
308 static CFTypeRef formCopyProperty(CFReadStreamRef, CFStringRef propertyName, void *context)
309 {
310     FormStreamFields* form = static_cast<FormStreamFields*>(context);
311
312     if (kCFCompareEqualTo == CFStringCompare(propertyName, formDataPointerPropertyName, 0)) {
313         long long formDataAsNumber = static_cast<long long>(reinterpret_cast<intptr_t>(form->formData.get()));
314         return CFNumberCreate(0, kCFNumberLongLongType, &formDataAsNumber);
315     }
316
317     if (kCFCompareEqualTo == CFStringCompare(propertyName, formDataStreamLengthPropertyName(), 0))
318         return CFStringCreateWithFormat(0, 0, CFSTR("%llu"), form->streamLength);
319
320     return 0;
321 }
322
323 static void formSchedule(CFReadStreamRef, CFRunLoopRef runLoop, CFStringRef runLoopMode, void* context)
324 {
325     FormStreamFields* form = static_cast<FormStreamFields*>(context);
326
327     if (form->currentStream)
328         CFReadStreamScheduleWithRunLoop(form->currentStream, runLoop, runLoopMode);
329     form->scheduledRunLoopPairs.add(SchedulePair::create(runLoop, runLoopMode));
330 }
331
332 static void formUnschedule(CFReadStreamRef, CFRunLoopRef runLoop, CFStringRef runLoopMode, void* context)
333 {
334     FormStreamFields* form = static_cast<FormStreamFields*>(context);
335
336     if (form->currentStream)
337         CFReadStreamUnscheduleFromRunLoop(form->currentStream, runLoop, runLoopMode);
338     form->scheduledRunLoopPairs.remove(SchedulePair::create(runLoop, runLoopMode));
339 }
340
341 static void formEventCallback(CFReadStreamRef stream, CFStreamEventType type, void* context)
342 {
343     FormStreamFields* form = static_cast<FormStreamFields*>(context);
344
345     switch (type) {
346     case kCFStreamEventHasBytesAvailable:
347         CFReadStreamSignalEvent(form->formStream, kCFStreamEventHasBytesAvailable, 0);
348         break;
349     case kCFStreamEventErrorOccurred: {
350         CFStreamError readStreamError = CFReadStreamGetError(stream);
351         CFReadStreamSignalEvent(form->formStream, kCFStreamEventErrorOccurred, &readStreamError);
352         break;
353     }
354     case kCFStreamEventEndEncountered:
355         openNextStream(form);
356         if (!form->currentStream)
357             CFReadStreamSignalEvent(form->formStream, kCFStreamEventEndEncountered, 0);
358         break;
359     case kCFStreamEventNone:
360         LOG_ERROR("unexpected kCFStreamEventNone");
361         break;
362     case kCFStreamEventOpenCompleted:
363         LOG_ERROR("unexpected kCFStreamEventOpenCompleted");
364         break;
365     case kCFStreamEventCanAcceptBytes:
366         LOG_ERROR("unexpected kCFStreamEventCanAcceptBytes");
367         break;
368     }
369 }
370
371 RetainPtr<CFReadStreamRef> createHTTPBodyCFReadStream(FormData& formData)
372 {
373     auto resolvedFormData = formData.resolveBlobReferences();
374
375     // Precompute the content length so CFNetwork doesn't use chunked mode.
376     unsigned long long length = 0;
377     for (auto& element : resolvedFormData->elements())
378         length += element.lengthInBytes();
379
380     FormCreationContext formContext = { WTFMove(resolvedFormData), length };
381     CFReadStreamCallBacksV1 callBacks = { 1, formCreate, formFinalize, nullptr, formOpen, nullptr, formRead, nullptr, formCanRead, formClose, formCopyProperty, nullptr, nullptr, formSchedule, formUnschedule };
382     return adoptCF(CFReadStreamCreate(nullptr, static_cast<const void*>(&callBacks), &formContext));
383 }
384
385 void setHTTPBody(CFMutableURLRequestRef request, FormData* formData)
386 {
387     if (!formData)
388         return;
389
390     // Handle the common special case of one piece of form data, with no files.
391     auto& elements = formData->elements();
392     if (elements.size() == 1 && !formData->alwaysStream()) {
393         if (auto* vector = WTF::get_if<Vector<char>>(elements[0].data)) {
394             auto data = adoptCF(CFDataCreate(nullptr, reinterpret_cast<const UInt8*>(vector->data()), vector->size()));
395             CFURLRequestSetHTTPRequestBody(request, data.get());
396             return;
397         }
398     }
399
400     CFURLRequestSetHTTPRequestBodyStream(request, createHTTPBodyCFReadStream(*formData).get());
401 }
402
403 FormData* httpBodyFromStream(CFReadStreamRef stream)
404 {
405     if (!stream)
406         return nullptr;
407
408     // Passing the pointer as property appears to be the only way to associate a stream with FormData.
409     // A new stream is always created in CFURLRequestCopyHTTPRequestBodyStream (or -[NSURLRequest HTTPBodyStream]),
410     // so a side HashMap wouldn't work.
411     // Even the stream's context pointer is different from the one we returned from formCreate().
412
413     RetainPtr<CFNumberRef> formDataPointerAsCFNumber = adoptCF(static_cast<CFNumberRef>(CFReadStreamCopyProperty(stream, formDataPointerPropertyName)));
414     if (!formDataPointerAsCFNumber)
415         return nullptr;
416
417     long formDataPointerAsNumber;
418     if (!CFNumberGetValue(formDataPointerAsCFNumber.get(), kCFNumberLongType, &formDataPointerAsNumber))
419         return nullptr;
420
421     return reinterpret_cast<FormData*>(static_cast<intptr_t>(formDataPointerAsNumber));
422 }
423
424 } // namespace WebCore