|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 HERE Europe B.V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <string> |
| 23 | +#include <memory> |
| 24 | + |
| 25 | +#include <olp/core/CoreApi.h> |
| 26 | +#include <olp/core/http/Network.h> |
| 27 | + |
| 28 | +namespace olp { |
| 29 | +namespace http { |
| 30 | + |
| 31 | +/** |
| 32 | + * @class HarCaptureAdapter |
| 33 | + * @brief A network adapter that captures HTTP requests and responses, |
| 34 | + * generating a HAR (HTTP Archive) file. |
| 35 | + * |
| 36 | + * HarCaptureAdapter implements the olp::http::Network interface, intercepting |
| 37 | + * network traffic for debugging, logging, and analysis. It records request |
| 38 | + * metadata, headers, and response details, allowing developers to |
| 39 | + * inspect network interactions in HAR format. |
| 40 | + * |
| 41 | + * @note Request timings are only available when Curl is used. |
| 42 | + * @note The HAR file is produced when the instance is destroyed. |
| 43 | + * |
| 44 | + * Features: |
| 45 | + * - Captures HTTP requests and responses. |
| 46 | + * - Logs request/response details, including headers, status codes and timings. |
| 47 | + * - Generates a HAR file for easy debugging and sharing. |
| 48 | + * |
| 49 | + * Example Usage: |
| 50 | + * @code |
| 51 | + * auto network = std::make_shared<HarCaptureAdapter>(network, "/tmp/out.har"); |
| 52 | + * @endcode |
| 53 | + */ |
| 54 | +class CORE_API HarCaptureAdapter final : public Network { |
| 55 | + public: |
| 56 | + /** |
| 57 | + * @brief Constructs a HarCaptureAdapter instance. |
| 58 | + * |
| 59 | + * @param network The underlying network implementation to forward requests |
| 60 | + * to. |
| 61 | + * @param har_out_path The file path where the HAR (HTTP Archive) file will be |
| 62 | + * saved. |
| 63 | + */ |
| 64 | + HarCaptureAdapter(std::shared_ptr<Network> network, std::string har_out_path); |
| 65 | + |
| 66 | + ~HarCaptureAdapter() override; |
| 67 | + |
| 68 | + /** |
| 69 | + * @copydoc Network::Send |
| 70 | + */ |
| 71 | + SendOutcome Send(NetworkRequest request, Payload payload, Callback callback, |
| 72 | + HeaderCallback header_callback, |
| 73 | + DataCallback data_callback) override; |
| 74 | + |
| 75 | + /** |
| 76 | + * @copydoc Network::Cancel |
| 77 | + */ |
| 78 | + void Cancel(RequestId id) override; |
| 79 | + |
| 80 | + private: |
| 81 | + class HarCaptureAdapterImpl; |
| 82 | + std::shared_ptr<HarCaptureAdapterImpl> impl_; |
| 83 | +}; |
| 84 | + |
| 85 | +} // namespace http |
| 86 | +} // namespace olp |
0 commit comments