Stan 1 giorno fa
parent
commit
dff2be21ba
2 ha cambiato i file con 30 aggiunte e 8 eliminazioni
  1. 5 2
      http_client.go
  2. 25 6
      http_server.go

+ 5 - 2
http_client.go

@@ -8,6 +8,7 @@ import (
 	"io"
 	"net/http"
 	"strings"
+	"time"
 )
 
 type HTTPClientRequest struct {
@@ -36,7 +37,7 @@ func NewHTTPClient(scheme string, host string, port string) *HTTPClient {
 	}
 }
 
-func (this *HTTPClient) Request(method int, path string, headers HTTPHeaders, request *HTTPClientRequest, response *HTTPCLientResponse) error {
+func (this *HTTPClient) Request(timeout int, method int, path string, headers HTTPHeaders, request *HTTPClientRequest, response *HTTPCLientResponse) error {
 	buffer, err := json.Marshal(request.Object)
 	if err != nil {
 		return err
@@ -67,7 +68,9 @@ func (this *HTTPClient) Request(method int, path string, headers HTTPHeaders, re
 		requestInternal.Header.Add(key, value)
 	}
 
-	client := &http.Client{}
+	client := &http.Client{
+		Timeout: time.Duration(timeout) * time.Millisecond,
+	}
 	responseInternal, err := client.Do(requestInternal)
 	if err != nil {
 		return err

+ 25 - 6
http_server.go

@@ -23,6 +23,14 @@ type HTTPServerResponse struct {
 	Data     any
 }
 
+func ResponseOK() *HTTPServerResponse {
+	return &HTTPServerResponse{
+		Headers:  HTTPHeaders{},
+		Plain:    []byte{},
+		Encoding: ENCODING_PLAIN,
+	}
+}
+
 func ResponsePlain(headers HTTPHeaders, plain []byte) *HTTPServerResponse {
 	return &HTTPServerResponse{
 		Headers:  headers,
@@ -46,7 +54,7 @@ func ResponseErr(status int, err error) *HTTPServerResponse {
 	}
 }
 
-type HTTPServerCallback func(log *Log, request *HTTPServerRequest) *HTTPServerResponse
+type HTTPServerCallback func(request *HTTPServerRequest) *HTTPServerResponse
 
 type HTTPServerHandler struct {
 	Callback HTTPServerCallback
@@ -122,19 +130,31 @@ func (this *HTTPServer) ServeHTTP(responseStream http.ResponseWriter, requestStr
 	}
 
 	// Parse GET query
-	if method == METHOD_GET {
-		query := map[string]string{}
+	if method == METHOD_GET && handler.Data != nil {
+		query := map[string]any{}
 		for key, values := range requestStream.URL.Query() {
 			for _, value := range values {
 				query[key] = value
 			}
 		}
 
-		handler.Data = query
+		buffer, err := json.Marshal(query)
+		if err != nil {
+			this.log.Error("malformed request body", LogValue("path", requestStream.URL.Path), LogError(err))
+			responseStream.WriteHeader(http.StatusInternalServerError)
+			return
+		}
+
+		err = json.Unmarshal(buffer, handler.Data)
+		if err != nil {
+			this.log.Error("malformed request body", LogValue("path", requestStream.URL.Path), LogError(err))
+			responseStream.WriteHeader(http.StatusInternalServerError)
+			return
+		}
 	}
 
 	// Parse POST body
-	if method == METHOD_POST {
+	if method == METHOD_POST && handler.Data != nil {
 		size := 1
 		if handler.Buffer > 0 {
 			size = handler.Buffer
@@ -176,7 +196,6 @@ func (this *HTTPServer) ServeHTTP(responseStream http.ResponseWriter, requestStr
 	}
 
 	response := handler.Callback(
-		this.log,
 		&HTTPServerRequest{
 			Headers: requestHeaders,
 			Data:    handler.Data,