Skip to content

Commit

Permalink
feat: support request body rewrite (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubikplanet authored Sep 13, 2023
1 parent 4cf8606 commit 791d440
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 9 deletions.
2 changes: 2 additions & 0 deletions ext-plugin.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
42 changes: 41 additions & 1 deletion go/A6/HTTPReqCall/Rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion java/io/github/api7/A6/ExtraInfo/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package io.github.api7.A6.ExtraInfo;

@SuppressWarnings("unused")
public final class Info {
private Info() { }
public static final byte NONE = 0;
Expand Down
18 changes: 15 additions & 3 deletions java/io/github/api7/A6/HTTPReqCall/Rewrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,29 @@ 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);
Rewrite.addPath(builder, pathOffset);
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(); }
Expand All @@ -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;
Expand Down
22 changes: 21 additions & 1 deletion lua/A6/HTTPReqCall/Rewrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,34 @@ 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
function Rewrite.AddArgs(builder, args) builder:PrependUOffsetTRelativeSlot(2, args, 0) end
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
37 changes: 36 additions & 1 deletion python/A6/HTTPReqCall/Rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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."""
Expand Down
36 changes: 34 additions & 2 deletions typescript/a6/h-t-t-p-req-call/rewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 791d440

Please sign in to comment.