/* Test-only Messages API stand-in: plain HTTP on 127.0.0.1, one scripted
* response per connection, every request saved for inspection. It exits on
* its own deadline, so a failed test cannot leave it running.
*
* Usage: fake_server PORT_FILE LOG_DIR DEADLINE_SECONDS SPEC...
* sse:FILE 200, chunked, FILE in 97-byte chunks
* status:CODE:FILE CODE with FILE as the body (- for none), Retry-After: 0
* retry:CODE:SECS CODE with no body, Retry-After: SECS
* cut:FILE:N 200, chunked, the first N bytes of FILE, then close
* hang:FILE:N 200, chunked, the first N bytes of FILE, then hold
* slow:FILE:MS 200, chunked, FILE in 97-byte chunks MS apart
* Connection k gets SPEC k; the last SPEC repeats. Requests are saved as
* LOG_DIR/k.req (headers and body). */
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
static time_t deadline;
static void die(const char *what) {
perror(what);
exit(2);
}
static char *slurp(const char *path, size_t *len) {
FILE *f = fopen(path, "rb");
char *buf;
long n = 0;
if (!f || fseek(f, 0, SEEK_END) != 0 || (n = ftell(f)) < 0 || fseek(f, 0, SEEK_SET) != 0) {
die(path);
}
buf = malloc((size_t)n + 1);
if (!buf || fread(buf, 1, (size_t)n, f) != (size_t)n) {
die(path);
}
fclose(f);
*len = (size_t)n;
return buf;
}
static int send_all(int fd, const char *p, size_t n) {
while (n > 0) {
ssize_t w = send(fd, p, n, 0);
if (w < 0) {
if (errno == EINTR) {
continue;
}
return -1;
}
p += w;
n -= (size_t)w;
}
return 0;
}
static int send_chunks(int fd, const char *p, size_t n, int gap_ms) {
char head[32];
for (size_t off = 0; off < n; off += 97) {
size_t k = n - off < 97 ? n - off : 97;
if (gap_ms > 0 && off > 0) {
struct timespec t = {gap_ms / 1000, (long)(gap_ms % 1000) * 1000000L};
nanosleep(&t, NULL);
}
int h = snprintf(head, sizeof head, "%zx\r\n", k);
if (send_all(fd, head, (size_t)h) != 0 || send_all(fd, p + off, k) != 0 || send_all(fd, "\r\n", 2) != 0) {
return -1;
}
}
return 0;
}
/* Reads one request (headers, then Content-Length bytes) and saves it. */
static void read_request(int fd, const char *log_path) {
size_t cap = 1 << 20, used = 0, want = 0;
char *buf = malloc(cap + 1), *end = NULL;
FILE *f;
if (!buf) {
die("malloc");
}
while (!end || used < want) {
struct pollfd p = {fd, POLLIN, 0};
ssize_t r;
if (time(NULL) >= deadline || poll(&p, 1, 1000) < 0) {
break;
}
if (!(p.revents & (POLLIN | POLLHUP))) {
continue;
}
r = recv(fd, buf + used, cap - used, 0);
if (r <= 0) {
break;
}
used += (size_t)r;
buf[used] = '\0';
if (!end && (end = strstr(buf, "\r\n\r\n"))) {
const char *cl = strstr(buf, "Content-Length: ");
want = (size_t)(end + 4 - buf) + (cl ? strtoul(cl + 16, NULL, 10) : 0);
}
if (used == cap) {
break;
}
}
f = fopen(log_path, "wb");
if (!f || fwrite(buf, 1, used, f) != used || fclose(f) != 0) {
die(log_path);
}
free(buf);
}
static void respond(int fd, const char *spec) {
char kind[16], file[1024], *colon;
const char *rest;
size_t len = 0;
char *body;
colon = strchr(spec, ':');
if (!colon || (size_t)(colon - spec) >= sizeof kind) {
die("bad spec");
}
memcpy(kind, spec, (size_t)(colon - spec));
kind[colon - spec] = '\0';
rest = colon + 1;
if (strcmp(kind, "retry") == 0) {
char head[256];
int code = atoi(rest), h;
const char *secs = strchr(rest, ':');
h = snprintf(head, sizeof head,
"HTTP/1.1 %d Scripted\r\nContent-Type: application/json\r\nRetry-After: %d\r\n"
"request-id: req_fake\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
code, secs ? atoi(secs + 1) : 0);
(void)send_all(fd, head, (size_t)h);
return;
}
if (strcmp(kind, "status") == 0) {
char head[256];
int code = atoi(rest), h;
const char *path = strchr(rest, ':');
body = path && strcmp(path + 1, "-") != 0 ? slurp(path + 1, &len) : NULL;
h = snprintf(head, sizeof head,
"HTTP/1.1 %d Scripted\r\nContent-Type: application/json\r\nRetry-After: 0\r\n"
"request-id: req_fake\r\nContent-Length: %zu\r\nConnection: close\r\n\r\n",
code, len);
(void)(send_all(fd, head, (size_t)h) || (body && send_all(fd, body, len)));
free(body);
return;
}
{
size_t limit = (size_t)-1;
int gap_ms = 0;
const char *last = strrchr(rest, ':');
static const char head[] = "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nrequest-id: req_fake\r\n"
"Transfer-Encoding: chunked\r\nConnection: close\r\n\r\n";
if (strcmp(kind, "sse") == 0) {
(void)snprintf(file, sizeof file, "%s", rest);
} else {
if (!last) {
die("bad spec");
}
(void)snprintf(file, sizeof file, "%.*s", (int)(last - rest), rest);
if (strcmp(kind, "slow") == 0) {
gap_ms = atoi(last + 1);
} else {
limit = strtoul(last + 1, NULL, 10);
}
}
body = slurp(file, &len);
if (limit < len) {
len = limit;
}
if (send_all(fd, head, sizeof head - 1) != 0 || send_chunks(fd, body, len, gap_ms) != 0) {
free(body);
return;
}
free(body);
if (strcmp(kind, "sse") == 0 || strcmp(kind, "slow") == 0) {
(void)send_all(fd, "0\r\n\r\n", 5);
} else if (strcmp(kind, "hang") == 0) {
while (time(NULL) < deadline) {
char sink[256];
struct pollfd p = {fd, POLLIN, 0};
if (poll(&p, 1, 200) > 0 && recv(fd, sink, sizeof sink, 0) <= 0) {
break; /* the client went away */
}
}
}
}
}
int main(int argc, char **argv) {
struct sockaddr_in addr;
socklen_t alen = sizeof addr;
int lfd, one = 1;
FILE *pf;
if (argc < 5) {
fprintf(stderr, "usage: fake_server PORT_FILE LOG_DIR DEADLINE_SECONDS SPEC...\n");
return 2;
}
signal(SIGPIPE, SIG_IGN);
deadline = time(NULL) + atoi(argv[3]);
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd < 0 || setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one) != 0) {
die("socket");
}
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (bind(lfd, (struct sockaddr *)&addr, sizeof addr) != 0 || listen(lfd, 8) != 0 ||
getsockname(lfd, (struct sockaddr *)&addr, &alen) != 0) {
die("bind");
}
{
char tmp[1100];
(void)snprintf(tmp, sizeof tmp, "%s.tmp", argv[1]);
pf = fopen(tmp, "w");
if (!pf || fprintf(pf, "%d\n", ntohs(addr.sin_port)) < 0 || fclose(pf) != 0 || rename(tmp, argv[1]) != 0) {
die(argv[1]);
}
}
for (int k = 1; time(NULL) < deadline; k++) {
struct pollfd p = {lfd, POLLIN, 0};
char log_path[1100];
int fd;
if (poll(&p, 1, 200) <= 0) {
k--;
continue;
}
fd = accept(lfd, NULL, NULL);
if (fd < 0) {
k--;
continue;
}
(void)snprintf(log_path, sizeof log_path, "%s/%d.req", argv[2], k);
read_request(fd, log_path);
respond(fd, argv[4 + (k - 1 < argc - 5 ? k - 1 : argc - 5)]);
close(fd);
}
return 0;
}