Newer
Older
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: BinFileInputStream.cpp 553903 2007-07-06 14:43:42Z amassari $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <xercesc/util/NetAccessors/BinHTTPInputStreamCommon.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/XMLExceptMsgs.hpp>
#include <xercesc/util/Janitor.hpp>
#include <xercesc/util/TransService.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/Base64.hpp>
XERCES_CPP_NAMESPACE_BEGIN
BinHTTPInputStreamCommon::BinHTTPInputStreamCommon(MemoryManager *manager)
: fBytesProcessed(0)
, fBuffer(1023, manager)
Alberto Massari
committed
, fBufferPos(0)
, fContentType(0)
Alberto Massari
committed
, fEncoding(0)
, fMemoryManager(manager)
{
}
BinHTTPInputStreamCommon::~BinHTTPInputStreamCommon()
{
if(fContentType) fMemoryManager->deallocate(fContentType);
Alberto Massari
committed
if(fEncoding) fMemoryManager->deallocate(fEncoding);
}
static const char *CRLF = "\r\n";
void BinHTTPInputStreamCommon::createHTTPRequest(const XMLURL &urlSource, const XMLNetHTTPInfo *httpInfo, CharBuffer &buffer)
{
static const char *GET = "GET ";
static const char *PUT = "PUT ";
static const char *POST = "POST ";
static const char *HTTP10 = " HTTP/1.0\r\n";
static const char *HOST = "Host: ";
static const char *AUTHORIZATION = "Authorization: Basic ";
static const char *COLON = ":";
XMLTransService::Codes failReason;
const XMLSize_t blockSize = 2048;
XMLTranscoder* trans = XMLPlatformUtils::fgTransService->makeNewTranscoderFor("ISO8859-1", failReason, blockSize, fMemoryManager);
Janitor<XMLTranscoder> janTrans(trans);
Boris Kolpackov
committed
TranscodeToStr hostName(urlSource.getHost(), trans, fMemoryManager);
TranscodeToStr path(urlSource.getPath(), trans, fMemoryManager);
TranscodeToStr fragment(urlSource.getFragment(), trans, fMemoryManager);
TranscodeToStr query(urlSource.getQuery(), trans, fMemoryManager);
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
// Build up the http GET command to send to the server.
// To do: We should really support http 1.1. This implementation
// is weak.
if(httpInfo) {
switch(httpInfo->fHTTPMethod) {
case XMLNetHTTPInfo::GET: buffer.append(GET); break;
case XMLNetHTTPInfo::PUT: buffer.append(PUT); break;
case XMLNetHTTPInfo::POST: buffer.append(POST); break;
}
}
else {
buffer.append(GET);
}
if(path.str() != 0) {
buffer.append((char*)path.str());
}
else {
buffer.append("/");
}
if(query.str() != 0) {
buffer.append("?");
buffer.append((char*)query.str());
}
if(fragment.str() != 0) {
buffer.append((char*)fragment.str());
}
buffer.append(HTTP10);
buffer.append(HOST);
buffer.append((char*)hostName.str());
if(urlSource.getPortNum() != 80)
{
buffer.append(COLON);
buffer.appendDecimalNumber(urlSource.getPortNum());
}
buffer.append(CRLF);
const XMLCh *username = urlSource.getUser();
const XMLCh *password = urlSource.getPassword();
if(username && password) {
XMLBuffer userPassBuf(256, fMemoryManager);
userPassBuf.append(username);
userPassBuf.append(chColon);
userPassBuf.append(password);
Boris Kolpackov
committed
TranscodeToStr userPass(userPassBuf.getRawBuffer(), trans, fMemoryManager);
XMLSize_t len;
Boris Kolpackov
committed
XMLByte* encodedData = Base64::encode(userPass.str(), userPass.length(), &len, fMemoryManager);
ArrayJanitor<XMLByte> janBuf2(encodedData, fMemoryManager);
if(encodedData) {
// HTTP doesn't want the 0x0A separating the data in chunks of 76 chars per line
XMLByte* authData = (XMLByte*)fMemoryManager->allocate((len+1)*sizeof(XMLByte));
ArrayJanitor<XMLByte> janBuf(authData, fMemoryManager);
XMLByte *cursor = authData;
for(XMLSize_t i = 0; i < len; ++i)
if(encodedData[i] != chLF)
*cursor++ = encodedData[i];
*cursor++ = 0;
buffer.append(AUTHORIZATION);
buffer.append((char*)authData);
buffer.append(CRLF);
}
}
if(httpInfo && httpInfo->fHeaders)
buffer.append(httpInfo->fHeaders, httpInfo->fHeadersLen);
buffer.append(CRLF);
}
XMLCh *BinHTTPInputStreamCommon::findHeader(const char *name)
{
char *p = strstr(fBuffer.getRawBuffer(), name);
while(p != 0) {
if(*(p - 1) == '\n' &&
*(p + len) == ':' &&
*(p + len + 1) == ' ') {
p += len + 2;
char *endP = strstr(p, CRLF);
if(endP == 0) {
for(endP = p; *endP != 0; ++endP) ;
}
Boris Kolpackov
committed
// Transcode from iso-8859-1
TranscodeFromStr value((XMLByte*)p, endP - p, "ISO8859-1", fMemoryManager);
return value.adopt();
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
}
p = strstr(p + 1, name);
}
return 0;
}
int BinHTTPInputStreamCommon::sendRequest(const XMLURL &url, const XMLNetHTTPInfo *httpInfo)
{
//
// Constants in ASCII to send/check in the HTTP request/response
//
static const char *CRLF2X = "\r\n\r\n";
static const char *LF2X = "\n\n";
// The port is open and ready to go.
// Build up the http GET command to send to the server.
CharBuffer requestBuffer(1023, fMemoryManager);
createHTTPRequest(url, httpInfo, requestBuffer);
// Send the http request
if(!send(requestBuffer.getRawBuffer(), requestBuffer.getLen())) {
ThrowXMLwithMemMgr1(NetAccessorException,
XMLExcepts::NetAcc_WriteSocket, url.getURLText(), fMemoryManager);
}
if(httpInfo && httpInfo->fPayload) {
if(!send(httpInfo->fPayload, httpInfo->fPayloadLen)) {
ThrowXMLwithMemMgr1(NetAccessorException,
XMLExcepts::NetAcc_WriteSocket, url.getURLText(), fMemoryManager);
}
}
//
// get the response, check the http header for errors from the server.
//
char tmpBuf[1024];
int ret;
fBuffer.reset();
while(true) {
ret = receive(tmpBuf, sizeof(tmpBuf));
if(ret == -1) {
ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, url.getURLText(), fMemoryManager);
}
// connection closed
if(ret == 0)
break;
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
fBuffer.append(tmpBuf, ret);
fBufferPos = strstr(fBuffer.getRawBuffer(), CRLF2X);
if(fBufferPos != 0) {
fBufferPos += 4;
*(fBufferPos - 2) = 0;
break;
}
fBufferPos = strstr(fBuffer.getRawBuffer(), LF2X);
if(fBufferPos != 0) {
fBufferPos += 2;
*(fBufferPos - 1) = 0;
break;
}
}
// Parse the response status
char *p = strstr(fBuffer.getRawBuffer(), "HTTP");
if(p == 0) {
ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, url.getURLText(), fMemoryManager);
}
p = strchr(p, chSpace);
if(p == 0) {
ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, url.getURLText(), fMemoryManager);
}
return atoi(p);
}
const XMLCh *BinHTTPInputStreamCommon::getContentType() const
{
if(fContentType == 0) {
// mutable
const_cast<BinHTTPInputStreamCommon*>(this)->fContentType =
const_cast<BinHTTPInputStreamCommon*>(this)->findHeader("Content-Type");
}
return fContentType;
}
Alberto Massari
committed
const XMLCh *BinHTTPInputStreamCommon::getEncoding() const
{
if(fEncoding == 0) {
const XMLCh* contentTypeHeader = getContentType();
if(contentTypeHeader)
{
const XMLCh szCharsetEquals[] = {chLatin_c, chLatin_h, chLatin_a, chLatin_r, chLatin_s, chLatin_e, chLatin_t, chEqual, chNull };
BaseRefVectorOf<XMLCh>* tokens=XMLString::tokenizeString(contentTypeHeader, chSemiColon, fMemoryManager);
for(XMLSize_t i=0;i<tokens->size();i++)
{
XMLString::removeWS(tokens->elementAt(i), fMemoryManager);
if(XMLString::startsWithI(tokens->elementAt(i), szCharsetEquals))
{
// mutable
const XMLCh* encodingName=tokens->elementAt(i)+XMLString::stringLen(szCharsetEquals);
const_cast<BinHTTPInputStreamCommon*>(this)->fEncoding = XMLString::replicate(encodingName, fMemoryManager);
break;
}
}
// if the charset=value entry was not present, check if we should use a default value
Alberto Massari
committed
if(fEncoding==0 && tokens->size()>0)
{
const XMLCh szTextSlash[] = { chLatin_t, chLatin_e, chLatin_x, chLatin_t, chForwardSlash, chNull };
const XMLCh szXml[] = {chLatin_x, chLatin_m, chLatin_l, chNull };
const XMLCh szXmlDash[] = {chLatin_x, chLatin_m, chLatin_l, chDash, chNull };
XMLBuffer contentType(XMLString::stringLen(contentTypeHeader), fMemoryManager);
contentType.set(tokens->elementAt(0));
XMLCh* strType = contentType.getRawBuffer();
XMLString::removeWS(strType, fMemoryManager);
if(XMLString::startsWithI(strType, szTextSlash))
{
// text/* has a default encoding of iso-8859-1
// text/xml, text/xml-external-parsed-entity, or a subtype like text/AnythingAtAll+xml
// has a default encoding of us-ascii
XMLCh* subType = strType+XMLString::stringLen(szTextSlash);
BaseRefVectorOf<XMLCh>* tokens=XMLString::tokenizeString(subType, chPlus, fMemoryManager);
for(XMLSize_t i=0;i<tokens->size();i++)
Alberto Massari
committed
{
XMLCh* part=tokens->elementAt(i);
if(XMLString::compareIStringASCII(part, szXml)==0 || XMLString::startsWithI(part, szXmlDash))
Alberto Massari
committed
{
const_cast<BinHTTPInputStreamCommon*>(this)->fEncoding = XMLString::replicate(XMLUni::fgUSASCIIEncodingString, fMemoryManager);
break;
}
Alberto Massari
committed
if(fEncoding==0)
const_cast<BinHTTPInputStreamCommon*>(this)->fEncoding = XMLString::replicate(XMLUni::fgISO88591EncodingString, fMemoryManager);
Alberto Massari
committed
}
}
delete tokens;
}
}
return fEncoding;
}
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
XMLSize_t BinHTTPInputStreamCommon::readBytes(XMLByte* const toFill,
const XMLSize_t maxToRead)
{
XMLSize_t len = fBuffer.getRawBuffer() + fBuffer.getLen() - fBufferPos;
if(len > 0)
{
// If there's any data left over in the buffer into which we first
// read from the server (to get the http header), return that.
if (len > maxToRead)
len = maxToRead;
memcpy(toFill, fBufferPos, len);
fBufferPos += len;
}
else
{
// There was no data in the local buffer.
// Read some from the socket, straight into our caller's buffer.
//
int cbRead = receive((char *)toFill, maxToRead);
if (cbRead == -1)
{
ThrowXMLwithMemMgr(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, fMemoryManager);
}
len = cbRead;
}
fBytesProcessed += len;
return len;
}
XERCES_CPP_NAMESPACE_END