summaryrefslogtreecommitdiffstats
path: root/pki/base/tps/src/httpClient/http.cpp
blob: 60ca48bf5f0a8898d027b6ebde2568cf98525c89 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/** 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 <string.h>

#include "httpClient/httpc/http.h"
#include "httpClient/httpc/engine.h"
#include "httpClient/httpc/request.h"
#include "httpClient/httpc/response.h"
//-- #include "httpClient/httpc/DebugLogger.h"
//-- #include "httpClient/httpc/ErrorLogger.h"
#include "httpClient/httpc/PSPRUtil.h"
#include "httpClient/httpc/Defines.h"
#include "engine/RA.h"
#include "main/Memory.h"


//-- static const char *DEBUG_MODULE = "httpclient";
//-- static const char *DEBUG_CLASS_NAME = "PSHttpServer";

/**
 * Constructor
 * @param addr The hostname:port of the server to connect to. The default
 * port is 80
 * @param af The protocol family like PR_AF_INET
 */
PSHttpServer::PSHttpServer(const char *addr, PRUint16 af) {
    SSLOn = PR_FALSE;
    PRUint16  port = 80;
//--	static const char *DEBUG_METHOD_NAME = "Constructor";
//-- 	DebugLogger *logger = DebugLogger::GetDebugLogger( DEBUG_MODULE );

    char *pPort;


    _addr = NULL;
//  if( _addr != NULL ) {
//      PL_strfree( _addr );
//      _addr = NULL;
//  }

    if (addr) {
        _addr = PL_strdup(addr); 
    }

    pPort = PL_strchr(_addr, ':');
    if (pPort) {
        *pPort = '\0';
        port = (PRUint16)  atoi(++pPort);
    }

    /* kludge for doing IPv6 tests on localhost */
    if (!PL_strcmp(_addr, "ip6-localhost") && (af == PR_AF_INET6)) {
         PL_strcpy(_addr, "::1");
    }

//    PR_InitializeNetAddr(PR_IpAddrNull, port, &_netAddr);

    if (PR_StringToNetAddr(_addr, &_netAddr) == PR_FAILURE) {
        char buf[2000];
        PRHostEnt ent;

        RA::Debug( LL_PER_PDU,
                                   "PSHttpServer::PSHttpServer ",
                                   " host %s port %d ",_addr,port );
        PR_InitializeNetAddr(PR_IpAddrNull, port, &_netAddr);
        if (PR_GetIPNodeByName(_addr, af, PR_AI_DEFAULT,
							   buf, sizeof(buf), &ent) == PR_SUCCESS) {
            PR_EnumerateHostEnt(0, &ent, port, &_netAddr);
        } else {
//-- 		    ErrorLogger::GetErrorLogger()->Log(
//--                 LOGLEVEL_SEVERE, PR_GetError(),
              RA::Debug( LL_PER_PDU,
                         "PSHttpServer::PSHttpServer: ",
                         "PR_GetIPNodeByName returned error %d [%s] for "
                         "address %s",
                         PR_GetError (),
                         "XXX",
                         addr );
//-- 		    logger->Log( LOGLEVEL_SEVERE, DEBUG_CLASS_NAME,
//-- 						 DEBUG_METHOD_NAME,
                    RA::Debug( LL_PER_PDU,
                               "PSHttpServer::PSHttpServer: ",
                               "PR_GetIPNodeByName returned error %d [%s] for "
                               "address %s",
                               PR_GetError(),
                               "XXX",
                               addr );
        }
    }
}

/**
 * Destructor of the Httpserver class 
 */
PSHttpServer::~PSHttpServer() {
    if( _addr != NULL ) {
        PL_strfree( _addr );
        _addr = NULL;
    }
}

/**
 * Turns SSL on or off for the connection
 * @param SSLstate PR_TRUE to make an SSL connection
 */
void PSHttpServer::setSSL(PRBool SSLstate) {
  SSLOn = SSLstate;
}

/**
 * Returns the current SSL state for this PSHttpServer object
 * @return PR_TRUE if SSL is enabled else PR_FALSE
 */
PRBool PSHttpServer::isSSL() const {
  return SSLOn;
}

/**
 * Returns the IP address of the HTTP server
 * @return IP address of the server as a long
 */

