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

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 | « no previous file | util/net/http_body.cc » ('j') | util/net/http_body.cc » ('J')
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..11717b9fb8ceefa492acc7049a2fb50acb3f049c
--- /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 <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[in] buffer A user supplied buffer into which this method will copy
Mark Mentovai 2014/10/23 19:31:03 \param[out]
Mark Mentovai 2014/10/23 19:31:03 user-supplied (hyphen).
Robert Sesek 2014/10/24 16:53:32 Done.
Robert Sesek 2014/10/24 16:53:33 Done.
+ //! bytes from the stream.
+ //! \param[in] max_len The maximum length of \a buffer. At most this many
Mark Mentovai 2014/10/23 19:31:04 It’s the length (or size) of buffer, or the maximu
Robert Sesek 2014/10/24 16:53:33 Done.
+ //! 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
Mark Mentovai 2014/10/23 19:31:03 failure, (comma).
Mark Mentovai 2014/10/23 19:31:03 Should the caller stop calling if this returns a f
Robert Sesek 2014/10/24 16:53:32 Done.
Robert Sesek 2014/10/24 16:53:33 Done.
+ //! the stream has no more data, returns 0.
Mark Mentovai 2014/10/23 19:31:03 I’ve been using `0` for things that are literal li
Robert Sesek 2014/10/24 16:53:33 Done.
+ virtual ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) = 0;
Mark Mentovai 2014/10/23 19:31:03 ssize_t is not guaranteed in <stdint.h>. #include
Robert Sesek 2014/10/24 16:53:33 Done.
+
+ //! \brief Returns true if the stream has more data available that can be
+ //! retrieved with GetBytesBuffer().
+ virtual bool HasBytesRemaining() = 0;
Mark Mentovai 2014/10/23 19:31:03 Is this even necessary? It seems like all the call
Robert Sesek 2014/10/24 16:53:33 No, it's not. I didn't read NSInputStream carefull
+
+ 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;
Mark Mentovai 2014/10/23 19:31:03 Blank line before this so it doesn’t look like the
Robert Sesek 2014/10/24 16:53:33 Done.
+
+ // HTTPBodyStream:
+ ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
+ bool HasBytesRemaining() 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 this HTTPBodyStream will read.
Mark Mentovai 2014/10/23 19:31:03 Missing word (“which”?)
Robert Sesek 2014/10/24 16:53:33 Done.
+ explicit FileHTTPBodyStream(const base::FilePath& path);
+ ~FileHTTPBodyStream() override;
+
+ // HTTPBodyStream:
+ ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
+ bool HasBytesRemaining() override;
+
+ private:
+ base::FilePath path_;
+
+ // If |fd_| is zero, then the file has not yet been opened (with |at_eof_|
Mark Mentovai 2014/10/23 19:31:03 This is dangerous, because 0 is a valid file descr
Robert Sesek 2014/10/24 16:53:33 Good point. Done, latter suggestion.
+ // set to false), or the file has been read completely (with |at_eof_| set
+ // to true). If |fd_| is greater than zero, then the file is open and is
+ // currently being read. If |fd_| is less than zero, then there was an error
+ // opening or reading the file.
+ int fd_;
+ bool at_eof_;
+
+ 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 string 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;
+ bool HasBytesRemaining() 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 | « no previous file | util/net/http_body.cc » ('j') | util/net/http_body.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698