~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSHTTPStream.h

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 18:24:24 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212182424-kgnm9osi7qo97at2
casts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
18
 *
 
19
 * Original author: Paul McCullagh (H&G2JCtL)
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-06-10
 
23
 *
 
24
 *
 
25
 */
 
26
 
 
27
#ifndef __CSHTTPSTREAM_H__
 
28
#define __CSHTTPSTREAM_H__
 
29
 
 
30
#include "CSDefs.h"
 
31
#include "CSStorage.h"
 
32
#include "CSStream.h"
 
33
 
 
34
// The http tags before and after the exception message text must be well defined so that
 
35
// the client api can parse the error reply and get the error text out.
 
36
#define EXCEPTION_REPLY_MESSAGE_PREFIX_TAG "<P><B>"
 
37
#define EXCEPTION_REPLY_MESSAGE_SUFFIX_TAG "</B></P><PRE>"
 
38
#define EXCEPTION_REPLY_STACK_TRACE_SUFFIX_TAG "</PRE><P><font size=-1>"
 
39
 
 
40
class CSHeader : public CSRefObject {
 
41
public:
 
42
        CSHeader():iName(NULL), iValue(NULL) { }
 
43
        virtual ~CSHeader();
 
44
 
 
45
        void setName(const char *name);
 
46
        void setName(const char *name, uint32_t len);
 
47
        void setName(CSString *name);
 
48
 
 
49
        void setValue(const char *value);
 
50
        void setValue(const char *value, uint32_t len);
 
51
        void setValue(CSString *value);
 
52
 
 
53
        const char *getNameCString() { return iName ? iName->getCString() : ""; }
 
54
        const char *getValueCString() { return iValue ? iValue->getCString() : ""; }
 
55
 
 
56
        void write(CSOutputStream *out, bool trace);
 
57
        
 
58
        friend class CSHTTPHeaders;
 
59
private:
 
60
        CSString *getName() { return iName; } // Return a none referenced object!!
 
61
        CSString *getValue() { return iValue; }// Return a none referenced object!!
 
62
 
 
63
        CSString        *iName;
 
64
        CSString        *iValue;
 
65
};
 
66
 
 
67
class CSHTTPHeaders {
 
68
public:
 
69
        CSHTTPHeaders(CSVector *list = NULL): iHeaders(list), iKeepAlive(false), iExpect100Continue(false), iUnknownEpectHeader(false) { }
 
70
        virtual ~CSHTTPHeaders() { if (iHeaders) iHeaders->release();}
 
71
 
 
72
        void clearHeaders();
 
73
        CSVector * takeHeaders();
 
74
        void setHeaders(CSVector *headers);
 
75
        void addHeaders(CSHTTPHeaders *h);
 
76
        void addHeader(CSHeader *h);
 
77
        void addHeader(const char *name, const char *value);
 
78
        void addHeader(const char *name, uint32_t nlen, const char *value, uint32_t vlen);
 
79
        void addHeader(CSString *name, CSString *value);
 
80
        void addHeader(const char *name, CSString *value);
 
81
        void addHeader(const char *name, uint64_t value);
 
82
        void removeHeader(const char *name);
 
83
        void removeHeader(CSString *name);
 
84
        CSString *getHeaderValue(const char *name);
 
85
        const char *getHeaderCStringValue(const char *name);
 
86
        void writeHeader(CSOutputStream *out, bool trace);
 
87
        bool keepAlive();
 
88
        bool expect100Continue();
 
89
        bool unknownEpectHeader();
 
90
 
 
91
        uint32_t numHeaders() { return (iHeaders)?iHeaders->size(): 0; }
 
92
        CSHeader *getHeader(uint32_t idx) 
 
93
        { 
 
94
                CSHeader *header = NULL;
 
95
                
 
96
                if (iHeaders) 
 
97
                        header = (CSHeader *)(iHeaders->get(idx));
 
98
                        
 
99
                if (header)
 
100
                        header->retain();
 
101
                return header;
 
102
        }
 
103
        
 
104
private:
 
105
        CSVector *iHeaders;
 
106
        bool iKeepAlive;
 
107
        bool iExpect100Continue;
 
108
        bool iUnknownEpectHeader;
 
109
};
 
110
 
 
111
class CSRefHTTPHeaders : public CSHTTPHeaders, public CSRefObject {
 
112
public:
 
113
        CSRefHTTPHeaders(CSVector *list):CSHTTPHeaders(list){}
 
114
        ~CSRefHTTPHeaders(){}   
 
115
};
 
