DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
HttpContent.cpp
Go to the documentation of this file.
1#include "pch.h"
6#include <cstring>
7#include <vector>
8
9namespace DotNetDupe {
10 namespace System {
11 namespace Net {
12 namespace Http {
13
16 if (stream.IsNull()) {
17 throw ArgumentNullException("stream");
18 }
21 if (data.GetLength() > 0) {
22 stream->Write(data.GetData(), 0, data.GetLength());
23 }
24 }
25
28 return -1;
29 }
30
31 // --- StringContent ---
32
34 : m_sContent(content) {
36 GetHeaders().Add("Content-Type", "text/plain; charset=utf-8");
37 }
38
39 StringContent::StringContent(const String& content, const String& mediaType)
40 : m_sContent(content) {
42 String contentType = mediaType;
43 if (!contentType.Contains("charset")) {
44 contentType = contentType + "; charset=utf-8";
45 }
46 GetHeaders().Add("Content-Type", contentType);
47 }
48
51 return m_sContent;
52 }
53
56 int len = m_sContent.GetLength();
57 if (len <= 0) return Array<char>(0);
58 return Array<char>(m_sContent.GetRawString(), len);
59 }
60
65
68 if (stream.IsNull()) {
69 throw ArgumentNullException("stream");
70 }
72 int len = m_sContent.GetLength();
73 if (len > 0) {
74 stream->Write(m_sContent.GetRawString(), 0, len);
75 }
76 }
77
80 return m_sContent.GetLength();
81 }
82
83 // --- ByteArrayContent ---
84
86 : m_arrContent(content), m_iOffset(0), m_iCount(content.GetLength()) {
88 GetHeaders().Add("Content-Type", "application/octet-stream");
89 }
90
91 ByteArrayContent::ByteArrayContent(const Array<char>& content, int offset, int count)
92 : m_arrContent(content), m_iOffset(offset), m_iCount(count) {
94 if (offset < 0 || count < 0 || offset + count > content.GetLength()) {
95 throw ArgumentOutOfRangeException("Offset or count is out of range.");
96 }
97 GetHeaders().Add("Content-Type", "application/octet-stream");
98 }
99
102 String s;
103 if (m_iCount > 0) {
104 std::string temp(m_arrContent.GetData() + m_iOffset, m_iCount);
105 s = String(temp.c_str());
106 }
107 return s;
108 }
109
112 if (m_iOffset == 0 && m_iCount == m_arrContent.GetLength()) {
113 return m_arrContent;
114 }
115 Array<char> arr(m_iCount);
116 if (m_iCount > 0) {
117 std::memcpy(arr.GetData(), m_arrContent.GetData() + m_iOffset, m_iCount);
118 }
119 return arr;
120 }
121
126
129 if (stream.IsNull()) {
130 throw ArgumentNullException("stream");
131 }
133 if (m_iCount > 0) {
134 stream->Write(m_arrContent.GetData(), m_iOffset, m_iCount);
135 }
136 }
137
140 return m_iCount;
141 }
142
143 // --- StreamContent ---
144
146 : m_pStream(stream) {
148 if (stream.IsNull()) {
149 throw ArgumentNullException("stream");
150 }
151 GetHeaders().Add("Content-Type", "application/octet-stream");
152 }
153
157 String s;
158 if (bytes.GetLength() > 0) {
159 std::string temp(bytes.GetData(), bytes.GetLength());
160 s = String(temp.c_str());
161 }
162 return s;
163 }
164
167 if (m_pStream.IsNull()) return Array<char>(0);
169 std::vector<char> vecData;
170 char buf[4096];
171 int iRead = 0;
172 while ((iRead = m_pStream->Read(buf, 0, sizeof(buf))) > 0) {
173 vecData.insert(vecData.end(), buf, buf + iRead);
174 }
175 Array<char> arr(static_cast<int>(vecData.size()));
176 if (!vecData.empty()) {
177 std::memcpy(arr.GetData(), vecData.data(), vecData.size());
178 }
179 return arr;
180 }
181
186
189 if (stream.IsNull()) {
190 throw ArgumentNullException("stream");
191 }
192 if (m_pStream.IsNull()) return;
194 char buf[4096];
195 int iRead = 0;
196 while ((iRead = m_pStream->Read(buf, 0, sizeof(buf))) > 0) {
197 stream->Write(buf, 0, iRead);
198 }
199 }
200
203 if (!m_pStream.IsNull() && m_pStream->CanSeek()) {
204 try {
205 return m_pStream->GetLength();
206 } catch (const Exception&) {
208 return -1;
209 }
210 }
211 return -1;
212 }
213
214 }
215 }
216 }
217}
Defines the exception thrown when a null reference is passed to a method that does not accept it.
Defines the exception thrown when an argument value is outside the acceptable range of values.
Represents an HTTP entity body and associated content headers.
ArgumentNullException(const String &sMessage)
Initializes a new instance of the ArgumentNullException class with a specified error message.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
Definition Array.h:142
T * GetData()
Gets a pointer to the contiguous internal element buffer.
Definition Array.h:146
Represents errors that occur during application execution.
Definition Exception.h:19
Creates a stream whose backing store is memory.
void CopyTo(const SmartPointer< IO::Stream > &stream) override
Copies the byte buffer into the target stream.
long GetLength() const override
Returns the byte count of the buffer slice.
String ReadAsString() override
Reads the byte buffer decoded as a string.
SmartPointer< IO::Stream > ReadAsStream() override
Reads the content as a MemoryStream.
ByteArrayContent(const Array< char > &content)
Initializes a new instance of ByteArrayContent with full byte buffer.
Array< char > ReadAsByteArray() override
Reads the content byte buffer.
virtual void CopyTo(const SmartPointer< IO::Stream > &stream)
Serializes the HTTP content to a stream.
virtual long GetLength() const
Gets the length in bytes of the HHTP content, or -1 if indeterminate.
virtual Array< char > ReadAsByteArray()=0
Serializes the HTTP content to a byte array.
Collections::Generic::Dictionary< String, String > & GetHeaders()
Gets the HTTP content headers defined in RFC 9110.
Definition HttpContent.h:51
StreamContent(const SmartPointer< IO::Stream > &stream)
Initializes a new instance of StreamContent wrapping an existing Stream.
SmartPointer< IO::Stream > ReadAsStream() override
Returns the underlying stream.
Array< char > ReadAsByteArray() override
Reads the remaining bytes in the stream into a byte array.
long GetLength() const override
Gets the length of the underlying stream if seekable, or -1.
void CopyTo(const SmartPointer< IO::Stream > &stream) override
Copies the underlying stream into the destination stream.
String ReadAsString() override
Reads the remaining bytes in the stream into a string.
Array< char > ReadAsByteArray() override
Reads the content payload as a byte array.
SmartPointer< IO::Stream > ReadAsStream() override
Reads the content payload as a readable MemoryStream.
String ReadAsString() override
Reads the content payload as a string.
void CopyTo(const SmartPointer< IO::Stream > &stream) override
Writes the content string directly to the destination stream.
long GetLength() const override
Gets the length in bytes of the UTF-8 content string.
StringContent(const String &content)
Creates a new instance of StringContent with text/plain default media type.
A unified smart pointer that supports both unique and shared ownership semantics.
bool IsNull() const noexcept
Checks if the SmartPointer is null.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
bool Contains(char ch) const
Definition String.cpp:292