拦截HTTP的多种方式

embedded/2024/9/24 9:20:56/
http://www.w3.org/2000/svg" style="display: none;">

部分场景下需要修改请求报文信息,可以利用 AOP 思维(切面编程),对请求进行拦截处理。Web 中有见的几种发送请求的方式:

  • XMLHttpRequest
  • fetch
  • window.navigator.sendBeacon
  • new Image
  • service worker

针对这几种不同的场景,分别拦截处理它的 URL 和请求参数

(一)设计

http://www.w3.org/2000/svg" height="123" viewbox="0 0 940.7765502929688 123" class="mermaid-svg">
符合条件
使用新的 URL , BDOY
不符合条件
请求
Filter
Adaptor
发送请求

FilterAdaptor 成对出现,满足过滤过滤器的条件后,使用对用的适配器修改 URLBDOY

(二)不同的请求方式

1. XMLHttpRequest

XHR 的核心思路:

  1. 拦截 open,缓存参数信息
  2. 拦截 send,对 urlbody处理
  3. 调用原生 open、send 发送请求

核心代码实现:

class CustomXhr extends NativeXhr {private _method!: string;private _src = "" as K;private _async!: boolean;private _username?: string | null;private _password?: string | null;private _headers: Record<string, string> = {};open(method: string,url: K,async: boolean = true,username?: string | null,password?: string | null) {this._method = method;this._src = url;this._async = async;this._username = username;this._password = password;}setRequestHeader(name: string, value: string) {this._headers[name] = value;}send(body?: T) {let url = "" as K;let data = body;if (!_this.useNative) {[url, data] = _this.callFilterAndAdapter(this._src, body);}// Opensuper.open(this._method, url, this._async, this._username, this._password);// 设置请请求头Object.keys(this._headers).forEach((key) => {super.setRequestHeader(key, this._headers[key]);});return super.send(data);}
}

2. fetch

fetch 拦截的思路:

  1. 使用函数对原生 fetch 进行包装

核心代码实现:

const NativeFetch = window.fetch;function CustomFetch(input: K, init?: RequestInit) {if (_this.useNative) {return NativeFetch(input, init);}const [url, data] = _this.callFilterAndAdapter(input, init?.body as T);if (init && data) {init.body = data;}return NativeFetch(url, init);
}window.fetch = CustomFetch as typeof window.fetch;

3. window.navigator.sendBeacon

sendBeacon 的拦截思路:

  1. 同 fetch 的实现思路,对原生的 window.navigator.sendBeacon 进行包装一层

核心代码:

const oldSendBeacon = window.navigator.sendBeacon;
window.navigator.sendBeacon = (url: K, data: T) => {// 使用原生方式if (this.useNative) {return oldSendBeacon.call(window.navigator, url, data);}const [newUrl, newData] = this.callFilterAndAdapter(url, data);return oldSendBeacon.call(window.navigator, newUrl, newData);
};

备注:sendBeacon 的上下文一定要是window.navigator,所以需要使用oldSendBeacon.call(window.navigator, url, data)

4. new Image

new Image拦截思路:

  1. CustomImage 类继承自 Window.Image
  2. 使用 set、get 拦截 src属性的读写

核心代码实现:

const NativeImage = window.Image;class CustomImage extends NativeImage {private _src!: K;set src(value: K) {if (_this.useNative) {this._src = value;return;}this._src = _this.newSetHandler(value);this.setAttribute("src", this._src);}get src() {return this._src;}
}window.Image = CustomImage;

5. service worker

这个就不细聊了,有兴趣的可以自己去看下

(三)源码

install

npm i @swl/http-interceptor

源代码: https://github.com/swlws/http-interceptor


http://www.ppmy.cn/embedded/56711.html

相关文章

数据结构-第八章(1.基本概念)

注&#xff1a;这一章我花了两天半的时间过完&#xff0c;有点赶&#xff0c;对于一轮复习&#xff0c;我没有做8.6各部分内部排序算法的比较及应用。有点算留了坑&#xff0c;这也是我二轮在这一章的重点补充。 考纲内容 知识框架 复习提示 堆排序、快速排序和归并排序是这章…

ANN文献综述

人工神经网络文献综述 摘要 人工神经网络&#xff08;Artificial Neural Networks, ANNs&#xff09;是由多个简单的、相互连接的处理单元组成的自适应系统&#xff0c;通过调整这些单元之间的连接强度&#xff0c;ANNs能够实现对复杂数据的建模和预测。本文综述了ANNs的基本…

CTFShow的RE题(三)

数学不及格 strtol 函数 long strtol(char str, char **endptr, int base); 将字符串转换为长整型 就是解这个方程组了 主要就是 v4, v9的关系&#xff0c; 3v9-(v10v11v12)62d10d4673 v4 v12 v11 v10 0x13A31412F8C 得到 3*v9v419D024E75FF(1773860189695) 重点&…

嵌入式通信协议全解析:SPI、I²C、UART详解(附带面试题)

目录 一、什么是通信 二、 通信的分类 同步通信&#xff08;Synchronous Communication&#xff09; 异步通信&#xff08;Asynchronous Communication&#xff09; 不同协议标准区分图&#xff1a; UART UART的特点&#xff1a; UART的通信过程&#xff1a; UART的配置…

【十三】图解 Spring 核心数据结构:BeanDefinition 其二

图解 Spring 核心数据结构&#xff1a;BeanDefinition 其二 概述 前面写过一篇相关文章作为开篇介绍了一下BeanDefinition&#xff0c;本篇将深入细节来向读者展示BeanDefinition的设计&#xff0c;让我们一起来揭开日常开发中使用的bean的神秘面纱&#xff0c;深入细节透彻理解…

【pearcmd】通过pearcmd.php 进行GetShell

https://cloud.tencent.com/developer/article/2204400 关于PHP 配置 register_argc_argv 小结 的一些研究文章。 应用例题 [NewStarCTF 2023 公开赛道]Include &#x1f350; <?phperror_reporting(0);if(isset($_GET[file])) {$file $_GET[file];if(preg_match(/flag|l…

MySQL之备份与恢复(八)

备份与恢复 还原逻辑备份 如果还原的是逻辑备份而不是物理备份&#xff0c;则与使用操作系统简单地复制文件到适当位置的方式不同&#xff0c;需要使用MySQL服务器本身来加载数据到表中。在加载导出文件之前&#xff0c;应该先花一点时间考虑文件有多大&#xff0c;需要多久加…

从入门到深入,Docker新手学习教程

编译整理&#xff5c;TesterHome社区 作者&#xff5c;Ishaan Gupta 以下为作者观点&#xff1a; Docker 彻底改变了我们开发、交付和运行应用程序的方式。它使开发人员能够将应用程序打包到容器中 - 标准化的可执行组件&#xff0c;将应用程序源代码与在任何环境中运行该代码…