Skip to content

Commit

Permalink
refactor: inHTTP.Header
Browse files Browse the repository at this point in the history
Header should manage its own changes and builds
  • Loading branch information
reatang committed Jan 17, 2024
1 parent a265bcd commit a9646b3
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 97 deletions.
93 changes: 88 additions & 5 deletions internal/http/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,101 @@

package http

import "net/http"
import (
"net/http"

"github.com/api7/ext-plugin-proto/go/A6"
hrc "github.com/api7/ext-plugin-proto/go/A6/HTTPReqCall"
flatbuffers "github.com/google/flatbuffers/go"
)

type ReadHeader interface {
HeadersLength() int
Headers(*A6.TextEntry, int) bool
}

type Header struct {
http.Header
hdr http.Header
rawHdr http.Header

deleteField []string
}

func newHeader() *Header {
func newHeader(r ReadHeader) *Header {
hh := http.Header{}
size := r.HeadersLength()
obj := A6.TextEntry{}
for i := 0; i < size; i++ {
if r.Headers(&obj, i) {
hh.Add(string(obj.Name()), string(obj.Value()))
}
}

return &Header{
Header: http.Header{},
hdr: http.Header{},
rawHdr: hh,

deleteField: make([]string, 0),
}
}

func (h *Header) Set(key, value string) {
h.hdr.Set(key, value)
}

func (h *Header) Del(key string) {
h.hdr.Del(key)

if h.rawHdr.Get(key) != "" {
h.deleteField = append(h.deleteField, key)
}
}

func (h *Header) Get(key string) string {
if v := h.hdr.Get(key); v != "" {
return v
}

return h.rawHdr.Get(key)
}

// View
//Deprecated: refactoring
func (h *Header) View() http.Header {
return h.Header
return h.hdr
}

func (h *Header) Build(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
var hdrs []flatbuffers.UOffsetT

// deleted
for _, d := range h.deleteField {
name := builder.CreateString(d)
A6.TextEntryStart(builder)
A6.TextEntryAddName(builder, name)
te := A6.TextEntryEnd(builder)
hdrs = append(hdrs, te)
}

// set
for hKey, hVal := range h.hdr {
if raw, ok := h.rawHdr[hKey]; !ok || raw[0] != hVal[0] {
name := builder.CreateString(hKey)
value := builder.CreateString(hVal[0])
A6.TextEntryStart(builder)
A6.TextEntryAddName(builder, name)
A6.TextEntryAddValue(builder, value)
te := A6.TextEntryEnd(builder)
hdrs = append(hdrs, te)
}
}

size := len(hdrs)
hrc.RewriteStartHeadersVector(builder, size)
for i := size - 1; i >= 0; i-- {
te := hdrs[i]
builder.PrependUOffsetT(te)
}

return builder.EndVector(size)
}
49 changes: 4 additions & 45 deletions internal/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ type Request struct {

path []byte

hdr *Header
rawHdr http.Header
hdr *Header

args url.Values
rawArgs url.Values
Expand Down Expand Up @@ -91,18 +90,9 @@ func (r *Request) SetPath(path []byte) {

func (r *Request) Header() pkgHTTP.Header {
if r.hdr == nil {
hdr := newHeader()
hh := hdr.View()
size := r.r.HeadersLength()
obj := A6.TextEntry{}
for i := 0; i < size; i++ {
if r.r.Headers(&obj, i) {
hh.Add(string(obj.Name()), string(obj.Value()))
}
}
r.hdr = hdr
r.rawHdr = hdr.Clone()
r.hdr = newHeader(r.r)
}

return r.hdr
}

Expand Down Expand Up @@ -225,38 +215,7 @@ func (r *Request) FetchChanges(id uint32, builder *flatbuffers.Builder) bool {

var hdrVec, respHdrVec flatbuffers.UOffsetT
if r.hdr != nil {
hdrs := []flatbuffers.UOffsetT{}
oldHdr := r.rawHdr
newHdr := r.hdr.View()
for n := range oldHdr {
if _, ok := newHdr[n]; !ok {
// deleted
name := builder.CreateString(n)
A6.TextEntryStart(builder)
A6.TextEntryAddName(builder, name)
te := A6.TextEntryEnd(builder)
hdrs = append(hdrs, te)
}
}
for n, v := range newHdr {
if raw, ok := oldHdr[n]; !ok || raw[0] != v[0] {
// set
name := builder.CreateString(n)
value := builder.CreateString(v[0])
A6.TextEntryStart(builder)
A6.TextEntryAddName(builder, name)
A6.TextEntryAddValue(builder, value)
te := A6.TextEntryEnd(builder)
hdrs = append(hdrs, te)
}
}
size := len(hdrs)
hrc.RewriteStartHeadersVector(builder, size)
for i := size - 1; i >= 0; i-- {
te := hdrs[i]
builder.PrependUOffsetT(te)
}
hdrVec = builder.EndVector(size)
hdrVec = r.hdr.Build(builder)
}

if r.respHdr != nil {
Expand Down
51 changes: 4 additions & 47 deletions internal/http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"bytes"
"encoding/binary"
"net"
"net/http"
"sync"

"github.com/apache/apisix-go-plugin-runner/internal/util"
Expand All @@ -30,7 +29,6 @@ import (

pkgHTTP "github.com/apache/apisix-go-plugin-runner/pkg/http"
"github.com/apache/apisix-go-plugin-runner/pkg/log"
"github.com/api7/ext-plugin-proto/go/A6"
hrc "github.com/api7/ext-plugin-proto/go/A6/HTTPRespCall"
flatbuffers "github.com/google/flatbuffers/go"
)
Expand All @@ -41,8 +39,7 @@ type Response struct {
conn net.Conn
extraInfoHeader []byte

hdr *Header
rawHdr http.Header
hdr *Header

statusCode int

Expand Down Expand Up @@ -120,18 +117,9 @@ func (r *Response) StatusCode() int {

func (r *Response) Header() pkgHTTP.Header {
if r.hdr == nil {
hdr := newHeader()
hh := hdr.View()
size := r.r.HeadersLength()
obj := A6.TextEntry{}
for i := 0; i < size; i++ {
if r.r.Headers(&obj, i) {
hh.Add(string(obj.Name()), string(obj.Value()))
}
}
r.hdr = hdr
r.rawHdr = hdr.Clone()
r.hdr = newHeader(r.r)
}

return r.hdr
}

Expand Down Expand Up @@ -214,38 +202,7 @@ func (r *Response) FetchChanges(builder *flatbuffers.Builder) bool {

var hdrVec flatbuffers.UOffsetT
if r.hdr != nil {
hdrs := []flatbuffers.UOffsetT{}
oldHdr := r.rawHdr
newHdr := r.hdr.View()
for n := range oldHdr {
if _, ok := newHdr[n]; !ok {
// deleted
name := builder.CreateString(n)
A6.TextEntryStart(builder)
A6.TextEntryAddName(builder, name)
te := A6.TextEntryEnd(builder)
hdrs = append(hdrs, te)
}
}
for n, v := range newHdr {
if raw, ok := oldHdr[n]; !ok || raw[0] != v[0] {
// set
name := builder.CreateString(n)
value := builder.CreateString(v[0])
A6.TextEntryStart(builder)
A6.TextEntryAddName(builder, name)
A6.TextEntryAddValue(builder, value)
te := A6.TextEntryEnd(builder)
hdrs = append(hdrs, te)
}
}
size := len(hdrs)
hrc.RespStartHeadersVector(builder, size)
for i := size - 1; i >= 0; i-- {
te := hdrs[i]
builder.PrependUOffsetT(te)
}
hdrVec = builder.EndVector(size)
hdrVec = r.hdr.Build(builder)
}

var bodyVec flatbuffers.UOffsetT
Expand Down
4 changes: 4 additions & 0 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,18 @@ type Header interface {
// It replaces any existing values associated with key.
// The key is case insensitive
Set(key, value string)

// Del deletes the values associated with key. The key is case insensitive
Del(key string)

// Get gets the first value associated with the given key.
// If there are no values associated with the key, Get returns "".
// It is case insensitive
Get(key string) string

// View returns the internal structure. It is expected for read operations. Any write operation
// won't be recorded
//Deprecated: refactoring
View() http.Header

// TODO: support Add
Expand Down

0 comments on commit a9646b3

Please sign in to comment.