annotate OrthancFramework/Sources/HttpServer/MultipartStreamReader.cpp @ 4544:ae63b301d522 Orthanc-1.7.4

close branch
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Mar 2021 16:52:26 +0100
parents 732ad6c618ba
children 785a2713323e
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>
3411
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3408
diff changeset
30
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3408
diff changeset
31 #if defined(_MSC_VER)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3408
diff changeset
32 # include <BaseTsd.h> // Definition of ssize_t
3408
1787ad594063 fix fox visual studio
s.jodogne@gmail.com
parents: 3406
diff changeset
33 #endif
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 namespace Orthanc
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 static void ParseHeaders(MultipartStreamReader::HttpHeaders& headers,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 StringMatcher::Iterator start,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 StringMatcher::Iterator end)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 std::string tmp(start, end);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 std::vector<std::string> lines;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 Toolbox::TokenizeString(lines, tmp, '\n');
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 headers.clear();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 for (size_t i = 0; i < lines.size(); i++)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 size_t separator = lines[i].find(':');
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 if (separator != std::string::npos)
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 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
54 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
55
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 Toolbox::ToLowerCase(key);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 headers[key] = value;
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 }
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 static bool LookupHeaderSizeValue(size_t& target,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 const MultipartStreamReader::HttpHeaders& headers,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 const std::string& key)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 MultipartStreamReader::HttpHeaders::const_iterator it = headers.find(key);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 if (it == headers.end())
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 return false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 int64_t value;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 try
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 value = boost::lexical_cast<int64_t>(it->second);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 catch (boost::bad_lexical_cast&)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 throw OrthancException(ErrorCode_ParameterOutOfRange);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 if (value < 0)
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 throw OrthancException(ErrorCode_ParameterOutOfRange);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 target = static_cast<size_t>(value);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 return true;
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 }
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 void MultipartStreamReader::ParseStream()
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 if (handler_ == NULL ||
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 state_ == State_Done)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 return;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 }
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 std::string corpus;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 buffer_.Flatten(corpus);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 StringMatcher::Iterator current = corpus.begin();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 StringMatcher::Iterator corpusEnd = corpus.end();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 if (state_ == State_UnusedArea)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 {
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 * "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
116 * 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
117 * a message to users of old non-MIME clients."
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 * https://en.wikipedia.org/wiki/MIME#Multipart_messages
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 **/
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 if (boundaryMatcher_.Apply(current, corpusEnd))
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 current = boundaryMatcher_.GetMatchBegin();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 state_ = State_Content;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 // 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
129 buffer_.AddChunk(current, corpusEnd);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 return;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
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 for (;;)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 size_t patternSize = boundaryMatcher_.GetPattern().size();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 size_t remainingSize = std::distance(current, corpusEnd);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 if (remainingSize < patternSize + 2)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 break; // Not enough data available
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 }
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 std::string boundary(current, current + patternSize + 2);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 if (boundary == boundaryMatcher_.GetPattern() + "--")
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 state_ = State_Done;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 return;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }
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 if (boundary != boundaryMatcher_.GetPattern() + "\r\n")
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 throw OrthancException(ErrorCode_NetworkProtocol,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 "Garbage between two items in a multipart stream");
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 }
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 StringMatcher::Iterator start = current + patternSize + 2;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 if (!headersMatcher_.Apply(start, corpusEnd))
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 break; // Not enough data available
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 }
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 HttpHeaders headers;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 ParseHeaders(headers, start, headersMatcher_.GetMatchBegin());
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165
3439
fbcde0d66ed8 fixed wrong warning about uninitialized variable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3412
diff changeset
166 size_t contentLength = 0;
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 if (!LookupHeaderSizeValue(contentLength, headers, "content-length"))
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 if (boundaryMatcher_.Apply(headersMatcher_.GetMatchEnd(), corpusEnd))
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 size_t d = std::distance(headersMatcher_.GetMatchEnd(), boundaryMatcher_.GetMatchBegin());
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 if (d <= 1)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 throw OrthancException(ErrorCode_NetworkProtocol);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 contentLength = d - 2;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 }
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 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 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
184 }
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
3412
327bace7e98a trying yet another fix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3411
diff changeset
187 // 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
188 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
189 if (d > std::distance(headersMatcher_.GetMatchEnd(), corpusEnd))
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 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
192 }
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 const char* p = headersMatcher_.GetPointerEnd() + contentLength;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 if (p[0] != '\r' ||
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 p[1] != '\n')
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 throw OrthancException(ErrorCode_NetworkProtocol,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 "No endline at the end of a part");
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
3399
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
202 handler_->HandlePart(headers, headersMatcher_.GetPointerEnd(), contentLength);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 current = headersMatcher_.GetMatchEnd() + contentLength + 2;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 }
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 if (current != corpusEnd)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 {
4148
732ad6c618ba removing ChunkedBuffer::AddChunkDestructive()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
208 buffer_.AddChunk(current, corpusEnd);
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 }
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 MultipartStreamReader::MultipartStreamReader(const std::string& boundary) :
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 state_(State_UnusedArea),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 handler_(NULL),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 headersMatcher_("\r\n\r\n"),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 boundaryMatcher_("--" + boundary),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 blockSize_(10 * 1024 * 1024)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 {
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 void MultipartStreamReader::SetBlockSize(size_t size)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 if (size == 0)
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 throw OrthancException(ErrorCode_ParameterOutOfRange);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 blockSize_ = size;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 }
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 void MultipartStreamReader::AddChunk(const void* chunk,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 size_t size)
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 if (state_ != State_Done &&
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 size != 0)
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 size_t oldSize = buffer_.GetNumBytes();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 buffer_.AddChunk(chunk, size);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 if (oldSize / blockSize_ != buffer_.GetNumBytes() / blockSize_)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 ParseStream();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 void MultipartStreamReader::AddChunk(const std::string& chunk)
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 if (!chunk.empty())
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 AddChunk(chunk.c_str(), chunk.size());
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 }
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 void MultipartStreamReader::CloseStream()
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 if (buffer_.GetNumBytes() != 0)
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 ParseStream();
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 }
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 bool MultipartStreamReader::GetMainContentType(std::string& contentType,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 const HttpHeaders& headers)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 HttpHeaders::const_iterator it = headers.find("content-type");
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 if (it == headers.end())
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 return false;
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 else
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 contentType = it->second;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 return true;
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 }
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
3399
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
289 bool MultipartStreamReader::ParseMultipartContentType(std::string& contentType,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
290 std::string& subType,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
291 std::string& boundary,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
292 const std::string& contentTypeHeader)
3398
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 std::vector<std::string> tokens;
3399
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
295 Orthanc::Toolbox::TokenizeString(tokens, contentTypeHeader, ';');
3398
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 if (tokens.empty())
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 return false;
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 contentType = Orthanc::Toolbox::StripSpaces(tokens[0]);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 Orthanc::Toolbox::ToLowerCase(contentType);
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 if (contentType.empty())
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 return false;
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 bool valid = false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 subType.clear();
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 for (size_t i = 0; i < tokens.size(); i++)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 std::vector<std::string> items;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 Orthanc::Toolbox::TokenizeString(items, tokens[i], '=');
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 if (items.size() == 2)
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 if (boost::iequals("boundary", Orthanc::Toolbox::StripSpaces(items[0])))
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 boundary = Orthanc::Toolbox::StripSpaces(items[1]);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 valid = !boundary.empty();
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 else if (boost::iequals("type", Orthanc::Toolbox::StripSpaces(items[0])))
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 subType = Orthanc::Toolbox::StripSpaces(items[1]);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 Orthanc::Toolbox::ToLowerCase(subType);
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 // 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
331 // https://tools.ietf.org/html/rfc7231#section-3.1.1.1
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 if (subType.size() >= 2 &&
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 subType[0] == '"' &&
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 subType[subType.size() - 1] == '"')
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 subType = subType.substr(1, subType.size() - 2);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 return valid;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 }