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
27
#ifndef __CSHTTPSTREAM_H__
28
#define __CSHTTPSTREAM_H__
31
#include "CSStorage.h"
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>"
40
class CSHeader : public CSRefObject {
42
CSHeader():iName(NULL), iValue(NULL) { }
45
void setName(const char *name);
46
void setName(const char *name, uint32_t len);
47
void setName(CSString *name);
49
void setValue(const char *value);
50
void setValue(const char *value, uint32_t len);
51
void setValue(CSString *value);
53
const char *getNameCString() { return iName ? iName->getCString() : ""; }
54
const char *getValueCString() { return iValue ? iValue->getCString() : ""; }
56
void write(CSOutputStream *out, bool trace);
58
friend class CSHTTPHeaders;
60
CSString *getName() { return iName; } // Return a none referenced object!!
61
CSString *getValue() { return iValue; }// Return a none referenced object!!
69
CSHTTPHeaders(CSVector *list = NULL): iHeaders(list), iKeepAlive(false), iExpect100Continue(false), iUnknownEpectHeader(false) { }
70
virtual ~CSHTTPHeaders() { if (iHeaders) iHeaders->release();}
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);
88
bool expect100Continue();
89
bool unknownEpectHeader();
91
uint32_t numHeaders() { return (iHeaders)?iHeaders->size(): 0; }
92
CSHeader *getHeader(uint32_t idx)
94
CSHeader *header = NULL;
97
header = (CSHeader *)(iHeaders->get(idx));
107
bool iExpect100Continue;
108
bool iUnknownEpectHeader;
111
class CSRefHTTPHeaders : public CSHTTPHeaders, public CSRefObject {
113
CSRefHTTPHeaders(CSVector *list):CSHTTPHeaders(list){}
114
~CSRefHTTPHeaders(){}
117
class CSHTTPInputStream : public CSInputStream, public CSHTTPHeaders {
119
CSHTTPInputStream(CSInputStream *s);
120
virtual ~CSHTTPInputStream();
122
void readHead(bool trace = false);
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);
134
virtual void close();
136
virtual size_t read(char *b, size_t len);
142
virtual void reset() { iInput->reset(); }
144
virtual const char *identify() { return iInput->identify(); }
146
static CSHTTPInputStream *newStream(CSInputStream* i);
151
// Request-Line = Method SP Request-URI SP HTTP-Version CRLF
152
CSInputStream *iInput;
155
CSString *iRequestURI;
156
CSString *iHTTPVersion;
157
CSString *iStatusPhrase;
158
CSStringBuffer iBody;
161
class CSHTTPOutputStream : public CSOutputStream, public CSHTTPHeaders {
163
CSHTTPOutputStream(CSOutputStream* s);
164
virtual ~CSHTTPOutputStream();
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;}
171
void writeHead(bool trace = false); // Writes a standard HTTP header.
172
void writeHeaders(bool trace = false); // Write the current headers.
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);
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);}
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);
197
virtual void close();
199
virtual void write(const char *b, size_t len);
201
virtual const char *getEOL() { return "\r\n"; };
203
virtual void flush();
205
virtual void write(char b);
207
virtual void reset() { iOutput->reset(); }
209
virtual const char *identify() { return iOutput->identify(); }
211
static const char *getReasonPhrase(int code);
213
static CSHTTPOutputStream *newStream(CSOutputStream* i);
216
// Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
217
CSOutputStream *iOutput;
219
uint64_t iContentLength;
220
CSStringBuffer iBody;
222
uint64_t iRangeOffset;
223
uint64_t iTotalLength;