userver: userver/server/handlers/http_handler_static.hpp Source File
Loading...
Searching...
No Matches
http_handler_static.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/http_handler_static.hpp
4/// @brief @copybrief server::handlers::HttpHandlerStatic
5
6#include <userver/components/fs_cache.hpp>
7#include <userver/dynamic_config/source.hpp>
8#include <userver/engine/task/task_processor_fwd.hpp>
9#include <userver/fs/fs_cache_client.hpp>
10#include <userver/server/handlers/http_handler_base.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace server::handlers {
15
16/// @ingroup userver_components userver_http_handlers
17///
18/// @brief Handler that returns HTTP 200 if file exist and returns file data with mapped content/type.
19///
20/// Path arguments of this handle are passed to `fs-cache-component` to get the file. In other words, for the following
21/// `fs-cache-main`:
22/// @code{.yaml}
23/// fs-cache-main:
24/// dir: /fs-cache-main-path/ # Path to the directory with files
25/// @endcode
26/// the `handler-static` with `path: /handler-static-path/*` on request to `/handler-static-path/some/file.html`
27/// would return file at path `/fs-cache-main-path/some/file.html`.
28///
29/// Files larger than @ref components::FsCache `max-size-to-cache` are streamed from disk.
30///
31/// ## HttpHandlerStatic Dynamic config
32/// * @ref USERVER_FILES_CONTENT_TYPE_MAP
33///
34/// \ref userver_http_handlers "Userver HTTP Handlers".
35///
36/// ## Static options of server::handlers::HttpHandlerStatic :
37/// @include{doc} scripts/docs/en/components_schema/core/src/server/handlers/http_handler_static.md
38///
39/// Options inherited from @ref server::handlers::HttpHandlerBase :
40/// @include{doc} scripts/docs/en/components_schema/core/src/server/handlers/http_handler_base.md
41///
42/// Options inherited from @ref server::handlers::HandlerBase :
43/// @include{doc} scripts/docs/en/components_schema/core/src/server/handlers/handler_base.md
44///
45/// Options inherited from @ref components::ComponentBase :
46/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
47///
48/// ## Example usage:
49///
50/// @snippet samples/static_service/main.cpp Static service sample - main
51class HttpHandlerStatic final : public HttpHandlerBase {
52public:
53 /// @ingroup userver_component_names
54 /// @brief The default name of server::handlers::HttpHandlerStatic
55 static constexpr std::string_view kName = "handler-static";
56
57 using HttpHandlerBase::HttpHandlerBase;
58
59 HttpHandlerStatic(const components::ComponentConfig& config, const components::ComponentContext& context);
60
61 void HandleMaybeStreamRequest(http::HttpRequest& request, request::RequestContext& context) const override;
62
63 static yaml_config::Schema GetStaticConfigSchema();
64
65private:
66 struct ResolvedFile {
67 fs::FileInfoWithDataConstPtr file;
68 bool is_not_found{false};
69 };
70
71 ResolvedFile ResolveFile(const http::HttpRequest& request) const;
72
73 dynamic_config::Source config_;
74 const fs::FsCacheClient& storage_;
75 const std::chrono::seconds cache_age_;
76 const std::string directory_file_;
77 const std::string not_found_file_;
78 engine::TaskProcessor& fs_task_processor_;
79};
80
81} // namespace server::handlers
82
83USERVER_NAMESPACE_END