Handling Protocol Transformation
1. Converting HTTP to HTTPS
Example: HTTP→HTTPS redirect
server {
listen 80;
server_name api.example.com;
return 301 https://$host$request_uri;
}
| Status | When |
|---|---|
| 301 | Permanent redirect |
| 308 | Preserves method (POST stays POST) |
| HSTS | Tells browser to skip redirect next time |
2. Configuring HTTP/1.1 to HTTP/2
| Feature | HTTP/1.1 | HTTP/2 |
|---|---|---|
| Multiplexing | No (1 req/conn) | Yes |
| Header compression | None | HPACK |
| Server push | No | Deprecated in Chrome |
| Binary framing | Text | Binary |
| Stream priority | No | Yes |
3. Implementing gRPC to REST Translation
Example: gRPC-Gateway annotation
service Users {
rpc GetUser(GetUserRequest) returns (User) {
option (google.api.http) = {
get: "/v1/users/{id}"
};
}
rpc CreateUser(User) returns (User) {
option (google.api.http) = {
post: "/v1/users"
body: "*"
};
}
}
4. Using SOAP to REST Transformation
| Aspect | SOAP | REST |
|---|---|---|
| Envelope | XML required | None |
| Action | SOAPAction header | HTTP verb + path |
| Payload | XML body | JSON typically |
| Faults | <Fault> element | HTTP status + JSON |
| Schema | WSDL/XSD | OpenAPI/JSON Schema |
5. Implementing WebSocket to HTTP
| Pattern | Use |
|---|---|
| SSE alternative | One-way push, plain HTTP |
| Long polling | Legacy clients |
| Webhook bridge | WS msg → POST to URL |
6. Converting GraphQL to REST
| GraphQL | REST Equivalent |
|---|---|
| Query | GET |
| Mutation | POST/PUT/DELETE |
| Subscription | SSE / WebSocket |
| Field selection | ?fields=id,name (sparse fieldsets) |
7. Using Protocol Buffer Transformation
Example: Envoy gRPC-JSON transcoder
http_filters:
- name: envoy.filters.http.grpc_json_transcoder
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.grpc_json_transcoder.v3.GrpcJsonTranscoder
proto_descriptor: /etc/proto/api.pb
services: ["users.UserService"]
print_options:
add_whitespace: false
always_print_primitive_fields: true
8. Configuring Message Format Conversion
| From → To | Tool |
|---|---|
| JSON ↔ XML | jq + xmlstarlet, schema-driven |
| JSON ↔ Protobuf | protoc reflection |
| JSON ↔ CSV | flat structures only |
| JSON ↔ MsgPack | Binary, schema-less |
9. Implementing Custom Protocol Handlers
Example: Envoy Wasm filter skeleton
#[no_mangle]
pub fn on_http_request_headers(&mut self, _: usize) -> Action {
if let Some(p) = self.get_http_request_header(":path") {
if p.starts_with("/legacy/") {
self.set_http_request_header(":path", Some(&p.replace("/legacy/", "/v2/")));
}
}
Action::Continue
}
10. Setting Up Protocol Negotiation
| Method | Detail |
|---|---|
| ALPN | TLS-level (h2, http/1.1, h3) |
| Upgrade header | HTTP/1.1 → WS/h2c |
| Alt-Svc | Advertise HTTP/3 endpoint |
| Content-Type | Pick handler by media type |