-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_parser.c
329 lines (314 loc) · 7.89 KB
/
link_parser.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*****
** link_parser.c
** - implements the methods declared in link_parser.h
** - 处理http响应字节流,提取网络链接
*/
#include <stdio.h>
#include <regex.h>
#include <string.h>
#include "link_parser.h"
#include "http_url.h"
#define BUFFER_SIZE 1024
/* return then root len of url */
int _search_root(const char *link, size_t offset, char *url, link_offset_t *link_offset)
{
int c = 0, layer = 1, flag = 1;
if(offset >= 7 && memcmp(link, "http://", 7) == 0) {
return 1;
} else {
while(flag) {
if(c + 2 < offset && memcmp(link + c, "./", 2) == 0) {
c += 2;
} else if(c + 3 < offset && memcmp(link + c, "../", 3) == 0) {
c += 3;
layer++;
} else {
flag = 0;
}
}
int i, len = strlen(url);
for(i = len - 1; i >= 0; i--) {
if(url[i] == '/') { layer--; }
if(layer == 0) { break; }
}
link_offset->path_so = c;
link_offset->path_eo = offset;
link_offset->pre_so = 0;
link_offset->pre_eo = i + 1;
return 0;
}
}
void extract_absolute_link_s(const char *htmltxt, list_t *urllist, char *url)
{
enum {S_START, S_LEFT, S_A, S_BLANK, S_H, S_R, S_E, S_F, S_EQ, S_LQ, S_RQ, S_RIGHT, S_END};
const char *ch = htmltxt;
int status = S_START;
const char *as = NULL, *ae = NULL, *ls = NULL, *le = NULL;
link_offset_t link_offset;
char link[4096];
int prefix_size, path_size, link_size;
int k = 0;
while(*ch) {
switch(status) {
case S_START:
if(*ch == '<') {
status = S_LEFT;
as = ch;
} else if(*ch == '\n') {
status = S_END;
}
k++;
ch++;
break;
case S_LEFT:
if(*ch == 'a') {
status = S_A;
} else if(*ch == '>') {
ae = ch;
status = S_RIGHT;
continue;
} else if(!(*ch == '\t' || *ch == ' ')) {
status = S_END;
}
k++;
ch++;
break;
case S_A:
if(*ch == '\t' || *ch == ' ') {
status = S_BLANK;
} else if(*ch == '>') {
status = S_RIGHT;
ae = ch;
continue;
} else if(*ch == '\n') {
status = S_END;
} else {
status = S_END;
}
k++;
ch++;
break;
case S_BLANK:
if(*ch == '>') {
status = S_RIGHT;
ae = ch;
continue;
} else if(*ch == '\n') {
status = S_END;
} else if(*ch == 'h') {
status = S_H;
}
k++;
ch++;
break;
case S_H:
if(*ch == '>') {
status = S_RIGHT;
ae = ch;
continue;
} else if(*ch == '\n') {
status = S_END;
} else if(*ch == 'r') {
status = S_R;
} else {
status = S_BLANK;
}
k++;
ch++;
break;
case S_R:
if(*ch == '>') {
status = S_RIGHT;
ae = ch;
continue;
} else if(*ch == '\n') {
status = S_END;
} else if(*ch == 'e') {
status = S_E;
} else {
status = S_BLANK;
}
k++;
ch++;
break;
case S_E:
if(*ch == '>') {
status = S_RIGHT;
ae = ch;
continue;
} else if(*ch == '\n') {
status = S_END;
} else if(*ch == 'f') {
status = S_F;
} else {
status = S_BLANK;
}
k++;
ch++;
break;
case S_F:
if(*ch == '>') {
status = S_RIGHT;
ae = ch;
continue;
} else if(*ch == '\n') {
status = S_END;
} else if(*ch == '=') {
status = S_EQ;
} else if(!(*ch == '\t' || *ch == ' ')) {
status = S_BLANK;
}
k++;
ch++;
break;
case S_EQ:
if(*ch == '>') {
status = S_RIGHT;
ae = ch;
continue;
} else if(*ch == '\n') {
status = S_END;
} else if(*ch == '"' || *ch == '\'') {
ls = ch + 1;
status = S_LQ;
} else if(!(*ch == '\t' || *ch == ' ')) {
status = S_BLANK;
}
k++;
ch++;
break;
case S_LQ:
if(*ch == '>') {
status = S_RIGHT;
ae = ch;
continue;
} else if(*ch == '\n') {
status = S_END;
} else if(*ch == '"' || *ch == '\'') {
le = ch - 1 ;
status = S_RQ;
}
k++;
ch++;
break;
case S_RQ:
if(*ch == '>') {
status = S_RIGHT;
ae = ch;
continue;
} else if(*ch == '\n') {
status = S_END;
}
k++;
ch++;
break;
case S_RIGHT:
status = S_START;
if(!(ls == NULL || le == NULL)) {
bzero(link, 4096);
link_size = le - ls + 1;
if(!_search_root(ls, link_size, url, &link_offset)) {
prefix_size = link_offset.pre_eo - link_offset.pre_so; /* prefix_size */
path_size = link_offset.path_eo - link_offset.path_so; /* path_size */
if(prefix_size > 0 && path_size > 0) {
memcpy(link, url + link_offset.pre_so, prefix_size); /* copy prefix */
memcpy(link + prefix_size, ls + link_offset.path_so, path_size); /* copy path */
list_push_back(urllist, link, prefix_size + path_size + 1);
}
} else {
if(link_size > 0) {
memcpy(link, ls, link_size);
list_push_back(urllist, link, link_size + 1);
}
}
}
/*
printf("===============<a>======================\n");
printf("url: \n");
if(ls != NULL && le != NULL) {
for(i = 0; i <= le - ls; i++) {
printf("%c", ls[i]);
}
putchar('\n');
}
printf("=====================================\n");
*/
as = ae = ls = le = NULL;
k++;
ch++;
break;
case S_END:
status = S_START;
as = ae = ls = le = NULL;
break;
}
}
}
/*
the extract_link will change the info of the htmltxt,
if you will call it and stay the content of htmltxt not change,
you should pass the copy of htmltxt
*/
void extract_absolute_link(const char *htmltxt, list_t *urllist, char *url)
{
const size_t nmatch = 2;
int cflag, ecode;
char ebuf[128];
regex_t reg;
regmatch_t pmatch[nmatch];
cflag = REG_NEWLINE | REG_EXTENDED;
const char *ptr = htmltxt, *link_ptr;
link_offset_t link_offset;
int link_size, prefix_size, path_size;
char link[4096];
const char *pattern = "<[[:blank:]]*a[^>]*href[[:blank:]]*=[[:blank:]]*['\"]([^'\"]*)['\"][^>]*>";
if((ecode = regcomp(®, pattern, cflag)) == 0) {
/* get a <a ...>(...</a>)? in a line */
ptr = htmltxt;
while(regexec(®, ptr, nmatch, pmatch, 0) != REG_NOMATCH) {
link_ptr = ptr + pmatch[1].rm_so;
link_size = pmatch[1].rm_eo - pmatch[1].rm_so;
bzero(link, 4096);
if(!_search_root(link_ptr, link_size, url, &link_offset)) {
prefix_size = link_offset.pre_eo - link_offset.pre_so; /* prefix_size */
path_size = link_offset.path_eo - link_offset.path_so; /* path_size */
memcpy(link, url + link_offset.pre_so, prefix_size); /* copy prefix */
memcpy(link + prefix_size, link_ptr + link_offset.path_so, path_size); /* copy path */
list_push_back(urllist, link, prefix_size + path_size + 1);
} else {
memcpy(link, link_ptr, link_size);
list_push_back(urllist, link, link_size + 1);
}
ptr = ptr + pmatch[0].rm_eo + 1;
}
} else {
regerror(ecode, ®, ebuf, sizeof(ebuf));
fprintf(stderr, "%s: pattern '%s' complie failed.\n", ebuf, pattern);
}
regfree(®);
}
void extract_link(const char *htmltxt, list_t *urllist) {
const size_t nmatch = 2;
int cflag, ecode;
char ebuf[128];
char link[4096];
regex_t reg;
regmatch_t pmatch[nmatch];
cflag = REG_NEWLINE | REG_EXTENDED;
const char *ptr = htmltxt;
const char *pattern = "<[[:blank:]]*a[^>]*href[[:blank:]]*=[[:blank:]]*['\"]([^'\"]*)['\"][^>]*>";
if((ecode = regcomp(®, pattern, cflag)) == 0) {
/* get a <a ...>(...</a>)? in a line */
ptr = htmltxt;
while(regexec(®, ptr, nmatch, pmatch, 0) != REG_NOMATCH) {
bzero(link, 4096);
memcpy(link, ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
list_push_back(urllist, link, pmatch[1].rm_eo - pmatch[1].rm_so + 1);
ptr = ptr + pmatch[0].rm_eo;
}
} else {
regerror(ecode, ®, ebuf, sizeof(ebuf));
fprintf(stderr, "%s: pattern '%s' complie failed.\n", ebuf, pattern);
}
regfree(®);
}