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