2008-05-07 Julien Chaffraix <jchaffraix@webkit.org>
authorjchaffraix@webkit.org <jchaffraix@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 7 May 2008 13:05:27 +0000 (13:05 +0000)
committerjchaffraix@webkit.org <jchaffraix@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 7 May 2008 13:05:27 +0000 (13:05 +0000)
        Reviewed by Eric.

        Bug 17971: [Curl] FormData processing should be moved to its own class

        Move FormData treatment into FormDataStream. The aim is to have FormDataStream behave like a stream that cURL
        could manipulate.

        First step into having complete file uploading facility in cURL.

        No test case as it is only code refactoring.

        * GNUmakefile.am: Add FormDataStreamCurl.cpp
        * platform/network/ResourceHandleInternal.h: Move code to FormStreamDataCurl.h
        (WebCore::ResourceHandleInternal::ResourceHandleInternal):
        * platform/network/curl/FormDataStreamCurl.cpp: Added.
        (WebCore::FormDataStream::~FormDataStream):
        (WebCore::FormDataStream::read):
        * platform/network/curl/FormDataStreamCurl.h: Added.
        (WebCore::FormDataStream::FormDataStream):
        * platform/network/curl/ResourceHandleCurl.cpp:
        (WebCore::ResourceHandleInternal::~ResourceHandleInternal):
        * platform/network/curl/ResourceHandleManager.cpp:
        (WebCore::readCallback): Move code to FormDataStreamCurl.cpp

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@32956 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/GNUmakefile.am
WebCore/platform/network/ResourceHandleInternal.h
WebCore/platform/network/curl/FormDataStreamCurl.cpp [new file with mode: 0644]
WebCore/platform/network/curl/FormDataStreamCurl.h [new file with mode: 0644]
WebCore/platform/network/curl/ResourceHandleCurl.cpp
WebCore/platform/network/curl/ResourceHandleManager.cpp

index b99aeb6..b7e9f5f 100644 (file)
@@ -1,3 +1,29 @@
+2008-05-07  Julien Chaffraix  <jchaffraix@webkit.org>
+
+        Reviewed by Eric.
+
+        Bug 17971: [Curl] FormData processing should be moved to its own class
+
+        Move FormData treatment into FormDataStream. The aim is to have FormDataStream behave like a stream that cURL
+        could manipulate.
+
+        First step into having complete file uploading facility in cURL.
+
+        No test case as it is only code refactoring.
+
+        * GNUmakefile.am: Add FormDataStreamCurl.cpp
+        * platform/network/ResourceHandleInternal.h: Move code to FormStreamDataCurl.h
+        (WebCore::ResourceHandleInternal::ResourceHandleInternal):
+        * platform/network/curl/FormDataStreamCurl.cpp: Added.
+        (WebCore::FormDataStream::~FormDataStream):
+        (WebCore::FormDataStream::read):
+        * platform/network/curl/FormDataStreamCurl.h: Added.
+        (WebCore::FormDataStream::FormDataStream):
+        * platform/network/curl/ResourceHandleCurl.cpp:
+        (WebCore::ResourceHandleInternal::~ResourceHandleInternal):
+        * platform/network/curl/ResourceHandleManager.cpp:
+        (WebCore::readCallback): Move code to FormDataStreamCurl.cpp
+
 2008-05-07  Adam Treat  <treat@kde.org>
 
         Reviewed by Simon.
index ce1b6d6..eb6ad29 100644 (file)
@@ -1083,6 +1083,7 @@ webcore_cppflags += \
 
 webcore_sources += \
        WebCore/platform/network/curl/CookieJarCurl.cpp \
+       WebCore/platform/network/curl/FormDataStreamCurl.cpp \
        WebCore/platform/network/curl/ResourceHandleCurl.cpp \
        WebCore/platform/network/curl/ResourceHandleManager.cpp
 
index 94eadec..cbdd7c5 100644 (file)
@@ -43,6 +43,7 @@
 
 #if USE(CURL)
 #include <curl/curl.h>
