Drizzled Public API Documentation

CSHTTPStream.h

00001 /* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
00002  *
00003  * PrimeBase Media Stream for MySQL
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018  *
00019  * Original author: Paul McCullagh (H&G2JCtL)
00020  * Continued development: Barry Leslie
00021  *
00022  * 2007-06-10
00023  *
00024  *
00025  */
00026 
00027 #pragma once
00028 #ifndef __CSHTTPSTREAM_H__
00029 #define __CSHTTPSTREAM_H__
00030 
00031 #include "CSDefs.h"
00032 #include "CSStorage.h"
00033 #include "CSStream.h"
00034 
00035 // The http tags before and after the exception message text must be well defined so that
00036 // the client api can parse the error reply and get the error text out.
00037 #define EXCEPTION_REPLY_MESSAGE_PREFIX_TAG "<P><B>"
00038 #define EXCEPTION_REPLY_MESSAGE_SUFFIX_TAG "</B></P><PRE>"
00039 #define EXCEPTION_REPLY_STACK_TRACE_SUFFIX_TAG "</PRE><P><font size=-1>"
00040 
00041 class CSHeader : public CSRefObject {
00042 public:
00043   CSHeader():iName(NULL), iValue(NULL) { }
00044   virtual ~CSHeader();
00045 
00046   void setName(const char *name);
00047   void setName(const char *name, uint32_t len);
00048   void setName(CSString *name);
00049 
00050   void setValue(const char *value);
00051   void setValue(const char *value, uint32_t len);
00052   void setValue(CSString *value);
00053 
00054   const char *getNameCString() { return iName ? iName->getCString() : ""; }
00055   const char *getValueCString() { return iValue ? iValue->getCString() : ""; }
00056 
00057   void write(CSOutputStream *out, bool trace);
00058   
00059   friend class CSHTTPHeaders;
00060 private:
00061   CSString *getName() { return iName; } // Return a none referenced object!!
00062   CSString *getValue() { return iValue; }// Return a none referenced object!!
00063 
00064   CSString  *iName;
00065   CSString  *iValue;
00066 };
00067 
00068 class CSHTTPHeaders {
00069 public:
00070   CSHTTPHeaders(CSVector *list = NULL): iHeaders(list), iKeepAlive(false), iExpect100Continue(false), iUnknownEpectHeader(false) { }
00071   virtual ~CSHTTPHeaders() { if (iHeaders) iHeaders->release();}
00072 
00073   void clearHeaders();
00074   CSVector * takeHeaders();
00075   void setHeaders(CSVector *headers);
00076   void addHeaders(CSHTTPHeaders *h);
00077   void addHeader(CSHeader *h);
00078   void addHeader(const char *name, const char *value);
00079   void addHeader(const char *name, uint32_t nlen, const char *value, uint32_t vlen);
00080   void addHeader(CSString *name, CSString *value);
00081   void addHeader(const char *name, CSString *value);
00082   void addHeader(const char *name, uint64_t value);
00083   void removeHeader(const char *name);
00084   void removeHeader(CSString *name);
00085   CSString *getHeaderValue(const char *name);
00086   const char *getHeaderCStringValue(const char *name);
00087   void writeHeader(CSOutputStream *out, bool trace);
00088   bool keepAlive();
00089   bool expect100Continue();
00090   bool unknownEpectHeader();
00091 
00092   uint32_t numHeaders() { return (iHeaders)?iHeaders->size(): 0; }
00093   CSHeader *getHeader(uint32_t idx) 
00094   { 
00095     CSHeader *header = NULL;
00096     
00097     if (iHeaders) 
00098       header = (CSHeader *)(iHeaders->get(idx));
00099       
00100     if (header)
00101       header->retain();
00102     return header;
00103   }
00104   
00105 private:
00106   CSVector *iHeaders;
00107   bool iKeepAlive;
00108   bool iExpect100Continue;
00109   bool iUnknownEpectHeader;
00110 };
00111 
00112 class CSRefHTTPHeaders : public CSHTTPHeaders, public CSRefObject {
00113 public:
00114   CSRefHTTPHeaders(CSVector *list):CSHTTPHeaders(list){}
00115   ~CSRefHTTPHeaders(){} 
00116 };
00117 
00118 class CSHTTPInputStream : public CSInputStream, public CSHTTPHeaders {
00119 public:
00120   CSHTTPInputStream(CSInputStream *s);
00121   virtual ~CSHTTPInputStream();
00122 
00123   void readHead(bool trace = false);
00124   void readBody();
00125   bool getContentLength(uint64_t *length);
00126   const char *getMethod();
00127   char *getBodyData() { return iBody.getCString(); };
00128   size_t getBodyLength() { return iBody.length(); };
00129   void setBody(CSStringBufferImpl *buf) { iBody.take(buf); }
00130   int getStatus() { return iStatus; }
00131   CSString *getStatusPhrase() { return iStatusPhrase; }
00132   CSString *getRequestURI() { return iRequestURI; }
00133   bool getRange(uint64_t *size, uint64_t *offset);
00134 
00135   virtual void close();
00136 
00137   virtual size_t read(char *b, size_t len);
00138 
00139   virtual int read();
00140 
00141   virtual int peek();
00142 
00143   virtual void reset() { iInput->reset(); }
00144 
00145   virtual const char *identify() { return iInput->identify(); }
00146 
00147   static CSHTTPInputStream *newStream(CSInputStream* i);
00148 
00149 private:
00150   void freeHead();
00151 
00152   // Request-Line   = Method SP Request-URI SP HTTP-Version CRLF
00153   CSInputStream *iInput;
00154   int       iStatus;
00155   CSString    *iMethod;
00156   CSString    *iRequestURI;
00157   CSString    *iHTTPVersion;
00158   CSString    *iStatusPhrase;
00159   CSStringBuffer  iBody;
00160 };
00161 
00162 class CSHTTPOutputStream : public CSOutputStream, public CSHTTPHeaders {
00163 public:
00164   CSHTTPOutputStream(CSOutputStream* s);
00165   virtual ~CSHTTPOutputStream();
00166 
00167   void setStatus(int status) { iStatus = status; }
00168   int getStatus() { return iStatus; }
00169   void setContentLength(uint64_t size) { iContentLength = size; }
00170   void setRange(uint64_t size, uint64_t offset, uint64_t total) { iRangeSize = size; iRangeOffset = offset; iTotalLength = total;}
00171 
00172   void writeHead(bool trace = false); // Writes a standard HTTP header.
00173   void writeHeaders(bool trace = false); // Write the current headers.
00174 
00175   void clearBody();
00176   void writeBody();
00177 
00178   // The virtual and non virtual print() methods
00179   // must be kept seperate to avoid possible compiler
00180   // warnings about hidden methods.
00181   void print(const char *str, bool trace);
00182   void print(int32_t value, bool trace);
00183   void print(uint64_t value, bool trace);
00184 
00185   virtual void print(const char *str) {print(str, false);}
00186   virtual void print(CSString *s) {print(s->getCString(), false);}
00187   virtual void print(int32_t value) {print(value, false);}
00188   virtual void print(uint64_t value) {print(value, false);}
00189 
00190   void appendBody(const char *str);
00191   void appendBody(int32_t value);
00192   void appendBody(uint32_t value);
00193   void appendBody(uint64_t value);
00194   const char *getBodyData();
00195   size_t getBodyLength();
00196   void setBody(CSStringBufferImpl *buf);
00197 
00198   virtual void close();
00199 
00200   virtual void write(const char *b, size_t len);
00201 
00202   virtual const char *getEOL() { return "\r\n"; };
00203 
00204   virtual void flush();
00205 
00206   virtual void write(char b);
00207 
00208   virtual void reset() { iOutput->reset(); }
00209 
00210   virtual const char *identify() { return iOutput->identify(); }
00211 
00212   static const char *getReasonPhrase(int code);
00213 
00214   static CSHTTPOutputStream *newStream(CSOutputStream* i);
00215 
00216 private:
00217   // Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
00218   CSOutputStream  *iOutput;
00219   int       iStatus;
00220   uint64_t      iContentLength;
00221   CSStringBuffer  iBody;
00222   uint64_t      iRangeSize;
00223   uint64_t      iRangeOffset;
00224   uint64_t      iTotalLength;
00225 };
00226 
00227 #endif