-
本文翻译自编程🐷的这篇文章
-
博主自己也去看了编程🐷的相关源码(通过vscode查找)
-
代码版权所有(c)2012 The Chromium Authors
-
从本文开始,博主将会对前端有关的Chrome源码进行分析,毕竟计网原理和DOM构建之类的学习要是有代码实现就很好了。
HTTP报文格式
- Chrome设置UA字段
Chrome的UA字段是这么拼的:
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3345.0 Safari/537.36
UA字段格式
Mozilla/5.0 ([os_info]) AppleWebKit/[webkit_major_version].[webkit_minor_version] (KHTML, like Gecko) [chrome_version] Safari/[webkit_major_version].[webkit_minor_version]
源码
(chromium/src.git-refs_tags_88.0.4322.1/content/common/user_agent.cc)
:这个文件就是想获取设备信息产品信息添加到UA字段中。
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
std::string BuildUserAgentFromOSAndProduct(const std::string& os_info,
const std::string& product) {
// Derived from Safari's UA string.
// This is done to expose our product name in a manner that is maximally
// compatible with Safari, we hope!!
std::string user_agent;
base::StringAppendF(
&user_agent,
"Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d",
os_info.c_str(