|
1 // Copyright (c) 2001-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 "chttpdefaultheaderwriter.h" |
|
17 |
|
18 #include <httperr.h> |
|
19 |
|
20 #include "CHeaderField.h" |
|
21 |
|
22 // "Sun, 06 Nov 1994 08:49:37 GMT" ; RFC 822, updated by RFC 1123 |
|
23 _LIT(KDateFormatRfc1123,"%F%*E, %D %*N %Y %H:%T:%S GMT"); |
|
24 |
|
25 CHttpDefaultHeaderWriter* CHttpDefaultHeaderWriter::NewL(RStringPool aStringPool) |
|
26 /** |
|
27 Factory constructor. |
|
28 @internalComponent |
|
29 @param aStringPool The current string pool. |
|
30 @return A pointer to a fully initialised object. |
|
31 @leave KErrNoMemory Not enough memory to create object. |
|
32 */ |
|
33 { |
|
34 return new (ELeave) CHttpDefaultHeaderWriter(aStringPool); |
|
35 } |
|
36 |
|
37 CHttpDefaultHeaderWriter::~CHttpDefaultHeaderWriter() |
|
38 /** |
|
39 Destructor |
|
40 @internalComponent |
|
41 */ |
|
42 { |
|
43 } |
|
44 |
|
45 CHttpDefaultHeaderWriter::CHttpDefaultHeaderWriter(RStringPool aStringPool) |
|
46 : CHeaderWriter(), iStringPool(aStringPool) |
|
47 /** |
|
48 Constructor |
|
49 @internalComponent |
|
50 @param aStringPool The current string pool. |
|
51 */ |
|
52 { |
|
53 } |
|
54 |
|
55 /* |
|
56 * Methods from CHeaderWriter |
|
57 */ |
|
58 |
|
59 void CHttpDefaultHeaderWriter::EncodeHeaderL(RHeaderField& aHeader) |
|
60 /** |
|
61 Encodes the header field value. The default behaviour for fields which are |
|
62 unknown consists of copying the first header part into the raw data. |
|
63 @internalComponent |
|
64 @param aHeader The header field to be encoded. |
|
65 @todo |
|
66 */ |
|
67 { |
|
68 // By default, the best is to write out the first part verbatim |
|
69 THeaderFieldPartIter iter = aHeader.PartsL(); |
|
70 iter.First(); |
|
71 if( iter.AtEnd() ) |
|
72 User::Leave(KErrHttpEncodeDefault); |
|
73 |
|
74 const CHeaderFieldPart* part = iter(); |
|
75 if( part == NULL ) |
|
76 User::Leave(KErrHttpEncodeDefault); |
|
77 |
|
78 THTTPHdrVal value = part->Value(); |
|
79 aHeader.BeginRawDataL(); |
|
80 |
|
81 // Encoding depends on value type |
|
82 switch( value.Type() ) |
|
83 { |
|
84 case THTTPHdrVal::KTIntVal: |
|
85 { |
|
86 // Integer - convert to descriptor form |
|
87 const TInt KNumDigitsInMaxInt = 8; |
|
88 TBuf8<KNumDigitsInMaxInt> valBuf; |
|
89 valBuf.Num(value.Int()); |
|
90 aHeader.WriteRawDataL(valBuf); |
|
91 } break; |
|
92 case THTTPHdrVal::KStrFVal: |
|
93 { |
|
94 // Folded string - encoce as is. |
|
95 aHeader.WriteRawDataL(value.StrF().DesC()); |
|
96 } break; |
|
97 case THTTPHdrVal::KStrVal: |
|
98 { |
|
99 // String - encoce as is. |
|
100 aHeader.WriteRawDataL(value.Str().DesC()); |
|
101 } break; |
|
102 case THTTPHdrVal::KDateVal: |
|
103 { |
|
104 // Date value - convert to form given in RFC 822 |
|
105 const TInt KNumCharsInRfc1123Date = 40; |
|
106 TBuf<KNumCharsInRfc1123Date> dateTimeStringRfc1123; |
|
107 TTime t(value.DateTime()); |
|
108 t.FormatL(dateTimeStringRfc1123,KDateFormatRfc1123); |
|
109 |
|
110 // Convert from unicode |
|
111 TBuf8<KNumCharsInRfc1123Date> dateTimeString8; |
|
112 dateTimeString8.Copy(dateTimeStringRfc1123); |
|
113 aHeader.WriteRawDataL(dateTimeString8); |
|
114 } |
|
115 break; |
|
116 default: |
|
117 // Nothing to write if no type is set. The value will be left empty. |
|
118 break; |
|
119 } |
|
120 aHeader.CommitRawData(); |
|
121 } |