Home | History | Annotate | Download | only in network
      1 /*
      2  * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved.
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB. If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  */
     19 
     20 #ifndef FormData_h
     21 #define FormData_h
     22 
     23 #include "platform/blob/BlobData.h"
     24 #include "platform/weborigin/KURL.h"
     25 #include "wtf/Forward.h"
     26 #include "wtf/RefCounted.h"
     27 #include "wtf/Vector.h"
     28 #include "wtf/text/WTFString.h"
     29 
     30 namespace WTF{
     31 class TextEncoding;
     32 }
     33 
     34 namespace WebCore {
     35 
     36 class BlobDataHandle;
     37 
     38 class PLATFORM_EXPORT FormDataElement {
     39 public:
     40     FormDataElement() : m_type(data) { }
     41     explicit FormDataElement(const Vector<char>& array) : m_type(data), m_data(array) { }
     42 
     43     FormDataElement(const String& filename, long long fileStart, long long fileLength, double expectedFileModificationTime) : m_type(encodedFile), m_filename(filename), m_fileStart(fileStart), m_fileLength(fileLength), m_expectedFileModificationTime(expectedFileModificationTime) { }
     44     explicit FormDataElement(const String& blobUUID, PassRefPtr<BlobDataHandle> optionalHandle) : m_type(encodedBlob), m_blobUUID(blobUUID), m_optionalBlobDataHandle(optionalHandle) { }
     45     FormDataElement(const KURL& fileSystemURL, long long start, long long length, double expectedFileModificationTime) : m_type(encodedFileSystemURL), m_fileSystemURL(fileSystemURL), m_fileStart(start), m_fileLength(length), m_expectedFileModificationTime(expectedFileModificationTime) { }
     46 
     47     enum Type {
     48         data,
     49         encodedFile,
     50         encodedBlob,
     51         encodedFileSystemURL
     52     } m_type;
     53     Vector<char> m_data;
     54     String m_filename;
     55     String m_blobUUID;
     56     RefPtr<BlobDataHandle> m_optionalBlobDataHandle;
     57     KURL m_fileSystemURL;
     58     long long m_fileStart;
     59     long long m_fileLength;
     60     double m_expectedFileModificationTime;
     61 };
     62 
     63 inline bool operator==(const FormDataElement& a, const FormDataElement& b)
     64 {
     65     if (&a == &b)
     66         return true;
     67 
     68     if (a.m_type != b.m_type)
     69         return false;
     70     if (a.m_type == FormDataElement::data)
     71         return a.m_data == b.m_data;
     72     if (a.m_type == FormDataElement::encodedFile)
     73         return a.m_filename == b.m_filename && a.m_fileStart == b.m_fileStart && a.m_fileLength == b.m_fileLength && a.m_expectedFileModificationTime == b.m_expectedFileModificationTime;
     74     if (a.m_type == FormDataElement::encodedBlob)
     75         return a.m_blobUUID == b.m_blobUUID;
     76     if (a.m_type == FormDataElement::encodedFileSystemURL)
     77         return a.m_fileSystemURL == b.m_fileSystemURL;
     78 
     79     return true;
     80 }
     81 
     82 inline bool operator!=(const FormDataElement& a, const FormDataElement& b)
     83 {
     84     return !(a == b);
     85 }
     86 
     87 class PLATFORM_EXPORT FormData : public RefCounted<FormData> {
     88 public:
     89     enum EncodingType {
     90         FormURLEncoded, // for application/x-www-form-urlencoded
     91         TextPlain, // for text/plain
     92         MultipartFormData // for multipart/form-data
     93     };
     94 
     95     static PassRefPtr<FormData> create();
     96     static PassRefPtr<FormData> create(const void*, size_t);
     97     static PassRefPtr<FormData> create(const CString&);
     98     static PassRefPtr<FormData> create(const Vector<char>&);
     99     PassRefPtr<FormData> copy() const;
    100     PassRefPtr<FormData> deepCopy() const;
    101     ~FormData();
    102 
    103     void appendData(const void* data, size_t);
    104     void appendFile(const String& filePath);
    105     void appendFileRange(const String& filename, long long start, long long length, double expectedModificationTime);
    106     void appendBlob(const String& blobUUID, PassRefPtr<BlobDataHandle> optionalHandle);
    107     void appendFileSystemURL(const KURL&);
    108     void appendFileSystemURLRange(const KURL&, long long start, long long length, double expectedModificationTime);
    109 
    110     void flatten(Vector<char>&) const; // omits files
    111     String flattenToString() const; // omits files
    112 
    113     bool isEmpty() const { return m_elements.isEmpty(); }
    114     const Vector<FormDataElement>& elements() const { return m_elements; }
    115 
    116     const Vector<char>& boundary() const { return m_boundary; }
    117     void setBoundary(Vector<char> boundary) { m_boundary = boundary; }
    118 
    119     bool alwaysStream() const { return m_alwaysStream; }
    120     void setAlwaysStream(bool alwaysStream) { m_alwaysStream = alwaysStream; }
    121 
    122     // Identifies a particular form submission instance.  A value of 0 is used
    123     // to indicate an unspecified identifier.
    124     void setIdentifier(int64_t identifier) { m_identifier = identifier; }
    125     int64_t identifier() const { return m_identifier; }
    126 
    127     bool containsPasswordData() const { return m_containsPasswordData; }
    128     void setContainsPasswordData(bool containsPasswordData) { m_containsPasswordData = containsPasswordData; }
    129 
    130     static EncodingType parseEncodingType(const String& type)
    131     {
    132         if (equalIgnoringCase(type, "text/plain"))
    133             return TextPlain;
    134         if (equalIgnoringCase(type, "multipart/form-data"))
    135             return MultipartFormData;
    136         return FormURLEncoded;
    137     }
    138 
    139     // Size of the elements making up the FormData.
    140     unsigned long long sizeInBytes() const;
    141 
    142 private:
    143     FormData();
    144     FormData(const FormData&);
    145 
    146     Vector<FormDataElement> m_elements;
    147 
    148     int64_t m_identifier;
    149     bool m_alwaysStream;
    150     Vector<char> m_boundary;
    151     bool m_containsPasswordData;
    152 };
    153 
    154 inline bool operator==(const FormData& a, const FormData& b)
    155 {
    156     return a.elements() == b.elements();
    157 }
    158 
    159 inline bool operator!=(const FormData& a, const FormData& b)
    160 {
    161     return !(a == b);
    162 }
    163 
    164 } // namespace WebCore
    165 
    166 #endif
    167