|
1 /* |
|
2 * Name : HttpClient.cpp |
|
3 * Description : HTTP helper class |
|
4 * Project : This file is part of OpenMAR, an Open Mobile Augmented Reality browser |
|
5 * Website : http://OpenMAR.org |
|
6 * |
|
7 * Copyright (c) 2010 David Caabeiro |
|
8 * |
|
9 * All rights reserved. This program and the accompanying materials are made available |
|
10 * under the terms of the Eclipse Public License v1.0 which accompanies this |
|
11 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html |
|
12 * |
|
13 */ |
|
14 |
|
15 #include "HttpClient.h" |
|
16 |
|
17 #include <Http.h> |
|
18 #include <http/RHttpHeaders.h> |
|
19 |
|
20 #include <cmmanager.h> |
|
21 #include <cmdestination.h> |
|
22 #include <CommDbConnPref.h> |
|
23 |
|
24 #include <es_enum.h> |
|
25 |
|
26 #include "Logger.h" |
|
27 |
|
28 CHttpClient* CHttpClient::NewL(MHTTPTransactionCallback& aCallback) |
|
29 { |
|
30 CHttpClient* self = new(ELeave) CHttpClient(aCallback); |
|
31 CleanupStack::PushL(self); |
|
32 self->ConstructL(); |
|
33 CleanupStack::Pop(self); |
|
34 |
|
35 return self; |
|
36 } |
|
37 |
|
38 CHttpClient::~CHttpClient() |
|
39 { |
|
40 iSession.Close(); |
|
41 iConnection.Close(); |
|
42 iSocketServ.Close(); |
|
43 } |
|
44 |
|
45 CHttpClient::CHttpClient(MHTTPTransactionCallback& aCallback) |
|
46 : iCallback(aCallback) |
|
47 { |
|
48 } |
|
49 |
|
50 void CHttpClient::ConstructL() |
|
51 { |
|
52 User::LeaveIfError(iSocketServ.Connect()); |
|
53 |
|
54 User::LeaveIfError(iConnection.Open(iSocketServ)); |
|
55 |
|
56 TUint connectionCount = 0; |
|
57 TInt error = iConnection.EnumerateConnections(connectionCount); |
|
58 |
|
59 // if (error || connectionCount < 1) |
|
60 |
|
61 TPckgBuf<TConnectionInfo> connectionInfo; |
|
62 error = iConnection.GetConnectionInfo(1, connectionInfo); // First connection |
|
63 |
|
64 // if (error) |
|
65 // LOGARG("Error %d retrieving connection info", error); |
|
66 |
|
67 error = iConnection.Attach(connectionInfo, RConnection::EAttachTypeNormal); |
|
68 |
|
69 // LOGARG("Connection attached %d", error); |
|
70 |
|
71 // LOGTXT("Opening http session"); |
|
72 |
|
73 iSession.OpenL(); |
|
74 |
|
75 RStringPool strPool = iSession.StringPool(); |
|
76 RHTTPConnectionInfo connInfo = iSession.ConnectionInfo(); |
|
77 connInfo.SetPropertyL(strPool.StringF(HTTP::EHttpSocketServ, |
|
78 RHTTPSession::GetTable()), THTTPHdrVal(iSocketServ.Handle())); |
|
79 TInt connPtr = reinterpret_cast<TInt>(&iConnection); |
|
80 connInfo.SetPropertyL(strPool.StringF(HTTP::EHttpSocketConnection, |
|
81 RHTTPSession::GetTable()), THTTPHdrVal(connPtr)); |
|
82 } |
|
83 |
|
84 RHTTPTransaction CHttpClient::GetL(const TDesC8& aUri) |
|
85 { |
|
86 TUriParser8 uri; |
|
87 uri.Parse(aUri); |
|
88 |
|
89 LOGARG("URI submitted is %S", &aUri); |
|
90 |
|
91 RStringPool strP = iSession.StringPool(); |
|
92 RStringF method = strP.StringF(HTTP::EGET, RHTTPSession::GetTable()); |
|
93 |
|
94 RHTTPTransaction transaction = iSession.OpenTransactionL(uri, iCallback, method); |
|
95 |
|
96 _LIT8(KUserAgent, "OpenMAR/1.0 (Symbian)"); |
|
97 _LIT8(KAccept, "*/*"); |
|
98 |
|
99 RHTTPHeaders hdr = transaction.Request().GetHeaderCollection(); |
|
100 // Add headers appropriate to all methods |
|
101 SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent); |
|
102 SetHeaderL(hdr, HTTP::EAccept, KAccept); |
|
103 |
|
104 transaction.SubmitL(); |
|
105 |
|
106 return transaction; |
|
107 } |
|
108 |
|
109 void CHttpClient::Stop(RHTTPTransaction& aTransaction) |
|
110 { |
|
111 aTransaction.Cancel(); |
|
112 } |
|
113 |
|
114 void CHttpClient::SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue) |
|
115 { |
|
116 RStringF valStr = iSession.StringPool().OpenFStringL(aHdrValue); |
|
117 THTTPHdrVal val(valStr); |
|
118 aHeaders.SetFieldL(iSession.StringPool().StringF(aHdrField, RHTTPSession::GetTable()), val); |
|
119 valStr.Close(); |
|
120 } |