1
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
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.
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.
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
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
28
#ifndef __CSHTTPSTREAM_H__
29
#define __CSHTTPSTREAM_H__
32
#include "CSStorage.h"
35
// The http tags before and after the exception message text must be well defined so that
36
// the client api can parse the error reply and get the error text out.
37
#define EXCEPTION_REPLY_MESSAGE_PREFIX_TAG "<P><B>"
38
#define EXCEPTION_REPLY_MESSAGE_SUFFIX_TAG "</B></P><PRE>"
39
#define EXCEPTION_REPLY_STACK_TRACE_SUFFIX_TAG "</PRE><P><font size=-1>"
41
class CSHeader : public CSRefObject {
43
CSHeader():iName(NULL), iValue(NULL) { }
46
void setName(const char *name);
47
void setName(const char *name, uint32_t len);
48
void setName(CSString *name);
50
void setValue(const char *value);
51
void setValue(const char *value, uint32_t len);
52
void setValue(CSString *value);
54
const char *getNameCString() { return iName ? iName->getCString() : ""; }
55
const char *getValueCString() { return iValue ? iValue->getCString() : ""; }
57
void write(CSOutputStream *out, bool trace);
59
friend class CSHTTPHeaders;
61
CSString *getName() { return iName; } // Return a none referenced object!!
62
CSString *getValue() { return iValue; }// Return a none referenced object!!
70
CSHTTPHeaders(CSVector *list = NULL): iHeaders(list), iKeepAlive(false), iExpect100Continue(false), iUnknownEpectHeader(false) { }
71
virtual ~CSHTTPHeaders() { if (iHeaders) iHeaders->release();}
74
CSVector * takeHeaders();
75
void setHeaders(CSVector *headers);
76
void addHeaders(CSHTTPHeaders *h);
77
void addHeader(CSHeader *h);
78
void addHeader(const char *name, const char *value);
79
void addHeader(const char *name, uint32_t nlen, const char *value, uint32_t vlen);
80
void addHeader(CSString *name, CSString *value);
81
void addHeader(const char *name, CSString *value);
82
void addHeader(const char *name, uint64_t value);
83
void removeHeader(const char *name);
84
void removeHeader(CSString *name);
85
CSString *getHeaderValue(const char *name);
86
const char *getHeaderCStringValue(const char *name);
87
void writeHeader(CSOutputStream *out, bool trace);
89
bool expect100Continue();
90
bool unknownEpectHeader();
92
uint32_t numHeaders() { return (iHeaders)?iHeaders->size(): 0; }
93
CSHeader *getHeader(uint32_t idx)
95
CSHeader *header = NULL;
98
header = (CSHeader *)(iHeaders->get(idx));
108
bool iExpect100Continue;
109
bool iUnknownEpectHeader;
112
class CSRefHTTPHeaders : public CSHTTPHeaders, public CSRefObject {
114
CSRefHTTPHeaders(CSVector *list):CSHTTPHeaders(list){}
115
~CSRefHTTPHeaders(){}
118
class CSHTTPInputStream : public CSInputStream, public CSHTTPHeaders {
120
CSHTTPInputStream(CSInputStream *s);
121
virtual ~CSHTTPInputStream();
123
void readHead(bool trace = false);
125
bool getContentLength(uint64_t *length);
126
const char *getMethod();
127
char *getBodyData() { return iBody.getCString(); };
128
size_t getBodyLength() { return iBody.length(); };
129
void setBody(CSStringBufferImpl *buf) { iBody.take(buf); }
130
int getStatus() { return iStatus; }
131
CSString *getStatusPhrase() { return iStatusPhrase; }
132
CSString *getRequestURI() { return iRequestURI; }
133
bool getRange(uint64_t *size, uint64_t *offset);
135
virtual void close();
137
virtual size_t read(char *b, size_t len);
143
virtual void reset() { iInput->reset(); }
145
virtual const char *identify() { return iInput->identify(); }
147
static CSHTTPInputStream *newStream(CSInputStream* i);
152
// Request-Line = Method SP Request-URI SP HTTP-Version CRLF
153
CSInputStream *iInput;
156
CSString *iRequestURI;
157
CSString *iHTTPVersion;
158
CSString *iStatusPhrase;
159
CSStringBuffer iBody;
162
class CSHTTPOutputStream : public CSOutputStream, public CSHTTPHeaders {
164
CSHTTPOutputStream(CSOutputStream* s);
165
virtual ~CSHTTPOutputStream();
167
void setStatus(int status) { iStatus = status; }
168
int getStatus() { return iStatus; }
169
void setContentLength(uint64_t size) { iContentLength = size; }
170
void setRange(uint64_t size, uint64_t offset, uint64_t total) { iRangeSize = size; iRangeOffset = offset; iTotalLength = total;}
172
void writeHead(bool trace = false); // Writes a standard HTTP header.
173
void writeHeaders(bool trace = false); // Write the current headers.
178
// The virtual and non virtual print() methods
179
// must be kept seperate to avoid possible compiler
180
// warnings about hidden methods.
181
void print(const char *str, bool trace);
182
void print(int32_t value, bool trace);
183
void print(uint64_t value, bool trace);
185
virtual void print(const char *str) {print(str, false);}
186
virtual void print(CSString *s) {print(s->getCString(), false);}
187
virtual void print(int32_t value) {print(value, false);}
188
virtual void print(uint64_t value) {print(value, false);}
190
void appendBody(const char *str);
191
void appendBody(int32_t value);
192
void appendBody(uint32_t value);
193
void appendBody(uint64_t value);
194
const char *getBodyData();
195
size_t getBodyLength();
196
void setBody(CSStringBufferImpl *buf);
198
virtual void close();
200
virtual void write(const char *b, size_t len);
202
virtual const char *getEOL() { return "\r\n"; };
204
virtual void flush();
206
virtual void write(char b);
208
virtual void reset() { iOutput->reset(); }
210
virtual const char *identify() { return iOutput->identify(); }
212
static const char *getReasonPhrase(int code);
214
static CSHTTPOutputStream *newStream(CSOutputStream* i);
217
// Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
218
CSOutputStream *iOutput;
220
uint64_t iContentLength;
221
CSStringBuffer iBody;
223
uint64_t iRangeOffset;
224
uint64_t iTotalLength;