annotate OrthancFramework/Sources/HttpServer/MultipartStreamReader.cpp @ 5639:b6a6179a2a69

TimezoneOffsetFromUTC is now ignored for C-Find matching
author Alain Mazy <am@orthanc.team>
date Fri, 24 May 2024 15:11:28 +0200
parents 48b8dae6dc77
children f7adfb22e20e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5432
diff changeset
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5432
diff changeset
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
9 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
11 * the License, or (at your option) any later version.
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
16 * Lesser General Public License for more details.
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
19 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
20 * <http://www.gnu.org/licenses/>.
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 **/
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "../PrecompiledHeaders.h"
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "MultipartStreamReader.h"
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
4453
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
27 #include "../Logging.h"
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include "../OrthancException.h"
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include "../Toolbox.h"
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include <boost/algorithm/string/predicate.hpp>
4304
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
32 #include <boost/lexical_cast.hpp>
3411
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3408
diff changeset
33
4361
98f55e7df5ab rollback incorrect removal for msvc2008
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4360
diff changeset
34 #if defined(_MSC_VER)
98f55e7df5ab rollback incorrect removal for msvc2008
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4360
diff changeset
35 # include <BaseTsd.h> // Definition of ssize_t
98f55e7df5ab rollback incorrect removal for msvc2008
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4360
diff changeset
36 #endif
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 namespace Orthanc
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 static void ParseHeaders(MultipartStreamReader::HttpHeaders& headers,
4652
0ad5736c8d62 use plain C strings in MultipartStreamReader instead of std::string to allow further optimizations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
41 const char* start,
0ad5736c8d62 use plain C strings in MultipartStreamReader instead of std::string to allow further optimizations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
42 const char* end /* exclusive */)
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
4652
0ad5736c8d62 use plain C strings in MultipartStreamReader instead of std::string to allow further optimizations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
44 assert(start <= end);
0ad5736c8d62 use plain C strings in MultipartStreamReader instead of std::string to allow further optimizations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
45 std::string tmp(start, end - start);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 std::vector<std::string> lines;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 Toolbox::TokenizeString(lines, tmp, '\n');
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 headers.clear();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 for (size_t i = 0; i < lines.size(); i++)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 size_t separator = lines[i].find(':');
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 if (separator != std::string::npos)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 std::string key = Toolbox::StripSpaces(lines[i].substr(0, separator));
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 std::string value = Toolbox::StripSpaces(lines[i].substr(separator + 1));
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 Toolbox::ToLowerCase(key);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 headers[key] = value;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 static bool LookupHeaderSizeValue(size_t& target,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 const MultipartStreamReader::HttpHeaders& headers,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 const std::string& key)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 MultipartStreamReader::HttpHeaders::const_iterator it = headers.find(key);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 if (it == headers.end())
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 return false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 int64_t value;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 try
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 value = boost::lexical_cast<int64_t>(it->second);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 catch (boost::bad_lexical_cast&)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 throw OrthancException(ErrorCode_ParameterOutOfRange);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 if (value < 0)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 throw OrthancException(ErrorCode_ParameterOutOfRange);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 target = static_cast<size_t>(value);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 return true;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
4653
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
102 void MultipartStreamReader::ParseBlock(const void* data,
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
103 size_t size)
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
104 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
105 if (handler_ == NULL ||
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
106 state_ == State_Done ||
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
107 size == 0)
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
108 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
109 return;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
110 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
111 else
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
112 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
113 const char* current = reinterpret_cast<const char*>(data);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
114 const char* corpusEnd = current + size;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
115
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
116 if (state_ == State_UnusedArea)
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
117 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
118 /**
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
119 * "Before the first boundary is an area that is ignored by
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
120 * MIME-compliant clients. This area is generally used to put
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
121 * a message to users of old non-MIME clients."
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
122 * https://en.wikipedia.org/wiki/MIME#Multipart_messages
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
123 **/
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
124
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
125 if (boundaryMatcher_.Apply(current, corpusEnd))
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
126 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
127 current = boundaryMatcher_.GetMatchBegin();
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
128 state_ = State_Content;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
129 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
130 else
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
131 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
132 // We have not seen the end of the unused area yet
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
133 assert(current <= corpusEnd);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
134 buffer_.AddChunk(current, corpusEnd - current);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
135 return;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
136 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
137 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
138
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
139 for (;;)
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
140 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
141 assert(current <= corpusEnd);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
142
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
143 size_t patternSize = boundaryMatcher_.GetPattern().size();
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
144 size_t remainingSize = corpusEnd - current;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
145 if (remainingSize < patternSize + 2)
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
146 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
147 break; // Not enough data available
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
148 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
149
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
150 std::string boundary(current, current + patternSize + 2);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
151 if (boundary == boundaryMatcher_.GetPattern() + "--")
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
152 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
153 state_ = State_Done;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
154 return;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
155 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
156
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
157 if (boundary != boundaryMatcher_.GetPattern() + "\r\n")
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
158 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
159 throw OrthancException(ErrorCode_NetworkProtocol,
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
160 "Garbage between two items in a multipart stream");
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
161 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
162
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
163 const char* start = current + patternSize + 2;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
164
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
165 if (!headersMatcher_.Apply(start, corpusEnd))
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
166 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
167 break; // Not enough data available
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
168 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
169
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
170 HttpHeaders headers;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
171 ParseHeaders(headers, start, headersMatcher_.GetMatchBegin());
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
172
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
173 size_t contentLength = 0;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
174 if (!LookupHeaderSizeValue(contentLength, headers, "content-length"))
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
175 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
176 if (boundaryMatcher_.Apply(headersMatcher_.GetMatchEnd(), corpusEnd))
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
177 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
178 assert(headersMatcher_.GetMatchEnd() <= boundaryMatcher_.GetMatchBegin());
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
179 size_t d = boundaryMatcher_.GetMatchBegin() - headersMatcher_.GetMatchEnd();
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
180 if (d <= 1)
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
181 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
182 throw OrthancException(ErrorCode_NetworkProtocol);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
183 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
184 else
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
185 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
186 contentLength = d - 2;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
187 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
188 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
189 else
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
190 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
191 break; // Not enough data available to have a full part
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
192 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
193 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
194
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
195 // "static_cast<>" to avoid warning about signed vs. unsigned comparison
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
196 assert(headersMatcher_.GetMatchEnd() <= corpusEnd);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
197 if (contentLength + 2 > static_cast<size_t>(corpusEnd - headersMatcher_.GetMatchEnd()))
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
198 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
199 break; // Not enough data available to have a full part
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
200 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
201
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
202 const char* p = headersMatcher_.GetMatchEnd() + contentLength;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
203 if (p[0] != '\r' ||
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
204 p[1] != '\n')
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
205 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
206 throw OrthancException(ErrorCode_NetworkProtocol,
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
207 "No endline at the end of a part");
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
208 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
209
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
210 handler_->HandlePart(headers, headersMatcher_.GetMatchEnd(), contentLength);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
211 current = headersMatcher_.GetMatchEnd() + contentLength + 2;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
212 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
213
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
214 if (current != corpusEnd)
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
215 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
216 assert(current < corpusEnd);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
217 buffer_.AddChunk(current, corpusEnd - current);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
218 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
219 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
220 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
221
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
222
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 void MultipartStreamReader::ParseStream()
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 if (handler_ == NULL ||
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 state_ == State_Done)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 return;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 }
4653
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
230 else
4652
0ad5736c8d62 use plain C strings in MultipartStreamReader instead of std::string to allow further optimizations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
231 {
4653
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
232 std::string corpus;
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
233 buffer_.Flatten(corpus);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234
4653
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
235 if (!corpus.empty())
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
4653
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
237 ParseBlock(corpus.c_str(), corpus.size());
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 MultipartStreamReader::MultipartStreamReader(const std::string& boundary) :
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 state_(State_UnusedArea),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 handler_(NULL),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 headersMatcher_("\r\n\r\n"),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 boundaryMatcher_("--" + boundary),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 blockSize_(10 * 1024 * 1024)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 void MultipartStreamReader::SetBlockSize(size_t size)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 if (size == 0)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 throw OrthancException(ErrorCode_ParameterOutOfRange);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 blockSize_ = size;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264
4298
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
265 size_t MultipartStreamReader::GetBlockSize() const
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
266 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
267 return blockSize_;
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
268 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
269
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
270 void MultipartStreamReader::SetHandler(MultipartStreamReader::IHandler &handler)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
271 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
272 handler_ = &handler;
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
273 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
274
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
275
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 void MultipartStreamReader::AddChunk(const void* chunk,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 size_t size)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 if (state_ != State_Done &&
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 size != 0)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 size_t oldSize = buffer_.GetNumBytes();
4653
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
283 if (oldSize == 0)
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
284 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
285 /**
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
286 * Optimization in Orthanc 1.9.3: Directly parse the input
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
287 * buffer instead of going through the ChunkedBuffer if the
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
288 * latter is still empty. This notably avoids one memcpy() in
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
289 * STOW-RS server if chunked transfers is disabled.
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
290 **/
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
291 ParseBlock(chunk, size);
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
292 }
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
293 else
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
294 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
295 buffer_.AddChunk(chunk, size);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296
4653
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
297 if (oldSize / blockSize_ != buffer_.GetNumBytes() / blockSize_)
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
298 {
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
299 ParseStream();
3d5d6e2dcf3f memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4652
diff changeset
300 }
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 void MultipartStreamReader::AddChunk(const std::string& chunk)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 if (!chunk.empty())
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 AddChunk(chunk.c_str(), chunk.size());
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 void MultipartStreamReader::CloseStream()
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 if (buffer_.GetNumBytes() != 0)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 ParseStream();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 bool MultipartStreamReader::GetMainContentType(std::string& contentType,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 const HttpHeaders& headers)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 HttpHeaders::const_iterator it = headers.find("content-type");
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 if (it == headers.end())
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 return false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 contentType = it->second;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 return true;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340
4333
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
341 static void RemoveSurroundingQuotes(std::string& value)
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
342 {
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
343 if (value.size() >= 2 &&
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
344 value[0] == '"' &&
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
345 value[value.size() - 1] == '"')
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
346 {
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
347 value = value.substr(1, value.size() - 2);
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
348 }
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
349 }
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
350
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
351
3399
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
352 bool MultipartStreamReader::ParseMultipartContentType(std::string& contentType,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
353 std::string& subType,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
354 std::string& boundary,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
355 const std::string& contentTypeHeader)
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 std::vector<std::string> tokens;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
358 Toolbox::TokenizeString(tokens, contentTypeHeader, ';');
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 if (tokens.empty())
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 return false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
365 contentType = Toolbox::StripSpaces(tokens[0]);
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
366 Toolbox::ToLowerCase(contentType);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 if (contentType.empty())
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 return false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 bool valid = false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 subType.clear();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375
4453
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
376 for (size_t i = 1; i < tokens.size(); i++)
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 std::vector<std::string> items;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
379 Toolbox::TokenizeString(items, tokens[i], '=');
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 if (items.size() == 2)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
383 if (boost::iequals("boundary", Toolbox::StripSpaces(items[0])))
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
385 boundary = Toolbox::StripSpaces(items[1]);
4333
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
386
5432
59e3b6f8c5be migration to UCLouvain servers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
387 // https://orthanc.uclouvain.be/bugs/show_bug.cgi?id=190
4333
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
388 RemoveSurroundingQuotes(boundary);
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
389
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 valid = !boundary.empty();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 }
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
392 else if (boost::iequals("type", Toolbox::StripSpaces(items[0])))
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
394 subType = Toolbox::StripSpaces(items[1]);
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
395 Toolbox::ToLowerCase(subType);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396
5432
59e3b6f8c5be migration to UCLouvain servers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
397 // https://orthanc.uclouvain.be/bugs/show_bug.cgi?id=54
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 // https://tools.ietf.org/html/rfc7231#section-3.1.1.1
4333
a85e74235a78 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4304
diff changeset
399 RemoveSurroundingQuotes(subType);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404 return valid;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 }
4453
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
406
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
407
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
408 bool MultipartStreamReader::ParseHeaderArguments(std::string& main,
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
409 std::map<std::string, std::string>& arguments,
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
410 const std::string& header)
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
411 {
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
412 std::vector<std::string> tokens;
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
413 Toolbox::TokenizeString(tokens, header, ';');
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
414
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
415 if (tokens.empty())
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
416 {
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
417 return false;
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
418 }
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
419
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
420 main = Toolbox::StripSpaces(tokens[0]);
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
421 Toolbox::ToLowerCase(main);
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
422 if (main.empty())
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
423 {
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
424 return false;
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
425 }
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
426
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
427 arguments.clear();
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
428
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
429 for (size_t i = 1; i < tokens.size(); i++)
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
430 {
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
431 std::vector<std::string> items;
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
432 Toolbox::TokenizeString(items, tokens[i], '=');
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
433
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
434 if (items.size() > 2)
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
435 {
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
436 return false;
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
437 }
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
438 else if (!items.empty())
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
439 {
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
440 std::string key = Toolbox::StripSpaces(items[0]);
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
441 Toolbox::ToLowerCase(key);
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
442
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
443 if (arguments.find(key) != arguments.end())
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
444 {
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
445 LOG(ERROR) << "The same argument was provided twice in an HTTP header: \""
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
446 << key << "\" in \"" << header << "\"";
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
447 return false;
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
448 }
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
449 else if (!key.empty())
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
450 {
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
451 if (items.size() == 1)
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
452 {
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
453 arguments[key] = "";
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
454 }
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
455 else
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
456 {
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
457 assert(items.size() == 2);
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
458 std::string value = Toolbox::StripSpaces(items[1]);
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
459 RemoveSurroundingQuotes(value);
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
460 arguments[key] = value;
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
461 }
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
462 }
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
463 }
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
464 }
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
465
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
466 return true;
4f8e77c650e8 new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
467 }
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 }