29 lines
1.3 KiB
C
29 lines
1.3 KiB
C
/* Synthetic runtime HTTP producer for the private network composition only. */
|
|
#include <arpa/inet.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
int main(int argc,char **argv) {
|
|
if(argc!=3)return 2;
|
|
int fd=socket(AF_INET,SOCK_STREAM,0),one=1;
|
|
struct sockaddr_in addr={.sin_family=AF_INET,.sin_port=htons(atoi(argv[2]))};
|
|
if(fd<0 || inet_pton(AF_INET,argv[1],&addr.sin_addr)!=1)return 3;
|
|
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&one,sizeof(one));
|
|
if(bind(fd,(void *)&addr,sizeof(addr)) || listen(fd,16))return 4;
|
|
for(;;) {
|
|
int client=accept(fd,0,0);if(client<0)continue;
|
|
char request[8193]={0};size_t used=0;
|
|
while(used<8192 && !strstr(request,"\r\n\r\n")) {
|
|
ssize_t n=read(client,request+used,8192-used);if(n<=0)break;
|
|
used+=(size_t)n;request[used]=0;
|
|
}
|
|
const char *body=strstr(request,"session=")?"fixture-session-leaked\n":"backend-retained-marker\n";
|
|
char response[256];int length=snprintf(response,sizeof(response),
|
|
"HTTP/1.1 200 OK\r\nContent-Length: %zu\r\nConnection: close\r\n\r\n%s",strlen(body),body);
|
|
if(length>0 && length<(int)sizeof(response))write(client,response,(size_t)length);
|
|
close(client);
|
|
}
|
|
}
|