Commit cd0f2361 authored by Takeshi Yoshino's avatar Takeshi Yoshino Committed by Commit Bot

Clean up XMLHttpRequest::FindCharsetInMediaType() and fix a bug in a while-loop condition in it

Bug: 674329
Change-Id: Ia815eb26487c09b69dabf61db018eb075c6b19e1
Reviewed-on: https://chromium-review.googlesource.com/773759
Commit-Queue: Takeshi Yoshino <tyoshino@chromium.org>
Reviewed-by: default avatarYutaka Hirano <yhirano@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517749}
parent 6ba6f646
......@@ -117,22 +117,23 @@ class ScopedEventDispatchProtect final {
// TODO(tyoshino): Switch XHR to use HttpUtil. See crbug.com/743311.
void FindCharsetInMediaType(const String& media_type,
unsigned& charset_pos,
unsigned& charset_len,
unsigned start) {
charset_pos = start;
unsigned& charset_len) {
charset_len = 0;
size_t pos = start;
size_t pos = charset_pos;
unsigned length = media_type.length();
while (pos < length) {
pos = media_type.FindIgnoringASCIICase("charset", pos);
if (pos == kNotFound || !pos) {
charset_len = 0;
if (pos == kNotFound)
return;
// Give up if we find "charset" at the head.
if (!pos)
return;
}
// is what we found a beginning of a word?
// Now check that "charset" is not a substring of some longer word.
if (media_type[pos - 1] > ' ' && media_type[pos - 1] != ';') {
pos += 7;
continue;
......@@ -140,55 +141,46 @@ void FindCharsetInMediaType(const String& media_type,
pos += 7;
// skip whitespace
while (pos != length && media_type[pos] <= ' ')
while (pos < length && media_type[pos] <= ' ')
++pos;
// this "charset" substring wasn't a parameter
// name, but there may be others
if (media_type[pos++] != '=')
continue;
// Treat this as a charset parameter.
if (media_type[pos++] == '=')
break;
}
while (pos != length && (media_type[pos] <= ' ' || media_type[pos] == '"' ||
media_type[pos] == '\''))
++pos;
while (pos < length && (media_type[pos] <= ' ' || media_type[pos] == '"' ||
media_type[pos] == '\''))
++pos;
// we don't handle spaces within quoted parameter values, because charset
// names cannot have any
unsigned endpos = pos;
while (pos != length && media_type[endpos] > ' ' &&
media_type[endpos] != '"' && media_type[endpos] != '\'' &&
media_type[endpos] != ';')
++endpos;
charset_pos = pos;
charset_pos = pos;
charset_len = endpos - pos;
return;
}
// we don't handle spaces within quoted parameter values, because charset
// names cannot have any
while (pos < length && media_type[pos] > ' ' && media_type[pos] != '"' &&
media_type[pos] != '\'' && media_type[pos] != ';')
++pos;
charset_len = pos - charset_pos;
}
String ExtractCharsetFromMediaType(const String& media_type) {
unsigned pos, len;
FindCharsetInMediaType(media_type, pos, len, 0);
unsigned pos = 0;
unsigned len = 0;
FindCharsetInMediaType(media_type, pos, len);
return media_type.Substring(pos, len);
}
void ReplaceCharsetInMediaType(String& media_type,
const String& charset_value) {
unsigned pos = 0, len = 0;
FindCharsetInMediaType(media_type, pos, len, 0);
unsigned pos = 0;
if (!len) {
// When no charset found, do nothing.
return;
}
// Found at least one existing charset, replace all occurrences with new
// charset.
while (len) {
while (true) {
unsigned len = 0;
FindCharsetInMediaType(media_type, pos, len);
if (!len)
return;
media_type.replace(pos, len, charset_value);
unsigned start = pos + charset_value.length();
FindCharsetInMediaType(media_type, pos, len, start);
pos += charset_value.length();
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment