|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Implementation of HttpPostDataSupplier |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "config.h" |
|
21 |
|
22 #include <e32std.h> |
|
23 #include <e32base.h> |
|
24 #include "HttpPostDataSupplier.h" |
|
25 #include "PostDataItem.h" |
|
26 #include "FormData.h" |
|
27 #include "brctl.h" |
|
28 |
|
29 using namespace WebCore; |
|
30 |
|
31 // EXTERNAL DATA STRUCTURES |
|
32 |
|
33 // EXTERNAL FUNCTION PROTOTYPES |
|
34 |
|
35 // CONSTANTS |
|
36 // The post is transmitted in parts. A buffer is allocated of max size to |
|
37 // contain the next part to be transmitted. |
|
38 const TInt KPartMaxSize = 10240; |
|
39 |
|
40 // MACROS |
|
41 |
|
42 // LOCAL CONSTANTS AND MACROS |
|
43 |
|
44 // MODULE DATA STRUCTURES |
|
45 |
|
46 // LOCAL FUNCTION PROTOTYPES |
|
47 |
|
48 // FORWARD DECLARATIONS |
|
49 |
|
50 // ============================= LOCAL FUNCTIONS =============================== |
|
51 |
|
52 |
|
53 // ============================ MEMBER FUNCTIONS =============================== |
|
54 |
|
55 |
|
56 void HttpPostDataSupplier::initL(FormData* formData) |
|
57 { |
|
58 m_firstPart = true; |
|
59 m_lastPart = false; |
|
60 m_dataPart = NULL; |
|
61 m_totalSize = 0; |
|
62 m_postItemIndex = 0; |
|
63 m_refCount = 0; |
|
64 |
|
65 PostDataItem* postDataItem = NULL; |
|
66 //iterate thru the post items and create CPostDataItem instance for each data |
|
67 size_t n = formData->elements().size(); |
|
68 for (size_t index = 0; index < n; ++index) { |
|
69 const FormDataElement& formDataElement = formData->elements()[index]; |
|
70 if (formDataElement.m_type == FormDataElement::data) { |
|
71 postDataItem = new (ELeave) PostDataItem; |
|
72 // No CleanupStack support for a class that is not based on CBase. |
|
73 m_postData.append(postDataItem); |
|
74 postDataItem->initL(&formDataElement); |
|
75 } |
|
76 else { |
|
77 ASSERT(formDataElement.m_type == FormDataElement::encodedFile); |
|
78 // if a file read failed, just ignore the form element and instead |
|
79 // post rest of the content |
|
80 postDataItem = new(ELeave) FileDataItem; |
|
81 m_postData.append(postDataItem); |
|
82 TRAPD(error, postDataItem->initL(&formDataElement)); |
|
83 if (error != KErrNone) { |
|
84 m_postData.removeLast(); |
|
85 delete postDataItem; |
|
86 if(error == KErrNoMemory) { |
|
87 User::Leave(error); |
|
88 } |
|
89 continue; |
|
90 } |
|
91 } |
|
92 // get the size of the data and add to the total size |
|
93 m_totalSize += postDataItem->pendingContentSize(); |
|
94 } |
|
95 } |
|
96 |
|
97 |
|
98 // ----------------------------------------------------------------------------- |
|
99 // HttpPostDataSupplier::HttpPostDataSupplier |
|
100 // C++ default constructor can NOT contain any code, that |
|
101 // might leave. |
|
102 // ----------------------------------------------------------------------------- |
|
103 // |
|
104 HttpPostDataSupplier::HttpPostDataSupplier(RHTTPTransaction* transaction, CBrCtl* brctl) |
|
105 :m_transaction(transaction), m_brctl(brctl) |
|
106 { |
|
107 } |
|
108 |
|
109 |
|
110 HttpPostDataSupplier::~HttpPostDataSupplier() |
|
111 { |
|
112 deleteAllValues(m_postData); |
|
113 } |
|
114 |
|
115 // ----------------------------------------------------------------------------- |
|
116 // HttpPostDataSupplier::ReleaseData |
|
117 // |
|
118 // ----------------------------------------------------------------------------- |
|
119 // |
|
120 void HttpPostDataSupplier::ReleaseData() |
|
121 { |
|
122 delete m_dataPart; |
|
123 m_dataPart = NULL; |
|
124 if(!m_lastPart) { |
|
125 TRAP_IGNORE(m_transaction->NotifyNewRequestBodyPartL()); |
|
126 } |
|
127 } |
|
128 |
|
129 // ----------------------------------------------------------------------------- |
|
130 // HttpPostDataSupplier::OverallDataSize |
|
131 // |
|
132 // ----------------------------------------------------------------------------- |
|
133 // |
|
134 TInt HttpPostDataSupplier::OverallDataSize() |
|
135 { |
|
136 return m_totalSize; |
|
137 } |
|
138 |
|
139 // ----------------------------------------------------------------------------- |
|
140 // HttpPostDataSupplier::OverallDataSize |
|
141 // |
|
142 // ----------------------------------------------------------------------------- |
|
143 // |
|
144 TInt HttpPostDataSupplier::Reset() |
|
145 { |
|
146 m_firstPart = true; |
|
147 m_postItemIndex = 0; |
|
148 m_lastPart = false; |
|
149 //iterate thru the post items and create CPostDataItem instance for each data |
|
150 size_t n = m_postData.size(); |
|
151 for (int index = 0 ; index < m_postData.size(); index++) { |
|
152 PostDataItem* postDataItem = m_postData[index]; |
|
153 postDataItem->reset(); |
|
154 } |
|
155 delete m_dataPart; |
|
156 m_dataPart = NULL; |
|
157 return KErrNone; |
|
158 } |
|
159 |
|
160 // ----------------------------------------------------------------------------- |
|
161 // HttpPostDataSupplier::getNextDataPartL |
|
162 // Accessor function to the request body. Return the body in one chunk. |
|
163 // ----------------------------------------------------------------------------- |
|
164 // |
|
165 void HttpPostDataSupplier::getNextDataPartL(TPtrC8& aDataPart) |
|
166 { |
|
167 if(m_totalSize > KPartMaxSize && m_firstPart /*&& iBrCtlLoadEventObserver*/) { |
|
168 m_brctl->HandleBrowserLoadEventL( |
|
169 TBrCtlDefs::EEventUploadStart, m_totalSize / 1000, 0) ; |
|
170 m_firstPart = false; |
|
171 } |
|
172 // release previous part if it is still valid |
|
173 delete m_dataPart; |
|
174 m_dataPart = NULL; |
|
175 int partSize = 0; |
|
176 int partPostItems = m_postItemIndex; |
|
177 // find out the amount of memory that needs to be allocated for this part |
|
178 while (partPostItems < m_postData.size()) { |
|
179 PostDataItem* postDataItem = m_postData[partPostItems]; |
|
180 if((partSize += postDataItem->pendingContentSize()) > KPartMaxSize ) { |
|
181 partSize = KPartMaxSize; |
|
182 break; |
|
183 } |
|
184 partPostItems++; |
|
185 } |
|
186 // read the content of the items |
|
187 m_dataPart = HBufC8::NewL(partSize); |
|
188 for (int index = m_postItemIndex; index < m_postData.size() && index <= partPostItems; index++) { |
|
189 PostDataItem* postDataItem = m_postData[index]; |
|
190 HBufC8* itemContent = postDataItem->dataL(partSize - m_dataPart->Length()); |
|
191 m_dataPart->Des().Append( *itemContent ); |
|
192 delete itemContent; |
|
193 } |
|
194 // if all the items have been read |
|
195 if(partPostItems == m_postData.size()) { |
|
196 m_lastPart = true; |
|
197 } |
|
198 else { |
|
199 // if the last part has more data to be posted , set m_postItemIndex to that index else advance the index |
|
200 if(m_postData[partPostItems]->pendingContentSize()) { |
|
201 m_postItemIndex = partPostItems; |
|
202 } |
|
203 // advance to the next item |
|
204 else { |
|
205 m_postItemIndex = partPostItems + 1; |
|
206 } |
|
207 } |
|
208 aDataPart.Set(m_dataPart->Des()); |
|
209 if(m_totalSize > KPartMaxSize) { |
|
210 if(!m_lastPart) { |
|
211 m_brctl->HandleBrowserLoadEventL( |
|
212 TBrCtlDefs::EEventUploadIncrement, aDataPart.Length()/1000, 0) ; |
|
213 } |
|
214 else { |
|
215 m_brctl->HandleBrowserLoadEventL( |
|
216 TBrCtlDefs::EEventUploadFinished, aDataPart.Length()/1000, 0) ; |
|
217 } |
|
218 } |
|
219 } |
|
220 |
|
221 // ----------------------------------------------------------------------------- |
|
222 // HttpPostDataSupplier::GetNextDataPart |
|
223 // Accessor function to the request body. Return the body in one chunk. |
|
224 // ----------------------------------------------------------------------------- |
|
225 // |
|
226 TBool HttpPostDataSupplier::GetNextDataPart(TPtrC8& aDataPart) |
|
227 { |
|
228 if(!m_lastPart) { |
|
229 TRAP_IGNORE( getNextDataPartL(aDataPart)); |
|
230 // TODO:how should the error be handled ????, should the transaction be cancelled |
|
231 } |
|
232 else { |
|
233 aDataPart.Set(KNullDesC8); |
|
234 } |
|
235 return m_lastPart; |
|
236 } |
|
237 |
|
238 // End of File |