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
20namespace rtc {
21
22///////////////////////////////////////////////////////////////////////////////
23// MultipartStream - Implements an RFC2046 multipart stream by concatenating
24// the supplied parts together, and adding the correct boundaries.
25///////////////////////////////////////////////////////////////////////////////
26
27class MultipartStream : public StreamInterface, public sigslot::has_slots<> {
28 public:
29  MultipartStream(const std::string& type, const std::string& boundary);
30  virtual ~MultipartStream();
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  virtual StreamState GetState() const;
52  virtual StreamResult Read(void* buffer, size_t buffer_len,
53                            size_t* read, int* error);
54  virtual StreamResult Write(const void* data, size_t data_len,
55                             size_t* written, int* error);
56  virtual void Close();
57  virtual bool SetPosition(size_t position);
58  virtual bool GetPosition(size_t* position) const;
59  virtual bool GetSize(size_t* size) const;
60  virtual bool GetAvailable(size_t* size) const;
61
62 private:
63  typedef std::vector<StreamInterface*> PartList;
64
65  // StreamInterface Slots
66  void OnEvent(StreamInterface* stream, int events, int error);
67
68  std::string type_, boundary_;
69  PartList parts_;
70  bool adding_;
71  size_t current_;  // The index into parts_ of the current read position.
72  size_t position_;  // The current read position in bytes.
73
74  DISALLOW_COPY_AND_ASSIGN(MultipartStream);
75};
76
77}  // namespace rtc
78
79#endif  // WEBRTC_BASE_MULTIPART_H__
80