147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/*
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Use of this source code is governed by a BSD-style license
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  that can be found in the LICENSE file in the root of the source
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  tree. An additional intellectual property rights grant can be found
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  in the file PATENTS.  All contributing project authors may
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org */
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#ifndef WEBRTC_BASE_MULTIPART_H__
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#define WEBRTC_BASE_MULTIPART_H__
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <string>
1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <vector>
1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/sigslot.h"
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/stream.h"
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc {
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org///////////////////////////////////////////////////////////////////////////////
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// MultipartStream - Implements an RFC2046 multipart stream by concatenating
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// the supplied parts together, and adding the correct boundaries.
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org///////////////////////////////////////////////////////////////////////////////
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass MultipartStream : public StreamInterface, public sigslot::has_slots<> {
2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org public:
2947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  MultipartStream(const std::string& type, const std::string& boundary);
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual ~MultipartStream();
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void GetContentType(std::string* content_type);
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Note: If content_disposition and/or content_type are the empty string,
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // they will be omitted.
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  bool AddPart(StreamInterface* data_stream,
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org               const std::string& content_disposition,
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org               const std::string& content_type);
3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  bool AddPart(const std::string& data,
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org               const std::string& content_disposition,
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org               const std::string& content_type);
4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void EndParts();
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Calculates the size of a part before actually adding the part.
4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  size_t GetPartSize(const std::string& data,
4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                     const std::string& content_disposition,
4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                     const std::string& content_type) const;
4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  size_t GetEndPartSize() const;
4947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // StreamInterface
5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual StreamState GetState() const;
5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual StreamResult Read(void* buffer, size_t buffer_len,
5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                            size_t* read, int* error);
5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual StreamResult Write(const void* data, size_t data_len,
5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                             size_t* written, int* error);
5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual void Close();
5747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual bool SetPosition(size_t position);
5847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual bool GetPosition(size_t* position) const;
5947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual bool GetSize(size_t* size) const;
6047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual bool GetAvailable(size_t* size) const;
6147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org private:
6347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  typedef std::vector<StreamInterface*> PartList;
6447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // StreamInterface Slots
6647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void OnEvent(StreamInterface* stream, int events, int error);
6747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string type_, boundary_;
6947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  PartList parts_;
7047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  bool adding_;
7147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  size_t current_;  // The index into parts_ of the current read position.
7247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  size_t position_;  // The current read position in bytes.
7347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  DISALLOW_COPY_AND_ASSIGN(MultipartStream);
7547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
7647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}  // namespace rtc
7847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_BASE_MULTIPART_H__
80