source: icXML/icXML-devel/src/xercesc/util/NetAccessors/BinHTTPInputStreamCommon.hpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 6.4 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: BinFileInputStream.hpp 553903 2007-07-06 14:43:42Z amassari $
20 */
21
22#if !defined(XERCESC_INCLUDE_GUARD_BINHTTPINPUTSTREAMCOMMON_HPP)
23#define XERCESC_INCLUDE_GUARD_BINHTTPINPUTSTREAMCOMMON_HPP
24
25#include <xercesc/util/XMLURL.hpp>
26#include <xercesc/util/BinInputStream.hpp>
27#include <xercesc/util/XMLNetAccessor.hpp>
28#include <xercesc/framework/MemoryManager.hpp>
29#include <string.h>
30
31XERCES_CPP_NAMESPACE_BEGIN
32
33//
34// This class implements a simple expanding character buffer
35//
36class XMLUTIL_EXPORT CharBuffer
37{
38public:
39    CharBuffer(XMLSize_t capacity = 1023,
40               MemoryManager *manager = XMLPlatformUtils::fgMemoryManager)
41        : fCapacity(capacity),
42          fIndex(0),
43          fMemoryManager(manager)
44    {
45        fBuffer = (char*)fMemoryManager->allocate((fCapacity + 1) * sizeof(char));
46    }
47
48    ~CharBuffer()
49    {
50        fMemoryManager->deallocate(fBuffer);
51    }
52
53    const char* getRawBuffer() const
54    {
55        fBuffer[fIndex] = 0;
56        return fBuffer;
57    }
58
59    char* getRawBuffer()
60    {
61        fBuffer[fIndex] = 0;
62        return fBuffer;
63    }
64
65    XMLSize_t getLen() const
66    {
67        return fIndex;
68    }
69
70    void reset()
71    {
72        fIndex = 0;
73    }
74
75    void append(const char *chars)
76    {
77        if(chars != 0 && *chars != 0) {
78            // get length of chars
79            XMLSize_t count = 0;
80            for(; *(chars+count); ++count) ;
81
82            if(fIndex + count >= fCapacity) {
83                ensureCapacity(count);
84            }
85            memcpy(&fBuffer[fIndex], chars, count * sizeof(char));
86            fIndex += count;
87        }
88    }
89
90    void append(const char *chars, XMLSize_t len)
91    {
92        if(chars != 0 && len != 0) {
93            if(fIndex + len >= fCapacity) {
94                ensureCapacity(len);
95            }
96            memcpy(&fBuffer[fIndex], chars, len * sizeof(char));
97            fIndex += len;
98        }
99    }
100
101    void appendDecimalNumber(unsigned int n)
102    {
103        if(n >= 10) {
104                appendDecimalNumber(n / 10);
105                n = n % 10;
106        }
107
108        if(fIndex + 1 >= fCapacity)
109            ensureCapacity(1);
110
111        fBuffer[fIndex] = '0' + n;
112        ++fIndex;
113    }
114
115    void set(const char *chars)
116    {
117        reset();
118        append(chars);
119    }
120
121private:
122    // -----------------------------------------------------------------------
123    //  Unimplemented constructors and operators
124    // -----------------------------------------------------------------------
125    CharBuffer(const CharBuffer &);
126    CharBuffer &operator=(const CharBuffer &);
127
128    void ensureCapacity(XMLSize_t extraNeeded)
129    {
130        // If we can't handle it, try doubling the buffer size.
131        XMLSize_t newCap = (fIndex + extraNeeded) * 2;
132
133        if(newCap > fCapacity)
134        {
135            // Allocate new buffer
136            char* newBuf = (char*)fMemoryManager->allocate((newCap + 1) * sizeof(char));
137
138            // Copy over the old stuff
139            memcpy(newBuf, fBuffer, fIndex * sizeof(char));
140
141            // Clean up old buffer and store new stuff
142            fMemoryManager->deallocate(fBuffer);
143            fBuffer = newBuf;
144            fCapacity = newCap;
145        }
146    }
147
148    // -----------------------------------------------------------------------
149    //  Private data members
150    // -----------------------------------------------------------------------
151    char *fBuffer;
152    XMLSize_t fCapacity;
153    XMLSize_t fIndex;
154    MemoryManager *fMemoryManager;
155};
156
157//
158// Common base implementation of HTTP BinInputStream that is used by some
159// platform-specific implementations.
160//
161class XMLUTIL_EXPORT BinHTTPInputStreamCommon : public BinInputStream
162{
163public :
164    virtual XMLFilePos curPos() const;
165    virtual XMLSize_t readBytes
166    (
167                XMLByte* const  toFill
168        , const XMLSize_t    maxToRead
169    );
170
171    virtual const XMLCh *getContentType() const;
172
173protected :
174    BinHTTPInputStreamCommon(MemoryManager *manager);
175    virtual ~BinHTTPInputStreamCommon();
176
177    /**
178     * \return The HTTP status code
179     */
180    int sendRequest(const XMLURL &url, const XMLNetHTTPInfo *httpInfo);
181    XMLCh *findHeader(const char *name);
182
183    virtual bool send(const char *buf, XMLSize_t len) = 0;
184    /**
185     * \return The length of the data received, or -1 if an error occured
186     */
187    virtual int receive(char *buf, XMLSize_t len) = 0;
188
189private :
190    // -----------------------------------------------------------------------
191    //  Unimplemented constructors and operators
192    // -----------------------------------------------------------------------
193    BinHTTPInputStreamCommon(const BinHTTPInputStreamCommon&);
194    BinHTTPInputStreamCommon& operator=(const BinHTTPInputStreamCommon&);
195
196    void createHTTPRequest(const XMLURL &urlSource, const XMLNetHTTPInfo *httpInfo, CharBuffer &buffer);
197
198    // -----------------------------------------------------------------------
199    //  Private data members
200    //
201    //  fBytesProcessed
202    //      Its a rolling count of the number of bytes processed off this
203    //      input stream.
204    //  fBuffer
205    //      Holds the http header, plus the first part of the actual
206    //      data.  Filled at the time the stream is opened, data goes
207    //      out to user in response to readBytes().
208    //  fBufferPos
209    //      Pointers into fBuffer, showing start and end+1 of content
210    //      that readBytes must return.
211    // -----------------------------------------------------------------------
212
213    XMLSize_t           fBytesProcessed;
214    CharBuffer          fBuffer;
215    char *              fBufferPos;
216    XMLCh *             fContentType;
217    MemoryManager*      fMemoryManager;
218};
219
220
221inline XMLFilePos BinHTTPInputStreamCommon::curPos() const
222{
223    return fBytesProcessed;
224}
225
226XERCES_CPP_NAMESPACE_END
227
228#endif
Note: See TracBrowser for help on using the repository browser.