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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include <sys/queue.h>
#include <sys/socket.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <imsg.h>
#include "extern.h"
static struct msgbuf httpq;
void
logx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
}
time_t
getmonotime(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
err(1, "clock_gettime");
return (ts.tv_sec);
}
int
valid_origin(const char *uri, const char *proto)
{
const char *to;
/* extract end of host from proto URI */
to = strstr(proto, "://");
if (to == NULL)
return 0;
to += strlen("://");
if ((to = strchr(to, '/')) == NULL)
return 0;
/* compare hosts including the / for the start of the path section */
if (strncasecmp(uri, proto, to - proto + 1) != 0)
return 0;
return 1;
}
static void
http_request(unsigned int id, const char *uri, const char *last_mod, int fd)
{
struct ibuf *b;
b = io_new_buffer();
io_simple_buffer(b, &id, sizeof(id));
io_str_buffer(b, uri);
io_str_buffer(b, last_mod);
/* pass file as fd */
b->fd = fd;
io_close_buffer(&httpq, b);
}
static const char *
http_result(enum http_result res)
{
switch (res) {
case HTTP_OK:
return "OK";
case HTTP_NOT_MOD:
return "not modified";
case HTTP_FAILED:
return "failed";
default:
errx(1, "unknown http result: %d", res);
}
}
static int
http_response(int fd)
{
struct ibuf *b, *httpbuf = NULL;
unsigned int id;
enum http_result res;
char *lastmod;
while ((b = io_buf_read(fd, &httpbuf)) == NULL)
/* nothing */ ;
io_read_buf(b, &id, sizeof(id));
io_read_buf(b, &res, sizeof(res));
io_read_str(b, &lastmod);
ibuf_free(b);
printf("transfer %s", http_result(res));
if (lastmod)
printf(", last-modified: %s" , lastmod);
printf("\n");
return res == HTTP_FAILED;
}
int
main(int argc, char **argv)
{
pid_t httppid;
int error, fd[2], outfd, http;
int fl = SOCK_STREAM | SOCK_CLOEXEC;
char *uri, *file, *mod;
unsigned int req = 0;
if (argc != 3 && argc != 4) {
fprintf(stderr, "usage: test-http uri file [last-modified]\n");
return 1;
}
uri = argv[1];
file = argv[2];
mod = argv[3];
if (socketpair(AF_UNIX, fl, 0, fd) == -1)
err(1, "socketpair");
if ((httppid = fork()) == -1)
err(1, "fork");
if (httppid == 0) {
close(fd[1]);
if (pledge("stdio rpath inet dns recvfd", NULL) == -1)
err(1, "pledge");
proc_http(NULL, fd[0]);
errx(1, "http process returned");
}
close(fd[0]);
http = fd[1];
msgbuf_init(&httpq);
httpq.fd = http;
if ((outfd = open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
err(1, "open %s", file);
http_request(req++, uri, mod, outfd);
switch (msgbuf_write(&httpq)) {
case 0:
errx(1, "write: connection closed");
case -1:
err(1, "write");
}
error = http_response(http);
return error;
}
|