Files
splinter/src/backend/http.h
T
lotherk 9af7695064 gateway streams: the answer grows while the provider talks
- stream:true uses the provider's send_stream and writes each text delta as a
  chat.completion.chunk as it arrives, instead of one chunk at the end. The
  SSE headers and the role chunk go out before the upstream call, so a slow
  provider still gives the caller a live connection.
- caller tool calls are sent as one tool_calls delta at the end of the step,
  when their names and arguments are complete; filtered gw_ calls are
  renumbered 0,1,... so strict accumulators see no gap. Local gw_ calls stay
  invisible.
- stream_options.include_usage asks the provider for usage and adds the summed
  token counts in a final chunk before [DONE]. Usage is now summed over every
  upstream round, for the non-stream reply too.
- an error after the first byte becomes an SSE error object plus [DONE]; a
  bad request is still an ordinary HTTP 400. SIGPIPE is ignored and
  TCP_NODELAY set, so a caller that closes early cannot kill the gateway.
- tests: fake_gateway.py has a slow stream, tests/stream_probe.py measures
  that chunks arrive while the upstream streams, usage is checked, and the
  mixed reply keeps index 0 after filtering. make check green.
2026-11-16 14:06:54 +00:00

39 lines
1.3 KiB
C

/*
* splinter -- the little HTTP/1.1 pieces the JSON servers share.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Not a web framework: one request per connection, Content-Length only, one
* reply, close. That is what a local tool endpoint needs, and it keeps the
* parsing auditable. The A2A and MCP servers carry their own older copies;
* new services use this one.
*/
#ifndef SPLINTER_HTTP_H
#define SPLINTER_HTTP_H
#include <stddef.h>
struct sp_buf;
#define SP_HTTP_HDR_MAX 16384
#define SP_HTTP_BODY_MAX (4 * 1024 * 1024)
int sp_http_write_all(int fd, const char *buf, size_t len);
/* the value of a header, terminated by the CRLF of the next line */
const char *sp_http_header_value(const char *hdr, const char *name);
int sp_http_bearer_is(const char *auth, const char *token);
/* one complete reply; type is the Content-Type, body is NUL-terminated */
void sp_http_reply(int fd, int code, const char *reason, const char *type,
const char *body);
/*
* Start an event stream: status 200, text/event-stream, no Content-Length,
* then the caller writes frames and closes the socket.
*/
int sp_http_begin_stream(int fd);
/* headers into hdr, exactly Content-Length bytes into body; 0 or -1 */
int sp_http_read_request(int fd, char *hdr, size_t cap,
struct sp_buf *body, size_t body_max);
#endif /* SPLINTER_HTTP_H */