working commit

This commit is contained in:
2026-05-29 13:34:07 +02:00
parent bcdd2766e6
commit 4e2c548e97
15 changed files with 188 additions and 91 deletions
+59
View File
@@ -0,0 +1,59 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package handler
import (
"net/http"
"io"
"context"
"time"
"mproxy/app/router"
)
func (hand *Handler) PlainCall(rctx *router.Context) {
hostaddr := rctx.GetHost()
hand.logg.Debugf("Hostaddr: [%s]", hostaddr)
ctx := rctx.GetContext()
ctx, _ = context.WithTimeout(ctx, 5*time.Second)
reqMethod := rctx.Request.Method
reqUrl := rctx.URL().String()
reqBody := rctx.Request.Body
req, err := http.NewRequestWithContext(ctx, reqMethod, reqUrl, reqBody)
if err != nil {
hand.logg.Errorf("Create request error: %v", err)
rctx.SetStatus(http.StatusInternalServerError)
return
}
for key := range rctx.Request.Header {
val := rctx.Request.Header.Get(key)
req.Header.Add(key, val)
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
hand.logg.Errorf("Call request error: %v", err)
rctx.SetStatus(http.StatusInternalServerError)
return
}
// Copy headers from remote side
for key := range resp.Header {
val := resp.Header.Get(key)
rctx.Writer.Header().Set(key, val)
}
// Copy status code
rctx.SetStatus(resp.StatusCode)
// Copy body
_, err = io.Copy(rctx.Writer, resp.Body)
if err != nil {
hand.logg.Errorf("Copy resp body error: %v", err)
return
}
return
}