annotate OrthancFramework/Sources/HttpServer/MultipartStreamReader.cpp @ 4138:1a26daefc3fe

DicomImageInformation::USEFUL_TAG_LENGTH
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 06 Aug 2020 17:56:10 +0200
parents bf7b9edf6b81
children 732ad6c618ba
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 std::string reminder(current, corpusEnd);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 buffer_.AddChunkDestructive(reminder);
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 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 std::string reminder(current, corpusEnd);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 buffer_.AddChunkDestructive(reminder);
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 MultipartStreamReader::MultipartStreamReader(const std::string& boundary) :
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 state_(State_UnusedArea),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 handler_(NULL),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 headersMatcher_("\r\n\r\n"),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 boundaryMatcher_("--" + boundary),
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 blockSize_(10 * 1024 * 1024)
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 void MultipartStreamReader::SetBlockSize(size_t size)
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 if (size == 0)
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 throw OrthancException(ErrorCode_ParameterOutOfRange);
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 else
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 blockSize_ = size;
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
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 void MultipartStreamReader::AddChunk(const void* chunk,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 size_t size)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 if (state_ != State_Done &&
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 size != 0)
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 size_t oldSize = buffer_.GetNumBytes();
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 buffer_.AddChunk(chunk, size);
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 if (oldSize / blockSize_ != buffer_.GetNumBytes() / blockSize_)
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 ParseStream();
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
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 void MultipartStreamReader::AddChunk(const std::string& chunk)
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 if (!chunk.empty())
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 AddChunk(chunk.c_str(), chunk.size());
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263
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::CloseStream()
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 (buffer_.GetNumBytes() != 0)
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 ParseStream();
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 bool MultipartStreamReader::GetMainContentType(std::string& contentType,
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 const HttpHeaders& headers)
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 HttpHeaders::const_iterator it = headers.find("content-type");
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 if (it == headers.end())
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 return false;
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 else
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 contentType = it->second;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 return true;
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 }
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
3399
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
291 bool MultipartStreamReader::ParseMultipartContentType(std::string& contentType,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
292 std::string& subType,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
293 std::string& boundary,
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
294 const std::string& contentTypeHeader)
3398
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 std::vector<std::string> tokens;
3399
4e8205871967 OrthancPluginRegisterMultipartRestCallback() is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3398
diff changeset
297 Orthanc::Toolbox::TokenizeString(tokens, contentTypeHeader, ';');
3398
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 if (tokens.empty())
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 return false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 contentType = Orthanc::Toolbox::StripSpaces(tokens[0]);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 Orthanc::Toolbox::ToLowerCase(contentType);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 if (contentType.empty())
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 return false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 bool valid = false;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 subType.clear();
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 for (size_t i = 0; i < tokens.size(); i++)
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 std::vector<std::string> items;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 Orthanc::Toolbox::TokenizeString(items, tokens[i], '=');
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 (items.size() == 2)
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 if (boost::iequals("boundary", Orthanc::Toolbox::StripSpaces(items[0])))
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 boundary = Orthanc::Toolbox::StripSpaces(items[1]);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 valid = !boundary.empty();
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 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
328 {
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 subType = Orthanc::Toolbox::StripSpaces(items[1]);
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 Orthanc::Toolbox::ToLowerCase(subType);
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 // 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
333 // 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
334 if (subType.size() >= 2 &&
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 subType[0] == '"' &&
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 subType[subType.size() - 1] == '"')
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 subType = subType.substr(1, subType.size() - 2);
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 }
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 return valid;
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 }
4acd1431e603 new classes: StringMatcher and MultipartStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 }