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 "KURL.h"
     24 #include "PlatformString.h"
     25 #include <wtf/Forward.h>
     26 #include <wtf/RefCounted.h>
     27 #include <wtf/Vector.h>
     28 
     29 namespace WebCore {
     30 
     31 class Document;
     32 class FormDataList;
     33 class TextEncoding;
     34 
     35 class FormDataElement {
     36 public:
     37     FormDataElement() : m_type(data) { }
     38     explicit FormDataElement(const Vector<char>& array) : m_type(data), m_data(array) { }
     39 
     40 #if ENABLE(BLOB)
     41     FormDataElement(const String& filename, long long fileStart, long long fileLength, double expectedFileModificationTime, bool shouldGenerateFile) : m_type(encodedFile), m_filename(filename), m_fileStart(fileStart), m_fileLength(fileLength), m_expectedFileModificationTime(expectedFileModificationTime), m_shouldGenerateFile(shouldGenerateFile) { }
     42     explicit FormDataElement(const KURL& blobURL) : m_type(encodedBlob), m_blobURL(blobURL) { }
     43 #else
     44     FormDataElement(const String& filename, bool shouldGenerateFile) : m_type(encodedFile), m_filename(filename), m_shouldGenerateFile(shouldGenerateFile) { }
     45 #endif
     46 
     47     enum {
     48         data,
     49         encodedFile
     50 #if ENABLE(BLOB)
     51         , encodedBlob
     52 #endif
     53     } m_type;
     54     Vector<char> m_data;
     55     String m_filename;
     56 #if ENABLE(BLOB)
     57     long long m_fileStart;
     58     long long m_fileLength;
     59     double m_expectedFileModificationTime;
     60     KURL m_blobURL;
     61 #endif
     62     String m_generatedFilename;
     63     bool m_shouldGenerateFile;
     64 };
     65 
     66 inline bool operator==(const FormDataElement& a, const FormDataElement& b)
     67 {
     68     if (&a == &b)
     69         return true;
     70 
     71     if (a.m_type != b.m_type)
     72         return false;
     73     if (a.m_type == FormDataElement::data)
     74         return a.m_data == b.m_data;
     75     if (a.m_type == FormDataElement::encodedFile)
     76 #if ENABLE(BLOB)
     77         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;
     78     if (a.m_type == FormDataElement::encodedBlob)
     79         return a.m_blobURL == b.m_blobURL;
     80 #else
     81         return a.m_filename == b.m_filename;
     82 #endif
     83 
     84     return true;
     85 }
     86 
     87 inline bool operator!=(const FormDataElement& a, const FormDataElement& b)
     88 {
     89     return !(a == b);
     90 }
     91 
     92 class FormData : public RefCounted<FormData> {
     93 public:
     94     static PassRefPtr<FormData> create();
     95     static PassRefPtr<FormData> create(const void*, size_t);
     96     static PassRefPtr<FormData> create(const CString&);
     97     static PassRefPtr<FormData> create(const Vector<char>&);
     98     static PassRefPtr<FormData> create(const FormDataList&, const TextEncoding&);
     99     static PassRefPtr<FormData> createMultiPart(const FormDataList&, const TextEncoding&, Document*);
    100     PassRefPtr<FormData> copy() const;
    101     PassRefPtr<FormData> deepCopy() const;
    102     ~FormData();
    103 
    104     void encodeForBackForward(Encoder&) const;
    105     static PassRefPtr<FormData> decodeForBackForward(Decoder&);
    106 
    107     void appendData(const void* data, size_t);
    108     void appendFile(const String& filePath, bool shouldGenerateFile = false);
    109 #if ENABLE(BLOB)
    110     void appendFileRange(const String& filename, long long start, long long length, double expectedModificationTime, bool shouldGenerateFile = false);
    111     void appendBlob(const KURL& blobURL);
    112 #endif
    113 
    114     void flatten(Vector<char>&) const; // omits files
    115     String flattenToString() const; // omits files
    116 
    117     bool isEmpty() const { return m_elements.isEmpty(); }
    118     const Vector<FormDataElement>& elements() const { return m_elements; }
    119     const Vector<char>& boundary() const { return m_boundary; }
    120 
    121     void generateFiles(Document*);
    122     void removeGeneratedFilesIfNeeded();
    123 
    124     bool alwaysStream() const { return m_alwaysStream; }
    125     void setAlwaysStream(bool alwaysStream) { m_alwaysStream = alwaysStream; }
    126 
    127     // Identifies a particular form submission instance.  A value of 0 is used
    128     // to indicate an unspecified identifier.
    129     void setIdentifier(int64_t identifier) { m_identifier = identifier; }
    130     int64_t identifier() const { return m_identifier; }
    131 
    132 private:
    133     FormData();
    134     FormData(const FormData&);
    135 
    136     void appendKeyValuePairItems(const FormDataList&, const TextEncoding&, bool isMultiPartForm, Document*);
    137 
    138     Vector<FormDataElement> m_elements;
    139 
    140     int64_t m_identifier;
    141     bool m_hasGeneratedFiles;
    142     bool m_alwaysStream;
    143     Vector<char> m_boundary;
    144 };
    145 
    146 inline bool operator==(const FormData& a, const FormData& b)
    147 {
    148     return a.elements() == b.elements();
    149 }
    150 
    151 inline bool operator!=(const FormData& a, const FormData& b)
    152 {
    153     return !(a == b);
    154 }
    155 
    156 } // namespace WebCore
    157 
    158 #endif
    159