-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathurl.c
141 lines (125 loc) · 2.55 KB
/
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/*********************************
* Determine if c is a valid URL character.
*/
int isURLchar(char c)
{
if (isalnum(c))
return 1;
switch (c)
{
case '-':
case '_':
case '?':
case '=':
case '%':
case '&':
case '/':
case '+':
case '#':
case '~':
case '.':
case ':':
return 1;
default:
return 0;
}
}
/******************************
* Determine if string s of length is the start of a URL.
* Returns:
* 0 means not a URL, >0 gives the length of the URL
*/
size_t isURL(const char *s, size_t length)
{
/* Must start with one of:
* http://
* https://
*/
if (length < 9 || s[6] != '/')
return 0;
//writefln("isURL(%s)", s);
if (!((s[0] == 'h' || s[0] == 'H') &&
(s[1] == 't' || s[1] == 'T') &&
(s[2] == 't' || s[2] == 'T') &&
(s[3] == 'p' || s[3] == 'P')))
return 0;
size_t i;
if (s[4] == ':' && s[5] == '/')
i = 7;
else if ((s[4] == 's' || s[4] == 'S') && s[5] == ':' && s[7] == '/')
i = 8;
else
return 0;
size_t lastdot;
for (; i < length; i++)
{
auto c = s[i];
if (isalnum(c))
continue;
if (c == '-' || c == '_' || c == '?' ||
c == '=' || c == '%' || c == '&' ||
c == '/' || c == '+' || c == '#' ||
c == '~')
continue;
if (c == '.')
{
lastdot = i;
continue;
}
break;
}
if (!lastdot)
return 0;
return i;
}
/****************************************************
* Determine if index is in a URL or not.
*/
int inURL(const char *s, size_t length, size_t index)
{
if (length < 9 || !isURLchar(s[index]))
return 0;
size_t i;
size_t end = length - 9;
if (index < end)
end = index + 1;
for (i = 0; i < end; ++i)
{
size_t j = isURL(s + i, length - i);
if (j)
{
if (i <= index && index < i + j)
return 1;
i = i + j - 1;
}
}
return 0;
}
/************************************************
* Determine URL that index is in.
* Return malloc'd string, NULL if not in a URL.
*/
char *getURL(const char *s, size_t length, size_t index)
{
size_t i;
for (i = 0; i <= index; ++i)
{
size_t j = isURL(s + i, length - i);
if (j)
{
if (i <= index && index < i + j)
{
char *p = (char *)malloc(j + 1);
memcpy(p, s + i, j);
p[j] = 0;
return p;
}
i = i + j - 1;
}
}
return NULL;
}