1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/prctl.h>
#include <time.h>
#include <string.h>
#include "network_util.h"
#include "server_test_util.h"
struct server_handle {
int port;
pid_t pid;
};
typedef server_handle_t serv_t;
serv_t *serv_init(int port, pid_t pid) {
serv_t *s = malloc(sizeof(serv_t));
s->port = port;
s->pid = pid;
return s;
}
serv_t *server_spawn(const char *bin, int port) {
pid_t pid = fork();
if (pid) { // parent
return serv_init(port, pid);
}
// child
// make file die when parent does
prctl(PR_SET_PDEATHSIG, SIGHUP);
assert(port < 100000);
char port_str[6];
sprintf(port_str, "%d", port);
char *argv[] = {
(char *) bin,
port_str,
NULL
};
// suppress server output
int devnull = open("/dev/null", O_RDWR);
dup2(devnull, STDOUT_FILENO);
execv(bin, argv);
// if the child fails to exec the server, it just aborts.
abort();
}
void server_kill(serv_t *s) {
kill(s->pid, SIGINT);
free(s);
}
char *server_query(serv_t *s, const char *request, bool delay) {
const char LOCALHOST_IP[] = "127.0.0.1";
const long NANOS_PER_SEC = 1000000000;
const long RETRY_NANOS = NANOS_PER_SEC / 100;
const struct timespec RETRY_TIME = {
.tv_sec = 0,
.tv_nsec = RETRY_NANOS,
};
const size_t NUM_ATTEMPTS = NANOS_PER_SEC / RETRY_NANOS / 10;
connection_t *conn = NULL;
char *response = NULL;
for (size_t i = 0; i < NUM_ATTEMPTS && !conn; i++) {
nanosleep(&RETRY_TIME, NULL);
conn = nu_connect_server(LOCALHOST_IP, s->port);
}
if (!conn) {
perror("failed to open connection");
goto EXIT_NO_CONN;
}
if (delay) {
sleep(1);
}
if (nu_send_str(conn, request) < 0) {
perror("failed to send request");
goto EXIT_CONN;
}
char *header = NULL;
while (!header) {
header = nu_try_read_header(conn);
}
const char CONTENT_LEN_HEADER[] = "Content-Length: ";
const char *len_loc = strstr(header, CONTENT_LEN_HEADER);
if (len_loc == NULL) {
fprintf(stderr, "server_query: no content length header\n");
goto EXIT_HEADER;
}
char *end;
size_t body_len = strtoul(len_loc + sizeof(CONTENT_LEN_HEADER) - 1, &end, 10);
if (end[0] != '\r' || end[1] != '\n') {
fprintf(stderr, "server_query: content length header invalid\n");
goto EXIT_HEADER;
}
char *body_bytes = nu_read_bytes(conn, body_len);
size_t header_len = strlen(header);
size_t response_len = header_len + body_len;
response = malloc(response_len + 1);
strcpy(response, header);
memcpy(response + header_len, body_bytes, body_len);
response[response_len] = '\0';
free(body_bytes);
EXIT_HEADER:
free(header);
EXIT_CONN:
nu_close_connection(conn);
EXIT_NO_CONN:
return response;
}