Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "modules/fetch/MultipartParser.h" | |
| 6 | |
| 7 #include "public/platform/Platform.h" | |
| 8 | |
| 9 #include <algorithm> | |
| 10 #include <utility> | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 namespace { | |
| 15 constexpr char kCloseDelimiterSuffix[] = "--\r\n"; | |
| 16 constexpr char kDelimiterSuffix[] = "\r\n"; | |
| 17 constexpr size_t kDelimiterOffsetForEmptyPreamble = 2u; // For no "\r\n" pre fix. | |
| 18 constexpr size_t kDelimiterOffsetForEmptyBody = kDelimiterOffsetForEmptyPrea mble; | |
| 19 } | |
|
yhirano
2016/09/12 02:20:49
// namespace
e_hakkinen
2016/09/16 13:41:36
Done.
| |
| 20 | |
| 21 MultipartParser::MultipartParser(Vector<char> boundary, Client* client) | |
| 22 : m_delimiter(std::move(boundary)) | |
| 23 , m_client(client) | |
| 24 , m_seenDelimiterLength(0u) | |
| 25 , m_seenDelimiterOffset(kDelimiterOffsetForEmptyPreamble) | |
| 26 { | |
| 27 // The delimiter consists of "\r\n" and a dash delimiter which consists of | |
| 28 // "--" and a boundary. | |
| 29 m_delimiter.prepend("\r\n--", 4u); | |
| 30 } | |
| 31 | |
| 32 bool MultipartParser::appendData(const char* bytes, size_t size) | |
| 33 { | |
| 34 DCHECK_NE(Finished, m_state); | |
| 35 DCHECK_NE(Cancelled, m_state); | |
| 36 | |
| 37 while (size > 0u) { | |
| 38 size_t index = 0u; | |
| 39 | |
| 40 switch (m_state) { | |
| 41 case ParsingPreamble: | |
| 42 // Parse either a preamble and a delimiter or a dash delimiter. | |
| 43 if (parseDelimiter(bytes, size, &index)) | |
| 44 m_state = ParsingDelimiterSuffix; | |
| 45 break; | |
| 46 | |
| 47 case ParsingDelimiterSuffix: | |
| 48 // Parse transport padding and "\r\n" after a delimiter. | |
| 49 if (parseDelimiterSuffix(bytes, size, &index, kDelimiterSuffix)) | |
| 50 m_state = ParsingPartHeaderFields; | |
| 51 break; | |
| 52 | |
| 53 case ParsingPartHeaderFields: { | |
| 54 // Parse part header fields (which ends with "\r\n") and an empty | |
| 55 // line (which also ends with "\r\n"). | |
| 56 WebURLResponse response; | |
| 57 | |
| 58 // Combine the current bytes with previously seen header bytes if | |
| 59 // needed. | |
| 60 const char* headerBytes = bytes + index; | |
| 61 size_t headerSize = size - index; | |
| 62 if (!m_seenHeaderBytes.isEmpty()) { | |
| 63 m_seenHeaderBytes.append(headerBytes, headerSize); | |
| 64 headerBytes = m_seenHeaderBytes.data(); | |
| 65 headerSize = m_seenHeaderBytes.size(); | |
| 66 } | |
| 67 | |
| 68 size_t end = 0; | |
| 69 if (!Platform::current()->parseMultipartHeadersFromBody(headerBytes, headerSize, &response, &end)) { | |
| 70 // Store the current bytes for the next call. | |
| 71 if (headerBytes != m_seenHeaderBytes.data()) | |
|
yhirano
2016/09/12 02:20:49
if (m_seenHeaderBytes.isEmpty()) is easier to unde
e_hakkinen
2016/09/16 13:41:36
Done.
| |
| 72 m_seenHeaderBytes.append(headerBytes, headerSize); | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 m_seenDelimiterLength = 0u; | |
| 77 m_seenDelimiterOffset = kDelimiterOffsetForEmptyBody; | |
| 78 m_seenHeaderBytes.shrink(0); | |
|
yhirano
2016/09/12 02:20:49
clear()
e_hakkinen
2016/09/16 13:41:36
Done.
| |
| 79 m_state = ParsingPartOctets; | |
| 80 index = size - (headerSize - end); | |
| 81 m_client->partHeaderFieldsInMultipartReceived( | |
| 82 response.toResourceResponse()); | |
| 83 break; | |
| 84 } | |
| 85 | |
| 86 case ParsingPartOctets: { | |
| 87 // Parse either a non-empty part octets and a delimiter or an empty | |
| 88 // part octets and a dash delimiter. | |
| 89 size_t initialSeenDelimiterLength = m_seenDelimiterLength; | |
| 90 size_t initialSeenDelimiterOffset = m_seenDelimiterOffset; | |
| 91 if (parseDelimiter(bytes, size, &index)) | |
| 92 m_state = ParsingDelimiterOrCloseDelimiterSuffix; | |
| 93 if (index >= m_seenDelimiterLength && initialSeenDelimiterLength > 0 u) | |
| 94 m_client->partDataInMultipartReceived(m_delimiter.data() + initi alSeenDelimiterOffset, initialSeenDelimiterLength); | |
| 95 if (index > m_seenDelimiterLength) | |
| 96 m_client->partDataInMultipartReceived(bytes, index - m_seenDelim iterLength); | |
| 97 if (m_state == ParsingDelimiterOrCloseDelimiterSuffix) | |
| 98 m_client->partDataInMultipartFullyReceived(); | |
| 99 break; | |
| 100 } | |
| 101 | |
| 102 case ParsingDelimiterOrCloseDelimiterSuffix: | |
| 103 m_state = bytes[index] != '-' ? ParsingDelimiterSuffix : ParsingClos eDelimiterSuffix; | |
|
yhirano
2016/09/12 02:20:49
This is not correct. When parsing the first "dash-
e_hakkinen
2016/09/16 13:41:36
No, it is correct.
yhirano
2016/09/20 09:49:53
Thank you, I understand.
| |
| 104 break; | |
| 105 | |
| 106 case ParsingCloseDelimiterSuffix: | |
| 107 // Parse "--", transport padding and "\r\n" after a delimiter | |
| 108 // (a delimiter and "--" constitute a close delimiter). | |
| 109 if (parseDelimiterSuffix(bytes, size, &index, kCloseDelimiterSuffix) ) | |
| 110 m_state = ParsingEpilogue; | |
| 111 break; | |
| 112 | |
| 113 case ParsingEpilogue: | |
| 114 // Data in an epilogue should be ignored. | |
| 115 return true; | |
| 116 | |
| 117 case Cancelled: | |
| 118 case Finished: | |
| 119 // The client changed the state. | |
| 120 return true; | |
| 121 | |
| 122 case Failed: | |
| 123 // Keep failing. | |
| 124 return false; | |
| 125 } | |
| 126 | |
| 127 bytes += index; | |
| 128 size -= index; | |
| 129 } | |
| 130 | |
| 131 return true; | |
| 132 } | |
| 133 | |
| 134 void MultipartParser::cancel() | |
| 135 { | |
| 136 m_state = Cancelled; | |
| 137 } | |
| 138 | |
| 139 bool MultipartParser::finish() | |
| 140 { | |
| 141 DCHECK_NE(Cancelled, m_state); | |
| 142 | |
| 143 State initialState = m_state; | |
| 144 | |
| 145 if (m_state == ParsingPartOctets && m_seenDelimiterLength > 0u && m_seenDeli miterOffset + m_seenDelimiterLength < m_delimiter.size()) { | |
| 146 // The end of append bytes looked like a delimiter but was not a full | |
| 147 // one, after all. Treat the those bytes as part of part octets. | |
| 148 m_client->partDataInMultipartReceived( | |
| 149 m_delimiter.data() + m_seenDelimiterOffset, m_seenDelimiterLength); | |
| 150 } | |
| 151 m_state = Finished; | |
| 152 | |
| 153 switch (initialState) { | |
| 154 case ParsingCloseDelimiterSuffix: | |
| 155 // Require a full close delimiter consisting of a delimiter and "--" | |
| 156 // but ignore missing or partial "\r\n" after that. | |
| 157 return seenDelimiterSuffixLength() >= 2u; | |
| 158 case ParsingEpilogue: | |
| 159 case Finished: | |
| 160 return true; | |
| 161 default: | |
| 162 return false; | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 size_t MultipartParser::countNonDelimiterBytes(const char* bytes, size_t size) c onst | |
| 167 { | |
| 168 const char* p = static_cast<const char*>(memchr(bytes, '\r', size)); | |
| 169 if (p) | |
| 170 return static_cast<size_t>(p - bytes); | |
| 171 return size; | |
| 172 } | |
| 173 | |
| 174 size_t MultipartParser::countPossibleDelimiterBytes(const char* bytes, size_t si ze) const | |
| 175 { | |
| 176 size_t index = 0u; | |
| 177 while (index < size && m_seenDelimiterOffset + m_seenDelimiterLength + index < m_delimiter.size() && bytes[index] == m_delimiter[m_seenDelimiterOffset + m_s eenDelimiterLength + index]) | |
| 178 ++index; | |
| 179 return index; | |
| 180 } | |
| 181 | |
| 182 size_t MultipartParser::countTransportPaddingBytes(const char* bytes, size_t siz e) const | |
| 183 { | |
| 184 size_t index = 0u; | |
| 185 while (index < size && (bytes[index] == '\t' || bytes[index] == ' ')) | |
| 186 ++index; | |
| 187 return index; | |
| 188 } | |
| 189 | |
| 190 bool MultipartParser::parseDelimiter(const char* bytes, size_t size, size_t* ind ex) | |
| 191 { | |
| 192 for (;;) { | |
| 193 // Try to continue reading a delimiter. | |
| 194 size_t possibleDelimiterBytes = countPossibleDelimiterBytes(bytes + *ind ex, size - *index); | |
| 195 if (possibleDelimiterBytes > 0u) { | |
|
yhirano
2016/09/12 02:20:49
Is it OK to execute these statements unconditional
| |
| 196 m_seenDelimiterLength += possibleDelimiterBytes; | |
| 197 *index += possibleDelimiterBytes; | |
| 198 } | |
|
yhirano
2016/09/12 02:20:49
DCHECK_LE(*index, size);
| |
| 199 size_t seenDelimiterEnd = m_seenDelimiterOffset + m_seenDelimiterLength; | |
| 200 if (seenDelimiterEnd == m_delimiter.size()) | |
| 201 return true; | |
| 202 if (*index >= size) | |
| 203 break; | |
| 204 | |
| 205 // Jump to the next possible delimiter or to the end of bytes. | |
| 206 m_seenDelimiterLength = 0u; | |
| 207 m_seenDelimiterOffset = 0u; | |
| 208 *index += countNonDelimiterBytes(bytes + *index, size - *index); | |
|
yhirano
2016/09/12 02:20:49
DCHECK_LE(*index, size);
| |
| 209 } | |
| 210 return false; | |
| 211 } | |
| 212 | |
| 213 bool MultipartParser::parseDelimiterSuffix(const char* bytes, size_t size, size_ t* index, const char* suffix) | |
| 214 { | |
| 215 while (char expected = suffix[seenDelimiterSuffixLength()]) { | |
| 216 if (expected == '\r') | |
|
yhirano
2016/09/12 02:20:49
Hmm... I don't like this special case. The functio
e_hakkinen
2016/09/16 13:41:36
Removed.
| |
| 217 *index += countTransportPaddingBytes(bytes + *index, size - *index); | |
| 218 if (*index >= size) | |
| 219 return false; | |
| 220 if (bytes[(*index)++] != expected) { | |
| 221 m_state = Failed; | |
| 222 return false; | |
| 223 } | |
| 224 ++m_seenDelimiterLength; | |
|
yhirano
2016/09/12 02:20:49
I don't understand - what you are seeing here is n
e_hakkinen
2016/09/16 13:41:36
Sorry for reusing m_seenDelimiterLength for m_seen
| |
| 225 } | |
| 226 return true; | |
| 227 } | |
| 228 | |
| 229 size_t MultipartParser::seenDelimiterSuffixLength() const | |
| 230 { | |
| 231 return m_seenDelimiterOffset + m_seenDelimiterLength - m_delimiter.size(); | |
| 232 } | |
| 233 | |
| 234 DEFINE_TRACE(MultipartParser) | |
| 235 { | |
| 236 visitor->trace(m_client); | |
| 237 } | |
| 238 | |
| 239 } // namespace blink | |
| OLD | NEW |