|
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 "chttpservice.h" |
|
17 #include "httpclientutils.h" |
|
18 |
|
19 const TInt KMaxNoOfConnections = 6; |
|
20 const TInt KMaxTransToPipeline = 5; |
|
21 |
|
22 /** |
|
23 * The default 2-phase constructor to create a CHttpService instance |
|
24 * |
|
25 */ |
|
26 EXPORT_C CHttpService* CHttpService::NewL() |
|
27 { |
|
28 CHttpService* self = new(ELeave) CHttpService; |
|
29 CleanupStack::PushL(self); |
|
30 self->ConstructL(); |
|
31 CleanupStack::Pop(); // self |
|
32 return self; |
|
33 } |
|
34 /** |
|
35 * Destructor |
|
36 */ |
|
37 |
|
38 EXPORT_C CHttpService::~CHttpService() |
|
39 { |
|
40 if(iHttpServiceStruct) |
|
41 { |
|
42 iHttpServiceStruct->iHttpSession.Close(); |
|
43 } |
|
44 delete iHttpServiceStruct; |
|
45 } |
|
46 |
|
47 /** |
|
48 * Retrieves the equivalent string for a give string ID from the |
|
49 * HTTP string pool |
|
50 * |
|
51 * @param @aStringId String ID |
|
52 * @param aPtr String |
|
53 * |
|
54 * @return KErrNotFound if the string ID is not found in the HTTP string pool |
|
55 * otherwise KErrNone for success |
|
56 */ |
|
57 EXPORT_C TInt CHttpService::String(TInt aStringId, TPtrC8& aPtr) |
|
58 { |
|
59 RStringF str = iHttpServiceStruct->iHttpSession.StringPool().StringF(aStringId, RHTTPSession::GetTable()); |
|
60 aPtr.Set(str.DesC()); |
|
61 return KErrNone; |
|
62 } |
|
63 /** |
|
64 * Set the proxy that applies to all HTTP client transactions that "this" |
|
65 * CHttpService instance creates |
|
66 * |
|
67 * @param aProxyAddress Proxy address. [for ex; http://myproxy.org:8081] |
|
68 * @return KErrNoMemory if the proxy information cannot be set other KErrNone for success. |
|
69 * |
|
70 */ |
|
71 |
|
72 EXPORT_C TInt CHttpService::SetProxy(const TDesC8& aProxyAddress) |
|
73 { |
|
74 RHTTPSession sess = iHttpServiceStruct->iHttpSession; |
|
75 RStringPool sp = sess.StringPool(); |
|
76 RHTTPConnectionInfo connInfo = sess.ConnectionInfo(); |
|
77 RStringF property = sp.StringF(HTTP::EProxyUsage, RHTTPSession::GetTable()); |
|
78 RStringF useProxy = sp.StringF(HTTP::EUseProxy, RHTTPSession::GetTable()); |
|
79 TInt err = connInfo.SetProperty(property, useProxy); |
|
80 if(err == KErrNone) |
|
81 { |
|
82 RStringF str; |
|
83 err = HttpClientUtils::OpenStringF(aProxyAddress, sp, str); |
|
84 if(err == KErrNone) |
|
85 { |
|
86 err = connInfo.SetProperty(sp.StringF(HTTP::EProxyAddress,RHTTPSession::GetTable()), str); |
|
87 str.Close(); |
|
88 } |
|
89 } |
|
90 return err; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Returns the proxy address that is set |
|
95 * |
|
96 * @return Proxy address otherwise KNullDesC8 if the proxy information |
|
97 * is not set. |
|
98 * |
|
99 */ |
|
100 |
|
101 EXPORT_C const TDesC8& CHttpService::ProxyAddress() const |
|
102 { |
|
103 RHTTPSession sess = iHttpServiceStruct->iHttpSession; |
|
104 RStringPool sp = sess.StringPool(); |
|
105 RHTTPConnectionInfo connInfo = sess.ConnectionInfo(); |
|
106 THTTPHdrVal useProxy; |
|
107 if(connInfo.Property(sp.StringF(HTTP::EProxyUsage, RHTTPSession::GetTable()), useProxy)) |
|
108 { |
|
109 THTTPHdrVal proxyAddress; |
|
110 if(connInfo.Property(sp.StringF(HTTP::EProxyAddress, RHTTPSession::GetTable()), proxyAddress)) |
|
111 { |
|
112 return proxyAddress.StrF().DesC(); |
|
113 } |
|
114 } |
|
115 return KNullDesC8(); |
|
116 } |
|
117 |
|
118 /** |
|
119 * Set the maxuimum number of TCP connections[socket] CHttpService instance that can activated |
|
120 * at any time. The default no. of connections that the CHttpService instance uses is 6. |
|
121 * |
|
122 * @param aValue aValue No. of connections |
|
123 */ |
|
124 |
|
125 EXPORT_C void CHttpService::SetMaxConnections(TInt aValue) |
|
126 { |
|
127 RHTTPSession sess = iHttpServiceStruct->iHttpSession; |
|
128 RStringPool sp = sess.StringPool(); |
|
129 RHTTPConnectionInfo connInfo = sess.ConnectionInfo(); |
|
130 |
|
131 connInfo.SetProperty(sp.StringF(HTTP::EMaxNumTransportHandlers, RHTTPSession::GetTable()), THTTPHdrVal(aValue)); |
|
132 } |
|
133 |
|
134 /** |
|
135 * Returns the maximum no. of TCP connections that is set. |
|
136 * |
|
137 */ |
|
138 |
|
139 EXPORT_C TInt CHttpService::MaxConnections() const |
|
140 { |
|
141 RHTTPSession sess = iHttpServiceStruct->iHttpSession; |
|
142 RStringPool sp = sess.StringPool(); |
|
143 RHTTPConnectionInfo connInfo = sess.ConnectionInfo(); |
|
144 THTTPHdrVal val; |
|
145 if(connInfo.Property(sp.StringF(HTTP::EMaxNumTransportHandlers, RHTTPSession::GetTable()), val)) |
|
146 { |
|
147 if(val.Type() == THTTPHdrVal::KTIntVal) |
|
148 return val.Int(); |
|
149 } |
|
150 return 0; |
|
151 } |
|
152 |
|
153 |
|
154 EXPORT_C void CHttpService::SetMaxTransactionsToPipeline(TInt aValue) |
|
155 { |
|
156 RHTTPSession sess = iHttpServiceStruct->iHttpSession; |
|
157 RStringPool sp = sess.StringPool(); |
|
158 RHTTPConnectionInfo connInfo = sess.ConnectionInfo(); |
|
159 |
|
160 connInfo.SetProperty(sp.StringF(HTTP::EMaxNumTransactionsToPipeline, RHTTPSession::GetTable()), THTTPHdrVal(aValue)); |
|
161 } |
|
162 |
|
163 EXPORT_C TInt CHttpService::MaxTransactionsToPipeline() const |
|
164 { |
|
165 RHTTPSession sess = iHttpServiceStruct->iHttpSession; |
|
166 RStringPool sp = sess.StringPool(); |
|
167 RHTTPConnectionInfo connInfo = sess.ConnectionInfo(); |
|
168 THTTPHdrVal val; |
|
169 if(connInfo.Property(sp.StringF(HTTP::EMaxNumTransactionsToPipeline, RHTTPSession::GetTable()), val)) |
|
170 { |
|
171 if(val.Type() == THTTPHdrVal::KTIntVal) |
|
172 return val.Int(); |
|
173 } |
|
174 return 0; |
|
175 } |
|
176 |
|
177 /** |
|
178 * This method facilitates to set the header once in the CHttpService instance that is applicable |
|
179 * for all HTTP client transactions. [For ex; User-Agent header] |
|
180 * |
|
181 * @param aStringId - Pre-defined String ID in the HTTP string pool |
|
182 * @param aHeaderValue - Value for the header |
|
183 */ |
|
184 EXPORT_C TInt CHttpService::AddRequestHeader(TInt aStringId, const THttpHeaderValueVariant& aHeaderValue) |
|
185 { |
|
186 RHTTPHeaders sessHeaders = iHttpServiceStruct->iSessionHeaders; |
|
187 RStringPool sp = iHttpServiceStruct->iHttpSession.StringPool(); |
|
188 THTTPHdrVal val = HttpClientUtils::CopyHttpHdrVal(aHeaderValue, sp); |
|
189 TInt err = sessHeaders.SetField(sp.StringF(aStringId, RHTTPSession::GetTable()), val); |
|
190 HttpClientUtils::CloseString(val); |
|
191 return err; |
|
192 } |
|
193 |
|
194 /** |
|
195 * This method facilitates to set the custom HTTP header once in the CHttpService instance that is |
|
196 * applicable for all HTTP client transactions. [For ex; User-Agent header] |
|
197 * |
|
198 * @param aHeaderName - Custom header name |
|
199 * @param aHeaderValue - Value for the header |
|
200 */ |
|
201 EXPORT_C TInt CHttpService::AddCustomRequestHeader(const TDesC8& aHeaderName, const TDesC8& aHeaderValue) |
|
202 { |
|
203 _LIT8(KFieldSeparator, "\n"); |
|
204 RStringPool sp = iHttpServiceStruct->iHttpSession.StringPool(); |
|
205 RStringF str; |
|
206 if(HttpClientUtils::OpenStringF(aHeaderName, sp, str) != KErrNone) |
|
207 { |
|
208 return KErrNoMemory; |
|
209 } |
|
210 TInt err = iHttpServiceStruct->iSessionHeaders.SetRawField(str, aHeaderValue, KFieldSeparator); |
|
211 str.Close(); |
|
212 return err; |
|
213 } |
|
214 |
|
215 /** |
|
216 * Constructor |
|
217 */ |
|
218 CHttpService::CHttpService() |
|
219 : iHttpServiceStruct(NULL) |
|
220 { |
|
221 } |
|
222 |
|
223 void CHttpService::ConstructL() |
|
224 { |
|
225 iHttpServiceStruct = new(ELeave) CHttpServiceStruct(); |
|
226 iHttpServiceStruct->iHttpSession.OpenL(); |
|
227 iHttpServiceStruct->iSessionHeaders = iHttpServiceStruct->iHttpSession.RequestSessionHeadersL(); |
|
228 SetMaxConnections(KMaxNoOfConnections); |
|
229 SetMaxTransactionsToPipeline(KMaxTransToPipeline); |
|
230 } |
|
231 |