Chromium form表单post提交过程分析c++

news2024/10/19 10:22:06

form表单get提交参考之前文章Chromium 前端form表单提交过程分析c++-CSDN博客

一、表单post提交测试例子:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>菜鸟教程(runoob.com)</title> 
</head>
<body>

    <h2>表单</h2>

    <form action="/" method="post">
        <!-- 文本输入框 -->
        <label for="name">用户名:</label>
        <input type="text" id="name" name="name" required>

        <br>

        <!-- 密码输入框 -->
        <label for="password">密码:</label>
        <input type="password" id="password" name="password" required>

        <br>

        <!-- 单选按钮 -->
        <label>性别:</label>
        <input type="radio" id="male" name="gender" value="male" checked>
        <label for="male">男</label>
        <input type="radio" id="female" name="gender" value="female">
        <label for="female">女</label>

        <br>

        <!-- 复选框 -->
        <input type="checkbox" id="subscribe" name="subscribe" checked>
        <label for="subscribe">订阅推送信息</label>

        <br>

        <!-- 下拉列表 -->
        <label for="country">国家:</label>
        <select id="country" name="country">
            <option value="cn">CN</option>
            <option value="usa">USA</option>
            <option value="uk">UK</option>
        </select>

        <br>

        <!-- 提交按钮 -->
        <input type="submit" value="提交">
    </form>

</body>
</html>

二、启动调试打开测试页:

    在\src\third_party\blink\renderer\core\loader\form_submission.cc 

FormSubmission::Create函数打断点:

 1.1)、 FormSubmission::Attributes copied_attributes;
              copied_attributes.CopyFrom(attributes);

获取前端:

名称类型
copied_attributes{method_=kPostMethod (1) is_multi_part_form_=false action_=[1] "/" ...}blink::FormSubmission::Attributes
method_kPostMethod (1)blink::FormSubmission::SubmitMethod
is_multi_part_form_falsebool
▶ action_[1] "/"WTF::String
▶ target_(null)WTF::AtomicString
▶ encoding_type_[33] "application/x-www-form-urlencoded"WTF::AtomicString
▶ accept_charset_(null)WTF::String

1.2)、  FormData* dom_form_data = form->ConstructEntryList(
      submit_button, data_encoding.EncodingForFormSubmission());
  DCHECK(dom_form_data);函数遍历form表单input属性 构建key=value

1.3)、调用form_data = dom_form_data->EncodeFormData()进行EncodeFormData

1.4)、将构造好的数据form_data设置到请求body里面:

    resource_request->SetHttpMethod(http_names::kPOST);
    resource_request->SetHttpBody(form_data);

看下设置到body中的值:

名称类型
form_body.ptr_->elements_.buffer_[0].data_[60] 0x000005500040df40 "name"...WTF::Vector<char,0,WTF::PartitionAllocator>

 

form_body.ptr_->elements_.buffer_[0].data_内存地址0x000005500040df40对应值如下:

已经将刚才填写的内容设置到body。 

 三、wireshark抓包验证:

可以看到和调试的一样结果:

四、结论:

1.application/x-www-form-urlencoded

  1. GET方式,会将表单中的数据(键值对)经过urlencode编码后追加到url中。
  2. POST方式,会将表单中的数据经过urlencode编码后放在request body 中。

2.multipart/form-data

当需要在表单内上传文件时(二进制流数据)时,就需要使用 multipart/form-data。

 附:FormSubmission::Create函数定义如下

