1 /*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "WebHTTPBody.h"
33
34 #include "FormData.h"
35 #include "WebFileInfo.h"
36
37 using namespace WebCore;
38
39 namespace WebKit {
40
41 class WebHTTPBodyPrivate : public FormData {
42 };
43
initialize()44 void WebHTTPBody::initialize()
45 {
46 assign(static_cast<WebHTTPBodyPrivate*>(FormData::create().releaseRef()));
47 }
48
reset()49 void WebHTTPBody::reset()
50 {
51 assign(0);
52 }
53
assign(const WebHTTPBody & other)54 void WebHTTPBody::assign(const WebHTTPBody& other)
55 {
56 WebHTTPBodyPrivate* p = const_cast<WebHTTPBodyPrivate*>(other.m_private);
57 if (p)
58 p->ref();
59 assign(p);
60 }
61
elementCount() const62 size_t WebHTTPBody::elementCount() const
63 {
64 ASSERT(!isNull());
65 return m_private->elements().size();
66 }
67
elementAt(size_t index,Element & result) const68 bool WebHTTPBody::elementAt(size_t index, Element& result) const
69 {
70 ASSERT(!isNull());
71
72 if (index >= m_private->elements().size())
73 return false;
74
75 const FormDataElement& element = m_private->elements()[index];
76
77 switch (element.m_type) {
78 case FormDataElement::data:
79 result.type = Element::TypeData;
80 result.data.assign(element.m_data.data(), element.m_data.size());
81 result.filePath.reset();
82 result.fileStart = 0;
83 result.fileLength = 0;
84 result.fileInfo.modificationTime = 0.0;
85 break;
86 case FormDataElement::encodedFile:
87 result.type = Element::TypeFile;
88 result.data.reset();
89 result.filePath = element.m_filename;
90 result.fileStart = 0; // FIXME: to be set from FormData.
91 result.fileLength = -1; // FIXME: to be set from FormData.
92 result.fileInfo.modificationTime = 0.0; // FIXME: to be set from FormData.
93 break;
94 default:
95 ASSERT_NOT_REACHED();
96 return false;
97 }
98
99 return true;
100 }
101
appendData(const WebData & data)102 void WebHTTPBody::appendData(const WebData& data)
103 {
104 ensureMutable();
105 // FIXME: FormDataElement::m_data should be a SharedBuffer<char>. Then we
106 // could avoid this buffer copy.
107 m_private->appendData(data.data(), data.size());
108 }
109
appendFile(const WebString & filePath)110 void WebHTTPBody::appendFile(const WebString& filePath)
111 {
112 ensureMutable();
113 m_private->appendFile(filePath);
114 }
115
appendFile(const WebString & filePath,long long fileStart,long long fileLength,const WebFileInfo & fileInfo)116 void WebHTTPBody::appendFile(const WebString& filePath, long long fileStart, long long fileLength, const WebFileInfo& fileInfo)
117 {
118 // FIXME: to be implemented.
119 }
120
identifier() const121 long long WebHTTPBody::identifier() const
122 {
123 ASSERT(!isNull());
124 return m_private->identifier();
125 }
126
setIdentifier(long long identifier)127 void WebHTTPBody::setIdentifier(long long identifier)
128 {
129 ensureMutable();
130 return m_private->setIdentifier(identifier);
131 }
132
WebHTTPBody(const PassRefPtr<FormData> & data)133 WebHTTPBody::WebHTTPBody(const PassRefPtr<FormData>& data)
134 : m_private(static_cast<WebHTTPBodyPrivate*>(data.releaseRef()))
135 {
136 }
137
operator =(const PassRefPtr<FormData> & data)138 WebHTTPBody& WebHTTPBody::operator=(const PassRefPtr<FormData>& data)
139 {
140 assign(static_cast<WebHTTPBodyPrivate*>(data.releaseRef()));
141 return *this;
142 }
143
operator PassRefPtr<FormData>() const144 WebHTTPBody::operator PassRefPtr<FormData>() const
145 {
146 return m_private;
147 }
148
assign(WebHTTPBodyPrivate * p)149 void WebHTTPBody::assign(WebHTTPBodyPrivate* p)
150 {
151 // p is already ref'd for us by the caller
152 if (m_private)
153 m_private->deref();
154 m_private = p;
155 }
156
ensureMutable()157 void WebHTTPBody::ensureMutable()
158 {
159 ASSERT(!isNull());
160 if (!m_private->hasOneRef())
161 assign(static_cast<WebHTTPBodyPrivate*>(m_private->copy().releaseRef()));
162 }
163
164 } // namespace WebKit
165