MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / NonStream

Method NonStream

internal/wsrelay/http.go:39–112  ·  view source on GitHub ↗

NonStream executes a non-streaming HTTP request using the websocket provider.

(ctx context.Context, provider string, req *HTTPRequest)

Source from the content-addressed store, hash-verified

37
38// NonStream executes a non-streaming HTTP request using the websocket provider.
39func (m *Manager) NonStream(ctx context.Context, provider string, req *HTTPRequest) (*HTTPResponse, error) {
40 if req == nil {
41 return nil, fmt.Errorf("wsrelay: request is nil")
42 }
43 msg := Message{ID: uuid.NewString(), Type: MessageTypeHTTPReq, Payload: encodeRequest(req)}
44 respCh, err := m.Send(ctx, provider, msg)
45 if err != nil {
46 return nil, err
47 }
48 var (
49 streamMode bool
50 streamResp *HTTPResponse
51 streamBody bytes.Buffer
52 )
53 for {
54 select {
55 case <-ctx.Done():
56 return nil, ctx.Err()
57 case msg, ok := <-respCh:
58 if !ok {
59 if streamMode {
60 if streamResp == nil {
61 streamResp = &HTTPResponse{Status: http.StatusOK, Headers: make(http.Header)}
62 } else if streamResp.Headers == nil {
63 streamResp.Headers = make(http.Header)
64 }
65 streamResp.Body = append(streamResp.Body[:0], streamBody.Bytes()...)
66 return streamResp, nil
67 }
68 return nil, errors.New("wsrelay: connection closed during response")
69 }
70 switch msg.Type {
71 case MessageTypeHTTPResp:
72 resp := decodeResponse(msg.Payload)
73 if streamMode && streamBody.Len() > 0 && len(resp.Body) == 0 {
74 resp.Body = append(resp.Body[:0], streamBody.Bytes()...)
75 }
76 return resp, nil
77 case MessageTypeError:
78 return nil, decodeError(msg.Payload)
79 case MessageTypeStreamStart, MessageTypeStreamChunk:
80 if msg.Type == MessageTypeStreamStart {
81 streamMode = true
82 streamResp = decodeResponse(msg.Payload)
83 if streamResp.Headers == nil {
84 streamResp.Headers = make(http.Header)
85 }
86 streamBody.Reset()
87 continue
88 }
89 if !streamMode {
90 streamMode = true
91 streamResp = &HTTPResponse{Status: http.StatusOK, Headers: make(http.Header)}
92 }
93 chunk := decodeChunk(msg.Payload)
94 if len(chunk) > 0 {
95 streamBody.Write(chunk)
96 }

Callers 4

HttpRequestMethod · 0.80
ExecuteMethod · 0.80
CountTokensMethod · 0.80
TranslateNonStreamMethod · 0.80

Calls 8

SendMethod · 0.95
encodeRequestFunction · 0.85
decodeResponseFunction · 0.85
decodeErrorFunction · 0.85
decodeChunkFunction · 0.85
BytesMethod · 0.80
LenMethod · 0.80
WriteMethod · 0.45

Tested by

no test coverage detected