annotate OrthancServer/Sources/OrthancHttpHandler.cpp @ 5152:c81f363d3aa3 malloc-trim

housekeeper thread to call malloc_trim and give back memory to the system
author Alain Mazy <am@osimis.io>
date Wed, 01 Feb 2023 18:38:39 +0100
parents 6eff25f70121
children 0ea402b4d901
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
4870
43e613a7756b upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4831
diff changeset
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
43e613a7756b upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4831
diff changeset
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * modify it under the terms of the GNU General Public License as
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * published by the Free Software Foundation, either version 3 of the
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * License, or (at your option) any later version.
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * General Public License for more details.
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "PrecompiledHeadersServer.h"
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "OrthancHttpHandler.h"
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
4045
05b8fd21089c fix path
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
26 #include "../../OrthancFramework/Sources/OrthancException.h"
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 namespace Orthanc
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
3414
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
31 bool OrthancHttpHandler::CreateChunkedRequestReader(
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
32 std::unique_ptr<IHttpHandler::IChunkedRequestReader>& target,
3414
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
33 RequestOrigin origin,
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
34 const char* remoteIp,
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
35 const char* username,
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
36 HttpMethod method,
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
37 const UriComponents& uri,
4330
a01b1c9cbef4 moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
38 const HttpToolbox::Arguments& headers)
3395
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
39 {
3414
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
40 if (method != HttpMethod_Post &&
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
41 method != HttpMethod_Put)
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
42 {
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
43 throw OrthancException(ErrorCode_InternalError);
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
44 }
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
45
3395
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
46 for (Handlers::const_iterator it = handlers_.begin(); it != handlers_.end(); ++it)
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
47 {
3414
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
48 if ((*it)->CreateChunkedRequestReader
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
49 (target, origin, remoteIp, username, method, uri, headers))
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
50 {
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
51 if (target.get() == NULL)
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
52 {
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
53 throw OrthancException(ErrorCode_InternalError);
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
54 }
3395
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
55
3414
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
56 return true;
3395
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
57 }
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
58 }
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
59
3414
b9cba6a91780 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3401
diff changeset
60 return false;
3395
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
61 }
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
62
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
63
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 bool OrthancHttpHandler::Handle(HttpOutput& output,
1571
3232f1c995a5 provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1446
diff changeset
65 RequestOrigin origin,
3232f1c995a5 provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1446
diff changeset
66 const char* remoteIp,
3232f1c995a5 provide the origin of the requests to HTTP handlers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1446
diff changeset
67 const char* username,
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 HttpMethod method,
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 const UriComponents& uri,
4330
a01b1c9cbef4 moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
70 const HttpToolbox::Arguments& headers,
a01b1c9cbef4 moving generic type definitions from IHttpHandler to HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
71 const HttpToolbox::GetArguments& getArguments,
3401
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3395
diff changeset
72 const void* bodyData,
1446
8dc80ba768aa refactoring: IHttpHandler does not use std::string to hold the request body
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
73 size_t bodySize)
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 {
3395
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
75 for (Handlers::const_iterator it = handlers_.begin(); it != handlers_.end(); ++it)
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
3395
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
77 if ((*it)->Handle(output, origin, remoteIp, username, method, uri,
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
78 headers, getArguments, bodyData, bodySize))
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
79 {
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
80 return true;
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
81 }
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 }
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
3395
0ce9b4f5fdf5 new abstraction: IHttpHandler::CreateStreamHandler()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
84 return false;
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 }
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
1443
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1442
diff changeset
88 void OrthancHttpHandler::Register(IHttpHandler& handler,
895ab369d63c refactoring: OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1442
diff changeset
89 bool isOrthancRestApi)
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 handlers_.push_back(&handler);
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 if (isOrthancRestApi)
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 orthancRestApi_ = &handler;
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
1445
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
100 IHttpHandler& OrthancHttpHandler::RestrictToOrthancRestApi(bool restrict)
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 {
1445
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
102 if (restrict)
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 {
1445
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
104 if (orthancRestApi_ == NULL)
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
105 {
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
106 throw OrthancException(ErrorCode_InternalError);
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
107 }
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
108
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
109 return *orthancRestApi_;
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 }
1445
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
111 else
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
112 {
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
113 return *this;
d26c8a93d05a refactoring: SimpleGet
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1443
diff changeset
114 }
1442
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
4ff8dd753d79 OrthancHttpHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }