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

Side by Side 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, 1 month 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 unified diff | Download patch
« no previous file with comments | « no previous file | util/net/http_body.cc » ('j') | util/net/http_body.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef CRASHPAD_UTIL_NET_HTTP_BODY_H_
16 #define CRASHPAD_UTIL_NET_HTTP_BODY_H_
17
18 #include <stdint.h>
19
20 #include <string>
21 #include <vector>
22
23 #include "base/basictypes.h"
24 #include "base/files/file_path.h"
25
26 namespace crashpad {
27
28 //! \brief An interface to a stream that can be used for an HTTP request body.
29 class HTTPBodyStream {
30 public:
31 virtual ~HTTPBodyStream() {}
32
33 //! \brief Copies up to \a max_len bytes into the user-supplied buffer.
34 //!
35 //! \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.
36 //! bytes from the stream.
37 //! \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.
38 //! bytes will be copied.
39 //!
40 //! \return On success, a positive number indicating the number of bytes
41 //! 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.
42 //! 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.
43 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.
44
45 //! \brief Returns true if the stream has more data available that can be
46 //! retrieved with GetBytesBuffer().
47 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
48
49 protected:
50 HTTPBodyStream() {}
51 };
52
53 //! \brief An implementation of HTTPBodyStream that turns a fixed string into
54 //! a stream.
55 class StringHTTPBodyStream : public HTTPBodyStream {
56 public:
57 //! \brief Creates a stream with the specified string.
58 //!
59 //! \param[in] string The string to turn into a stream.
60 explicit StringHTTPBodyStream(const std::string& string);
61 ~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.
62
63 // HTTPBodyStream:
64 ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
65 bool HasBytesRemaining() override;
66
67 private:
68 std::string string_;
69 size_t bytes_read_;
70
71 DISALLOW_COPY_AND_ASSIGN(StringHTTPBodyStream);
72 };
73
74 //! \brief An implementation of HTTPBodyStream that reads from the specified
75 //! file and provides its contents for an HTTP body.
76 class FileHTTPBodyStream : public HTTPBodyStream {
77 public:
78 //! \brief Creates a stream for reading the file at the specified \a path.
79 //!
80 //! \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.
81 explicit FileHTTPBodyStream(const base::FilePath& path);
82 ~FileHTTPBodyStream() override;
83
84 // HTTPBodyStream:
85 ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
86 bool HasBytesRemaining() override;
87
88 private:
89 base::FilePath path_;
90
91 // 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.
92 // set to false), or the file has been read completely (with |at_eof_| set
93 // to true). If |fd_| is greater than zero, then the file is open and is
94 // currently being read. If |fd_| is less than zero, then there was an error
95 // opening or reading the file.
96 int fd_;
97 bool at_eof_;
98
99 DISALLOW_COPY_AND_ASSIGN(FileHTTPBodyStream);
100 };
101
102 //! \brief An implementation of HTTPBodyStream that combines an array of
103 //! several other HTTPBodyStream objects into a single, unified stream.
104 class CompositeHTTPBodyStream : public HTTPBodyStream {
105 public:
106 using PartsList = std::vector<HTTPBodyStream*>;
107
108 //! \brief Creates a stream from an array of other stream parts.
109 //!
110 //! \param[in] parts A vector of HTTPBodyStream objects, of which this object
111 //! takes ownership, that will be represented as a single unified stream.
112 //! Callers should not mutate the string objects after passing them to
113 //! an instance of this class.
114 explicit CompositeHTTPBodyStream(const PartsList& parts);
115 ~CompositeHTTPBodyStream() override;
116
117 // HTTPBodyStream:
118 ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
119 bool HasBytesRemaining() override;
120
121 private:
122 PartsList parts_;
123 PartsList::iterator current_part_;
124
125 DISALLOW_COPY_AND_ASSIGN(CompositeHTTPBodyStream);
126 };
127
128 } // namespace crashpad
129
130 #endif // CRASHPAD_UTIL_NET_HTTP_BODY_H_
OLDNEW
« 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