annotate OrthancFramework/Sources/HttpServer/MultipartStreamReader.cpp @ 4427:30efda56500d

trying to enable TLS support in DCMTK
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 31 Dec 2020 15:16:18 +0100
parents 98f55e7df5ab
children d9473bd5ed43
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
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3439
diff changeset
5 * Copyright (C) 2017-2020 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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "../OrthancException.h"
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include "../Toolbox.h"
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <boost/algorithm/string/predicate.hpp>
4304
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
30 #include <boost/lexical_cast.hpp>
3411
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3408
diff changeset
31
4361
98f55e7df5ab rollback incorrect removal for msvc2008
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4360
diff changeset
32 #if defined(_MSC_VER)
98f55e7df5ab rollback incorrect removal for msvc2008
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4360
diff changeset
33 # include <BaseTsd.h> // Definition of ssize_t
98f55e7df5ab rollback incorrect removal for msvc2008
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4360
diff changeset
34 #endif
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 namespace Orthanc
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 static void ParseHeaders(MultipartStreamReader::HttpHeaders& headers,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 StringMatcher::Iterator start,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 StringMatcher::Iterator end)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 std::string tmp(start, end);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 std::vector<std::string> lines;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 Toolbox::TokenizeString(lines, tmp, '\n');
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 headers.clear();
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 for (size_t i = 0; i < lines.size(); i++)
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 size_t separator = lines[i].find(':');
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 if (separator != std::string::npos)
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 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
55 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
56
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 Toolbox::ToLowerCase(key);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 headers[key] = value;
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 }
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 static bool LookupHeaderSizeValue(size_t& target,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 const MultipartStreamReader::HttpHeaders& headers,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 const std::string& key)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 MultipartStreamReader::HttpHeaders::const_iterator it = headers.find(key);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 if (it == headers.end())
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 return false;
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 else
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 int64_t value;
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 try
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 value = boost::lexical_cast<int64_t>(it->second);
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 catch (boost::bad_lexical_cast&)
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 throw OrthancException(ErrorCode_ParameterOutOfRange);
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 if (value < 0)
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 throw OrthancException(ErrorCode_ParameterOutOfRange);
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 else
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 target = static_cast<size_t>(value);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 return true;
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 }
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 void MultipartStreamReader::ParseStream()
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 if (handler_ == NULL ||
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 state_ == State_Done)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 return;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 std::string corpus;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 buffer_.Flatten(corpus);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 StringMatcher::Iterator current = corpus.begin();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 StringMatcher::Iterator corpusEnd = corpus.end();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 if (state_ == State_UnusedArea)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 /**
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 * "Before the first boundary is an area that is ignored by
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 * MIME-compliant clients. This area is generally used to put
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 * a message to users of old non-MIME clients."
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 * https://en.wikipedia.org/wiki/MIME#Multipart_messages
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 **/
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 if (boundaryMatcher_.Apply(current, corpusEnd))
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 current = boundaryMatcher_.GetMatchBegin();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 state_ = State_Content;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 // We have not seen the end of the unused area yet
4148
732ad6c618ba removing ChunkedBuffer::AddChunkDestructive()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
130 buffer_.AddChunk(current, corpusEnd);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 return;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 for (;;)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 size_t patternSize = boundaryMatcher_.GetPattern().size();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 size_t remainingSize = std::distance(current, corpusEnd);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 if (remainingSize < patternSize + 2)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 break; // Not enough data available
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 std::string boundary(current, current + patternSize + 2);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 if (boundary == boundaryMatcher_.GetPattern() + "--")
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 state_ = State_Done;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 return;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 if (boundary != boundaryMatcher_.GetPattern() + "\r\n")
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 throw OrthancException(ErrorCode_NetworkProtocol,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 "Garbage between two items in a multipart stream");
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 StringMatcher::Iterator start = current + patternSize + 2;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 if (!headersMatcher_.Apply(start, corpusEnd))
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 break; // Not enough data available
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 HttpHeaders headers;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 ParseHeaders(headers, start, headersMatcher_.GetMatchBegin());
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166
3439
fbcde0d66ed8 fixed wrong warning about uninitialized variable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3412
diff changeset
167 size_t contentLength = 0;
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 if (!LookupHeaderSizeValue(contentLength, headers, "content-length"))
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 if (boundaryMatcher_.Apply(headersMatcher_.GetMatchEnd(), corpusEnd))
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 size_t d = std::distance(headersMatcher_.GetMatchEnd(), boundaryMatcher_.GetMatchBegin());
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 if (d <= 1)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 throw OrthancException(ErrorCode_NetworkProtocol);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 contentLength = d - 2;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 break; // Not enough data available to have a full part
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
3412
327bace7e98a trying yet another fix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3411
diff changeset
188 // Explicit conversion to avoid warning about signed vs. unsigned comparison
327bace7e98a trying yet another fix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3411
diff changeset
189 std::iterator_traits<StringMatcher::Iterator>::difference_type d = contentLength + 2;
327bace7e98a trying yet another fix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3411
diff changeset
190 if (d > std::distance(headersMatcher_.GetMatchEnd(), corpusEnd))
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 break; // Not enough data available to have a full part
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 const char* p = headersMatcher_.GetPointerEnd() + contentLength;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 if (p[0] != '\r' ||
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 p[1] != '\n')
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 throw OrthancException(ErrorCode_NetworkProtocol,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 "No endline at the end of a part");
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
3399
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
203 handler_->HandlePart(headers, headersMatcher_.GetPointerEnd(), contentLength);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 current = headersMatcher_.GetMatchEnd() + contentLength + 2;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 if (current != corpusEnd)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 {
4148
732ad6c618ba removing ChunkedBuffer::AddChunkDestructive()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
209 buffer_.AddChunk(current, corpusEnd);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 MultipartStreamReader::MultipartStreamReader(const std::string& boundary) :
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 state_(State_UnusedArea),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 handler_(NULL),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 headersMatcher_("\r\n\r\n"),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 boundaryMatcher_("--" + boundary),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 blockSize_(10 * 1024 * 1024)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
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 void MultipartStreamReader::SetBlockSize(size_t size)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 if (size == 0)
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 throw OrthancException(ErrorCode_ParameterOutOfRange);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 blockSize_ = size;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235
4298
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
236 size_t MultipartStreamReader::GetBlockSize() const
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
237 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
238 return blockSize_;
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
239 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
240
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
241 void MultipartStreamReader::SetHandler(MultipartStreamReader::IHandler &handler)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
242 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
243 handler_ = &handler;
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
244 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
245
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
246
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 void MultipartStreamReader::AddChunk(const void* chunk,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 size_t size)
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 if (state_ != State_Done &&
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 size != 0)
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 size_t oldSize = buffer_.GetNumBytes();
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 buffer_.AddChunk(chunk, size);
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 if (oldSize / blockSize_ != buffer_.GetNumBytes() / blockSize_)
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 ParseStream();
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 }
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 void MultipartStreamReader::AddChunk(const std::string& chunk)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 if (!chunk.empty())
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 AddChunk(chunk.c_str(), chunk.size());
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 void MultipartStreamReader::CloseStream()
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 if (buffer_.GetNumBytes() != 0)
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 ParseStream();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 }
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 bool MultipartStreamReader::GetMainContentType(std::string& contentType,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 const HttpHeaders& headers)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 HttpHeaders::const_iterator it = headers.find("content-type");
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 if (it == headers.end())
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 return false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 contentType = it->second;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 return true;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299
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
300 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
301 {
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
302 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
303 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
304 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
305 {
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
306 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
307 }
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
308 }
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
309
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
310
3399
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
311 bool MultipartStreamReader::ParseMultipartContentType(std::string& contentType,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
312 std::string& subType,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
313 std::string& boundary,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
314 const std::string& contentTypeHeader)
3398
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 std::vector<std::string> tokens;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
317 Toolbox::TokenizeString(tokens, contentTypeHeader, ';');
3398
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 if (tokens.empty())
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 return false;
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
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
324 contentType = Toolbox::StripSpaces(tokens[0]);
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
325 Toolbox::ToLowerCase(contentType);
3398
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 if (contentType.empty())
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 return false;
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 bool valid = false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 subType.clear();
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 for (size_t i = 0; i < tokens.size(); i++)
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 std::vector<std::string> items;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
338 Toolbox::TokenizeString(items, tokens[i], '=');
3398
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 if (items.size() == 2)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
342 if (boost::iequals("boundary", Toolbox::StripSpaces(items[0])))
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
344 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
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 // 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
347 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
348
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 valid = !boundary.empty();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 }
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
351 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
352 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
353 subType = Toolbox::StripSpaces(items[1]);
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4148
diff changeset
354 Toolbox::ToLowerCase(subType);
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 // https://bitbucket.org/sjodogne/orthanc/issues/54/decide-what-to-do-wrt-quoting-of-multipart
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 // 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
358 RemoveSurroundingQuotes(subType);
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 }
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 return valid;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 }