Mercurial > hg > orthanc
annotate OrthancFramework/Sources/HttpServer/MultipartStreamReader.cpp @ 4706:dbee8bd8ee1f
display OpenSSL version in the logs
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 Jun 2021 09:13:03 +0200 |
parents | 3d5d6e2dcf3f |
children | 7053502fbf97 |
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 |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4361
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 * |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * 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
|
8 * 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
|
9 * 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
|
10 * 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
|
11 * |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * 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
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 * 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
|
15 * Lesser General Public License for more details. |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * |
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
|
17 * 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
|
18 * 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
|
19 * <http://www.gnu.org/licenses/>. |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 **/ |
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 #include "../PrecompiledHeaders.h" |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 #include "MultipartStreamReader.h" |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 |
4453
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
26 #include "../Logging.h" |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 #include "../OrthancException.h" |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 #include "../Toolbox.h" |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 #include <boost/algorithm/string/predicate.hpp> |
4304 | 31 #include <boost/lexical_cast.hpp> |
3411 | 32 |
4361
98f55e7df5ab
rollback incorrect removal for msvc2008
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4360
diff
changeset
|
33 #if defined(_MSC_VER) |
98f55e7df5ab
rollback incorrect removal for msvc2008
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4360
diff
changeset
|
34 # include <BaseTsd.h> // Definition of ssize_t |
98f55e7df5ab
rollback incorrect removal for msvc2008
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4360
diff
changeset
|
35 #endif |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 namespace Orthanc |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 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
|
40 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
|
41 const char* end /* exclusive */) |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 { |
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
|
43 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
|
44 std::string tmp(start, end - start); |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 std::vector<std::string> lines; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 Toolbox::TokenizeString(lines, tmp, '\n'); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 headers.clear(); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 for (size_t i = 0; i < lines.size(); i++) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 size_t separator = lines[i].find(':'); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 if (separator != std::string::npos) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 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
|
57 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
|
58 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 Toolbox::ToLowerCase(key); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 headers[key] = value; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 } |
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 static bool LookupHeaderSizeValue(size_t& target, |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 const MultipartStreamReader::HttpHeaders& headers, |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 const std::string& key) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 MultipartStreamReader::HttpHeaders::const_iterator it = headers.find(key); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 if (it == headers.end()) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 return false; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 } |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 else |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 int64_t value; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 try |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 value = boost::lexical_cast<int64_t>(it->second); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 } |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 catch (boost::bad_lexical_cast&) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 throw OrthancException(ErrorCode_ParameterOutOfRange); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 } |
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 if (value < 0) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 throw OrthancException(ErrorCode_ParameterOutOfRange); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 } |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 else |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 target = static_cast<size_t>(value); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
95 return true; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
96 } |
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 |
4653
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
101 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
|
102 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
|
103 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
104 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
|
105 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
|
106 size == 0) |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
107 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
108 return; |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
109 } |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
110 else |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
111 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
112 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
|
113 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
|
114 |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
115 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
|
116 { |
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 * "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
|
119 * 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
|
120 * 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
|
121 * 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
|
122 **/ |
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 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
|
125 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
126 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
|
127 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
|
128 } |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
129 else |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
130 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
131 // 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
|
132 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
|
133 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
|
134 return; |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
135 } |
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 for (;;) |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
139 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
140 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
|
141 |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
142 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
|
143 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
|
144 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
|
145 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
146 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
|
147 } |
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 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
|
150 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
|
151 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
152 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
|
153 return; |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
154 } |
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 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
|
157 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
158 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
|
159 "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
|
160 } |
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 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
|
163 |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
164 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
|
165 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
166 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
|
167 } |
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 HttpHeaders headers; |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
170 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
|
171 |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
172 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
|
173 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
|
174 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
175 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
|
176 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
177 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
|
178 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
|
179 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
|
180 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
181 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
|
182 } |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
183 else |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
184 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
185 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
|
186 } |
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 else |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
189 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
190 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
|
191 } |
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 // "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
|
195 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
|
196 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
|
197 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
198 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
|
199 } |
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 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
|
202 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
|
203 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
|
204 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
205 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
|
206 "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
|
207 } |
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 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
|
210 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
|
211 } |
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 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
|
214 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
215 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
|
216 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
|
217 } |
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 |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
222 void MultipartStreamReader::ParseStream() |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
223 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
224 if (handler_ == NULL || |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
225 state_ == State_Done) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
226 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
227 return; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
228 } |
4653
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
229 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
|
230 { |
4653
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
231 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
|
232 buffer_.Flatten(corpus); |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
233 |
4653
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
234 if (!corpus.empty()) |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
235 { |
4653
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
236 ParseBlock(corpus.c_str(), corpus.size()); |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
237 } |
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 MultipartStreamReader::MultipartStreamReader(const std::string& boundary) : |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
243 state_(State_UnusedArea), |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
244 handler_(NULL), |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
245 headersMatcher_("\r\n\r\n"), |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
246 boundaryMatcher_("--" + boundary), |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
247 blockSize_(10 * 1024 * 1024) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
248 { |
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 void MultipartStreamReader::SetBlockSize(size_t size) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
253 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
254 if (size == 0) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
255 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
256 throw OrthancException(ErrorCode_ParameterOutOfRange); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
257 } |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
258 else |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
259 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
260 blockSize_ = size; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
261 } |
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 |
4298 | 264 size_t MultipartStreamReader::GetBlockSize() const |
265 { | |
266 return blockSize_; | |
267 } | |
268 | |
269 void MultipartStreamReader::SetHandler(MultipartStreamReader::IHandler &handler) | |
270 { | |
271 handler_ = &handler; | |
272 } | |
273 | |
274 | |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
275 void MultipartStreamReader::AddChunk(const void* chunk, |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
276 size_t size) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
277 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
278 if (state_ != State_Done && |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
279 size != 0) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
280 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
281 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
|
282 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
|
283 { |
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 * 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
|
286 * 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
|
287 * 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
|
288 * 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
|
289 **/ |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
290 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
|
291 } |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
292 else |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
293 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
294 buffer_.AddChunk(chunk, size); |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
295 |
4653
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
296 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
|
297 { |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
298 ParseStream(); |
3d5d6e2dcf3f
memory optimization in Orthanc::MultipartStreamReader if parsing one single big block
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4652
diff
changeset
|
299 } |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
300 } |
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 void MultipartStreamReader::AddChunk(const std::string& chunk) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
306 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
307 if (!chunk.empty()) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
308 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
309 AddChunk(chunk.c_str(), chunk.size()); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
310 } |
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 void MultipartStreamReader::CloseStream() |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
315 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
316 if (buffer_.GetNumBytes() != 0) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
317 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
318 ParseStream(); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
319 } |
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 bool MultipartStreamReader::GetMainContentType(std::string& contentType, |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
324 const HttpHeaders& headers) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
325 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
326 HttpHeaders::const_iterator it = headers.find("content-type"); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
327 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
328 if (it == headers.end()) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
329 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
330 return false; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
331 } |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
332 else |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
333 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
334 contentType = it->second; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
335 return true; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
336 } |
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 |
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
|
340 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
|
341 { |
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 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
|
343 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
|
344 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
|
345 { |
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 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
|
347 } |
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 |
3399
4e8205871967
OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3398
diff
changeset
|
351 bool MultipartStreamReader::ParseMultipartContentType(std::string& contentType, |
4e8205871967
OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3398
diff
changeset
|
352 std::string& subType, |
4e8205871967
OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3398
diff
changeset
|
353 std::string& boundary, |
4e8205871967
OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3398
diff
changeset
|
354 const std::string& contentTypeHeader) |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
355 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
356 std::vector<std::string> tokens; |
4297 | 357 Toolbox::TokenizeString(tokens, contentTypeHeader, ';'); |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
358 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
359 if (tokens.empty()) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
360 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
361 return false; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
362 } |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
363 |
4297 | 364 contentType = Toolbox::StripSpaces(tokens[0]); |
365 Toolbox::ToLowerCase(contentType); | |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
366 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
367 if (contentType.empty()) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
368 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
369 return false; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
370 } |
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 bool valid = false; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
373 subType.clear(); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
374 |
4453
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
375 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
|
376 { |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
377 std::vector<std::string> items; |
4297 | 378 Toolbox::TokenizeString(items, tokens[i], '='); |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
379 |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
380 if (items.size() == 2) |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
381 { |
4297 | 382 if (boost::iequals("boundary", Toolbox::StripSpaces(items[0]))) |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
383 { |
4297 | 384 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
|
385 |
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 // https://bugs.orthanc-server.com/show_bug.cgi?id=190 |
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
|
387 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
|
388 |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
389 valid = !boundary.empty(); |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
390 } |
4297 | 391 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
|
392 { |
4297 | 393 subType = Toolbox::StripSpaces(items[1]); |
394 Toolbox::ToLowerCase(subType); | |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
395 |
4476
c1f36fd13730
migrate remaining links to issues in bitbucket to mercurial server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4453
diff
changeset
|
396 // https://bugs.orthanc-server.com/show_bug.cgi?id=54 |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
397 // 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
|
398 RemoveSurroundingQuotes(subType); |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
399 } |
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 return valid; |
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
404 } |
4453
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
405 |
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 bool MultipartStreamReader::ParseHeaderArguments(std::string& main, |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
408 std::map<std::string, std::string>& arguments, |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
409 const std::string& header) |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
410 { |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
411 std::vector<std::string> tokens; |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
412 Toolbox::TokenizeString(tokens, header, ';'); |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
413 |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
414 if (tokens.empty()) |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
415 { |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
416 return false; |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
417 } |
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 main = Toolbox::StripSpaces(tokens[0]); |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
420 Toolbox::ToLowerCase(main); |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
421 if (main.empty()) |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
422 { |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
423 return false; |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
424 } |
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 arguments.clear(); |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
427 |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
428 for (size_t i = 1; i < tokens.size(); i++) |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
429 { |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
430 std::vector<std::string> items; |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
431 Toolbox::TokenizeString(items, tokens[i], '='); |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
432 |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
433 if (items.size() > 2) |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
434 { |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
435 return false; |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
436 } |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
437 else if (!items.empty()) |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
438 { |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
439 std::string key = Toolbox::StripSpaces(items[0]); |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
440 Toolbox::ToLowerCase(key); |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
441 |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
442 if (arguments.find(key) != arguments.end()) |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
443 { |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
444 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
|
445 << key << "\" in \"" << header << "\""; |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
446 return false; |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
447 } |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
448 else if (!key.empty()) |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
449 { |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
450 if (items.size() == 1) |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
451 { |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
452 arguments[key] = ""; |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
453 } |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
454 else |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
455 { |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
456 assert(items.size() == 2); |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
457 std::string value = Toolbox::StripSpaces(items[1]); |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
458 RemoveSurroundingQuotes(value); |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
459 arguments[key] = value; |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
460 } |
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 return true; |
4f8e77c650e8
new function MultipartStreamReader::ParseHeaderArguments()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
466 } |
3398
4acd1431e603
new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
467 } |