Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Unified Diff: util/net/http_body.h

Issue 669153006: Add HTTPBodyStream interface, three concrete implementations, and their tests. (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « util/file/fd_io.cc ('k') | util/net/http_body.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: util/net/http_body.h
diff --git a/util/net/http_body.h b/util/net/http_body.h
new file mode 100644
index 0000000000000000000000000000000000000000..0347e636f1b103d6910d7f2c540cd5587e949977
--- /dev/null
+++ b/util/net/http_body.h
@@ -0,0 +1,130 @@
+// Copyright 2014 The Crashpad Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef CRASHPAD_UTIL_NET_HTTP_BODY_H_
+#define CRASHPAD_UTIL_NET_HTTP_BODY_H_
+
+#include <stdint.h>
+#include <sys/types.h>
+
+#include <string>
+#include <vector>
+
+#include "base/basictypes.h"
+#include "base/files/file_path.h"
+
+namespace crashpad {
+
+//! \brief An interface to a stream that can be used for an HTTP request body.
+class HTTPBodyStream {
+ public:
+ virtual ~HTTPBodyStream() {}
+
+ //! \brief Copies up to \a max_len bytes into the user-supplied buffer.
+ //!
+ //! \param[out] buffer A user-supplied buffer into which this method will copy
+ //! bytes from the stream.
+ //! \param[in] max_len The length (or size) of \a buffer. At most this many
+ //! bytes will be copied.
+ //!
+ //! \return On success, a positive number indicating the number of bytes
+ //! actually copied to \a buffer. On failure, a negative number. When
+ //! the stream has no more data, returns `0`.
+ virtual ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) = 0;
+
+ protected:
+ HTTPBodyStream() {}
+};
+
+//! \brief An implementation of HTTPBodyStream that turns a fixed string into
+//! a stream.
+class StringHTTPBodyStream : public HTTPBodyStream {
+ public:
+ //! \brief Creates a stream with the specified string.
+ //!
+ //! \param[in] string The string to turn into a stream.
+ explicit StringHTTPBodyStream(const std::string& string);
+
+ ~StringHTTPBodyStream() override;
+
+ // HTTPBodyStream:
+ ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
+
+ private:
+ std::string string_;
+ size_t bytes_read_;
+
+ DISALLOW_COPY_AND_ASSIGN(StringHTTPBodyStream);
+};
+
+//! \brief An implementation of HTTPBodyStream that reads from the specified
+//! file and provides its contents for an HTTP body.
+class FileHTTPBodyStream : public HTTPBodyStream {
+ public:
+ //! \brief Creates a stream for reading the file at the specified \a path.
+ //!
+ //! \param[in] path The file from which this HTTPBodyStream will read.
+ explicit FileHTTPBodyStream(const base::FilePath& path);
+
+ ~FileHTTPBodyStream() override;
+
+ // HTTPBodyStream:
+ ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
+
+ private:
+ enum InvalidFD {
+ kUnopenedFile = -1,
+ kFileOpenError = -2,
+ kClosedAtEOF = -3,
+ };
+
+ base::FilePath path_;
+
+ // If |fd_| is greater than or equal to zero, it is an opened descriptor
+ // from which an instance of this class is reading. If |fd_| is less than
+ // zero, the value corresponds to an InvalidFD value.
+ int fd_;
+
+ DISALLOW_COPY_AND_ASSIGN(FileHTTPBodyStream);
+};
+
+//! \brief An implementation of HTTPBodyStream that combines an array of
+//! several other HTTPBodyStream objects into a single, unified stream.
+class CompositeHTTPBodyStream : public HTTPBodyStream {
+ public:
+ using PartsList = std::vector<HTTPBodyStream*>;
+
+ //! \brief Creates a stream from an array of other stream parts.
+ //!
+ //! \param[in] parts A vector of HTTPBodyStream objects, of which this object
+ //! takes ownership, that will be represented as a single unified stream.
+ //! Callers should not mutate the stream objects after passing them to
+ //! an instance of this class.
+ explicit CompositeHTTPBodyStream(const PartsList& parts);
+
+ ~CompositeHTTPBodyStream() override;
+
+ // HTTPBodyStream:
+ ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
+
+ private:
+ PartsList parts_;
+ PartsList::iterator current_part_;
+
+ DISALLOW_COPY_AND_ASSIGN(CompositeHTTPBodyStream);
+};
+
+} // namespace crashpad
+
+#endif // CRASHPAD_UTIL_NET_HTTP_BODY_H_
« no previous file with comments | « util/file/fd_io.cc ('k') | util/net/http_body.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698