working commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*/
|
||||
package servcli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"mproxy/app/servoper"
|
||||
)
|
||||
|
||||
func (cli *Client) GetHello(ctx context.Context, rawpath string) error {
|
||||
var err error
|
||||
req := servoper.GetHelloParams{}
|
||||
reqdata, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = cli.DoCall(ctx, rawpath, reqdata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) DoCall(ctx context.Context, rawpath string, reqdata []byte) ([]byte, error) {
|
||||
var err error
|
||||
res := make([]byte, 0)
|
||||
|
||||
ref, err := ParsePath(rawpath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
uri := ref.HelloEP()
|
||||
reader := bytes.NewReader(reqdata)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, reader)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
req.Header.Set("User-Agent", cli.userAgent)
|
||||
req.Header.Set("Accept", "*/*")
|
||||
resp, err := cli.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return res, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err := fmt.Errorf("Unexpected response code %s", resp.Status)
|
||||
return res, err
|
||||
}
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
if contentLength == "" {
|
||||
err := fmt.Errorf("Content-Length header is missing")
|
||||
return res, err
|
||||
}
|
||||
blobSize, err := strconv.ParseInt(contentLength, 10, 64)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
recSize, err := Copy(ctx, buffer, resp.Body)
|
||||
if blobSize != recSize {
|
||||
err := fmt.Errorf("Mismatch declared and actual body size: %d and %d", blobSize, recSize)
|
||||
return res, err
|
||||
}
|
||||
res = buffer.Bytes()
|
||||
return res, err
|
||||
}
|
||||
Reference in New Issue
Block a user