Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
+124
View File
@@ -0,0 +1,124 @@
// 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/files/file_path.h"
#include "base/macros.h"
#include "util/file/file_io.h"
#include "util/file/file_reader.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 FileOperationResult 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:
FileOperationResult 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 a
//! FileReaderInterface and provides its contents for an HTTP body.
class FileReaderHTTPBodyStream : public HTTPBodyStream {
public:
//! \brief Creates a stream for reading from a FileReaderInterface.
//!
//! \param[in] reader A FileReaderInterface from which this HTTPBodyStream
//! will read.
explicit FileReaderHTTPBodyStream(FileReaderInterface* reader);
~FileReaderHTTPBodyStream() override;
// HTTPBodyStream:
FileOperationResult GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
private:
FileReaderInterface* reader_; // weak
bool reached_eof_;
DISALLOW_COPY_AND_ASSIGN(FileReaderHTTPBodyStream);
};
//! \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:
FileOperationResult 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_
@@ -0,0 +1,67 @@
// Copyright 2017 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_GZIP_H_
#define CRASHPAD_UTIL_NET_HTTP_BODY_GZIP_H_
#include <stdint.h>
#include <sys/types.h>
#include <memory>
#include "base/macros.h"
#include "util/file/file_io.h"
#include "util/net/http_body.h"
extern "C" {
typedef struct z_stream_s z_stream;
} // extern "C"
namespace crashpad {
//! \brief An implementation of HTTPBodyStream that `gzip`-compresses another
//! HTTPBodyStream.
class GzipHTTPBodyStream : public HTTPBodyStream {
public:
explicit GzipHTTPBodyStream(std::unique_ptr<HTTPBodyStream> source);
~GzipHTTPBodyStream() override;
// HTTPBodyStream:
FileOperationResult GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
private:
enum State : int {
kUninitialized,
kOperating,
kInputEOF,
kFinished,
kError,
};
// Calls deflateEnd() and transitions state_ to state. If deflateEnd() fails,
// logs a message and transitions state_ to State::kError.
void Done(State state);
uint8_t input_[4096];
std::unique_ptr<HTTPBodyStream> source_;
std::unique_ptr<z_stream> z_stream_;
State state_;
DISALLOW_COPY_AND_ASSIGN(GzipHTTPBodyStream);
};
} // namespace crashpad
#endif // CRASHPAD_UTIL_NET_HTTP_BODY_GZIP_H_
@@ -0,0 +1,49 @@
// 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_TEST_UTIL_H_
#define CRASHPAD_UTIL_NET_HTTP_BODY_TEST_UTIL_H_
#include <sys/types.h>
#include <string>
namespace crashpad {
class HTTPBodyStream;
namespace test {
//! \brief Reads a HTTPBodyStream to a string. If an error occurs, adds a
//! test failure and returns an empty string.
//!
//! \param[in] stream The stream from which to read.
//!
//! \return The contents of the stream, or an empty string on failure.
std::string ReadStreamToString(HTTPBodyStream* stream);
//! \brief Reads a HTTPBodyStream to a string. If an error occurs, adds a
//! test failure and returns an empty string.
//!
//! \param[in] stream The stream from which to read.
//! \param[in] buffer_size The size of the buffer to use when reading from the
//! stream.
//!
//! \return The contents of the stream, or an empty string on failure.
std::string ReadStreamToString(HTTPBodyStream* stream, size_t buffer_size);
} // namespace test
} // namespace crashpad
#endif // CRASHPAD_UTIL_NET_HTTP_BODY_TEST_UTIL_H_
@@ -0,0 +1,37 @@
// 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_HEADERS_H_
#define CRASHPAD_UTIL_NET_HTTP_HEADERS_H_
#include <map>
#include <string>
namespace crashpad {
//! \brief A map of HTTP header fields to their values.
using HTTPHeaders = std::map<std::string, std::string>;
//! \brief The header name `"Content-Type"`.
constexpr char kContentType[] = "Content-Type";
//! \brief The header name `"Content-Length"`.
constexpr char kContentLength[] = "Content-Length";
//! \brief The header name `"Content-Encoding"`.
constexpr char kContentEncoding[] = "Content-Encoding";
} // namespace crashpad
#endif // CRASHPAD_UTIL_NET_HTTP_HEADERS_H_
@@ -0,0 +1,104 @@
// 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_MULTIPART_BUILDER_H_
#define CRASHPAD_UTIL_NET_HTTP_MULTIPART_BUILDER_H_
#include <map>
#include <memory>
#include <string>
#include "base/macros.h"
#include "util/file/file_reader.h"
#include "util/net/http_headers.h"
namespace crashpad {
class HTTPBodyStream;
//! \brief This class is used to build a MIME multipart message, conforming to
//! RFC 2046, for use as a HTTP request body.
class HTTPMultipartBuilder {
public:
HTTPMultipartBuilder();
~HTTPMultipartBuilder();
//! \brief Enables or disables `gzip` compression.
//!
//! \param[in] gzip_enabled Whether to enable or disable `gzip` compression.
//!
//! When `gzip` compression is enabled, the body stream returned by
//! GetBodyStream() will be `gzip`-compressed, and the content headers set by
//! PopulateContentHeaders() will contain `Content-Encoding: gzip`.
void SetGzipEnabled(bool gzip_enabled);
//! \brief Sets a `Content-Disposition: form-data` key-value pair.
//!
//! \param[in] key The key of the form data, specified as the `name` in the
//! multipart message. Any data previously set on this class with this
//! key will be overwritten.
//! \param[in] value The value to set at the \a key.
void SetFormData(const std::string& key, const std::string& value);
//! \brief Specifies the contents read from \a reader to be uploaded as
//! multipart data, available at `name` of \a upload_file_name.
//!
//! \param[in] key The key of the form data, specified as the `name` in the
//! multipart message. Any data previously set on this class with this
//! key will be overwritten.
//! \param[in] upload_file_name The `filename` to specify for this multipart
//! data attachment.
//! \param[in] reader A FileReaderInterface from which to read the content to
//! upload.
//! \param[in] content_type The `Content-Type` to specify for the attachment.
//! If this is empty, `"application/octet-stream"` will be used.
void SetFileAttachment(const std::string& key,
const std::string& upload_file_name,
FileReaderInterface* reader,
const std::string& content_type);
//! \brief Generates the HTTPBodyStream for the data currently supplied to
//! the builder.
//!
//! \return A caller-owned HTTPBodyStream object.
std::unique_ptr<HTTPBodyStream> GetBodyStream();
//! \brief Adds the appropriate content headers to \a http_headers.
//!
//! Any headers that this method adds will replace existing headers by the
//! same name in \a http_headers.
void PopulateContentHeaders(HTTPHeaders* http_headers) const;
private:
struct FileAttachment {
std::string filename;
std::string content_type;
FileReaderInterface* reader;
};
// Removes elements from both data maps at the specified |key|, to ensure
// uniqueness across the entire HTTP body.
void EraseKey(const std::string& key);
std::string boundary_;
std::map<std::string, std::string> form_data_;
std::map<std::string, FileAttachment> file_attachments_;
bool gzip_enabled_;
DISALLOW_COPY_AND_ASSIGN(HTTPMultipartBuilder);
};
} // namespace crashpad
#endif // CRASHPAD_UTIL_NET_HTTP_MULTIPART_BUILDER_H_
@@ -0,0 +1,106 @@
// 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_TRANSPORT_H_
#define CRASHPAD_UTIL_NET_HTTP_TRANSPORT_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "util/net/http_headers.h"
namespace crashpad {
class HTTPBodyStream;
//! \brief HTTPTransport executes a HTTP request using the specified URL, HTTP
//! method, headers, and body. This class can only issue a synchronous
//! HTTP request.
//!
//! This class cannot be instantiated directly. A concrete subclass must be
//! instantiated instead, which provides an implementation to execute the
//! request that is appropriate for the host operating system.
class HTTPTransport {
public:
virtual ~HTTPTransport();
//! \brief Instantiates a concrete HTTPTransport class for the current
//! operating system.
//!
//! \return A new caller-owned HTTPTransport object.
static std::unique_ptr<HTTPTransport> Create();
//! \brief Sets URL to which the request will be made.
//!
//! \param[in] url The request URL.
void SetURL(const std::string& url);
//! \brief Sets the HTTP method to execute. E.g., GET, POST, etc. The default
//! method is `"POST"`.
//!
//! \param[in] http_method The HTTP method.
void SetMethod(const std::string& http_method);
//! \brief Sets a HTTP header-value pair.
//!
//! \param[in] header The HTTP header name. Any previous value set at this
//! name will be overwritten.
//! \param[in] value The value to set for the header.
void SetHeader(const std::string& header, const std::string& value);
//! \brief Sets the stream object from which to generate the HTTP body.
//!
//! \param[in] stream A HTTPBodyStream, of which this class will take
//! ownership.
void SetBodyStream(std::unique_ptr<HTTPBodyStream> stream);
//! \brief Sets the timeout for the HTTP request. The default is 15 seconds.
//!
//! \param[in] timeout The request timeout, in seconds.
void SetTimeout(double timeout);
//! \brief Performs the HTTP request with the configured parameters and waits
//! for the execution to complete.
//!
//! \param[out] response_body On success, this will be set to the HTTP
//! response body. This parameter is optional and may be set to `nullptr`
//! if the response body is not required.
//!
//! \return Whether or not the request was successful, defined as returning
//! a HTTP status 200 (OK) code.
virtual bool ExecuteSynchronously(std::string* response_body) = 0;
protected:
HTTPTransport();
const std::string& url() const { return url_; }
const std::string& method() const { return method_; }
const HTTPHeaders& headers() const { return headers_; }
HTTPBodyStream* body_stream() const { return body_stream_.get(); }
double timeout() const { return timeout_; }
private:
std::string url_;
std::string method_;
HTTPHeaders headers_;
std::unique_ptr<HTTPBodyStream> body_stream_;
double timeout_;
DISALLOW_COPY_AND_ASSIGN(HTTPTransport);
};
} // namespace crashpad
#endif // CRASHPAD_UTIL_NET_HTTP_TRANSPORT_H_
+31
View File
@@ -0,0 +1,31 @@
// Copyright 2017 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_URL_H_
#define CRASHPAD_UTIL_NET_URL_H_
#include <string>
namespace crashpad {
//! \brief Performs percent-encoding (URL encoding) on the input string,
//! following RFC 3986 paragraph 2.
//!
//! \param[in] url The string to be encoded.
//! \return The encoded string.
std::string URLEncode(const std::string& url);
} // namespace crashpad
#endif // CRASHPAD_UTIL_NET_URL_H_