-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.c
185 lines (157 loc) · 3.54 KB
/
fetch.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "fetch.h"
#include "url.h"
#include "linebuf.h"
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <err.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
static void write_line(int fd, const char *);
struct session
{
int fd;
struct linebuf *lb;
char *firstline;
};
struct session *
fetch_open(const struct url *url, int *code, char **firstline)
{
struct sockaddr_in sa = { 0 };
char *s, *p;
struct session *sess;
sess = calloc(1, sizeof(struct session));
if (sess == NULL)
return NULL;
sess->fd = socket(AF_INET, SOCK_STREAM, 0);
if (sess->fd < 0)
err(1, "socket");
sa.sin_family = AF_INET;
sa.sin_port = htons(1965);
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(sess->fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
err(1, "connect");
write_line(sess->fd, url_str(url, NULL));
sess->lb = linebuf_create();
s = NULL;
while (linebuf_fill_from_fd(sess->lb, sess->fd) > 0)
if ((s = linebuf_read(sess->lb)) != NULL)
break;
if (s == NULL) {
warnx("couldn't read first line");
return NULL;
}
p = strchr(s, ' ');
if (p != NULL) {
*p++ = '\0';
sess->firstline = strdup(p);
*code = atoi(s);
}
*firstline = sess->firstline;
return sess;
}
void
fetch_close(struct session *sess)
{
close(sess->fd);
if (sess->firstline != NULL)
free(sess->firstline);
free(sess);
}
void
fetch_linestream(struct session *sess,
void (*linecb)(const char *line, void *data), void *data)
{
char *s;
while (linebuf_fill_from_fd(sess->lb, sess->fd) > 0)
while ((s = linebuf_read(sess->lb)) != NULL)
linecb(s, data);
linebuf_free(sess->lb);
}
int
fetch(const char *s, void (*linecb)(const char *line, void *data), void *data)
{
struct session *sess;
int status;
char *meta;
struct url url;
char *ustr;
char *p;
int ret;
ustr = strdup(s);
if (ustr == NULL) {
err(1, "strdup");
return -1;
}
if (url_parse(&url, ustr) == -1) {
errx(1, "error parsing url");
return -1;
}
if (strcmp(url.scheme, "gemini") != 0) {
errx(1, "only 'gemini' scheme is supported");
return -1;
}
sess = fetch_open(&url, &status, &meta);
if (sess == NULL) {
errx(1, "couldn't connect caching server");
return -1;
}
if (status >= 10 && status <= 19) {
errx(1, "requires input");
} else if (status >= 30 && status <= 39) {
printf("redirect %s\n", meta);
p = strdup(meta);
fetch_close(sess);
ret = fetch(p, linecb, data);
free(p);
return ret;
} else if (status >= 40 && status <= 49) {
errx(1, "temp failure: %s", meta);
} else if (status >= 50 && status <= 59) {
errx(1, "permanent failure: %s", meta);
} else if (status >= 60 && status <= 69) {
errx(1, "client certificate required");
} else if (status < 20 || status > 29) {
errx(1, "unsupported status: %d", status);
}
if (!(strncmp(meta, "text/gemini", strlen("text/gemini")) == 0 ||
strncmp(meta, "text/plain", strlen("text/plain")) == 0)) {
errx(1, "unsupported content type");
return -1;
}
fetch_linestream(sess, linecb, data);
fetch_close(sess);
return 0;
}
static void
write_line(int fd, const char *str)
{
size_t len, newlen;
ssize_t ret;
const char *p;
char *s;
len = strlen(str);
newlen = len + strlen("\r\n");
if ((s = malloc(newlen + 1)) == NULL)
err(1, "malloc");
#ifdef __OpenBSD__
strlcpy(s, str, newlen);
#else
snprintf(s, newlen, "%s", str);
#endif
s[newlen-2] = '\r';
s[newlen-1] = '\n';
s[newlen] = '\0';
len = newlen;
p = s;
while (len > 0) {
ret = write(fd, p, len);
if (ret == -1)
err(1, "write");
p += ret;
len -= ret;
}
free(s);
}