1 // Copyright (c) 2011 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.
5 #include "chrome_frame/html_utils.h"
10 #include "base/string_util.h"
11 #include "base/string_tokenizer.h"
12 #include "base/stringprintf.h"
13 #include "chrome/common/chrome_version_info.h"
14 #include "chrome_frame/utils.h"
15 #include "net/base/net_util.h"
16 #include "webkit/glue/user_agent.h"
18 const wchar_t kQuotes
[] = L
"\"'";
19 const char kXFrameOptionsHeader
[] = "X-Frame-Options";
20 const char kXFrameOptionsValueAllowAll
[] = "allowall";
22 HTMLScanner::StringRange::StringRange() {
25 HTMLScanner::StringRange::StringRange(StrPos start
, StrPos end
)
26 : start_(start
), end_(end
) {
29 bool HTMLScanner::StringRange::LowerCaseEqualsASCII(const char* other
) const {
30 return ::LowerCaseEqualsASCII(start_
, end_
, other
);
33 bool HTMLScanner::StringRange::Equals(const wchar_t* other
) const {
34 int ret
= wcsncmp(&start_
[0], other
, end_
- start_
);
36 ret
= (other
[end_
- start_
] == L
'\0') ? 0 : -1;
40 std::wstring
HTMLScanner::StringRange::Copy() const {
41 return std::wstring(start_
, end_
);
44 bool HTMLScanner::StringRange::GetTagName(std::wstring
* tag_name
) const {
45 if (*start_
!= L
'<') {
46 LOG(ERROR
) << "Badly formatted tag found";
50 StrPos name_start
= start_
;
52 while (name_start
< end_
&& IsWhitespace(*name_start
))
55 if (name_start
>= end_
) {
56 // We seem to have a degenerate tag (i.e. < >). Return false here.
60 StrPos name_end
= name_start
+ 1;
61 while (name_end
< end_
&& !IsWhitespace(*name_end
))
64 if (name_end
> end_
) {
65 // This looks like an improperly formatted tab ('<foo'). Return false here.
69 tag_name
->assign(name_start
, name_end
);
74 bool HTMLScanner::StringRange::GetTagAttribute(const wchar_t* attribute_name
,
75 StringRange
* attribute_value
) const {
76 if (NULL
== attribute_name
|| NULL
== attribute_value
) {
81 // Use this so we can use the convenience method LowerCaseEqualsASCII()
82 // from string_util.h.
83 std::string
search_name_ascii(WideToASCII(attribute_name
));
85 WStringTokenizer
tokenizer(start_
, end_
, L
" =/");
86 tokenizer
.set_options(WStringTokenizer::RETURN_DELIMS
);
88 // Set up the quote chars so that we get quoted attribute values as single
90 tokenizer
.set_quote_chars(L
"\"'");
92 const bool PARSE_STATE_NAME
= true;
93 const bool PARSE_STATE_VALUE
= false;
94 bool parse_state
= PARSE_STATE_NAME
;
96 // Used to skip the first token, which is the tag name.
97 bool first_token_skipped
= false;
99 // This is set during a loop iteration in which an '=' sign was spotted.
100 // It is used to filter out degenerate tags such as:
102 bool last_token_was_delim
= false;
104 // Set this if the attribute name has been found that we might then
105 // pick up the value in the next loop iteration.
106 bool attribute_name_found
= false;
108 while (tokenizer
.GetNext()) {
109 // If we have a whitespace delimiter, just keep going. Cases of this should
110 // be reduced by the CollapseWhitespace call. If we have an '=' character,
111 // we update our state and reiterate.
112 if (tokenizer
.token_is_delim()) {
113 if (*tokenizer
.token_begin() == L
'=') {
114 if (last_token_was_delim
) {
115 // Looks like we have a badly formed tag, just stop parsing now.
118 parse_state
= !parse_state
;
119 last_token_was_delim
= true;
124 last_token_was_delim
= false;
126 // The first non-delimiter token is the tag name, which we don't want.
127 if (!first_token_skipped
) {
128 first_token_skipped
= true;
132 if (PARSE_STATE_NAME
== parse_state
) {
133 // We have a tag name, check to see if it matches our target name:
134 if (::LowerCaseEqualsASCII(tokenizer
.token_begin(), tokenizer
.token_end(),
135 search_name_ascii
.c_str())) {
136 attribute_name_found
= true;
139 } else if (PARSE_STATE_VALUE
== parse_state
&& attribute_name_found
) {
140 attribute_value
->start_
= tokenizer
.token_begin();
141 attribute_value
->end_
= tokenizer
.token_end();
143 // Unquote the attribute value if need be.
144 attribute_value
->UnQuote();
147 } else if (PARSE_STATE_VALUE
== parse_state
) {
148 // If we haven't found the attribute name we want yet, ignore this token
149 // and go back to looking for our name.
150 parse_state
= PARSE_STATE_NAME
;
157 bool HTMLScanner::StringRange::UnQuote() {
158 if (start_
+ 2 > end_
) {
159 // String's too short to be quoted, bail.
163 if ((*start_
== L
'\'' && *(end_
- 1) == L
'\'') ||
164 (*start_
== L
'"' && *(end_
- 1) == L
'"')) {
173 HTMLScanner::HTMLScanner(const wchar_t* html_string
)
174 : html_string_(CollapseWhitespace(html_string
, true)),
178 void HTMLScanner::GetTagsByName(const wchar_t* name
, StringRangeList
* tag_list
,
179 const wchar_t* stop_tag
) {
180 DCHECK(NULL
!= name
);
181 DCHECK(NULL
!= tag_list
);
182 DCHECK(NULL
!= stop_tag
);
184 StringRange
remaining_html(html_string_
.begin(), html_string_
.end());
186 std::wstring
search_name(name
);
187 TrimWhitespace(search_name
, TRIM_ALL
, &search_name
);
189 // Use this so we can use the convenience method LowerCaseEqualsASCII()
190 // from string_util.h.
191 std::string
search_name_ascii(WideToASCII(search_name
));
192 std::string
stop_tag_ascii(WideToASCII(stop_tag
));
194 StringRange current_tag
;
195 std::wstring current_name
;
196 while (NextTag(&remaining_html
, ¤t_tag
)) {
197 if (current_tag
.GetTagName(¤t_name
)) {
198 if (LowerCaseEqualsASCII(current_name
, search_name_ascii
.c_str())) {
199 tag_list
->push_back(current_tag
);
200 } else if (LowerCaseEqualsASCII(current_name
, stop_tag_ascii
.c_str())) {
201 // We hit the stop tag so it's time to go home.
212 ScanState() : in_quote(false), in_escape(false) {}
215 bool HTMLScanner::IsQuote(wchar_t c
) {
216 return quotes_
.find(c
) != std::wstring::npos
;
219 bool HTMLScanner::IsHTMLCommentClose(const StringRange
* html_string
,
221 if (pos
< html_string
->end_
&& pos
> html_string
->start_
+ 2 &&
223 return *(pos
-1) == L
'-' && *(pos
-2) == L
'-';
228 bool HTMLScanner::IsIEConditionalCommentClose(const StringRange
* html_string
,
230 if (pos
< html_string
->end_
&& pos
> html_string
->start_
+ 2 &&
232 return *(pos
-1) == L
']';
238 bool HTMLScanner::NextTag(StringRange
* html_string
, StringRange
* tag
) {
239 DCHECK(NULL
!= html_string
);
242 tag
->start_
= html_string
->start_
;
243 while (tag
->start_
< html_string
->end_
&& *tag
->start_
!= L
'<') {
247 // we went past the end of the string.
248 if (tag
->start_
>= html_string
->end_
) {
252 tag
->end_
= tag
->start_
+ 1;
254 // Get the tag name to see if we are in an HTML comment. If we are, then
255 // don't consider quotes. This should work for example:
256 // <!-- foo ' --> <meta foo='bar'>
257 std::wstring tag_name
;
258 StringRange
start_range(tag
->start_
, html_string
->end_
);
259 start_range
.GetTagName(&tag_name
);
260 if (StartsWith(tag_name
, L
"!--[if", true)) {
261 // This looks like the beginning of an IE conditional comment, scan until
262 // we hit the end which always looks like ']>'. For now we disregard the
263 // contents of the condition, and always assume true.
264 // TODO(robertshield): Optionally support the grammar defined by
265 // http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx#syntax.
266 while (tag
->end_
< html_string
->end_
&&
267 !IsIEConditionalCommentClose(html_string
, tag
->end_
)) {
270 } else if (StartsWith(tag_name
, L
"!--", true)) {
271 // We're inside a comment tag which ends in '-->'. Keep going until we
273 while (tag
->end_
< html_string
->end_
&&
274 !IsHTMLCommentClose(html_string
, tag
->end_
)) {
277 } else if (StartsWith(tag_name
, L
"![endif", true)) {
278 // We're inside the closing tag of an IE conditional comment which ends in
279 // either '-->' of ']>'. Keep going until we reach the end.
280 while (tag
->end_
< html_string
->end_
&&
281 !IsIEConditionalCommentClose(html_string
, tag
->end_
) &&
282 !IsHTMLCommentClose(html_string
, tag
->end_
)) {
286 // Properly handle quoted strings within non-comment tags by maintaining
287 // some state while scanning. Specifically, we have to maintain state on
288 // whether we are inside a string, what the string terminating character
289 // will be and whether we are inside an escape sequence.
291 while (tag
->end_
< html_string
->end_
) {
292 if (state
.in_quote
) {
293 if (state
.in_escape
) {
294 state
.in_escape
= false;
295 } else if (*tag
->end_
== '\\') {
296 state
.in_escape
= true;
297 } else if (*tag
->end_
== state
.quote_char
) {
298 state
.in_quote
= false;
301 state
.in_quote
= IsQuote(state
.quote_char
= *tag
->end_
);
304 if (!state
.in_quote
&& *tag
->end_
== L
'>') {
311 // We hit the end_ but found no matching tag closure. Consider this an
312 // incomplete tag and do not report it.
313 if (tag
->end_
>= html_string
->end_
)
316 // Modify html_string to point to just beyond the end_ of the current tag.
317 html_string
->start_
= tag
->end_
+ 1;
322 namespace http_utils
{
324 const char kChromeFrameUserAgent
[] = "chromeframe";
325 static char g_cf_user_agent
[100] = {0};
326 static char g_chrome_user_agent
[255] = {0};
328 const char* GetChromeFrameUserAgent() {
329 if (!g_cf_user_agent
[0]) {
330 _pAtlModule
->m_csStaticDataInitAndTypeInfo
.Lock();
331 if (!g_cf_user_agent
[0]) {
332 uint32 high_version
= 0, low_version
= 0;
333 GetModuleVersion(reinterpret_cast<HMODULE
>(&__ImageBase
), &high_version
,
335 wsprintfA(g_cf_user_agent
, "%s/%i.%i.%i.%i", kChromeFrameUserAgent
,
336 HIWORD(high_version
), LOWORD(high_version
),
337 HIWORD(low_version
), LOWORD(low_version
));
339 _pAtlModule
->m_csStaticDataInitAndTypeInfo
.Unlock();
341 return g_cf_user_agent
;
344 std::string
AddChromeFrameToUserAgentValue(const std::string
& value
) {
346 DLOG(WARNING
) << "empty user agent value";
350 DCHECK_EQ(false, StartsWithASCII(value
, "User-Agent:", true));
352 if (value
.find(kChromeFrameUserAgent
) != std::string::npos
) {
353 // Our user agent has already been added.
357 std::string
ret(value
);
358 std::string::size_type insert_position
= ret
.find(')');
359 if (insert_position
!= std::string::npos
) {
360 if (insert_position
> 1 && isalnum(ret
[insert_position
- 1]))
361 ret
.insert(insert_position
++, ";");
362 ret
.insert(insert_position
++, " ");
363 ret
.insert(insert_position
, GetChromeFrameUserAgent());
366 ret
+= GetChromeFrameUserAgent();
372 std::string
GetDefaultUserAgentHeaderWithCFTag() {
373 std::string
ua(GetDefaultUserAgent());
374 return "User-Agent: " + AddChromeFrameToUserAgentValue(ua
);
377 const char* GetChromeUserAgent() {
378 if (!g_chrome_user_agent
[0]) {
379 _pAtlModule
->m_csStaticDataInitAndTypeInfo
.Lock();
380 if (!g_chrome_user_agent
[0]) {
383 chrome::VersionInfo version_info
;
384 std::string
product("Chrome/");
385 product
+= version_info
.is_valid() ? version_info
.Version()
388 ua
= webkit_glue::BuildUserAgentFromProduct(product
);
390 DCHECK(ua
.length() < arraysize(g_chrome_user_agent
));
391 lstrcpynA(g_chrome_user_agent
, ua
.c_str(),
392 arraysize(g_chrome_user_agent
) - 1);
394 _pAtlModule
->m_csStaticDataInitAndTypeInfo
.Unlock();
396 return g_chrome_user_agent
;
399 std::string
GetDefaultUserAgent() {
401 DWORD size
= MAX_PATH
;
402 HRESULT hr
= E_OUTOFMEMORY
;
403 for (int retries
= 1; hr
== E_OUTOFMEMORY
&& retries
<= 10; ++retries
) {
404 hr
= ::ObtainUserAgentString(0, WriteInto(&ret
, size
+ 1), &size
);
405 if (hr
== E_OUTOFMEMORY
) {
406 size
= MAX_PATH
* retries
;
407 } else if (SUCCEEDED(hr
)) {
408 // Truncate the extra allocation.
410 ret
.resize(size
- 1);
415 NOTREACHED() << base::StringPrintf("ObtainUserAgentString==0x%08X", hr
);
416 return std::string();
422 bool HasFrameBustingHeader(const std::string
& http_headers
) {
423 // NOTE: We cannot use net::GetSpecificHeader() here since when there are
424 // multiple instances of a header that returns the first value seen, and we
425 // need to look at all instances.
426 net::HttpUtil::HeadersIterator
it(http_headers
.begin(), http_headers
.end(),
428 while (it
.GetNext()) {
429 if (!lstrcmpiA(it
.name().c_str(), kXFrameOptionsHeader
) &&
430 lstrcmpiA(it
.values().c_str(), kXFrameOptionsValueAllowAll
))
436 std::string
GetHttpHeaderFromHeaderList(const std::string
& header
,
437 const std::string
& headers
) {
438 net::HttpUtil::HeadersIterator
it(headers
.begin(), headers
.end(), "\r\n");
439 while (it
.GetNext()) {
440 if (!lstrcmpiA(it
.name().c_str(), header
.c_str()))
441 return std::string(it
.values_begin(), it
.values_end());
443 return std::string();
446 } // namespace http_utils