Files
mproxy/app/handler/plaincall.go
T
2026-05-30 13:37:43 +02:00

65 lines
1.4 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package handler
import (
"context"
"io"
"net/http"
"strings"
"time"
"mproxy/app/router"
)
func (hand *Handler) PlainCall(rctx *router.Context) {
hostaddr := rctx.GetHost()
hand.logg.Debugf("Hostaddr: [%s]", hostaddr)
if !strings.Contains(hostaddr, "http://") {
hand.logg.Errorf("Non-proxy request, not found schema definition in Host")
rctx.SetStatus(http.StatusBadRequest)
return
}
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
}