Home | History | Annotate | Download | only in base
      1 // libjingle
      2 // Copyright 2004--2010, Google Inc.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are met:
      6 //
      7 //  1. Redistributions of source code must retain the above copyright notice,
      8 //     this list of conditions and the following disclaimer.
      9 //  2. Redistributions in binary form must reproduce the above copyright notice,
     10 //     this list of conditions and the following disclaimer in the documentation
     11 //     and/or other materials provided with the distribution.
     12 //  3. The name of the author may not be used to endorse or promote products
     13 //     derived from this software without specific prior written permission.
     14 //
     15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25 
     26 #ifndef TALK_BASE_MULTIPART_H__
     27 #define TALK_BASE_MULTIPART_H__
     28 
     29 #include <string>
     30 #include <vector>
     31 
     32 #include "talk/base/sigslot.h"
     33 #include "talk/base/stream.h"
     34 
     35 namespace talk_base {
     36 
     37 ///////////////////////////////////////////////////////////////////////////////
     38 // MultipartStream - Implements an RFC2046 multipart stream by concatenating
     39 // the supplied parts together, and adding the correct boundaries.
     40 ///////////////////////////////////////////////////////////////////////////////
     41 
     42 class MultipartStream : public StreamInterface, public sigslot::has_slots<> {
     43  public:
     44   MultipartStream(const std::string& type, const std::string& boundary);
     45   virtual ~MultipartStream();
     46 
     47   void GetContentType(std::string* content_type);
     48 
     49   // Note: If content_disposition and/or content_type are the empty string,
     50   // they will be omitted.
     51   bool AddPart(StreamInterface* data_stream,
     52                const std::string& content_disposition,
     53                const std::string& content_type);
     54   bool AddPart(const std::string& data,
     55                const std::string& content_disposition,
     56                const std::string& content_type);
     57   void EndParts();
     58 
     59   // Calculates the size of a part before actually adding the part.
     60   size_t GetPartSize(const std::string& data,
     61                      const std::string& content_disposition,
     62                      const std::string& content_type) const;
     63   size_t GetEndPartSize() const;
     64 
     65   // StreamInterface
     66   virtual StreamState GetState() const;
     67   virtual StreamResult Read(void* buffer, size_t buffer_len,
     68                             size_t* read, int* error);
     69   virtual StreamResult Write(const void* data, size_t data_len,
     70                              size_t* written, int* error);
     71   virtual void Close();
     72   virtual bool SetPosition(size_t position);
     73   virtual bool GetPosition(size_t* position) const;
     74   virtual bool GetSize(size_t* size) const;
     75   virtual bool GetAvailable(size_t* size) const;
     76 
     77  private:
     78   typedef std::vector<StreamInterface*> PartList;
     79 
     80   // StreamInterface Slots
     81   void OnEvent(StreamInterface* stream, int events, int error);
     82 
     83   std::string type_, boundary_;
     84   PartList parts_;
     85   bool adding_;
     86   size_t current_;  // The index into parts_ of the current read position.
     87   size_t position_;  // The current read position in bytes.
     88 
     89   DISALLOW_COPY_AND_ASSIGN(MultipartStream);
     90 };
     91 
     92 }  // namespace talk_base
     93 
     94 #endif  // TALK_BASE_MULTIPART_H__
     95