1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
// --- BEGIN COPYRIGHT BLOCK ---
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301 USA
//
// Copyright (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cms/HttpConnection.h"
#include "main/Memory.h"
#include "main/NameValueSet.h"
#include "engine/RA.h"
#ifdef XP_WIN32
#define TPS_PUBLIC __declspec(dllexport)
#else /* !XP_WIN32 */
#define TPS_PUBLIC
#endif /* !XP_WIN32 */
/**
* Constructs a base class for HttpConnection
*/
TPS_PUBLIC HttpConnection::HttpConnection(const char *id, ConnectionInfo *cinfo, int retries, int timeout,
bool isSSL, const char *nickname, bool keepAlive, NameValueSet *headers)
{
m_failoverList = cinfo;
m_retries = retries;
m_timeout = timeout;
m_Id = PL_strdup(id);
m_isSSL = isSSL;
m_clientnickname = PL_strdup(nickname);
m_keepAlive = keepAlive;
m_headers = headers;
m_curr = 0;
m_lock = PR_NewLock();
}
/**
* Destructs processor.
*/
TPS_PUBLIC HttpConnection::~HttpConnection ()
{
if( m_clientnickname != NULL ) {
PL_strfree( m_clientnickname );
m_clientnickname = NULL;
}
if( m_Id != NULL ) {
PL_strfree( m_Id );
m_Id = NULL;
}
if( m_failoverList != NULL ) {
delete m_failoverList;
m_failoverList = NULL;
}
if( m_headers != NULL ) {
delete m_headers;
m_headers = NULL;
}
if( m_lock != NULL ) {
PR_DestroyLock( m_lock );
m_lock = NULL;
}
}
TPS_PUBLIC int HttpConnection::GetNumOfRetries() {
return m_retries;
}
int HttpConnection::GetTimeout() {
return m_timeout;
}
TPS_PUBLIC ConnectionInfo *HttpConnection::GetFailoverList() {
return m_failoverList;
}
TPS_PUBLIC char *HttpConnection::GetId() {
return m_Id;
}
TPS_PUBLIC bool HttpConnection::IsSSL() {
return m_isSSL;
}
TPS_PUBLIC char * HttpConnection::GetClientNickname() {
return m_clientnickname;
}
TPS_PUBLIC bool HttpConnection::IsKeepAlive() {
return m_keepAlive;
}
TPS_PUBLIC PSHttpResponse *HttpConnection::getResponse(int index, const char *servlet, const char *body) {
char *host_port;
char uri[800];
char *nickname;
const char *httpprotocol;
ConnectionInfo *failoverList = GetFailoverList();
int len = failoverList->ConnectionInfo::GetHostPortListLen();
if (index >= len) {
index = len - 1; // use the last one
}
host_port= (failoverList->GetHostPortList())[index];
if (IsSSL()) {
httpprotocol = "https";
} else {
httpprotocol = "http";
}
PR_snprintf((char *)uri, 800,
"%s://%s/%s",
httpprotocol, host_port, servlet);
RA::Debug("HttpConnection::getResponse", "Send request to host %s servlet %s", host_port, servlet);
RA::Debug(LL_PER_PDU, "HttpConnection::getResponse", "uri=%s", uri);
RA::Debug(LL_PER_PDU, "HttpConnection::getResponse", "host_port=%s", host_port);
char *pPort = NULL;
char *pPortActual = NULL;
char hostName[512];
/*
* Isolate the host name, account for IPV6 numeric addresses.
*
*/
if(host_port)
strncpy(hostName,host_port,512);
pPort = hostName;
while(1) {
pPort = strchr(pPort, ':');
if (pPort) {
pPortActual = pPort;
pPort++;
} else
break;
}
if(pPortActual)
*pPortActual = '\0';
/*
* Rifle through the values for the host
*/
PRAddrInfo *ai;
void *iter;
PRNetAddr addr;
int family = PR_AF_INET;
ai = PR_GetAddrInfoByName(hostName, PR_AF_UNSPEC, PR_AI_ADDRCONFIG);
if (ai) {
printf("%s\n", PR_GetCanonNameFromAddrInfo(ai));
iter = NULL;
while ((iter = PR_EnumerateAddrInfo(iter, ai, 0, &addr)) != NULL) {
char buf[512];
PR_NetAddrToString(&addr, buf, sizeof buf);
RA::Debug( LL_PER_PDU,
"HttpConnection::getResponse: ",
"Sending addr -- Msg='%s'\n",
buf );
family = PR_NetAddrFamily(&addr);
RA::Debug( LL_PER_PDU,
"HttpConnection::getResponse: ",
"Sending family -- Msg='%d'\n",
family );
break;
}
PR_FreeAddrInfo(ai);
}
PSHttpServer httpserver(host_port, family);
nickname = GetClientNickname();
if (IsSSL())
httpserver.setSSL(PR_TRUE);
else
httpserver.setSSL(PR_FALSE);
PSHttpRequest httprequest(&httpserver, uri, HTTP11, 0);
if (IsSSL()) {
httprequest.setSSL(PR_TRUE);
if (nickname != NULL) {
httprequest.setCertNickName(nickname);
} else {
return NULL;
}
} else
httprequest.setSSL(PR_FALSE);
httprequest.setMethod("POST");
if (body != NULL) {
httprequest.setBody( strlen(body), body);
}
httprequest.addHeader( "Content-Type", "application/x-www-form-urlencoded" );
if (m_headers != NULL) {
for (int i=0; i<m_headers->Size(); i++) {
char *name = m_headers->GetNameAt(i);
httprequest.addHeader(name, m_headers->GetValue(name));
}
}
if (IsKeepAlive())
httprequest.addHeader( "Connection", "keep-alive" );
HttpEngine httpEngine;
return httpEngine.makeRequest(httprequest, httpserver, (PRIntervalTime)GetTimeout(),
PR_FALSE /*expectChunked*/);
}
TPS_PUBLIC PRLock * HttpConnection::GetLock() {
return m_lock;
}
TPS_PUBLIC int HttpConnection::GetCurrentIndex() {
return m_curr;
}
TPS_PUBLIC void HttpConnection::SetCurrentIndex(int index) {
m_curr = index;
}
|