Skip to content

Commit

Permalink
Merge pull request #27 from goinsane/develop
Browse files Browse the repository at this point in the history
v0.11.3
  • Loading branch information
orkunkaraduman authored Dec 26, 2023
2 parents 99a8e89 + f40e4d8 commit e4eadd9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
2 changes: 0 additions & 2 deletions caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ func NewFactory(client *http.Client, u *url.URL, opts ...CallOption) (f *Factory

// Caller creates a new Caller with the given endpoint and method.
func (f *Factory) Caller(endpoint string, method string, out interface{}, opts ...CallOption) *Caller {
method = strings.ToUpper(method)

switch method {
case http.MethodHead, http.MethodGet, http.MethodDelete:
case http.MethodPost, http.MethodPut, http.MethodPatch:
Expand Down
36 changes: 25 additions & 11 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ func (h *patternHandler) Register(method string, in interface{}, do DoFunc, opts
panic(fmt.Errorf("unable to copy input: %w", err))
}

method = strings.ToUpper(method)

switch method {
case "", http.MethodGet, http.MethodDelete:
if inVal.Elem().Kind() != reflect.Struct {
Expand Down Expand Up @@ -183,6 +181,14 @@ func (h *methodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
panic(errors.New("already sent"))
}

var data []byte
data, err = json.Marshal(out)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
panic(fmt.Errorf("unable to encode output: %w", err))
}
data = append(data, '\n')

var nopcw io.WriteCloser = nopCloserForWriter{w}
wc := nopcw
if h.options.AllowEncoding {
Expand All @@ -194,15 +200,18 @@ func (h *methodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

var data []byte
data, err = json.Marshal(out)
if err != nil {
panic(fmt.Errorf("unable to encode output: %w", err))
}
data = append(data, '\n')

for _, hdr := range headers {
for k, v := range hdr {
lk := strings.ToLower(k)
if lk == "accept" || strings.HasPrefix(lk, "accept-") {
continue
}
if lk == "content" || strings.HasPrefix(lk, "content-") {
continue
}
if lk == "transfer" || strings.HasPrefix(lk, "transfer-") {
continue
}
for _, v2 := range v {
w.Header().Add(k, v2)
}
Expand All @@ -211,8 +220,6 @@ func (h *methodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if wc == nopcw {
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(data)), 10))
} else {
w.Header().Del("Content-Length")
}
w.WriteHeader(code)
if r.Method == http.MethodHead {
Expand Down Expand Up @@ -266,12 +273,14 @@ func (h *methodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
inVal := reflect.ValueOf(h.in)
copiedInVal, err := copyReflectValue(inVal)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
panic(fmt.Errorf("unable to copy input: %w", err))
}

if contentType == "" &&
(r.Method == http.MethodHead || r.Method == http.MethodGet || r.Method == http.MethodDelete) {
if copiedInVal.Elem().Kind() != reflect.Struct {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
panic(errors.New("input must be struct or struct pointer"))
}
err = valuesToStruct(r.URL.Query(), copiedInVal.Interface())
Expand Down Expand Up @@ -330,4 +339,9 @@ func (h *methodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
})
}
do[len(do)-1](req, send)

if sent == 0 {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
panic(errors.New("send must be called"))
}
}

0 comments on commit e4eadd9

Please sign in to comment.