60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
/*
|
|
* 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
|
|
}
|