gmessages/libgm/http.go

131 lines
3.8 KiB
Go
Raw Permalink Normal View History

package libgm
import (
"bytes"
2023-09-04 11:24:45 +00:00
"context"
"crypto/sha1"
2023-09-04 11:24:45 +00:00
"encoding/base64"
"fmt"
"io"
"mime"
"net/http"
"time"
2023-09-04 11:24:45 +00:00
"github.com/rs/zerolog"
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/events"
2023-09-04 11:24:45 +00:00
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
"go.mau.fi/mautrix-gmessages/libgm/pblite"
"go.mau.fi/mautrix-gmessages/libgm/util"
)
const ContentTypeProtobuf = "application/x-protobuf"
const ContentTypePBLite = "application/json+protobuf"
func (c *Client) makeProtobufHTTPRequest(url string, data proto.Message, contentType string) (*http.Response, error) {
ctx := c.Logger.WithContext(context.TODO())
2024-03-05 17:14:02 +00:00
return c.makeProtobufHTTPRequestContext(ctx, url, data, contentType, false)
}
2024-03-05 17:14:02 +00:00
func (c *Client) makeProtobufHTTPRequestContext(ctx context.Context, url string, data proto.Message, contentType string, longPoll bool) (*http.Response, error) {
var body []byte
var err error
switch contentType {
case ContentTypeProtobuf:
body, err = proto.Marshal(data)
case ContentTypePBLite:
body, err = pblite.Marshal(data)
default:
return nil, fmt.Errorf("unknown request content type %s", contentType)
}
if err != nil {
return nil, err
}
2023-09-04 11:24:45 +00:00
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return nil, err
}
util.BuildRelayHeaders(req, contentType, "*/*")
2024-04-16 21:17:18 +00:00
c.AuthData.AddCookiesToRequest(req)
2024-03-05 17:14:02 +00:00
client := c.http
if longPoll {
client = c.lphttp
}
res, reqErr := client.Do(req)
if reqErr != nil {
return res, reqErr
}
2024-04-16 21:17:18 +00:00
c.AuthData.UpdateCookiesFromResponse(res)
return res, nil
}
func sapisidHash(origin, sapisid string) string {
ts := time.Now().Unix()
hash := sha1.Sum([]byte(fmt.Sprintf("%d %s %s", ts, sapisid, origin)))
return fmt.Sprintf("SAPISIDHASH %d_%x", ts, hash[:])
}
2023-09-04 11:24:45 +00:00
func decodeProtoResp(body []byte, contentType string, into proto.Message) error {
contentType, _, err := mime.ParseMediaType(contentType)
if err != nil {
return fmt.Errorf("failed to parse content-type: %w", err)
}
switch contentType {
case ContentTypeProtobuf:
return proto.Unmarshal(body, into)
case ContentTypePBLite, "text/plain":
2023-09-04 11:24:45 +00:00
return pblite.Unmarshal(body, into)
default:
return fmt.Errorf("unknown content type %s in response", contentType)
}
}
func typedHTTPResponse[T proto.Message](resp *http.Response, err error) (parsed T, retErr error) {
if err != nil {
retErr = err
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
retErr = fmt.Errorf("failed to read response body: %w", err)
return
}
2023-09-04 11:24:45 +00:00
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
logEvt := zerolog.Ctx(resp.Request.Context()).Debug().
Int("status_code", resp.StatusCode).
Str("url", resp.Request.URL.String()).
Str("response_body", base64.StdEncoding.EncodeToString(body))
httpErr := events.HTTPError{Resp: resp, Body: body}
retErr = httpErr
var errorResp gmproto.ErrorResponse
errErr := decodeProtoResp(body, resp.Header.Get("Content-Type"), &errorResp)
if errErr == nil && errorResp.Message != "" {
logEvt = logEvt.Any("response_proto_err", &errorResp)
retErr = events.RequestError{
HTTP: &httpErr,
Data: &errorResp,
}
} else {
logEvt = logEvt.AnErr("proto_parse_err", errErr)
}
logEvt.Msg("HTTP request to Google Messages failed")
return
}
parsed = parsed.ProtoReflect().New().Interface().(T)
2023-09-04 11:24:45 +00:00
retErr = decodeProtoResp(body, resp.Header.Get("Content-Type"), parsed)
successEvt := zerolog.Ctx(resp.Request.Context()).Trace()
if successEvt.Enabled() {
successEvt.
Int("status_code", resp.StatusCode).
Str("url", resp.Request.URL.String()).
Str("response_body", base64.StdEncoding.EncodeToString(body)).
Bool("parsed_has_unknown_fields", len(parsed.ProtoReflect().GetUnknown()) > 0).
Type("parsed_data_type", parsed).
Any("parsed_data", parsed).
Msg("HTTP request to Google Messages succeeded")
}
return
}