Delete multipart.h.
BUG=webrtc:6424 Review-Url: https://codereview.webrtc.org/2374013003 Cr-Commit-Position: refs/heads/master@{#14517}
This commit is contained in:
parent
3d7901d1bd
commit
8a72369c2a
@ -365,7 +365,6 @@ if (rtc_include_tests) {
|
||||
"base/messagedigest_unittest.cc",
|
||||
"base/messagequeue_unittest.cc",
|
||||
"base/mod_ops_unittest.cc",
|
||||
"base/multipart_unittest.cc",
|
||||
"base/nat_unittest.cc",
|
||||
"base/network_unittest.cc",
|
||||
"base/onetimeevent_unittest.cc",
|
||||
|
||||
@ -448,8 +448,6 @@ rtc_static_library("rtc_base") {
|
||||
"logsinks.cc",
|
||||
"logsinks.h",
|
||||
"mathutils.h",
|
||||
"multipart.cc",
|
||||
"multipart.h",
|
||||
"natserver.cc",
|
||||
"natserver.h",
|
||||
"natsocketfactory.cc",
|
||||
|
||||
@ -409,8 +409,6 @@
|
||||
'logsinks.cc',
|
||||
'logsinks.h',
|
||||
'mathutils.h',
|
||||
'multipart.cc',
|
||||
'multipart.h',
|
||||
'natserver.cc',
|
||||
'natserver.h',
|
||||
'natsocketfactory.cc',
|
||||
|
||||
@ -1,253 +0,0 @@
|
||||
/*
|
||||
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include "webrtc/base/common.h"
|
||||
#include "webrtc/base/httpcommon.h"
|
||||
#include "webrtc/base/multipart.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// MultipartStream
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
MultipartStream::MultipartStream(const std::string& type,
|
||||
const std::string& boundary)
|
||||
: type_(type),
|
||||
boundary_(boundary),
|
||||
adding_(true),
|
||||
current_(0),
|
||||
position_(0) {
|
||||
// The content type should be multipart/*.
|
||||
ASSERT(0 == strncmp(type_.c_str(), "multipart/", 10));
|
||||
}
|
||||
|
||||
MultipartStream::~MultipartStream() {
|
||||
Close();
|
||||
}
|
||||
|
||||
void MultipartStream::GetContentType(std::string* content_type) {
|
||||
ASSERT(NULL != content_type);
|
||||
content_type->assign(type_);
|
||||
content_type->append("; boundary=");
|
||||
content_type->append(boundary_);
|
||||
}
|
||||
|
||||
bool MultipartStream::AddPart(StreamInterface* data_stream,
|
||||
const std::string& content_disposition,
|
||||
const std::string& content_type) {
|
||||
if (!AddPart("", content_disposition, content_type))
|
||||
return false;
|
||||
parts_.push_back(data_stream);
|
||||
data_stream->SignalEvent.connect(this, &MultipartStream::OnEvent);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MultipartStream::AddPart(const std::string& data,
|
||||
const std::string& content_disposition,
|
||||
const std::string& content_type) {
|
||||
ASSERT(adding_);
|
||||
if (!adding_)
|
||||
return false;
|
||||
std::stringstream ss;
|
||||
if (!parts_.empty()) {
|
||||
ss << "\r\n";
|
||||
}
|
||||
ss << "--" << boundary_ << "\r\n";
|
||||
if (!content_disposition.empty()) {
|
||||
ss << ToString(HH_CONTENT_DISPOSITION) << ": "
|
||||
<< content_disposition << "\r\n";
|
||||
}
|
||||
if (!content_type.empty()) {
|
||||
ss << ToString(HH_CONTENT_TYPE) << ": "
|
||||
<< content_type << "\r\n";
|
||||
}
|
||||
ss << "\r\n" << data;
|
||||
parts_.push_back(new MemoryStream(ss.str().data(), ss.str().size()));
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultipartStream::EndParts() {
|
||||
ASSERT(adding_);
|
||||
if (!adding_)
|
||||
return;
|
||||
|
||||
std::stringstream ss;
|
||||
if (!parts_.empty()) {
|
||||
ss << "\r\n";
|
||||
}
|
||||
ss << "--" << boundary_ << "--" << "\r\n";
|
||||
parts_.push_back(new MemoryStream(ss.str().data(), ss.str().size()));
|
||||
|
||||
ASSERT(0 == current_);
|
||||
ASSERT(0 == position_);
|
||||
adding_ = false;
|
||||
SignalEvent(this, SE_OPEN | SE_READ, 0);
|
||||
}
|
||||
|
||||
size_t MultipartStream::GetPartSize(const std::string& data,
|
||||
const std::string& content_disposition,
|
||||
const std::string& content_type) const {
|
||||
size_t size = 0;
|
||||
if (!parts_.empty()) {
|
||||
size += 2; // for "\r\n";
|
||||
}
|
||||
size += boundary_.size() + 4; // for "--boundary_\r\n";
|
||||
if (!content_disposition.empty()) {
|
||||
// for ToString(HH_CONTENT_DISPOSITION): content_disposition\r\n
|
||||
size += std::string(ToString(HH_CONTENT_DISPOSITION)).size() + 2 +
|
||||
content_disposition.size() + 2;
|
||||
}
|
||||
if (!content_type.empty()) {
|
||||
// for ToString(HH_CONTENT_TYPE): content_type\r\n
|
||||
size += std::string(ToString(HH_CONTENT_TYPE)).size() + 2 +
|
||||
content_type.size() + 2;
|
||||
}
|
||||
size += 2 + data.size(); // for \r\ndata
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t MultipartStream::GetEndPartSize() const {
|
||||
size_t size = 0;
|
||||
if (!parts_.empty()) {
|
||||
size += 2; // for "\r\n";
|
||||
}
|
||||
size += boundary_.size() + 6; // for "--boundary_--\r\n";
|
||||
return size;
|
||||
}
|
||||
|
||||
//
|
||||
// StreamInterface
|
||||
//
|
||||
|
||||
StreamState MultipartStream::GetState() const {
|
||||
if (adding_) {
|
||||
return SS_OPENING;
|
||||
}
|
||||
return (current_ < parts_.size()) ? SS_OPEN : SS_CLOSED;
|
||||
}
|
||||
|
||||
StreamResult MultipartStream::Read(void* buffer, size_t buffer_len,
|
||||
size_t* read, int* error) {
|
||||
if (adding_) {
|
||||
return SR_BLOCK;
|
||||
}
|
||||
size_t local_read;
|
||||
if (!read) read = &local_read;
|
||||
while (current_ < parts_.size()) {
|
||||
StreamResult result = parts_[current_]->Read(buffer, buffer_len, read,
|
||||
error);
|
||||
if (SR_EOS != result) {
|
||||
if (SR_SUCCESS == result) {
|
||||
position_ += *read;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
++current_;
|
||||
}
|
||||
return SR_EOS;
|
||||
}
|
||||
|
||||
StreamResult MultipartStream::Write(const void* data, size_t data_len,
|
||||
size_t* written, int* error) {
|
||||
if (error) {
|
||||
*error = -1;
|
||||
}
|
||||
return SR_ERROR;
|
||||
}
|
||||
|
||||
void MultipartStream::Close() {
|
||||
for (size_t i = 0; i < parts_.size(); ++i) {
|
||||
delete parts_[i];
|
||||
}
|
||||
parts_.clear();
|
||||
adding_ = false;
|
||||
current_ = 0;
|
||||
position_ = 0;
|
||||
}
|
||||
|
||||
bool MultipartStream::SetPosition(size_t position) {
|
||||
if (adding_) {
|
||||
return false;
|
||||
}
|
||||
size_t part_size, part_offset = 0;
|
||||
for (size_t i = 0; i < parts_.size(); ++i) {
|
||||
if (!parts_[i]->GetSize(&part_size)) {
|
||||
return false;
|
||||
}
|
||||
if (part_offset + part_size > position) {
|
||||
for (size_t j = i + 1; j < std::min(parts_.size(), current_ + 1); ++j) {
|
||||
if (!parts_[j]->Rewind()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!parts_[i]->SetPosition(position - part_offset)) {
|
||||
return false;
|
||||
}
|
||||
current_ = i;
|
||||
position_ = position;
|
||||
return true;
|
||||
}
|
||||
part_offset += part_size;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MultipartStream::GetPosition(size_t* position) const {
|
||||
if (position) {
|
||||
*position = position_;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MultipartStream::GetSize(size_t* size) const {
|
||||
size_t part_size, total_size = 0;
|
||||
for (size_t i = 0; i < parts_.size(); ++i) {
|
||||
if (!parts_[i]->GetSize(&part_size)) {
|
||||
return false;
|
||||
}
|
||||
total_size += part_size;
|
||||
}
|
||||
if (size) {
|
||||
*size = total_size;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MultipartStream::GetAvailable(size_t* size) const {
|
||||
if (adding_) {
|
||||
return false;
|
||||
}
|
||||
size_t part_size, total_size = 0;
|
||||
for (size_t i = current_; i < parts_.size(); ++i) {
|
||||
if (!parts_[i]->GetAvailable(&part_size)) {
|
||||
return false;
|
||||
}
|
||||
total_size += part_size;
|
||||
}
|
||||
if (size) {
|
||||
*size = total_size;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// StreamInterface Slots
|
||||
//
|
||||
|
||||
void MultipartStream::OnEvent(StreamInterface* stream, int events, int error) {
|
||||
if (adding_ || (current_ >= parts_.size()) || (parts_[current_] != stream)) {
|
||||
return;
|
||||
}
|
||||
SignalEvent(this, events, error);
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
@ -1,84 +0,0 @@
|
||||
/*
|
||||
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef WEBRTC_BASE_MULTIPART_H__
|
||||
#define WEBRTC_BASE_MULTIPART_H__
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "webrtc/base/constructormagic.h"
|
||||
#include "webrtc/base/sigslot.h"
|
||||
#include "webrtc/base/stream.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// MultipartStream - Implements an RFC2046 multipart stream by concatenating
|
||||
// the supplied parts together, and adding the correct boundaries.
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class MultipartStream : public StreamInterface, public sigslot::has_slots<> {
|
||||
public:
|
||||
MultipartStream(const std::string& type, const std::string& boundary);
|
||||
~MultipartStream() override;
|
||||
|
||||
void GetContentType(std::string* content_type);
|
||||
|
||||
// Note: If content_disposition and/or content_type are the empty string,
|
||||
// they will be omitted.
|
||||
bool AddPart(StreamInterface* data_stream,
|
||||
const std::string& content_disposition,
|
||||
const std::string& content_type);
|
||||
bool AddPart(const std::string& data,
|
||||
const std::string& content_disposition,
|
||||
const std::string& content_type);
|
||||
void EndParts();
|
||||
|
||||
// Calculates the size of a part before actually adding the part.
|
||||
size_t GetPartSize(const std::string& data,
|
||||
const std::string& content_disposition,
|
||||
const std::string& content_type) const;
|
||||
size_t GetEndPartSize() const;
|
||||
|
||||
// StreamInterface
|
||||
StreamState GetState() const override;
|
||||
StreamResult Read(void* buffer,
|
||||
size_t buffer_len,
|
||||
size_t* read,
|
||||
int* error) override;
|
||||
StreamResult Write(const void* data,
|
||||
size_t data_len,
|
||||
size_t* written,
|
||||
int* error) override;
|
||||
void Close() override;
|
||||
bool SetPosition(size_t position) override;
|
||||
bool GetPosition(size_t* position) const override;
|
||||
bool GetSize(size_t* size) const override;
|
||||
bool GetAvailable(size_t* size) const override;
|
||||
|
||||
private:
|
||||
typedef std::vector<StreamInterface*> PartList;
|
||||
|
||||
// StreamInterface Slots
|
||||
void OnEvent(StreamInterface* stream, int events, int error);
|
||||
|
||||
std::string type_, boundary_;
|
||||
PartList parts_;
|
||||
bool adding_;
|
||||
size_t current_; // The index into parts_ of the current read position.
|
||||
size_t position_; // The current read position in bytes.
|
||||
|
||||
RTC_DISALLOW_COPY_AND_ASSIGN(MultipartStream);
|
||||
};
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
#endif // WEBRTC_BASE_MULTIPART_H__
|
||||
@ -1,125 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "webrtc/base/gunit.h"
|
||||
#include "webrtc/base/helpers.h"
|
||||
#include "webrtc/base/logging.h"
|
||||
#include "webrtc/base/pathutils.h"
|
||||
#include "webrtc/base/multipart.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
static const std::string kTestMultipartBoundary = "123456789987654321";
|
||||
static const std::string kTestContentType =
|
||||
"multipart/form-data; boundary=123456789987654321";
|
||||
static const char kTestData[] = "This is a test.";
|
||||
static const char kTestStreamContent[] = "This is a test stream.";
|
||||
|
||||
TEST(MultipartTest, TestBasicOperations) {
|
||||
MultipartStream multipart("multipart/form-data", kTestMultipartBoundary);
|
||||
std::string content_type;
|
||||
multipart.GetContentType(&content_type);
|
||||
EXPECT_EQ(kTestContentType, content_type);
|
||||
|
||||
EXPECT_EQ(rtc::SS_OPENING, multipart.GetState());
|
||||
|
||||
// The multipart stream contains only --boundary--\r\n
|
||||
size_t end_part_size = multipart.GetEndPartSize();
|
||||
multipart.EndParts();
|
||||
EXPECT_EQ(rtc::SS_OPEN, multipart.GetState());
|
||||
size_t size;
|
||||
EXPECT_TRUE(multipart.GetSize(&size));
|
||||
EXPECT_EQ(end_part_size, size);
|
||||
|
||||
// Write is not supported.
|
||||
EXPECT_EQ(rtc::SR_ERROR,
|
||||
multipart.Write(kTestData, sizeof(kTestData), NULL, NULL));
|
||||
|
||||
multipart.Close();
|
||||
EXPECT_EQ(rtc::SS_CLOSED, multipart.GetState());
|
||||
EXPECT_TRUE(multipart.GetSize(&size));
|
||||
EXPECT_EQ(0U, size);
|
||||
}
|
||||
|
||||
TEST(MultipartTest, TestAddAndRead) {
|
||||
MultipartStream multipart("multipart/form-data", kTestMultipartBoundary);
|
||||
|
||||
size_t part_size =
|
||||
multipart.GetPartSize(kTestData, "form-data; name=\"text\"", "text");
|
||||
EXPECT_TRUE(multipart.AddPart(kTestData, "form-data; name=\"text\"", "text"));
|
||||
size_t size;
|
||||
EXPECT_TRUE(multipart.GetSize(&size));
|
||||
EXPECT_EQ(part_size, size);
|
||||
|
||||
std::unique_ptr<rtc::MemoryStream> stream(
|
||||
new rtc::MemoryStream(kTestStreamContent));
|
||||
size_t stream_size = 0;
|
||||
EXPECT_TRUE(stream->GetSize(&stream_size));
|
||||
part_size +=
|
||||
multipart.GetPartSize("", "form-data; name=\"stream\"", "stream");
|
||||
part_size += stream_size;
|
||||
|
||||
EXPECT_TRUE(multipart.AddPart(
|
||||
new rtc::MemoryStream(kTestStreamContent),
|
||||
"form-data; name=\"stream\"",
|
||||
"stream"));
|
||||
EXPECT_TRUE(multipart.GetSize(&size));
|
||||
EXPECT_EQ(part_size, size);
|
||||
|
||||
// In adding state, block read.
|
||||
char buffer[1024];
|
||||
EXPECT_EQ(rtc::SR_BLOCK,
|
||||
multipart.Read(buffer, sizeof(buffer), NULL, NULL));
|
||||
// Write is not supported.
|
||||
EXPECT_EQ(rtc::SR_ERROR,
|
||||
multipart.Write(buffer, sizeof(buffer), NULL, NULL));
|
||||
|
||||
part_size += multipart.GetEndPartSize();
|
||||
multipart.EndParts();
|
||||
EXPECT_TRUE(multipart.GetSize(&size));
|
||||
EXPECT_EQ(part_size, size);
|
||||
|
||||
// Read the multipart stream into StringStream
|
||||
std::string str;
|
||||
rtc::StringStream str_stream(&str);
|
||||
EXPECT_EQ(rtc::SR_SUCCESS,
|
||||
Flow(&multipart, buffer, sizeof(buffer), &str_stream));
|
||||
EXPECT_EQ(size, str.length());
|
||||
|
||||
// Search three boundaries and two parts in the order.
|
||||
size_t pos = 0;
|
||||
pos = str.find(kTestMultipartBoundary);
|
||||
EXPECT_NE(std::string::npos, pos);
|
||||
pos += kTestMultipartBoundary.length();
|
||||
|
||||
pos = str.find(kTestData, pos);
|
||||
EXPECT_NE(std::string::npos, pos);
|
||||
pos += sizeof(kTestData);
|
||||
|
||||
pos = str.find(kTestMultipartBoundary, pos);
|
||||
EXPECT_NE(std::string::npos, pos);
|
||||
pos += kTestMultipartBoundary.length();
|
||||
|
||||
pos = str.find(kTestStreamContent, pos);
|
||||
EXPECT_NE(std::string::npos, pos);
|
||||
pos += sizeof(kTestStreamContent);
|
||||
|
||||
pos = str.find(kTestMultipartBoundary, pos);
|
||||
EXPECT_NE(std::string::npos, pos);
|
||||
pos += kTestMultipartBoundary.length();
|
||||
|
||||
pos = str.find(kTestMultipartBoundary, pos);
|
||||
EXPECT_EQ(std::string::npos, pos);
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
Loading…
x
Reference in New Issue
Block a user