+#include "FormDataStreamCurl.h"
 #endif
 
 #if USE(SOUP)
@@ -103,9 +104,7 @@ namespace WebCore {
             , m_url(0)
             , m_customHeaders(0)
             , m_cancelled(false)
-            , m_file(0)
-            , m_formDataElementIndex(0)
-            , m_formDataElementDataOffset(0)
+            , m_formDataStream(loader)
 #endif
 #if USE(SOUP)
             , m_msg(0)
@@ -173,9 +172,7 @@ namespace WebCore {
         ResourceResponse m_response;
         bool m_cancelled;
 
-        FILE* m_file;
-        size_t m_formDataElementIndex;
-        size_t m_formDataElementDataOffset;
+        FormDataStream m_formDataStream;
         Vector<char> m_postBytes;
 #endif
 #if USE(SOUP)
diff --git a/WebCore/platform/network/curl/FormDataStreamCurl.cpp b/WebCore/platform/network/curl/FormDataStreamCurl.cpp
new file mode 100644 (file)
index 0000000..7971ae2
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2008 Apple Computer, Inc.  All rights reserved.
+ * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
+ * Copyright (C) 2007 Alp Toker <alp.toker@collabora.co.uk>
+ * Copyright (C) 2007 Holger Hans Peter Freyther
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "FormDataStreamCurl.h"
+
+#include "CString.h"
+#include "FormData.h"
+#include "ResourceRequest.h"
+
+namespace WebCore {
+
+FormDataStream::~FormDataStream()
+{
+    if (m_file)
+        fclose(m_file);
+}
+
+size_t FormDataStream::read(void* ptr, size_t blockSize, size_t numberOfBlocks)
+{
+    // Check for overflow.
+    if (!numberOfBlocks || blockSize > SIZE_MAX / numberOfBlocks)
+        return 0;
+
+    Vector<FormDataElement> elements;
+    if (m_resourceHandle->request().httpBody())
+        elements = m_resourceHandle->request().httpBody()->elements();
+
+    if (m_formDataElementIndex >= elements.size())
+        return 0;
+
+    FormDataElement element = elements[m_formDataElementIndex];
+
+    size_t toSend = blockSize * numberOfBlocks;
+    size_t sent;
+
+    if (element.m_type == FormDataElement::encodedFile) {
+        if (!m_file)
+            m_file = fopen(element.m_filename.utf8().data(), "rb");
+
+        if (!m_file) {
+            // FIXME: show a user error?
+#ifndef NDEBUG
+            printf("Failed while trying to open %s for upload\n", element.m_filename.utf8().data());
+#endif
+            return 0;
+        }
+
+        sent = fread(ptr, blockSize, numberOfBlocks, m_file);
+        if (!blockSize && ferror(m_file)) {
+            // FIXME: show a user error?
+#ifndef NDEBUG
+            printf("Failed while trying to read %s for upload\n", element.m_filename.utf8().data());
+#endif
+            return 0;
+        }
+        if (feof(m_file)) {
+            fclose(m_file);
+            m_file = 0;
+            m_formDataElementIndex++;
+        }
+    } else {
+        size_t elementSize = element.m_data.size() - m_formDataElementDataOffset;
+        sent = elementSize > toSend ? toSend : elementSize;
+        memcpy(ptr, element.m_data.data() + m_formDataElementDataOffset, sent);
+        if (elementSize > sent)
+            m_formDataElementDataOffset += sent;
+        else {
+            m_formDataElementDataOffset = 0;
+            m_formDataElementIndex++;
+        }
+    }
+
+    return sent;
+}
+
+} // namespace WebCore
diff --git a/WebCore/platform/network/curl/FormDataStreamCurl.h b/WebCore/platform/network/curl/FormDataStreamCurl.h
new file mode 100644 (file)
index 0000000..20a7542
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2008 Apple Computer, Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef FormatDataStreamCurl_h
+#define FormatDataStreamCurl_h
+
+#include "config.h"
+
+#include <cstdint>
+#include "FileSystem.h"
+#include "ResourceHandle.h"
+
+namespace WebCore {
+
+class FormDataStream {
+public:
+    FormDataStream(ResourceHandle* handle)
+        : m_resourceHandle(handle)
+        , m_file(0)
+        , m_formDataElementIndex(0)
+        , m_formDataElementDataOffset(0)
+    {
+    }
+
+    ~FormDataStream();
+
+    size_t read(void* ptr, size_t blockSize, size_t numberOfBlocks);
+
+private:
+    // We can hold a weak reference to our ResourceHandle as it holds a strong reference
+    // to us through its ResourceHandleInternal.
+    ResourceHandle* m_resourceHandle;
+
+    FILE* m_file;
+    size_t m_formDataElementIndex;
+    size_t m_formDataElementDataOffset;
+};
+
+} // namespace WebCore
+
+#endif // FormDataStreamCurl_h
index fe53cbd..ea40767 100644 (file)
@@ -90,8 +90,6 @@ ResourceHandleInternal::~ResourceHandleInternal()
     free(m_url);
     if (m_customHeaders)
         curl_slist_free_all(m_customHeaders);
-    if (m_file)
-        fclose(m_file);
 }
 
 ResourceHandle::~ResourceHandle()
index 40f8d98..6b5ea1f 100644 (file)
@@ -31,7 +31,6 @@
 #include "ResourceHandleManager.h"
 
 #include "CString.h"
-#include "FileSystem.h"
 #include "MIMETypeRegistry.h"
 #include "NotImplemented.h"
 #include "ResourceError.h"
@@ -212,58 +211,14 @@ size_t readCallback(void* ptr, size_t size, size_t nmemb, void* data)
     if (d->m_cancelled)
         return 0;
 
-    size_t sent = 0;
-    size_t toSend = size * nmemb;
-    if (!toSend)
+    if (!size || !nmemb)
         return 0;
 
-    Vector<FormDataElement> elements;
-    if (job->request().httpBody())
-        elements = job->request().httpBody()->elements();
-
-    if (d->m_formDataElementIndex >= elements.size())
-        return 0;
+    size_t sent = d->m_formDataStream.read(ptr, size, nmemb);
 
-    FormDataElement element = elements[d->m_formDataElementIndex];
-
-    if (element.m_type == FormDataElement::encodedFile) {
-        if (!d->m_file)
-            d->m_file = fopen(element.m_filename.utf8().data(), "rb");
-
-        if (!d->m_file) {
-            // FIXME: show a user error?
-#ifndef NDEBUG
-            printf("Failed while trying to open %s for upload\n", element.m_filename.utf8().data());
-#endif
-            job->cancel();
-            return 0;
-        }
-
-        sent = fread(ptr, size, nmemb, d->m_file);
-        if (!size && ferror(d->m_file)) {
-            // FIXME: show a user error?
-#ifndef NDEBUG
-            printf("Failed while trying to read %s for upload\n", element.m_filename.utf8().data());
-#endif
-            job->cancel();
-            return 0;
-        }
-        if (feof(d->m_file)) {
-            fclose(d->m_file);
-            d->m_file = 0;
-            d->m_formDataElementIndex++;
-        }
-    } else {
-        size_t elementSize = element.m_data.size() - d->m_formDataElementDataOffset;
-        sent = elementSize > toSend ? toSend : elementSize;
-        memcpy(ptr, element.m_data.data() + d->m_formDataElementDataOffset, sent);
-        if (elementSize > sent)
-            d->m_formDataElementDataOffset += sent;
-        else {
-            d->m_formDataElementDataOffset = 0;
-            d->m_formDataElementIndex++;
-        }
-    }
+    // Something went wrong so cancel the job.
+    if (!sent)
+        job->cancel();
 
     return sent;
 }