PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
document_source.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11
12#include "common/http_client.h"
15#include "common/wss_signer.h"
16
18
19#include <pugixml.hpp>
20
21#include <chrono>
22#include <cstdint>
23#include <random>
24#include <sstream>
25#include <string>
26#include <utility>
27
28namespace kcenon::pacs::ihe::xds {
29
30namespace {
31
32std::string make_cid(std::size_t index) {
33 static thread_local std::mt19937_64 rng(
34 static_cast<std::uint64_t>(
35 std::chrono::steady_clock::now().time_since_epoch().count()));
36 std::uniform_int_distribution<std::uint64_t> dist;
37 std::ostringstream oss;
38 oss << "doc-" << index << '-' << std::hex << dist(rng)
39 << "@kcenon.pacs";
40 return oss.str();
41}
42
43} // namespace
44
46public:
48 : opts_(std::move(opts)), signing_(std::move(signing)) {}
49
51 std::shared_ptr<kcenon::pacs::security::xds_audit_sink> sink) {
52 audit_sink_ = std::move(sink);
53 }
54
56 // Collect document unique ids up-front so an audit event can
57 // reference them even when submission fails before the wire
58 // payload is built.
59 std::vector<std::string> document_uids;
60 document_uids.reserve(s.documents.size());
61 for (const auto& d : s.documents) {
62 document_uids.push_back(d.unique_id);
63 }
64
65 auto do_audit = [&](const kcenon::common::Result<submit_response>& r) {
66 audit(s, document_uids, r);
67 return r;
68 };
69
70 std::vector<std::string> cids;
71 cids.reserve(s.documents.size());
72 for (std::size_t i = 0; i < s.documents.size(); ++i) {
73 cids.push_back(make_cid(i));
74 }
75
76 auto env_result = detail::build_iti41_envelope(s, cids);
77 if (env_result.is_err()) {
78 return do_audit(kcenon::common::make_error<submit_response>(
79 env_result.error()));
80 }
81 auto env = env_result.value();
82
83 auto sign_result = detail::sign_envelope(
86 if (sign_result.is_err()) {
87 return do_audit(kcenon::common::make_error<submit_response>(
88 sign_result.error()));
89 }
90
91 std::vector<detail::mtom_part> parts;
92 parts.reserve(s.documents.size());
93 for (std::size_t i = 0; i < s.documents.size(); ++i) {
95 p.content_id = cids[i];
96 p.mime_type = s.documents[i].mime_type;
97 p.content = s.documents[i].content;
98 parts.push_back(std::move(p));
99 }
100
101 auto mtom_result = detail::package_mtom(env.xml, parts);
102 if (mtom_result.is_err()) {
103 return do_audit(kcenon::common::make_error<submit_response>(
104 mtom_result.error()));
105 }
106 auto mtom = mtom_result.value();
107
108 auto http_result =
109 detail::http_post(opts_, mtom.content_type, mtom.body);
110 if (http_result.is_err()) {
111 return do_audit(kcenon::common::make_error<submit_response>(
112 http_result.error()));
113 }
114 auto response = http_result.value();
115
116 return do_audit(parse_registry_response(response.body,
118 }
119
120private:
121 // Belt-and-suspenders cap. The HTTP layer already enforces 8 MiB via
122 // the write callback, so any response reaching us here is already
123 // bounded. This defensive check survives future refactors that might
124 // add a non-libcurl transport path without a size cap.
125 static constexpr std::size_t kMaxRegistryResponseBytes =
126 8 * 1024 * 1024;
127
129 const std::string& body, const std::string& submission_set_uid) {
130 if (body.size() > kMaxRegistryResponseBytes) {
131 return kcenon::common::make_error<submit_response>(
133 "RegistryResponse exceeds 8 MiB cap (" +
134 std::to_string(body.size()) + " bytes) - rejected to "
135 "bound pugixml memory footprint",
136 std::string(error_source));
137 }
138 pugi::xml_document doc;
139 auto parse = doc.load_buffer(body.data(), body.size(),
140 pugi::parse_default, pugi::encoding_utf8);
141 if (!parse) {
142 return kcenon::common::make_error<submit_response>(
144 std::string("failed to parse RegistryResponse: ") +
145 parse.description(),
146 std::string(error_source));
147 }
148
149 auto registry_response =
150 doc.select_node(
151 "//*[local-name()='RegistryResponse' or "
152 "local-name()='RegistryResponseType']")
153 .node();
154 if (!registry_response) {
155 return kcenon::common::make_error<submit_response>(
157 "response contains no RegistryResponse element",
158 std::string(error_source));
159 }
160
161 const std::string status =
162 registry_response.attribute("status").as_string();
163 if (status.empty()) {
164 return kcenon::common::make_error<submit_response>(
166 "RegistryResponse is missing status attribute",
167 std::string(error_source));
168 }
169
170 static constexpr std::string_view success_status =
171 "urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Success";
172 static constexpr std::string_view partial_success_status =
173 "urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:PartialSuccess";
174
176 out.registry_response_status = status;
177 out.submission_set_unique_id = submission_set_uid;
178
179 if (status == success_status) {
180 return out;
181 }
182 if (status == partial_success_status) {
183 return kcenon::common::make_error<submit_response>(
184 static_cast<int>(error_code::registry_partial_success),
185 "registry reported PartialSuccess: " + status,
186 std::string(error_source));
187 }
188 return kcenon::common::make_error<submit_response>(
190 "registry reported non-success status: " + status,
191 std::string(error_source));
192 }
193
194 void audit(const submission_set& s,
195 const std::vector<std::string>& document_uids,
197 if (!audit_sink_) return;
198 kcenon::pacs::security::xds_iti41_event e;
199 e.source_id = s.source_id;
200 e.destination_endpoint = opts_.endpoint;
201 e.patient_id = s.patient_id;
202 e.submission_set_unique_id = s.submission_set_unique_id;
203 e.document_unique_ids = document_uids;
204 if (r.is_ok()) {
206 } else {
207 e.outcome =
209 e.failure_description = r.error().message;
210 }
211 audit_sink_->on_iti41_submit(e);
212 }
213
216 std::shared_ptr<kcenon::pacs::security::xds_audit_sink> audit_sink_;
217};
218
220 : impl_(std::make_unique<impl>(std::move(opts), std::move(signing))) {}
221
223document_source& document_source::operator=(document_source&&) noexcept = default;
224
225document_source::~document_source() = default;
226
228 const submission_set& s) {
229 return impl_->submit(s);
230}
231
233 std::shared_ptr<kcenon::pacs::security::xds_audit_sink> sink) {
234 impl_->set_audit_sink(std::move(sink));
235}
236
237} // namespace kcenon::pacs::ihe::xds
void set_audit_sink(std::shared_ptr< kcenon::pacs::security::xds_audit_sink > sink)
impl(http_options opts, signing_material signing)
std::shared_ptr< kcenon::pacs::security::xds_audit_sink > audit_sink_
kcenon::common::Result< submit_response > parse_registry_response(const std::string &body, const std::string &submission_set_uid)
static constexpr std::size_t kMaxRegistryResponseBytes
void audit(const submission_set &s, const std::vector< std::string > &document_uids, const kcenon::common::Result< submit_response > &r)
kcenon::common::Result< submit_response > submit(const submission_set &s)
ITI-41 Document Source actor.
void set_audit_sink(std::shared_ptr< kcenon::pacs::security::xds_audit_sink > sink)
Install an ATNA audit sink.
kcenon::common::Result< submit_response > submit(const submission_set &s)
Submit an XDS.b submission set.
document_source(http_options opts, signing_material signing)
Construct an ITI-41 Document Source bound to an endpoint and a signing credential.
IHE XDS.b Document Source Actor (ITI-41 Provide and Register Document Set-b).
libcurl-backed HTTPS POST client for ITI-41 transport.
MTOM / XOP multipart/related packager for ITI-41.
kcenon::common::Result< packaged_mtom > package_mtom(const std::string &envelope_xml, const std::vector< mtom_part > &parts)
Assemble a multipart/related MTOM body.
kcenon::common::Result< http_response > http_post(const http_options &opts, const std::string &content_type, const std::string &body)
POST body to opts.endpoint with the given Content-Type.
kcenon::common::Result< bool > sign_envelope(built_envelope &env, std::string_view cert_pem, std::string_view key_pem, std::string_view key_password)
Sign the body + timestamp of env in place.
kcenon::common::Result< built_envelope > build_iti41_envelope(const submission_set &s, const std::vector< std::string > &cid_refs)
Build a SOAP envelope for ITI-41 from submission metadata and a list of content-id references for MTO...
constexpr std::string_view error_source
Short identifier used as the "source" field of make_error.
Definition errors.h:95
std::string out
SOAP 1.2 envelope builder for IHE XDS.b ITI-41 requests.
One attachment part, mapped 1:1 to an xop:Include in the envelope.
std::vector< std::uint8_t > content
HTTPS transport options for the Document Source actor.
Signing material for WS-Security XML-DSig.
ebRIM/ebRS-shaped submission set metadata.
std::vector< xds_document > documents
WS-Security XML-DSig signer backed by OpenSSL EVP_DigestSign.
ATNA audit event helpers for the IHE XDS.b actors (ITI-18/41/43).