FormSubmission* FormSubmission::Create(HTMLFormElement* form,
                                       const Attributes& attributes,
                                       const Event* event,
                                       HTMLFormControlElement* submit_button) {
  DCHECK(form);

  FormSubmission::Attributes copied_attributes;
  copied_attributes.CopyFrom(attributes);
  if (submit_button) {
    AtomicString attribute_value;
    if (!(attribute_value =
              submit_button->FastGetAttribute(html_names::kFormactionAttr))
             .IsNull())
      copied_attributes.ParseAction(attribute_value);
    if (!(attribute_value =
              submit_button->FastGetAttribute(html_names::kFormenctypeAttr))
             .IsNull())
      copied_attributes.UpdateEncodingType(attribute_value);
    if (!(attribute_value =
              submit_button->FastGetAttribute(html_names::kFormmethodAttr))
             .IsNull())
      copied_attributes.UpdateMethodType(attribute_value);
    if (!(attribute_value =
              submit_button->FastGetAttribute(html_names::kFormtargetAttr))
             .IsNull())
      copied_attributes.SetTarget(attribute_value);
  }

  if (copied_attributes.Method() == kDialogMethod) {
    if (submit_button) {
      return MakeGarbageCollected<FormSubmission>(
          submit_button->ResultForDialogSubmit());
    }
    return MakeGarbageCollected<FormSubmission>("");
  }

  Document& document = form->GetDocument();
  KURL action_url = document.CompleteURL(copied_attributes.Action().empty()
                                             ? document.Url().GetString()
                                             : copied_attributes.Action());

  if ((document.domWindow()->GetSecurityContext().GetInsecureRequestPolicy() &
       mojom::blink::InsecureRequestPolicy::kUpgradeInsecureRequests) !=
          mojom::blink::InsecureRequestPolicy::kLeaveInsecureRequestsAlone &&
      action_url.ProtocolIs("http") &&
      !network::IsUrlPotentiallyTrustworthy(GURL(action_url))) {
    UseCounter::Count(document,
                      WebFeature::kUpgradeInsecureRequestsUpgradedRequestForm);
    action_url.SetProtocol("https");
    if (action_url.Port() == 80)
      action_url.SetPort(443);
  }

  bool is_mailto_form = action_url.ProtocolIs("mailto");
  bool is_multi_part_form = false;
  AtomicString encoding_type = copied_attributes.EncodingType();

  if (copied_attributes.Method() == kPostMethod) {
    is_multi_part_form = copied_attributes.IsMultiPartForm();
    if (is_multi_part_form && is_mailto_form) {
      encoding_type = AtomicString("application/x-www-form-urlencoded");
      is_multi_part_form = false;
    }
  }
  WTF::TextEncoding data_encoding =
      is_mailto_form
          ? UTF8Encoding()
          : FormDataEncoder::EncodingFromAcceptCharset(
                copied_attributes.AcceptCharset(), document.Encoding());
  FormData* dom_form_data = form->ConstructEntryList(
      submit_button, data_encoding.EncodingForFormSubmission());
  DCHECK(dom_form_data);

  scoped_refptr<EncodedFormData> form_data;
  String boundary;

  if (is_multi_part_form) {
    form_data = dom_form_data->EncodeMultiPartFormData();
    boundary = form_data->Boundary().data();
  } else {
    form_data = dom_form_data->EncodeFormData(
        attributes.Method() == kGetMethod
            ? EncodedFormData::kFormURLEncoded
            : EncodedFormData::ParseEncodingType(encoding_type));
    if (copied_attributes.Method() == kPostMethod && is_mailto_form) {
      // Convert the form data into a string that we put into the URL.
      AppendMailtoPostFormDataToURL(action_url, *form_data, encoding_type);
      form_data = EncodedFormData::Create();
    }
  }

  form_data->SetIdentifier(GenerateFormDataIdentifier());
  form_data->SetContainsPasswordData(dom_form_data->ContainsPasswordData());

  if (copied_attributes.Method() != FormSubmission::kPostMethod &&
      !action_url.ProtocolIsJavaScript()) {
    action_url.SetQuery(form_data->FlattenToString());
  }

  std::unique_ptr<ResourceRequest> resource_request =
      std::make_unique<ResourceRequest>(action_url);
  ClientNavigationReason reason = ClientNavigationReason::kFormSubmissionGet;
  if (copied_attributes.Method() == FormSubmission::kPostMethod) {
    reason = ClientNavigationReason::kFormSubmissionPost;
    resource_request->SetHttpMethod(http_names::kPOST);
    resource_request->SetHttpBody(form_data);

    // construct some user headers if necessary
    if (boundary.empty()) {
      resource_request->SetHTTPContentType(encoding_type);
    } else {
      resource_request->SetHTTPContentType(encoding_type +
                                           "; boundary=" + boundary);
    }
  }
  resource_request->SetHasUserGesture(
      LocalFrame::HasTransientUserActivation(form->GetDocument().GetFrame()));
  resource_request->SetFormSubmission(true);

  mojom::blink::TriggeringEventInfo triggering_event_info;
  if (event) {
    triggering_event_info =
        event->isTrusted()
            ? mojom::blink::TriggeringEventInfo::kFromTrustedEvent
            : mojom::blink::TriggeringEventInfo::kFromUntrustedEvent;
    if (event->UnderlyingEvent())
      event = event->UnderlyingEvent();
  } else {
    triggering_event_info = mojom::blink::TriggeringEventInfo::kNotFromEvent;
  }

  FrameLoadRequest frame_request(form->GetDocument().domWindow(),
                                 *resource_request);
  NavigationPolicy navigation_policy = NavigationPolicyFromEvent(event);
  if (navigation_policy == kNavigationPolicyLinkPreview) {
    return nullptr;
  }
  frame_request.SetNavigationPolicy(navigation_policy);
  frame_request.SetClientRedirectReason(reason);
  if (submit_button) {
    frame_request.SetSourceElement(submit_button);
  } else {
    frame_request.SetSourceElement(form);
  }
  frame_request.SetTriggeringEventInfo(triggering_event_info);
  AtomicString target_or_base_target = frame_request.CleanNavigationTarget(
      copied_attributes.Target().empty() ? document.BaseTarget()
                                         : copied_attributes.Target());

  if (form->HasRel(HTMLFormElement::kNoReferrer)) {
    frame_request.SetNoReferrer();
    frame_request.SetNoOpener();
  }
  if (form->HasRel(HTMLFormElement::kNoOpener) ||
      (EqualIgnoringASCIICase(target_or_base_target, "_blank") &&
       !form->HasRel(HTMLFormElement::kOpener) &&
       form->GetDocument()
           .domWindow()
           ->GetFrame()
           ->GetSettings()
           ->GetTargetBlankImpliesNoOpenerEnabledWillBeRemoved())) {
    frame_request.SetNoOpener();
  }

  Frame* target_frame =
      form->GetDocument()
          .GetFrame()
          ->Tree()
          .FindOrCreateFrameForNavigation(frame_request, target_or_base_target)
          .frame;

  // Apply replacement now, before any async steps, as the result may change.
  WebFrameLoadType load_type = WebFrameLoadType::kStandard;
  LocalFrame* target_local_frame = DynamicTo<LocalFrame>(target_frame);
  if (target_local_frame &&
      target_local_frame->NavigationShouldReplaceCurrentHistoryEntry(
          frame_request, load_type)) {
    load_type = WebFrameLoadType::kReplaceCurrentItem;
  }

  return MakeGarbageCollected<FormSubmission>(
      copied_attributes.Method(), action_url, target_or_base_target,
      encoding_type, frame_request.GetSourceElement(), std::move(form_data),
      event, frame_request.GetNavigationPolicy(), triggering_event_info, reason,
      std::move(resource_request), target_frame, load_type,
      form->GetDocument().domWindow(),
      form->GetDocument().GetFrame()->GetLocalFrameToken(),
      CaptureSourceLocation(form->GetDocument().domWindow()),
      form->GetDocument()
          .domWindow()
          ->GetPolicyContainer()
          ->IssueKeepAliveHandle());
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2217476.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【C++11】可变模板参数详解

个人主页&#xff1a;chian-ocean 文章专栏 C 可变模板参数详解 1. 引言 C模板是现代C编程中一个非常强大且灵活的工具。在C11标准中&#xff0c;引入了可变模板参数&#xff08;variadic templates&#xff09;&#xff0c;它为模板编程带来了革命性改变。它的出现允许我们…

第五课:Python学习之if语句

判断&#xff08;if&#xff09;语句 目标 开发中的应用场景if 语句体验if 语句进阶综合应用 01. 开发中的应用场景 生活中的判断几乎是无所不在的&#xff0c;我们每天都在做各种各样的选择&#xff0c;如果这样&#xff1f;如果那样&#xff1f;…… 程序中的判断 # 定义…

faust,一个神奇的 Python 库!

大家好&#xff0c;今天为大家分享一个神奇的 Python 库 - faust。 Github地址&#xff1a;https://github.com/robinhood/faust 在分布式系统和实时数据处理的世界里&#xff0c;消息流处理&#xff08;Stream Processing&#xff09;变得越来越重要。Faust 是一个 Python 库…

Linux 命令:每日一学,一文说尽打包压缩工具实践

[ 知识是人生的灯塔&#xff0c;只有不断学习&#xff0c;才能照亮前行的道路 ] 文章目录&#xff1a; 0x00 前言简述 前面&#xff0c;我们介绍了Linux中文件查找find命令以及与之联用最勤的xargs命令&#xff0c;作者以一个个简单的实例给各位看友展示了在运维中两个命令的使…

智慧供排水管网在线监测为城市安全保驾护航

一、方案背景 随着城市化进程的不断推进&#xff0c;城市供排水管网作为城市基础设施的关键组成部分&#xff0c;其安全稳定的运行对于确保城市居民的日常生活、工业生产活动以及整个生态环境的健康具有至关重要的作用。近年来&#xff0c;由于各种原因&#xff0c;城市供排水管…

Python基础:16、Python数据容器

1&#xff09;数据容器入门 一种可以存储多个元素的Python数据类型 数据容器&#xff1a;一种可以存储多个元素的Python数据类型数据容器包括&#xff1a;list&#xff08;列表&#xff09;、tuple&#xff08;元组&#xff09;、str&#xff08;字符串&#xff09;、set&…

k8s的安装与部署

一、部署 1、实验环境 k8s-master172.25.254.200k8s-node1172.25.254.10k8s-node2172.25.254.20docker-node1 172.25.254.100(harbor仓库&#xff09; 2、相关操作 1.基础配置 所有节点关闭selinux和防火墙 systemctl disabled firewalld systemctl stop firewalld grubb…

商贸物流产业大脑:打造“产-供-销,仓-运-配”全流程供应链

商贸物流产业大脑&#xff1a;打造“产-供-销&#xff0c;仓-运-配”全流程供应链 在全球化竞争日益激烈的今天&#xff0c;商贸物流产业的效率和创新力成为企业能否脱颖而出的关键因素。然而&#xff0c;信息不对称、资源配套不准确、系统独立运作等痛点严重阻碍了商贸物流产…

# LangGraph 入门(二)- ChatBot demo

在这个快速入门 demo 中&#xff0c;我们将会使用 langGraph 构建一个基本的对话机器人和可是使用网络搜索的机器人。通过这个 demo 我们来快速对 langgraph 有一定感知。 概念补充 顾名思义langGraph是基于图&#xff08;Graph Theory&#xff09;的&#xff0c;如果你学过图…

【算法】约瑟夫环问题

据说著名的犹太历史学家Josephus有过以下故事&#xff0c; 罗马人占领乔塔帕特&#xff0c; 39个犹太人与Josephus和他的朋友躲在洞中&#xff0c;其中39个犹太人决定自杀&#xff0c; &#xff0c;他们的自杀方式是41个人绕成一圈&#xff0c;第一个人报数1&#xff0c;报数到…

RK3588的demo板学习

表层的线宽是3.8mil: 换层之后线宽变成了4.2mil: (说明对于一根线&#xff0c;不同层线宽不同) 经典&#xff1a; 开窗加锡&#xff0c;增强散热&#xff0c;扩大电流&#xff1a; R14的作用&#xff1a;与LDO进行分压&#xff0c;降低LDOP的压差从而减小其散热&#xff1a;第…

如何系统的从0到1学习大模型?有哪些书籍推荐?

大模型应用得好&#xff0c;不仅需要海量的基础数据、大规模算力、综合人工智能发展成果的技术&#xff0c;还需要政产学研用各方的共同推进。 大模型不仅能生成结果、生成数据&#xff0c;更能传递价值观。应用于我国的大模型需要懂中文、懂中国文化、懂中国国情。大模型是全…

【Linux系统编程】环境基础开发工具使用

目录 1、Linux软件包管理器yum 1.1 什么是软件包 1.2 安装软件 1.3 查看软件包 1.4 卸载软件 2、Linux编辑器-vim 2.1 vim的概念 2.2 vim的基本操作 2.3 vim的配置 3、Linux编译器-gcc/g 3.1 gcc编译的过程​编辑​编辑​编辑 3.2 详解链接 动态链接 静态链接 4…

纯HTML实现标签页切换

纯HTML实现标签页切换 实现原理&#xff1a; HTML结构&#xff1a; 使用无序列表&#xff08;<ul>&#xff09;创建标签导航。每个标签是一个列表项&#xff08;<li>&#xff09;&#xff0c;包含一个链接&#xff08;<a>&#xff09;。每个链接指向对应的内…

商品计划:零售企业的痛点破解与运营优化指南

在现代零售业的激烈竞争中&#xff0c;商品计划不仅是企业盈利的关键&#xff0c;更是解决众多痛点的有效途径。零售企业在运营过程中常常面临各种挑战&#xff0c;如财务问题、库存管理、市场分析等。而科学、系统的商品计划可以帮助企业有效应对这些挑战&#xff0c;提升整体…

气膜:冰雪产业的创新解决方案—轻空间

随着冰雪运动的普及和发展&#xff0c;如何在不同季节和地区有效开展冰雪项目&#xff0c;成为了行业内的一个重要课题。气膜作为一种新兴的建筑形式&#xff0c;凭借其独特的优势&#xff0c;正在逐渐成为冰雪产业的创新解决方案。 优越的建筑特性 气膜建筑以其轻便、快速搭建…

Web Storage:数据储存机制

前言 在HTML5之前&#xff0c;开发人员一般是通过使用Cookie在客户端保存一些简单的信息的。在HTML5发布后&#xff0c;提供了一种新的客户端本地保存数据的方法&#xff0c;那就是Web Storage&#xff0c;它也被分为&#xff1a;LocalStorage和SessionStorage&#xff0c;它允…

【黑马redis高级篇】持久化

//来源[01,05]分布式缓存 除了黑马&#xff0c;还参考了别的。 目录 1.单点redis问题及解决方案2.为什么需要持久化&#xff1f;3.Redis持久化有哪些方式呢&#xff1f;为什么我们需要重点学RDB和AOF&#xff1f;4.RDB4.1 定义4.2 触发方式4.2.1手动触发save4.2.2被动触发bgsa…

软件工程:需求规格说明书(图书管理系统)

目录 1 导言 1.1 编写目的 1.2 参考资料 2 项目介绍 2.1 项目背景 2.2 项目目标 3 应用环境 3.1 系统运行网络环境 ​编辑 3.2 系统软硬件环境 4 功能模型 4.1 功能角色分析 4.1.1 图书管理员 4.1.2 普通读者 4.1.3 邮件系统 4.2 功能性需求 4.2.1 预定图…

AI+Xmind彻底解决你的思维导图

在写作领域、老师授课、产品经理等都会使用到思维导图&#xff0c;如果是一个个拖拉撰写太麻烦了。 本篇内容小索奇就教会大家利用AI结合Xmind制作思维导图。 先打开我们的AI软件 这里小索奇用ChatGPT&#xff08;可以使用kimi&#xff0c;豆包等大模型都可以&#xff09; P…