Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_BASE_MULTIPART_H__
     12 #define WEBRTC_BASE_MULTIPART_H__
     13 
     14 #include <string>
     15 #include <vector>
     16 
     17 #include "webrtc/base/sigslot.h"
     18 #include "webrtc/base/stream.h"
     19 
     20 namespace rtc {
     21 
     22 ///////////////////////////////////////////////////////////////////////////////
     23 // MultipartStream - Implements an RFC2046 multipart stream by concatenating
     24 // the supplied parts together, and adding the correct boundaries.
     25 ///////////////////////////////////////////////////////////////////////////////
     26 
     27 class MultipartStream : public StreamInterface, public sigslot::has_slots<> {
     28  public:
     29   MultipartStream(const std::string& type, const std::string& boundary);
     30   ~MultipartStream() override;
     31 
     32   void GetContentType(std::string* content_type);
     33 
     34   // Note: If content_disposition and/or content_type are the empty string,
     35   // they will be omitted.
     36   bool AddPart(StreamInterface* data_stream,
     37                const std::string& content_disposition,
     38                const std::string& content_type);
     39   bool AddPart(const std::string& data,
     40                const std::string& content_disposition,
     41                const std::string& content_type);
     42   void EndParts();
     43 
     44   // Calculates the size of a part before actually adding the part.
     45   size_t GetPartSize(const std::string& data,
     46                      const std::string& content_disposition,
     47                      const std::string& content_type) const;
     48   size_t GetEndPartSize() const;
     49 
     50   // StreamInterface
     51   StreamState GetState() const override;
     52   StreamResult Read(void* buffer,
     53                     size_t buffer_len,
     54                     size_t* read,
     55                     int* error) override;
     56   StreamResult Write(const void* data,
     57                      size_t data_len,
     58                      size_t* written,
     59                      int* error) override;
     60   void Close() override;
     61   bool SetPosition(size_t position) override;
     62   bool GetPosition(size_t* position) const override;
     63   bool GetSize(size_t* size) const override;
     64   bool GetAvailable(size_t* size) const override;
     65 
     66  private:
     67   typedef std::vector<StreamInterface*> PartList;
     68 
     69   // StreamInterface Slots
     70   void OnEvent(StreamInterface* stream, int events, int error);
     71 
     72   std::string type_, boundary_;
     73   PartList parts_;
     74   bool adding_;
     75   size_t current_;  // The index into parts_ of the current read position.
     76   size_t position_;  // The current read position in bytes.
     77 
     78   RTC_DISALLOW_COPY_AND_ASSIGN(MultipartStream);
     79 };
     80 
     81 }  // namespace rtc
     82 
     83 #endif  // WEBRTC_BASE_MULTIPART_H__
     84