From 111bc58fb1b6bebd4613b97b75e5a2f8668466d0 Mon Sep 17 00:00:00 2001 From: rubikplanet <1106628276@qq.com> Date: Wed, 19 Jul 2023 12:03:02 +0800 Subject: [PATCH] feat: support request body rewrite --- ext-plugin.fbs | 2 + go/A6/HTTPReqCall/Rewrite.go | 42 ++++++++++++++++++- java/io/github/api7/A6/ExtraInfo/Info.java | 1 - .../github/api7/A6/HTTPReqCall/Rewrite.java | 18 ++++++-- lua/A6/HTTPReqCall/Rewrite.lua | 22 +++++++++- python/A6/HTTPReqCall/Rewrite.py | 37 +++++++++++++++- typescript/a6/h-t-t-p-req-call/rewrite.ts | 36 +++++++++++++++- 7 files changed, 149 insertions(+), 9 deletions(-) diff --git a/ext-plugin.fbs b/ext-plugin.fbs index c367c92..562fc49 100644 --- a/ext-plugin.fbs +++ b/ext-plugin.fbs @@ -104,6 +104,8 @@ table Rewrite { args:[TextEntry]; // To change the upstream response header resp_headers:[TextEntry]; + // To change the request body + body:[ubyte]; } union Action { diff --git a/go/A6/HTTPReqCall/Rewrite.go b/go/A6/HTTPReqCall/Rewrite.go index 15a5c53..1b1f618 100644 --- a/go/A6/HTTPReqCall/Rewrite.go +++ b/go/A6/HTTPReqCall/Rewrite.go @@ -103,8 +103,42 @@ func (rcv *Rewrite) RespHeadersLength() int { return 0 } +func (rcv *Rewrite) Body(j int) byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(12)) + if o != 0 { + a := rcv._tab.Vector(o) + return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j*1)) + } + return 0 +} + +func (rcv *Rewrite) BodyLength() int { + o := flatbuffers.UOffsetT(rcv._tab.Offset(12)) + if o != 0 { + return rcv._tab.VectorLen(o) + } + return 0 +} + +func (rcv *Rewrite) BodyBytes() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(12)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func (rcv *Rewrite) MutateBody(j int, n byte) bool { + o := flatbuffers.UOffsetT(rcv._tab.Offset(12)) + if o != 0 { + a := rcv._tab.Vector(o) + return rcv._tab.MutateByte(a+flatbuffers.UOffsetT(j*1), n) + } + return false +} + func RewriteStart(builder *flatbuffers.Builder) { - builder.StartObject(4) + builder.StartObject(5) } func RewriteAddPath(builder *flatbuffers.Builder, path flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(path), 0) @@ -127,6 +161,12 @@ func RewriteAddRespHeaders(builder *flatbuffers.Builder, respHeaders flatbuffers func RewriteStartRespHeadersVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems, 4) } +func RewriteAddBody(builder *flatbuffers.Builder, body flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(4, flatbuffers.UOffsetT(body), 0) +} +func RewriteStartBodyVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { + return builder.StartVector(1, numElems, 1) +} func RewriteEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/java/io/github/api7/A6/ExtraInfo/Info.java b/java/io/github/api7/A6/ExtraInfo/Info.java index 5c30a62..69a4952 100644 --- a/java/io/github/api7/A6/ExtraInfo/Info.java +++ b/java/io/github/api7/A6/ExtraInfo/Info.java @@ -2,7 +2,6 @@ package io.github.api7.A6.ExtraInfo; -@SuppressWarnings("unused") public final class Info { private Info() { } public static final byte NONE = 0; diff --git a/java/io/github/api7/A6/HTTPReqCall/Rewrite.java b/java/io/github/api7/A6/HTTPReqCall/Rewrite.java index ef95742..a368419 100644 --- a/java/io/github/api7/A6/HTTPReqCall/Rewrite.java +++ b/java/io/github/api7/A6/HTTPReqCall/Rewrite.java @@ -33,13 +33,21 @@ public final class Rewrite extends Table { public int respHeadersLength() { int o = __offset(10); return o != 0 ? __vector_len(o) : 0; } public io.github.api7.A6.TextEntry.Vector respHeadersVector() { return respHeadersVector(new io.github.api7.A6.TextEntry.Vector()); } public io.github.api7.A6.TextEntry.Vector respHeadersVector(io.github.api7.A6.TextEntry.Vector obj) { int o = __offset(10); return o != 0 ? obj.__assign(__vector(o), 4, bb) : null; } + public int body(int j) { int o = __offset(12); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; } + public int bodyLength() { int o = __offset(12); return o != 0 ? __vector_len(o) : 0; } + public ByteVector bodyVector() { return bodyVector(new ByteVector()); } + public ByteVector bodyVector(ByteVector obj) { int o = __offset(12); return o != 0 ? obj.__assign(__vector(o), bb) : null; } + public ByteBuffer bodyAsByteBuffer() { return __vector_as_bytebuffer(12, 1); } + public ByteBuffer bodyInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 12, 1); } public static int createRewrite(FlatBufferBuilder builder, int pathOffset, int headersOffset, int argsOffset, - int resp_headersOffset) { - builder.startTable(4); + int resp_headersOffset, + int bodyOffset) { + builder.startTable(5); + Rewrite.addBody(builder, bodyOffset); Rewrite.addRespHeaders(builder, resp_headersOffset); Rewrite.addArgs(builder, argsOffset); Rewrite.addHeaders(builder, headersOffset); @@ -47,7 +55,7 @@ public static int createRewrite(FlatBufferBuilder builder, return Rewrite.endRewrite(builder); } - public static void startRewrite(FlatBufferBuilder builder) { builder.startTable(4); } + public static void startRewrite(FlatBufferBuilder builder) { builder.startTable(5); } public static void addPath(FlatBufferBuilder builder, int pathOffset) { builder.addOffset(0, pathOffset, 0); } public static void addHeaders(FlatBufferBuilder builder, int headersOffset) { builder.addOffset(1, headersOffset, 0); } public static int createHeadersVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); } @@ -58,6 +66,10 @@ public static int createRewrite(FlatBufferBuilder builder, public static void addRespHeaders(FlatBufferBuilder builder, int respHeadersOffset) { builder.addOffset(3, respHeadersOffset, 0); } public static int createRespHeadersVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); } public static void startRespHeadersVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); } + public static void addBody(FlatBufferBuilder builder, int bodyOffset) { builder.addOffset(4, bodyOffset, 0); } + public static int createBodyVector(FlatBufferBuilder builder, byte[] data) { return builder.createByteVector(data); } + public static int createBodyVector(FlatBufferBuilder builder, ByteBuffer data) { return builder.createByteVector(data); } + public static void startBodyVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); } public static int endRewrite(FlatBufferBuilder builder) { int o = builder.endTable(); return o; diff --git a/lua/A6/HTTPReqCall/Rewrite.lua b/lua/A6/HTTPReqCall/Rewrite.lua index 5bf00dd..4505f82 100644 --- a/lua/A6/HTTPReqCall/Rewrite.lua +++ b/lua/A6/HTTPReqCall/Rewrite.lua @@ -84,7 +84,25 @@ function Rewrite_mt:RespHeadersLength() end return 0 end -function Rewrite.Start(builder) builder:StartObject(4) end +function Rewrite_mt:Body(j) + local o = self.view:Offset(12) + if o ~= 0 then + local a = self.view:Vector(o) + return self.view:Get(flatbuffers.N.Uint8, a + ((j-1) * 1)) + end + return 0 +end +function Rewrite_mt:BodyAsString(start, stop) + return self.view:VectorAsString(12, start, stop) +end +function Rewrite_mt:BodyLength() + local o = self.view:Offset(12) + if o ~= 0 then + return self.view:VectorLen(o) + end + return 0 +end +function Rewrite.Start(builder) builder:StartObject(5) end function Rewrite.AddPath(builder, path) builder:PrependUOffsetTRelativeSlot(0, path, 0) end function Rewrite.AddHeaders(builder, headers) builder:PrependUOffsetTRelativeSlot(1, headers, 0) end function Rewrite.StartHeadersVector(builder, numElems) return builder:StartVector(4, numElems, 4) end @@ -92,6 +110,8 @@ function Rewrite.AddArgs(builder, args) builder:PrependUOffsetTRelativeSlot(2, a function Rewrite.StartArgsVector(builder, numElems) return builder:StartVector(4, numElems, 4) end function Rewrite.AddRespHeaders(builder, respHeaders) builder:PrependUOffsetTRelativeSlot(3, respHeaders, 0) end function Rewrite.StartRespHeadersVector(builder, numElems) return builder:StartVector(4, numElems, 4) end +function Rewrite.AddBody(builder, body) builder:PrependUOffsetTRelativeSlot(4, body, 0) end +function Rewrite.StartBodyVector(builder, numElems) return builder:StartVector(1, numElems, 1) end function Rewrite.End(builder) return builder:EndObject() end return Rewrite -- return the module \ No newline at end of file diff --git a/python/A6/HTTPReqCall/Rewrite.py b/python/A6/HTTPReqCall/Rewrite.py index 36eac5b..5cbb1c9 100644 --- a/python/A6/HTTPReqCall/Rewrite.py +++ b/python/A6/HTTPReqCall/Rewrite.py @@ -106,7 +106,34 @@ def RespHeadersIsNone(self): o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(10)) return o == 0 -def Start(builder): builder.StartObject(4) + # Rewrite + def Body(self, j): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12)) + if o != 0: + a = self._tab.Vector(o) + return self._tab.Get(flatbuffers.number_types.Uint8Flags, a + flatbuffers.number_types.UOffsetTFlags.py_type(j * 1)) + return 0 + + # Rewrite + def BodyAsNumpy(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12)) + if o != 0: + return self._tab.GetVectorAsNumpy(flatbuffers.number_types.Uint8Flags, o) + return 0 + + # Rewrite + def BodyLength(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12)) + if o != 0: + return self._tab.VectorLen(o) + return 0 + + # Rewrite + def BodyIsNone(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12)) + return o == 0 + +def Start(builder): builder.StartObject(5) def RewriteStart(builder): """This method is deprecated. Please switch to Start.""" return Start(builder) @@ -138,6 +165,14 @@ def StartRespHeadersVector(builder, numElems): return builder.StartVector(4, num def RewriteStartRespHeadersVector(builder, numElems): """This method is deprecated. Please switch to Start.""" return StartRespHeadersVector(builder, numElems) +def AddBody(builder, body): builder.PrependUOffsetTRelativeSlot(4, flatbuffers.number_types.UOffsetTFlags.py_type(body), 0) +def RewriteAddBody(builder, body): + """This method is deprecated. Please switch to AddBody.""" + return AddBody(builder, body) +def StartBodyVector(builder, numElems): return builder.StartVector(1, numElems, 1) +def RewriteStartBodyVector(builder, numElems): + """This method is deprecated. Please switch to Start.""" + return StartBodyVector(builder, numElems) def End(builder): return builder.EndObject() def RewriteEnd(builder): """This method is deprecated. Please switch to End.""" diff --git a/typescript/a6/h-t-t-p-req-call/rewrite.ts b/typescript/a6/h-t-t-p-req-call/rewrite.ts index b31b50b..989706a 100644 --- a/typescript/a6/h-t-t-p-req-call/rewrite.ts +++ b/typescript/a6/h-t-t-p-req-call/rewrite.ts @@ -60,8 +60,23 @@ respHeadersLength():number { return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0; } +body(index: number):number|null { + const offset = this.bb!.__offset(this.bb_pos, 12); + return offset ? this.bb!.readUint8(this.bb!.__vector(this.bb_pos + offset) + index) : 0; +} + +bodyLength():number { + const offset = this.bb!.__offset(this.bb_pos, 12); + return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0; +} + +bodyArray():Uint8Array|null { + const offset = this.bb!.__offset(this.bb_pos, 12); + return offset ? new Uint8Array(this.bb!.bytes().buffer, this.bb!.bytes().byteOffset + this.bb!.__vector(this.bb_pos + offset), this.bb!.__vector_len(this.bb_pos + offset)) : null; +} + static startRewrite(builder:flatbuffers.Builder) { - builder.startObject(4); + builder.startObject(5); } static addPath(builder:flatbuffers.Builder, pathOffset:flatbuffers.Offset) { @@ -116,17 +131,34 @@ static startRespHeadersVector(builder:flatbuffers.Builder, numElems:number) { builder.startVector(4, numElems, 4); } +static addBody(builder:flatbuffers.Builder, bodyOffset:flatbuffers.Offset) { + builder.addFieldOffset(4, bodyOffset, 0); +} + +static createBodyVector(builder:flatbuffers.Builder, data:number[]|Uint8Array):flatbuffers.Offset { + builder.startVector(1, data.length, 1); + for (let i = data.length - 1; i >= 0; i--) { + builder.addInt8(data[i]!); + } + return builder.endVector(); +} + +static startBodyVector(builder:flatbuffers.Builder, numElems:number) { + builder.startVector(1, numElems, 1); +} + static endRewrite(builder:flatbuffers.Builder):flatbuffers.Offset { const offset = builder.endObject(); return offset; } -static createRewrite(builder:flatbuffers.Builder, pathOffset:flatbuffers.Offset, headersOffset:flatbuffers.Offset, argsOffset:flatbuffers.Offset, respHeadersOffset:flatbuffers.Offset):flatbuffers.Offset { +static createRewrite(builder:flatbuffers.Builder, pathOffset:flatbuffers.Offset, headersOffset:flatbuffers.Offset, argsOffset:flatbuffers.Offset, respHeadersOffset:flatbuffers.Offset, bodyOffset:flatbuffers.Offset):flatbuffers.Offset { Rewrite.startRewrite(builder); Rewrite.addPath(builder, pathOffset); Rewrite.addHeaders(builder, headersOffset); Rewrite.addArgs(builder, argsOffset); Rewrite.addRespHeaders(builder, respHeadersOffset); + Rewrite.addBody(builder, bodyOffset); return Rewrite.endRewrite(builder); } }