Closed eipi10ydz closed 5 months ago
The trpc-go HTTP protocol server supports three main types:
The http
package primarily caters to HTTP RPC, which provides a basic mapping between HTTP content and RPC's request/response. This works well for 'application/json' and 'application/x-www-form-urlencoded', where we can use JSON serialization to convert between the HTTP body and the request/response structure for service stubs.
However, this approach falls short for HTTP streaming or scenarios like 'multipart/form-data', where raw binary data is difficult to map to structures.
Let's consider 'multipart/form-data' as an example. The reading and processing of multipart body are handled within the framework's code, and it's unrelated to the codec.SerializationTypeFormData
type. This type only affects the Form
part (like key1=value1&key2=value2
). That's why the Unmarshal
implementation for codec.FormDataSerialization
behaves similarly to 'application/x-www-form-urlencoded'. Service stub requests are unmarshaled from these form values. The actual multipart/formdata
is manually accessed by thttp.Head(ctx)
to retrieve the parsed Request.MultipartForm
.
For the response of a stub, a serialization type should also be provided. The developers chose to use JSON. The reason is that the serialization type in the context's message is the same for both request and response. So, we have codec.FormDataSerialization
for both, but the response for 'multipart/formdata' usually differs in content type. Even though there's logic in the server codec's encode to set the expected content type (JSON), it's too late as encoding occurs after serialization. Therefore, the developers decided to provide a different implementation for codec.FormDataSerialization
's Marshal
.
Preliminary Research
Question
I read the code about
FormDataSerialization.Marshal
and find that theMarshal
member function ofFormDataSerialization
usesJSONPBSerialization.Marshal
to implement.From my point of view, the
Marshal
member function ofFormDataSerialization
needs to implement HTTP body serialization ofContent-Type: multipart/form-data
but notContent-Type: application/json
.This implementation can be confusing to users.
Additional Information
Related codes are shown below.