-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.h
162 lines (150 loc) · 3.64 KB
/
parse.h
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
#include"queue.h"
#include"crawler.h"
#define OVECCOUNT 30
#define BUFLEN 1024
#define MAX_URL_LENGTH 8192
#define EBUFLEN 128
const char pattern[] = "<a\\s[^>]*?href\\s*=\\s*[\"']?([^\"'\\s>]*)[^>]*>";
int isContainHost(char *url)
{
int i, flag = 0;
int urllen = strlen(url);
if (url[0] == '/')
return 0;
else
return 1;
}
void setFileNameAndHost(char *url, char* name, char* host)
{
int urllen = strlen(url);
int i, flag=0;
for (i = 0; i<urllen; i++)
{
if (flag == 0 && url[i]=='/')
{
flag = 1;
break;
}
}
if (flag == 0) //no /
{
name[0]='/';
name[1]=0;
strncpy(host, url, i);
host[i] = 0;
}
else if (flag == 1 && i <= urllen-1 && i > 0)
{
strncpy(host, url, i);
strncpy(name, url+i, urllen-i);
host[i] = 0;
name[urllen-i]=0;
}
}
int substr(char *des, const char*str, unsigned start, unsigned end)
{
unsigned n;
unsigned i=start;
char exam[1000];
strncpy(exam,str+start,end-start);
exam[end-start]=0;
if ((str[start]=='"' && str[end]=='"') )
{
start++;
end--;
i = start;
}
if((i+3) <= end && str[i]=='h'&&str[i+1]=='t'&&str[i+2]=='t'&&str[i+3]=='p')
start += 7;
n = end - start;
strncpy(des, str+start, n);
des[n] = 0;
//printf("match content:%s,and the sub url is:%s\n",exam,des);
return 0;
}
int parseUrl(char* content, char* host)
{
char url[MAX_URL_LENGTH];
char* temp;
redisReply *reply;
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
unsigned int offset = 0;
size_t urllen;
unsigned int contentlen;
int hostlen = strlen(host);
char logStr[LOG_LENGTH];
re = pcre_compile(pattern, 0,&error, &erroffset,NULL);
if (re == NULL)
{
printf("pcre compile failed at offset %d: \n",erroffset);
return 1;
}
offset = 0;
contentlen = strlen(content);
while (offset < contentlen && (rc = pcre_exec(re,0,content,contentlen,offset,0,ovector,sizeof(ovector))) == 2)
{
substr(url,content,ovector[2],ovector[3]); //get the url
urllen = strlen(url); //because url is just the only var,so malloc new
if (isContainHost(url) == 0)
{
temp = (char*)malloc( urllen + hostlen + 1);
strncpy(temp,host,hostlen);
strncpy(temp + hostlen, url, urllen);
temp[ urllen + hostlen ] = 0;
}
else
{
temp = (char*)malloc(urllen + 1);
strncpy(temp, url, urllen); //temp is the url,just the length of the url,not the sizeof
temp[urllen] = 0; //the last char is 0,for default
}
if (strstr(temp,"8684") != NULL) //the url is in the 8684 domain
{
pthread_mutex_lock(&queue_mutex);
reply = redisCommand(context,"sismember %s %s",setName,temp);
if (reply->type == REDIS_REPLY_INTEGER && reply->integer == 0)
{
listAddNodeTail(queue, temp); //add to the tail,always.lock
freeReplyObject(reply);
reply = redisCommand(context,"sadd %s %s",setName,temp);
freeReplyObject(reply);
pthread_cond_signal(&queue_cond);
snprintf(logStr,sizeof(logStr),"add new url:%s, queue size:%d",temp,queue->len);
logger(logStr);
}
else if (reply->type == REDIS_REPLY_INTEGER && reply->integer == 1)
{
//printf("the url:%s exits in the set,queue size:%d\n",temp,queue->len);
free(temp);
freeReplyObject(reply);
}
else
{
free(temp);
freeReplyObject(reply);
printf("some error occurs!\n");
}
pthread_mutex_unlock(&queue_mutex);
}
else
{
//printf("the url:%s is not the 8684 domain\n",temp);
free(temp); //is not the domain, free the array;
}
offset = ovector[3];
}
pcre_free(re);
return 0;
}
/*
int main()
{
char content[]="<a test=\"dfsdfd\" href=\"www.eee.com/sfdsds\">xxx</a>dfsd<a href=\"www.dfd.com\"></a><a href=\"/aas\">";
printf("%s\n",content);
parseUrl(content,"www.jjj.com");
}
*/