long PSHttpServer::getIp() const {
    return _netAddr.inet.ip;
}

/**
 * Returns the port for the HTTP server
 * @return port of the server
 */

long PSHttpServer::getPort() const {
    return (long)  PR_ntohs(_netAddr.inet.port);
}

/**
 * Returns the server IP address as a string
 * @return server address as string
*/
const char * PSHttpServer::getAddr() const {
    return _addr;
}

/**
 * Gets the server addr as a PR_NetAddr structure
 * @param addr PR_netaddr struct in which server address is returned
 */
void PSHttpServer::getAddr(PRNetAddr *addr) const {
    memcpy(addr, &_netAddr, sizeof(_netAddr));
}

/**
 * Fets the protocol as string: "HTTP/1.0" "HTTP/1.1" etc
 * @return Protocol string
 */
const char *HttpProtocolToString(HttpProtocol proto) {
    switch(proto) {
        case HTTP09:
            return "";
        case HTTP10:
            return "HTTP/1.0";
        case HTTP11:
            return "HTTP/1.1";
        case HTTPBOGUS:
            return "BOGO-PROTO";
        case HTTPNA:
			return NULL;
    }

    return NULL;
}

/**
* Constructor for HttpMessage. This is a base class for PSHttpRequest
*/
HttpMessage :: HttpMessage(long len, const char* buf) {
    firstline = NULL;
    cl = 0;
    proto = HTTPNA;

    // search for the first line
    int counter=0;
    PRBool found = PR_FALSE;
    while ( ( (counter++<len) && (PR_FALSE == found) ) ) {
        if (buf[counter] != '\n') {
            continue;
		}
        found = PR_TRUE;
    }

    // extract the first line
    if (PR_TRUE == found) {
        firstline=new char[counter+1];
        memcpy(firstline, buf, counter);
        firstline[counter] = '\0';
    }
}

HttpMessage :: ~HttpMessage() {
    if( firstline != NULL ) {
        delete firstline;
        firstline = NULL;
    }
}

/*SecurityProtocols :: SecurityProtocols(PRBool s2, PRBool s3, PRBool t)
{
    ssl2 = s2;
    ssl3 = s3;
    tls = t;
};

const SecurityProtocols& SecurityProtocols :: operator = (const RWTPtrSlist<char>& protlist)
{
    ssl2 = PR_FALSE;
    ssl3 = PR_FALSE;
    tls = PR_FALSE;
    PRInt32 i;
    for (i = 0;i<protlist.entries();i++)
    {
        if (0 == strcmp(protlist.at(i), "SSL2"))
        {
            ssl2 = PR_TRUE;
        };
        if (0 == strcmp(protlist.at(i), "SSL3"))
        {
            ssl3 = PR_TRUE;
        };
        if (0 == strcmp(protlist.at(i), "TLS"))
        {
            tls = PR_TRUE;
        };
    };
    return *this;
};

const SecurityProtocols& SecurityProtocols :: operator = (const SecurityProtocols& rhs)
{
    ssl2 = rhs.ssl2;
    ssl3 = rhs.ssl3;
    tls = rhs.tls;
    return *this;
};
*/


PRBool PSHttpServer::putFile(const char* localFile,
							 const char* remoteUri) const {
    PSHttpRequest request(this, remoteUri, HTTP10, Engine::globaltimeout);
    request.setMethod("PUT");
    request.useLocalFileAsBody(localFile);

    PRBool rv = _putFile(request);
    return rv;
}

PRBool PSHttpServer::putFile(const char *uri, int size) const {
    PSHttpRequest request(this, uri, HTTP10, Engine::globaltimeout);
    request.setMethod("PUT");
    request.addRandomBody(size);

    PRBool rv = _putFile(request);;
    return rv;
}

PRBool PSHttpServer::_putFile(PSHttpRequest& request) const {
    HttpEngine engine;
    PRBool rv = PR_TRUE;

    PSHttpResponse* response = engine.makeRequest(request, *this);

    if (response) {
        int status = response->getStatus();
        if (status == 200 || status == 201 || status == 204) {
            rv = PR_TRUE;
        } else {
            rv = PR_FALSE;
        }
        if( response != NULL ) {
            delete response;
            response = NULL;
        }
    } else {
        rv = PR_FALSE;
	}
    return rv;
}