116
 
 
117
class CSHTTPInputStream : public CSInputStream, public CSHTTPHeaders {
 
118
public:
 
119
        CSHTTPInputStream(CSInputStream *s);
 
120
        virtual ~CSHTTPInputStream();
 
121
 
 
122
        void readHead(bool trace = false);
 
123
        void readBody();
 
124
        bool getContentLength(uint64_t *length);
 
125
        const char *getMethod();
 
126
        char *getBodyData() { return iBody.getCString(); };
 
127
        size_t getBodyLength() { return iBody.length(); };
 
128
        void setBody(CSStringBufferImpl *buf) { iBody.take(buf); }
 
129
        int getStatus() { return iStatus; }
 
130
        CSString *getStatusPhrase() { return iStatusPhrase; }
 
131
        CSString *getRequestURI() { return iRequestURI; }
 
132
        bool getRange(uint64_t *size, uint64_t *offset);
 
133
 
 
134
        virtual void close();
 
135
 
 
136
        virtual size_t read(char *b, size_t len);
 
137
 
 
138
        virtual int read();
 
139
 
 
140
        virtual int peek();
 
141
 
 
142
        virtual void reset() { iInput->reset(); }
 
143
 
 
144
        virtual const char *identify() { return iInput->identify(); }
 
145
 
 
146
        static CSHTTPInputStream *newStream(CSInputStream* i);
 
147
 
 
148
private:
 
149
        void freeHead();
 
150
 
 
151
        // Request-Line   = Method SP Request-URI SP HTTP-Version CRLF
 
152
        CSInputStream   *iInput;
 
153
        int                             iStatus;
 
154
        CSString                *iMethod;
 
155
        CSString                *iRequestURI;
 
156
        CSString                *iHTTPVersion;
 
157
        CSString                *iStatusPhrase;
 
158
        CSStringBuffer  iBody;
 
159
};
 
160
 
 
161
class CSHTTPOutputStream : public CSOutputStream, public CSHTTPHeaders {
 
162
public:
 
163
        CSHTTPOutputStream(CSOutputStream* s);
 
164
        virtual ~CSHTTPOutputStream();
 
165
 
 
166
        void setStatus(int status) { iStatus = status; }
 
167
        int getStatus() { return iStatus; }
 
168
        void setContentLength(uint64_t size) { iContentLength = size; }
 
169
        void setRange(uint64_t size, uint64_t offset, uint64_t total) { iRangeSize = size; iRangeOffset = offset; iTotalLength = total;}
 
170
 
 
171
        void writeHead(bool trace = false);     // Writes a standard HTTP header.
 
172
        void writeHeaders(bool trace = false); // Write the current headers.
 
173
 
 
174
        void clearBody();
 
175
        void writeBody();
 
176
 
 
177
        // The virtual and non virtual print() methods
 
178
        // must be kept seperate to avoid possible compiler
 
179
        // warnings about hidden methods.
 
180
        void print(const char *str, bool trace);
 
181
        void print(int32_t value, bool trace);
 
182
        void print(uint64_t value, bool trace);
 
183
 
 
184
        virtual void print(const char *str) {print(str, false);}
 
185
        virtual void print(CSString *s) {print(s->getCString(), false);}
 
186
        virtual void print(int32_t value) {print(value, false);}
 
187
        virtual void print(uint64_t value) {print(value, false);}
 
188
 
 
189
        void appendBody(const char *str);
 
190
        void appendBody(int32_t value);
 
191
        void appendBody(uint32_t value);
 
192
        void appendBody(uint64_t value);
 
193
        const char *getBodyData();
 
194
        size_t getBodyLength();
 
195
        void setBody(CSStringBufferImpl *buf);
 
196
 
 
197
        virtual void close();
 
198
 
 
199
        virtual void write(const char *b, size_t len);
 
200
 
 
201
        virtual const char *getEOL() { return "\r\n"; };
 
202
 
 
203
        virtual void flush();
 
204
 
 
205
        virtual void write(char b);
 
206
 
 
207
        virtual void reset() { iOutput->reset(); }
 
208
 
 
209
        virtual const char *identify() { return iOutput->identify(); }
 
210
 
 
211
        static const char *getReasonPhrase(int code);
 
212
 
 
213
        static CSHTTPOutputStream *newStream(CSOutputStream* i);
 
214
 
 
215
private:
 
216
        // Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
 
217
        CSOutputStream  *iOutput;
 
218
        int                             iStatus;
 
219
        uint64_t                        iContentLength;
 
220
        CSStringBuffer  iBody;
 
221
        uint64_t                        iRangeSize;
 
222
        uint64_t                        iRangeOffset;
 
223
        uint64_t                        iTotalLength;
 
224
};
 
225
 
 
226
#endif