|
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "chttpresponseparser.h" |
|
17 |
|
18 #include <inetprottextutils.h> |
|
19 #include <httpstringconstants.h> |
|
20 #include <http/rhttpheaders.h> |
|
21 #include <http/framework/mrxdataobserver.h> |
|
22 #include <httperr.h> |
|
23 |
|
24 #include "chttpclienttransaction.h" |
|
25 #include "chttpconnectionmanager.h" |
|
26 #include "chttprequestcomposer.h" |
|
27 #include "mhttpresponseobserver.h" |
|
28 #include "thttpclientpanic.h" |
|
29 #include "chttpconnectioninfo.h" |
|
30 |
|
31 _LIT8(KHeaderSeparator, "\n"); |
|
32 |
|
33 CHttpResponseParser* CHttpResponseParser::NewL(CProtTransaction& aProtTrans, MRxDataObserver& aObserver, MHttpResponseObserver& aResponseObserver) |
|
34 { |
|
35 CHttpResponseParser* self = new (ELeave) CHttpResponseParser(aProtTrans, aObserver, aResponseObserver); |
|
36 CleanupStack::PushL(self); |
|
37 self->ConstructL(); |
|
38 CleanupStack::Pop(self); |
|
39 return self; |
|
40 } |
|
41 |
|
42 CHttpResponseParser::~CHttpResponseParser() |
|
43 { |
|
44 iBodyParts.Reset(); |
|
45 iMessageParser.Close(); |
|
46 // __FLOG_CLOSE; |
|
47 } |
|
48 |
|
49 CHttpResponseParser::CHttpResponseParser(CProtTransaction& aProtTrans, MRxDataObserver& aObserver, MHttpResponseObserver& aResponseObserver) |
|
50 : CRxData(aProtTrans, aObserver), iStringTable(RHTTPSession::GetTable()), iResponseObserver(aResponseObserver) |
|
51 { |
|
52 // __FLOG_OPEN("http", "httpclienthandler.txt"); |
|
53 } |
|
54 |
|
55 void CHttpResponseParser::ConstructL() |
|
56 { |
|
57 iMessageParser.OpenL(*this); |
|
58 } |
|
59 |
|
60 TInt CHttpResponseParser::CancelTransaction(TInt aError) |
|
61 { |
|
62 __FLOG_0(_T8("!! Cancelling transaction - internal error")); |
|
63 |
|
64 // Tell the connection manager to cancel this submission. |
|
65 CHttpClientTransaction& trans = static_cast<CHttpClientTransaction&>(*iProtTrans); |
|
66 CHttpConnectionManager* manager = trans.ConnectionManager(); |
|
67 |
|
68 __ASSERT_DEBUG( manager != NULL, User::Invariant() ); |
|
69 |
|
70 __FLOG_1(_T8("-> Trans %d : cancelling its connection manager"), iProtTrans->Transaction().Id()); |
|
71 |
|
72 // Transaction is still alive - ask its connection manager to cancel it. |
|
73 MHttpRequest& request = static_cast<CHttpRequestComposer&>(iProtTrans->TxData()); |
|
74 MHttpResponse& response = *this; |
|
75 manager->CancelSubmission(request, response); |
|
76 |
|
77 // Connection is now cancelled - remove the connection manager from the |
|
78 // transaction. |
|
79 trans.RemoveConnectionManager(); |
|
80 |
|
81 // Notify the rx-data observer that an error has occurred |
|
82 NotifyObserverError(aError); |
|
83 |
|
84 return KErrNone; |
|
85 } |
|
86 |
|
87 void CHttpResponseParser::NotifyObserverError(TInt aError) |
|
88 { |
|
89 TRAPD( err, iObserver->SetStatusL(*this, aError) ); |
|
90 if(err != KErrNone) |
|
91 { |
|
92 // Something has gone seriously wrong - fail the transaction |
|
93 iProtTrans->Transaction().Fail(THTTPFilterHandle::EProtocolHandler); |
|
94 } |
|
95 } |
|
96 |
|
97 /* |
|
98 * Methods from CRxData |
|
99 */ |
|
100 |
|
101 void CHttpResponseParser::ResetRxData() |
|
102 { |
|
103 iMessageParser.Reset(); |
|
104 iBodyParts.Reset(); |
|
105 iOverallDataSize = 0; |
|
106 iFlags = 0; |
|
107 } |
|
108 |
|
109 /* |
|
110 * Methods from MHTTPDataSupplier via CRxData |
|
111 */ |
|
112 |
|
113 TBool CHttpResponseParser::GetNextDataPart(TPtrC8& aDataPart) |
|
114 { |
|
115 TInt bodyPartsCount = iBodyParts.Count(); |
|
116 __ASSERT_ALWAYS(bodyPartsCount > 0, THttpClientPanic::Panic(THttpClientPanic::ENoBodyPartInDataSupplier)); |
|
117 |
|
118 // Provide the first chunk. |
|
119 aDataPart.Set(iBodyParts[0]); |
|
120 |
|
121 return (BodyComplete() && bodyPartsCount == 1); |
|
122 } |
|
123 |
|
124 void CHttpResponseParser::ReleaseData() |
|
125 { |
|
126 // Remove the oldest chunk. |
|
127 if( iBodyParts.Count() > 0 ) |
|
128 { |
|
129 iBodyParts.Remove(0); |
|
130 } |
|
131 |
|
132 // Are there any more chunks? |
|
133 if( iBodyParts.Count() > 0 ) |
|
134 { |
|
135 // Yep - notify the client of more body data |
|
136 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
137 if(trans.SendEvent(THTTPEvent::EGotResponseBodyData, THTTPEvent::EIncoming, THTTPFilterHandle(THTTPFilterHandle::EProtocolHandler)) != KErrNone) |
|
138 trans.Fail(THTTPFilterHandle::EProtocolHandler); |
|
139 } |
|
140 else |
|
141 { |
|
142 // Does this data packet need to be released? |
|
143 if( NotifyReleaseData() ) |
|
144 { |
|
145 iResponseObserver.ResponseDataParsed(); |
|
146 iFlags &= ~ENotifyReleaseData; |
|
147 } |
|
148 |
|
149 // Is the message complete? |
|
150 if( MessageComplete() ) |
|
151 { |
|
152 // All the body data has been read by the client - this transaction |
|
153 // is complete. |
|
154 TRAPD(err, iObserver->SetStatusL(*this, THTTPEvent::EResponseComplete)); |
|
155 if(err != KErrNone) |
|
156 iProtTrans->Transaction().Fail(THTTPFilterHandle::EProtocolHandler); |
|
157 } |
|
158 } |
|
159 } |
|
160 |
|
161 TInt CHttpResponseParser::OverallDataSize() |
|
162 { |
|
163 return (iOverallDataSize < 0) ? KErrNotFound : iOverallDataSize; |
|
164 } |
|
165 |
|
166 TInt CHttpResponseParser::Reset() |
|
167 { |
|
168 return KErrNotSupported; |
|
169 } |
|
170 |
|
171 void CHttpResponseParser::OnResponseReceiveTimeOut () |
|
172 { |
|
173 // Notify the client of Response Receive TimeOut. |
|
174 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
175 if(trans.SendEvent(THTTPEvent::EReceiveTimeOut, THTTPEvent::EIncoming, THTTPFilterHandle(THTTPFilterHandle::EProtocolHandler)) != KErrNone) |
|
176 { |
|
177 trans.Fail(THTTPFilterHandle::EProtocolHandler); |
|
178 } |
|
179 else |
|
180 { |
|
181 // Notify the client that Transaction Failed. |
|
182 if(trans.SendEvent(THTTPEvent::EFailed, THTTPEvent::EIncoming, THTTPFilterHandle(THTTPFilterHandle::EProtocolHandler)) != KErrNone) |
|
183 { |
|
184 trans.Fail(THTTPFilterHandle::EProtocolHandler); |
|
185 } |
|
186 } |
|
187 } |
|
188 |
|
189 TInt CHttpResponseParser::ReceiveTimeOutValue () |
|
190 { |
|
191 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
192 RStringPool stringPool = trans.Session().StringPool(); |
|
193 RStringF receiveTimeOut = stringPool.StringF(HTTP::EReceiveTimeOutValue, iStringTable); |
|
194 THTTPHdrVal receiveTimeOutVal; |
|
195 |
|
196 TBool ret = trans.PropertySet().Property(receiveTimeOut,receiveTimeOutVal); |
|
197 if(ret && (receiveTimeOutVal.Type() == THTTPHdrVal::KTIntVal)) |
|
198 { |
|
199 return receiveTimeOutVal.Int(); |
|
200 } |
|
201 return 0; |
|
202 } |
|
203 |
|
204 TBool CHttpResponseParser::ResponseInformational () |
|
205 { |
|
206 return (HTTPStatus::IsInformational(iProtTrans->Transaction().Response().StatusCode())); |
|
207 } |
|
208 |
|
209 |
|
210 /* |
|
211 * Methods from MHttpResponse |
|
212 */ |
|
213 |
|
214 void CHttpResponseParser::ResponseDataReceived(const TDesC8& aData) |
|
215 { |
|
216 __FLOG_2(_T8("Trans %d : parsing %d bytes of data"), iProtTrans->Transaction().Id(), aData.Length()); |
|
217 |
|
218 iRawData.Set(aData); |
|
219 iMessageParser.ReceivedMessageData(); |
|
220 } |
|
221 |
|
222 void CHttpResponseParser::CancelResponse() |
|
223 { |
|
224 __FLOG_1(_T8("-> Trans %d : response cancelled"), iProtTrans->Transaction().Id()); |
|
225 |
|
226 iCancellingResponse = ETrue; |
|
227 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
228 RHTTPResponse response = trans.Response(); |
|
229 |
|
230 iMessageParser.Flush (); |
|
231 iMessageParser.Reset(); |
|
232 iCancellingResponse = EFalse; |
|
233 } |
|
234 |
|
235 void CHttpResponseParser::ConnectionError(TInt aError) |
|
236 { |
|
237 // Check whether we need to resubmit this request once again. |
|
238 CHttpClientTransaction* clientTrans = static_cast<CHttpClientTransaction*>(iProtTrans); |
|
239 if ( !clientTrans->NeedDisconnectNotification() && (iOverallDataSize != MHttpMessageParserObserver::EUnknown) |
|
240 && (clientTrans->RetryNeeded () && ( aError == KErrEof || aError == KErrCancel || aError == KErrDisconnected ) ) ) |
|
241 { |
|
242 if ( !clientTrans->ConnectionManager()->ConnectionInfo().IsNonPersistent () ) |
|
243 aError = KErrHttpNonPipeliningError; |
|
244 } |
|
245 |
|
246 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
247 RHTTPResponse response = trans.Response(); |
|
248 |
|
249 // Check there is a status code, no body and error status ( KErrEof & KErrDisconnected ) |
|
250 if ( response.StatusCode() && !response.HasBody() && ( aError == KErrEof || aError == KErrDisconnected ) ) |
|
251 { |
|
252 // Notify the client that all the response headers have been parsed. |
|
253 trans.SendEvent(THTTPEvent::EGotResponseHeaders, THTTPEvent::EIncoming, THTTPFilterHandle(THTTPFilterHandle::EProtocolHandler)); |
|
254 } |
|
255 |
|
256 // Is the server closing the connection been expected? |
|
257 if( iOverallDataSize == MHttpMessageParserObserver::EUnknown && !BodyComplete() && |
|
258 (aError == KErrEof || aError == KErrDisconnected) ) |
|
259 { |
|
260 |
|
261 __FLOG_1(_T8("Trans %d : waiting for connection close to mark end of response body"), iProtTrans->Transaction().Id()); |
|
262 |
|
263 if (BodyPresent()) |
|
264 { |
|
265 iFlags |= ( EMessageComplete | EBodyComplete ) ; |
|
266 |
|
267 // Append NULL to the body chunk |
|
268 |
|
269 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
270 TInt err = iBodyParts.Append(TPtrC8()); |
|
271 if ( err!=KErrNone ) |
|
272 trans.Fail(THTTPFilterHandle::EProtocolHandler); |
|
273 |
|
274 |
|
275 // Notify the client that there is a body part |
|
276 if(iProtTrans->Transaction().SendEvent(THTTPEvent::EGotResponseBodyData, |
|
277 THTTPEvent::EIncoming, |
|
278 THTTPFilterHandle(THTTPFilterHandle::EProtocolHandler)) != KErrNone) |
|
279 { |
|
280 trans.Fail (THTTPFilterHandle::EProtocolHandler); |
|
281 } |
|
282 |
|
283 } |
|
284 else |
|
285 { |
|
286 // Yep, since from RFC2616 section 4.4 Message Length, the end of a |
|
287 // response body can be determined by the server closing the connection |
|
288 // Notify the parser that the connection close has happened. |
|
289 TRAPD( err, iMessageParser.CompletedBodyDataL() ); |
|
290 if( err != KErrNone ) |
|
291 { |
|
292 // Something has gone seriously wrong - fail the transaction |
|
293 iProtTrans->Transaction().Fail(THTTPFilterHandle::EProtocolHandler); |
|
294 } |
|
295 } |
|
296 |
|
297 } |
|
298 else |
|
299 { |
|
300 // Need to map to appropriate error code if the disconnect notification is |
|
301 // asked by the client |
|
302 if ( clientTrans->NeedDisconnectNotification() ) |
|
303 { |
|
304 CHttpRequestComposer& request = static_cast<CHttpRequestComposer&>(iProtTrans->TxData()); |
|
305 // if we are consuming response then the request has been not sent fully. |
|
306 if ( ConsumingResponse () || !request.RequestSent() ) |
|
307 aError = KErrHttpRequestNotSent; |
|
308 // Check if we received some response |
|
309 // Check for status code that means we received something as response |
|
310 // for this transaction |
|
311 else if ( response.StatusCode () ) |
|
312 aError = KErrHttpPartialResponseReceived; |
|
313 else |
|
314 aError = KErrHttpResponseNotReceived; |
|
315 } |
|
316 |
|
317 // Notify the rx-data observer that an error has occurred |
|
318 NotifyObserverError(aError); |
|
319 } |
|
320 |
|
321 } |
|
322 |
|
323 /* |
|
324 * Methods from MHttpMessageParserObserver |
|
325 */ |
|
326 |
|
327 void CHttpResponseParser::GetDataPacket(TPtrC8& aData) |
|
328 { |
|
329 aData.Set(iRawData); |
|
330 } |
|
331 |
|
332 void CHttpResponseParser::ReleaseDataPacket() |
|
333 { |
|
334 if( ConsumingResponse() && MessageComplete() ) |
|
335 { |
|
336 // Ok, the response was a 1xx message which has been consumed. Reset the |
|
337 // this object and continue parsing the next part of the response. |
|
338 ResetRxData(); |
|
339 iMessageParser.ReceivedMessageData(); |
|
340 } |
|
341 else if( iBodyParts.Count() == 0 ) |
|
342 { |
|
343 // Can release as there are no body chunks waiting to be passed to the |
|
344 // client. |
|
345 iResponseObserver.ResponseDataParsed(); |
|
346 } |
|
347 else |
|
348 { |
|
349 // Flag that the data needs to be released |
|
350 iFlags |= ENotifyReleaseData; |
|
351 } |
|
352 } |
|
353 |
|
354 void CHttpResponseParser::StartLineL(const TDesC8& aStartLine) |
|
355 { |
|
356 __START_PERFORMANCE_LOGGER(); |
|
357 // The RFC2616 defines the Status-Line as follows - |
|
358 // |
|
359 // Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF |
|
360 // |
|
361 // First extract the HTTP-Version |
|
362 TPtrC8 data = aStartLine; |
|
363 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveLeft)); |
|
364 |
|
365 // HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT |
|
366 TInt consumed = data.Locate('/'); |
|
367 User::LeaveIfError(consumed); |
|
368 |
|
369 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
370 RStringPool stringPool = trans.Session().StringPool(); |
|
371 |
|
372 TPtrC8 http = data.Left(consumed); |
|
373 if( http.CompareF(stringPool.StringF(HTTP::EHTTP, iStringTable).DesC()) != 0 ) |
|
374 User::Leave(KErrCorrupt); |
|
375 |
|
376 // Skip past the HTTP and "/" |
|
377 data.Set(data.Mid(consumed + 1)); |
|
378 |
|
379 // Extract the major version number |
|
380 TInt major; |
|
381 consumed = InetProtTextUtils::ConvertDescriptorToInt(data, major); |
|
382 User::LeaveIfError(consumed); |
|
383 |
|
384 if( data.Length() > consumed ) |
|
385 { |
|
386 // Skip past major version number and the "." |
|
387 data.Set(data.Mid(consumed + 1)); |
|
388 } |
|
389 else |
|
390 { |
|
391 User::Leave(KErrCorrupt); |
|
392 } |
|
393 |
|
394 // Extract the minor version number |
|
395 TInt minor; |
|
396 consumed = InetProtTextUtils::ConvertDescriptorToInt(data, minor); |
|
397 User::LeaveIfError(consumed); |
|
398 |
|
399 if( data.Length() > consumed ) |
|
400 { |
|
401 // Skip past minor version number and the SP |
|
402 data.Set(data.Mid(consumed + 1)); |
|
403 |
|
404 // Clear any extra surrounding whitespace |
|
405 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveBoth)); |
|
406 } |
|
407 else |
|
408 { |
|
409 User::Leave(KErrCorrupt); |
|
410 } |
|
411 |
|
412 // Extract the status code |
|
413 TInt status; |
|
414 consumed = InetProtTextUtils::ConvertDescriptorToInt(data, status); |
|
415 User::LeaveIfError(consumed); |
|
416 |
|
417 if( data.Length() > consumed ) |
|
418 { |
|
419 // Skip past status code and the SP |
|
420 data.Set(data.Mid(consumed + 1)); |
|
421 |
|
422 // Remaining data is the status reason - trim any leading whitespace as right side already trimmed |
|
423 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveLeft)); |
|
424 } |
|
425 else |
|
426 { |
|
427 // No reason phrase following the status code |
|
428 data.Set(KNullDesC8()); |
|
429 } |
|
430 // Populate transaction with Status-Line info |
|
431 RStringF reason = stringPool.OpenFStringL(data); |
|
432 |
|
433 RHTTPResponse response = trans.Response(); |
|
434 |
|
435 TVersion version(major, minor, 0); |
|
436 response.SetVersion(version); |
|
437 response.SetStatusCode(status); |
|
438 response.SetStatusText(reason); |
|
439 reason.Close(); |
|
440 |
|
441 __FLOG_5(_T8("Trans %d : status-line -> HTTP/%d.%d %d %S"), iProtTrans->Transaction().Id(), major, minor, status, &response.StatusText().DesC()); |
|
442 |
|
443 // Check to see if a body is expected |
|
444 if( HTTPStatus::IsInformational(status) ) |
|
445 { |
|
446 // 1xx status - no body and need to consume this respons |
|
447 iFlags |= EConsumingResponse; |
|
448 |
|
449 __FLOG_1(_T8("Trans %d : informational 1xx status - consuming response"), iProtTrans->Transaction().Id()); |
|
450 } |
|
451 __END_PERFORMANCE_LOGGER(_L(",CHttpResponseParser::StartLineL()")); |
|
452 } |
|
453 |
|
454 void CHttpResponseParser::HeaderL(const TDesC8& aFieldName, TDesC8& aFieldValue) |
|
455 { |
|
456 // Only set the headers if we are not consuming the response. |
|
457 if( !ConsumingResponse() ) |
|
458 { |
|
459 // Add the header to the response header collection |
|
460 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
461 RStringPool stringPool = trans.Session().StringPool(); |
|
462 |
|
463 RStringF name = stringPool.OpenFStringL(aFieldName); |
|
464 CleanupClosePushL(name); |
|
465 |
|
466 RHTTPHeaders headers = trans.Response().GetHeaderCollection(); |
|
467 headers.SetRawFieldL(name, aFieldValue, KHeaderSeparator); |
|
468 |
|
469 /************************************************************************************** |
|
470 * DEF143319 - HTTPS page stops downloading between ~25 and 50 KBytes |
|
471 * When multiple requests are concatenated in ONE buffer and sent to the server, |
|
472 * the WebLogic server does not process all requests. |
|
473 * So pipelining feature has to be disabled. |
|
474 * This fix enhances the robustness of HTTP stack to interoperate with different servers. |
|
475 ***************************************************************************************/ |
|
476 if (name.DesC().CompareF(stringPool.StringF(HTTP::EServer,RHTTPSession::GetTable()).DesC()) == KErrNone) |
|
477 { |
|
478 _LIT8(KWebLogicServerName,"WebLogic"); |
|
479 if (aFieldValue.FindF(KWebLogicServerName)!= KErrNotFound) |
|
480 { |
|
481 RHTTPHeaders headReq = trans.Request().GetHeaderCollection(); |
|
482 RStringF hostStr = stringPool.StringF(HTTP::EHost, RHTTPSession::GetTable()); |
|
483 THTTPHdrVal hostVal; |
|
484 |
|
485 if( headReq.GetField(hostStr, 0, hostVal) == KErrNotFound ) |
|
486 { |
|
487 // No Host header - do not know which host to connect to. |
|
488 User::Leave(KErrHttpGeneralHeaderMissingHost); |
|
489 } |
|
490 |
|
491 __ASSERT_DEBUG( hostVal.Type() == THTTPHdrVal::KStrFVal, User::Invariant() ); |
|
492 |
|
493 CHttpClientTransaction& protTran = static_cast<CHttpClientTransaction&>(*iProtTrans); |
|
494 CHttpConnectionManager* manager = protTran.ConnectionManager(); |
|
495 #if defined (_DEBUG) && defined (_LOGGING) |
|
496 __FLOG_0(_T8("Pipelining is disabled for WebLogic Server- \n")); |
|
497 #endif |
|
498 manager->InsertPipelineFailedHost(hostVal.StrF().DesC()); |
|
499 } |
|
500 } |
|
501 |
|
502 CleanupStack::PopAndDestroy(&name); |
|
503 |
|
504 if( BodyComplete() && !GotTrailers() ) |
|
505 { |
|
506 // This was a trailer header - flag that there are trailers. |
|
507 iFlags |= EGotTrailers; |
|
508 } |
|
509 |
|
510 #if defined (_DEBUG) && defined (_LOGGING) |
|
511 if( GotTrailers() ) |
|
512 __FLOG_3(_T8("Trans %d : response trailer -> %S: %S"), iProtTrans->Transaction().Id(), &aFieldName, &aFieldValue); |
|
513 else |
|
514 __FLOG_3(_T8("Trans %d : response header -> %S: %S"), iProtTrans->Transaction().Id(), &aFieldName, &aFieldValue); |
|
515 #endif |
|
516 } |
|
517 else |
|
518 { |
|
519 __FLOG_1(_T8("Trans %d : informational 1xx status - Headers will not be added"), iProtTrans->Transaction().Id()); |
|
520 // Do nothing |
|
521 } |
|
522 } |
|
523 |
|
524 TInt CHttpResponseParser::BodySizeL() |
|
525 { |
|
526 __START_PERFORMANCE_LOGGER(); |
|
527 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
528 RHTTPResponse response = trans.Response(); |
|
529 |
|
530 if( !ConsumingResponse() ) |
|
531 { |
|
532 // Notify the client that all the response headers have been parsed. |
|
533 trans.SendEventL( |
|
534 THTTPEvent::EGotResponseHeaders, |
|
535 THTTPEvent::EIncoming, |
|
536 THTTPFilterHandle(THTTPFilterHandle::EProtocolHandler) |
|
537 ); |
|
538 } |
|
539 |
|
540 RStringPool stringPool = trans.Session().StringPool(); |
|
541 RHTTPHeaders headers = response.GetHeaderCollection(); |
|
542 |
|
543 if (CheckForNonPersistentConnection()) |
|
544 { |
|
545 // Notify the connection manager that the connection should be |
|
546 // non-persistent. |
|
547 CHttpClientTransaction& trans = static_cast<CHttpClientTransaction&>(*iProtTrans); |
|
548 CHttpConnectionManager* manager = trans.ConnectionManager(); |
|
549 |
|
550 __ASSERT_DEBUG( manager != NULL, User::Invariant() ); |
|
551 |
|
552 manager->MakeConnectionNonPersistent(); |
|
553 } |
|
554 |
|
555 // Check for a body... |
|
556 if( ConsumingResponse() || // this implies that the status code was 1xx - no body |
|
557 response.StatusCode() == 204 || |
|
558 response.StatusCode() == 304 || |
|
559 trans.Request().Method().Index(iStringTable) == HTTP::EHEAD || |
|
560 trans.Request().Method().Index(iStringTable) == HTTP::ECONNECT && // if 2xx response... |
|
561 ( HTTPStatus::IsSuccessful(response.StatusCode()) ) ) |
|
562 { |
|
563 __FLOG_1(_T8("Trans %d : no response entity body"), iProtTrans->Transaction().Id()); |
|
564 |
|
565 // No entity body is expected as specified in RFC2616 section 4.4. |
|
566 iOverallDataSize = MHttpMessageParserObserver::ENoBody; |
|
567 iFlags |= EBodyComplete; |
|
568 __END_PERFORMANCE_LOGGER(_L(",CHttpResponseParser::BodySizeL()")); |
|
569 return iOverallDataSize; |
|
570 } |
|
571 |
|
572 // A body is expected - find the length. First check for a Transfer-Encoding |
|
573 // header field. |
|
574 response.SetBody(*this); |
|
575 THTTPHdrVal value; |
|
576 RStringF name = stringPool.StringF(HTTP::ETransferEncoding, iStringTable); |
|
577 |
|
578 if( headers.GetField(name, 0, value) == KErrNone ) |
|
579 { |
|
580 // It exists - what's the value? |
|
581 if( value.Type() == THTTPHdrVal::KStrFVal && |
|
582 value.StrF().Index(iStringTable) == HTTP::EChunked ) |
|
583 { |
|
584 // The Transfer-Encoding header is Chunked and as the chunked |
|
585 // encoding is removed, we remove the header. |
|
586 headers.RemoveField(name); |
|
587 |
|
588 __FLOG_1(_T8("Trans %d : chunked response entity body"), iProtTrans->Transaction().Id()); |
|
589 |
|
590 // As the entity body is chunked the overall data size is unknown. |
|
591 iOverallDataSize = MHttpMessageParserObserver::EChunked; |
|
592 __END_PERFORMANCE_LOGGER(_L(",CHttpResponseParser::BodySizeL()")); |
|
593 return iOverallDataSize; |
|
594 } |
|
595 } |
|
596 |
|
597 // Either no Transfer-Encoding header was present - now check for a |
|
598 // Content-Length header. |
|
599 name = stringPool.StringF(HTTP::EContentLength, iStringTable); |
|
600 TInt err = headers.GetField(name, 0, value); |
|
601 |
|
602 if( err != KErrNone && err != KErrNotFound ) |
|
603 User::Leave(err); |
|
604 |
|
605 if( err == KErrNone && value.Type() == THTTPHdrVal::KTIntVal ) |
|
606 { |
|
607 // Content-Length header value specified the length of entity in bytes. |
|
608 iOverallDataSize = value.Int(); |
|
609 |
|
610 __FLOG_2(_T8("Trans %d : response entity body length = %d"), iProtTrans->Transaction().Id(), iOverallDataSize); |
|
611 |
|
612 __END_PERFORMANCE_LOGGER(_L(",CHttpResponseParser::BodySizeL()")); |
|
613 return iOverallDataSize; |
|
614 } |
|
615 |
|
616 __FLOG_1(_T8("Trans %d : unknown entity body length"), iProtTrans->Transaction().Id()); |
|
617 |
|
618 // There was no Content-Length header either, so the server will signal the |
|
619 // end of the message by closing the connection - overall data size unknown. |
|
620 iOverallDataSize = MHttpMessageParserObserver::EUnknown; |
|
621 __END_PERFORMANCE_LOGGER(_L(",CHttpResponseParser::BodySizeL()")); |
|
622 return iOverallDataSize; |
|
623 } |
|
624 |
|
625 void CHttpResponseParser::BodyChunkL(const TDesC8& aData) |
|
626 { |
|
627 // Append the body chunk to the array. |
|
628 iFlags |= EBodyPresent; |
|
629 TPtrC8 chunk = aData; |
|
630 User::LeaveIfError(iBodyParts.Append(chunk)); |
|
631 |
|
632 __FLOG_2(_T8("Trans %d : received %d bytes of body data"), iProtTrans->Transaction().Id(), chunk.Length()); |
|
633 |
|
634 |
|
635 // Only notify the client that there is a body part if there is only one. |
|
636 if( iBodyParts.Count() == 1 ) |
|
637 { |
|
638 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
639 trans.SendEventL( |
|
640 THTTPEvent::EGotResponseBodyData, |
|
641 THTTPEvent::EIncoming, |
|
642 THTTPFilterHandle(THTTPFilterHandle::EProtocolHandler) |
|
643 ); |
|
644 } |
|
645 } |
|
646 |
|
647 void CHttpResponseParser::BodyCompleteL() |
|
648 { |
|
649 __FLOG_1(_T8("Trans %d : body complete"), iProtTrans->Transaction().Id()); |
|
650 |
|
651 iFlags |= EBodyComplete; |
|
652 } |
|
653 |
|
654 void CHttpResponseParser::MessageCompleteL(const TPtrC8& aExcessData) |
|
655 { |
|
656 __FLOG_2(_T8("Trans %d : message complete - received %d bytes of excess data"), iProtTrans->Transaction().Id(), aExcessData.Length()); |
|
657 |
|
658 iFlags |= EMessageComplete; |
|
659 |
|
660 if( !ConsumingResponse() ) |
|
661 { |
|
662 iResponseObserver.ResponseComplete(aExcessData); |
|
663 |
|
664 if ( iCancellingResponse ) |
|
665 { |
|
666 return; |
|
667 } |
|
668 if( GotTrailers() ) |
|
669 { |
|
670 // Inform the client that there were trailers |
|
671 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
672 trans.SendEventL( |
|
673 THTTPEvent::EGotResponseTrailerHeaders, |
|
674 THTTPEvent::EIncoming, |
|
675 THTTPFilterHandle(THTTPFilterHandle::EProtocolHandler) |
|
676 ); |
|
677 } |
|
678 if( iBodyParts.Count() == 0 ) |
|
679 { |
|
680 // All the body data has been read by the client - this transaction is |
|
681 // complete. |
|
682 iObserver->SetStatusL(*this, THTTPEvent::EResponseComplete); |
|
683 } |
|
684 } |
|
685 else |
|
686 { |
|
687 // There could be excess data - this data is for this response and needs |
|
688 // to be parsed. Set it as the raw data. |
|
689 CHttpRequestComposer& request = static_cast<CHttpRequestComposer&>(iProtTrans->TxData()); |
|
690 if(request.IsSuspendedRequest()) |
|
691 { |
|
692 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
693 trans.SendEventL( |
|
694 THTTPEvent::EReceived100Continue, |
|
695 THTTPEvent::EIncoming, |
|
696 THTTPFilterHandle(THTTPFilterHandle::EProtocolHandler) |
|
697 ); |
|
698 |
|
699 request.ResumeSuspendedRequest(); |
|
700 } |
|
701 |
|
702 else |
|
703 { |
|
704 iRawData.Set(aExcessData); |
|
705 } |
|
706 } |
|
707 } |
|
708 |
|
709 TInt CHttpResponseParser::HandleParserError(TInt aError) |
|
710 { |
|
711 __FLOG_1(_T8("!! Error : %d"), aError); |
|
712 __FLOG_1(_T8("-> Trans %d : response parsing error - cancelling transaction"), iProtTrans->Transaction().Id()); |
|
713 |
|
714 // Cancel the transaction - this also notifies the rx-data observer that an |
|
715 // error has occurred. |
|
716 return CancelTransaction(aError); |
|
717 } |
|
718 |
|
719 TBool CHttpResponseParser::CheckForNonPersistentConnection() |
|
720 { |
|
721 RHTTPTransaction trans = iProtTrans->Transaction(); |
|
722 RStringPool stringPool = trans.Session().StringPool(); |
|
723 TBool keepAlive = EFalse; |
|
724 |
|
725 // Check for a Connection: close header |
|
726 // |
|
727 RStringF name = stringPool.StringF(HTTP::EConnection, iStringTable); |
|
728 THTTPHdrVal value; |
|
729 RHTTPHeaders headers = trans.Response().GetHeaderCollection(); |
|
730 if( headers.GetField(name, 0, value) == KErrNone && value.Type() == THTTPHdrVal::KStrFVal) |
|
731 { |
|
732 // It exists - what's the value? |
|
733 if( value.StrF().Index(iStringTable) == HTTP::EClose ) |
|
734 { |
|
735 // The Connection header has a value of close. |
|
736 __FLOG_1(_T8("Trans %d : server has specified a non-persistent connection"), iProtTrans->Transaction().Id()); |
|
737 return ETrue; |
|
738 } |
|
739 else if( value.StrF().Index(iStringTable) == HTTP::EKeepAlive ) |
|
740 { |
|
741 // This request by the server to persist the connection is used by HTTP/1.0 |
|
742 // servers to override default behaviour |
|
743 keepAlive = ETrue; |
|
744 } |
|
745 } |
|
746 |
|
747 |
|
748 // Is this a HTTP/1.0 response? |
|
749 // |
|
750 TVersion version = trans.Response().Version(); |
|
751 if( !keepAlive && version.iMinor == 0 && version.iMajor == 1 && |
|
752 trans.Request().Method().Index(iStringTable) != HTTP::ECONNECT ) |
|
753 { |
|
754 __FLOG_1(_T8("Trans %d : an HTTP/1.0 server, default to non-persistent connection"), iProtTrans->Transaction().Id()); |
|
755 return ETrue; |
|
756 } |
|
757 |
|
758 // So treat connection as persistent. |
|
759 return EFalse; |
|
760 } |
|
761 |
|
762 TBool CHttpResponseParser::ResponseCompleted () |
|
763 { |
|
764 // Check if we received a full response. No matter if client is not fully notified |
|
765 __FLOG_2 (_T8 ("Message completed? %d Body Completed? %d"), MessageComplete (), BodyComplete () ); |
|
766 return MessageComplete (); |
|
767 } |
|
768 |
|
769 TBool CHttpResponseParser::NeedCompletion () |
|
770 { |
|
771 RHTTPTransaction trans = iProtTrans->Transaction (); |
|
772 if ( HTTPStatus::IsRedirection ( trans.Response().StatusCode() ) ) |
|
773 { |
|
774 // If it is a redirection message then we need to complete the response |
|
775 return ETrue; |
|
776 } |
|
777 return EFalse; |
|
778 } |
|
779 |
|
780 TBool CHttpResponseParser::CompleteResponse ( const TDesC8& aData ) |
|
781 { |
|
782 return iMessageParser.CompleteMessage ( aData ); |
|
783 } |
|
784 |
|
785 void CHttpResponseParser::FailTransactionL() |
|
786 { |
|
787 iProtTrans->Transaction().SendEventL(KErrHttpOptimiserFailsTrans, |
|
788 THTTPEvent::EIncoming, |
|
789 THTTPFilterHandle::EProtocolHandler); |
|
790 } |
|
791 |
|
792 void CHttpResponseParser::Reserved_MHttpMessageParserObserver() |
|
793 { |
|
794 User::Invariant(); |
|
795 } |
|
796 |
|
797 |