-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopher-url.c
157 lines (134 loc) · 2.67 KB
/
gopher-url.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
/*
* rfc 4266
*
*/
#pragma noroot
#pragma optimize 79
#include "gopher.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
typedef struct range {
unsigned location;
unsigned length;
} range;
typedef struct url {
range host;
range selector;
unsigned port;
unsigned type;
} url;
window_cookie *parse_url(const char *cp, unsigned length) {
// leading gopher:// is optional.
// any other scheme is an error.
// unsigned bits = 0;
unsigned i;
// expect host [:port] /
range host = { 0, 0 };
range selector = { 0, 0 };
unsigned port = 70;
unsigned type = '1';
unsigned st;
unsigned extra;
window_cookie *cookie;
char *p;
st = 0;
if (length >= 9 && !memcmp(cp, "gopher://", 9)) {
cp += 9;
length -= 9;
}
for (i = 0; i < length; ++i) {
unsigned c = cp[i];
switch(st) {
case 0:
if (c == '/') {
host.length = i - host.location;
st = 3;
}
else if (c == ':') {
host.length = i - host.location;
port = 0;
st = 2;
}
break;
case 1:
// host
if (c == '/') {
host.length = i - host.location;
st = 3;
}
else if (c == ':') {
host.length = i - host.location;
port = 0;
st = 2;
}
break;
case 2:
// port
if (isdigit(c)) {
port = port * 10;
port += (c & 0x0f);
}
else if (c == '/') {
// catches http://
if (port == 0) return NULL;
st = 3;
} else return NULL;
break;
case 3:
// type
type = c;
selector.location = i + 1;
++st;
break;
case 4:
// selector
break;
}
}
if (st <= 1) {
host.length = length - host.location;
}
if (st == 4) {
selector.length = length - selector.location;
}
extra = 3 + 3 + 2 + (host.length << 1) + (selector.length << 1);
cookie = malloc(sizeof(window_cookie) + extra);
if (!cookie) return NULL;
memset(cookie, 0, sizeof(window_cookie) + extra);
cookie->type = type;
cookie->port = port;
p = cookie->data;
cookie->title = p;
if (selector.length) {
// host + selector
*p++ = host.length + selector.length + 3 + 2;
*p++ = ' ';
memcpy(p, cp + host.location, host.length);
p += host.length;
*p++ = ' ';
*p++ = '-';
*p++ = ' ';
memcpy(p, cp + selector.location, selector.length);
p += selector.length;
*p++ = ' ';
} else {
// just the host
*p++ = host.length + 2;
*p++ = ' ';
memcpy(p, cp + host.location, host.length);
p += host.length;
*p++ = ' ';
}
cookie->host = p;
*p++ = host.length;
memcpy(p, cp + host.location, host.length);
p += host.length;
if (selector.length) {
cookie->selector = p;
*p++ = selector.length;
memcpy(p, cp + selector.location, selector.length);
p += selector.length;
}
return cookie;
}