Implementation:Tensorflow Serving Server Request Interface
| Knowledge Sources | |
|---|---|
| Domains | HTTP, Abstraction |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
A pure interface class defining the minimum API for handling an HTTP request on the server side, including request body reading, response body writing, header manipulation, and reply/abort semantics.
Description
ServerRequestInterface defines a portable, implementation-agnostic API for server-side HTTP request handling. It provides: uri_path() and http_method() for request metadata; ReadRequestBytes() for reading the request body (returning owned memory via a custom BlockDeleter); WriteResponseBytes() and WriteResponseString() for building the response body; GetRequestHeader() and request_headers() for accessing request headers; OverwriteResponseHeader() and AppendResponseHeader() for setting response headers; PartialReply()/PartialReplyWithStatus() for streaming responses; PartialReplyWithFlushCallback() for flow-controlled writes with callback notification; Reply()/ReplyWithStatus() to complete the response; and Abort() to forcibly terminate the request. The interface uses a BlockDeleter struct that deallocates memory via std::allocator, decoupling memory management from the specific allocator implementation. BodyStatus and CallbackStatus enums provide I/O state tracking. Helper functions SetContentType, SetContentTypeHTML, and SetContentTypeTEXT are provided for common response header patterns. The interface is thread-compatible: once dispatched to a handler, the server runtime does not access the request until Reply() is called.
Usage
This is the interface that all HTTP request handlers in TensorFlow Serving interact with. Handler functions receive a ServerRequestInterface pointer and use it to read the request, build a response, and send the reply.
Code Reference
Source Location
- Repository: Tensorflow_Serving
- File:
tensorflow_serving/util/net_http/server/public/server_request_interface.h - Lines: 1-211
Signature
class ServerRequestInterface {
public:
struct BlockDeleter {
explicit BlockDeleter(int64_t size);
void operator()(char* ptr) const;
};
virtual absl::string_view uri_path() const = 0;
virtual absl::string_view http_method() const = 0;
virtual void WriteResponseBytes(const char* data, int64_t size) = 0;
virtual void WriteResponseString(absl::string_view data) = 0;
virtual std::unique_ptr<char[], BlockDeleter> ReadRequestBytes(int64_t* size) = 0;
virtual absl::string_view GetRequestHeader(absl::string_view header) const = 0;
virtual std::vector<absl::string_view> request_headers() const = 0;
virtual void OverwriteResponseHeader(absl::string_view header, absl::string_view value) = 0;
virtual void AppendResponseHeader(absl::string_view header, absl::string_view value) = 0;
virtual void ReplyWithStatus(HTTPStatusCode status) = 0;
virtual void Reply() = 0;
virtual void Abort() = 0;
};
Import
#include "tensorflow_serving/util/net_http/server/public/server_request_interface.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| data | const char* |
Yes (WriteResponseBytes) | Response body data to write |
| size | int64_t |
Yes (WriteResponseBytes) | Number of bytes to write |
| header | absl::string_view |
Yes (header methods) | Header name (case-insensitive for request headers) |
| status | HTTPStatusCode |
No | HTTP status code for the response (defaults to 200) |
Outputs
| Name | Type | Description |
|---|---|---|
| uri_path() | absl::string_view |
The request URI path, query, and fragment |
| http_method() | absl::string_view |
The HTTP method in upper case |
| ReadRequestBytes() | std::unique_ptr<char[], BlockDeleter> |
Owned request body block; nullptr on EOF or no body |
| GetRequestHeader() | absl::string_view |
Header value or nullptr if not present |
Usage Examples
Implementing a Request Handler
void PredictHandler(ServerRequestInterface* request) {
// Read the request body
int64_t size;
auto body = request->ReadRequestBytes(&size);
// Process the request
std::string response = ProcessPrediction(body.get(), size);
// Write response
SetContentType(request, "application/json");
request->WriteResponseString(response);
request->ReplyWithStatus(HTTPStatusCode::OK);
}