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);
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(): iHeaders(NULL), 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 removeHeader(const char *name);
82
void removeHeader(CSString *name);
83
CSString *getHeaderValue(const char *name);
84
void writeHeader(CSOutputStream *out);
86
bool expect100Continue();
87
bool unknownEpectHeader();
89
uint32_t numHeaders() { return (iHeaders)?iHeaders->size(): 0; }
90
CSHeader *getHeader(uint32_t idx)
92
CSHeader *header = NULL;
95
header = (CSHeader *)(iHeaders->get(idx));
105
bool iExpect100Continue;
106
bool iUnknownEpectHeader;
109
class CSHTTPInputStream : public CSInputStream, public CSHTTPHeaders {
111
CSHTTPInputStream(CSInputStream *s);
112
virtual ~CSHTTPInputStream();
115
uint64_t getContentLength();
116
const char *getMethod();
117
int getStatus() { return iStatus; }
118
CSString *getStatusPhrase() { return iStatusPhrase; }
119
CSString *getRequestURI() { return iRequestURI; }
120
bool getRange(uint64_t *size, uint64_t *offset);
122
virtual void close();
124
virtual size_t read(char *b, size_t len);
130
virtual void reset() {iInput->reset();}
132
static CSHTTPInputStream *newStream(CSInputStream* i);
137
// Request-Line = Method SP Request-URI SP HTTP-Version CRLF
138
CSInputStream *iInput;
141
CSString *iRequestURI;
142
CSString *iHTTPVersion;
143
CSString *iStatusPhrase;
146
class CSHTTPOutputStream : public CSOutputStream, public CSHTTPHeaders {
148
CSHTTPOutputStream(CSOutputStream* s);
149
virtual ~CSHTTPOutputStream();
151
void setStatus(int status) { iStatus = status; }
152
int getStatus() { return iStatus; }
153
void setContentLength(uint64_t size) { iContentLength = size; }
154
void setRange(uint64_t size, uint64_t offset, uint64_t total) { iRangeSize = size; iRangeOffset = offset; iTotalLength = total;}
156
void writeHead(); // Writes a standard HTTP header.
157
void writeHeaders(); // Write the current headers.
162
void appendBody(const char *str);
163
void appendBody(int value);
165
virtual void close();
167
virtual void write(const char *b, size_t len);
169
virtual const char *getEOL() { return "\r\n"; };
171
virtual void flush();
173
virtual void write(char b);
175
virtual void reset() {iOutput->reset();}
177
static const char *getReasonPhrase(int code);
179
static CSHTTPOutputStream *newStream(CSOutputStream* i);
182
// Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
183
CSOutputStream *iOutput;
185
uint64_t iContentLength;
186
CSStringBuffer iBody;
188
uint64_t iRangeOffset;
189
uint64_t iTotalLength;