htmlparse.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. //Wiby HTML Parser
  2. //Separates text from an HTML file
  3. //Remember to also set sql_mode = "NO_BACKSLASH_ESCAPES" in my.cnf
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #define window_len 100
  9. #define charset_len 100
  10. #define mysqlcharset_len 100
  11. #define title_len 152
  12. #define keywords_len 1024
  13. #define description_len 182
  14. #define robots_len 100
  15. #define body_len 81920
  16. #define urlList_len 102400
  17. #define strURL_len 102400
  18. FILE *bodyfile,*titlefile, *keywordsfile, *descriptionfile, *noindexfile, *nofollowfile, *charsetfile, *urlfile, *shuffledurlfile;
  19. static char filename[] = "page.out";
  20. char window[window_len],windowWithSpaces[window_len],charset[charset_len+1],mysqlcharset[mysqlcharset_len+1],title[title_len+1],keywords[keywords_len+1],description[description_len+1],robots[robots_len+1],body[body_len+1];
  21. char urlList[urlList_len+1],strURL[strURL_len+1],urlListShuffled[urlList_len+1],urlListHoldShuffled[urlList_len+1];
  22. int titlefound=0,charsetfound=0,descriptionfound=0,keywordsfound=0,robotsfound=0,nofollow=0,noindex=0,scriptfound=0,stylefound=0,urlFound=0,urlTagFound=0,numURL=0,emptytitle=1,spaces=0,seeded=0,num_stylesheets=0,num_scripts=0,getURLs=1;
  23. long charsetsize=0,titlesize=0,keywordssize=0,descriptionsize=0,robotssize=0,bodysize=0;
  24. int matchMySQLcharset(int html_charset_length, char *html_charset, int html_match_length, char *html_lowercase_match, char *html_uppercase_match);
  25. int locateInWindow(char *window, char *birdLower, char *birdUpper, int length);
  26. int locateInURL(char *url, char *birdLower, char *birdUpper, int length, int urlSize);
  27. int canCrawl(int urlSize);
  28. void shuffleURLs(int iterations, long urlListSize);
  29. void sqlsafe();
  30. void charset2mysql();
  31. FILE *f;
  32. char *fileStr;
  33. char c;
  34. void htmlparse(){
  35. long urlListSize=0;
  36. numURL=0;
  37. int intag=0,incomment=0,inscript=0,instyle=0,inlink=0,putspace=0,spacecount=0;
  38. int urlSize=0,dqcount=0;
  39. titlefound=charsetfound=descriptionfound=keywordsfound=robotsfound=nofollow=noindex=scriptfound=stylefound=0;
  40. charsetsize=titlesize=keywordssize=descriptionsize=robotssize=bodysize=0;
  41. memset(window,'#',window_len);
  42. window[window_len]=0;
  43. memset(windowWithSpaces,'#',window_len);
  44. windowWithSpaces[window_len]=0;
  45. memset(charset,0,charset_len+1);
  46. memset(mysqlcharset,0,mysqlcharset_len+1);
  47. memset(title,0,title_len+1);
  48. memset(keywords,0,keywords_len+1);
  49. memset(description,0,description_len+1);
  50. memset(robots,0,robots_len+1);
  51. memset(body,0,body_len+1);
  52. memset(urlList,0,urlList_len+1);
  53. memset(strURL,0,strURL_len+1);
  54. memset(urlListShuffled,0,urlList_len+1);
  55. memset(urlListHoldShuffled,0,urlList_len+1);
  56. printf("Parsing HTML... ");
  57. //open html file and load into memory
  58. f = fopen(filename, "rb");
  59. fseek(f, 0, SEEK_END);
  60. long fsize = ftell(f);
  61. fseek(f, 0, SEEK_SET); /* same as rewind(f); */
  62. fileStr = malloc(fsize + 1);
  63. if(fread(fileStr, 1, fsize, f)){};
  64. fclose(f);
  65. fileStr[fsize] = 0;
  66. //Locate the charset, title, description, keywords, robots, body
  67. //must accomodate human error in markup
  68. //must double all single quotes for mysql safety
  69. //dont allow extra whitespace, ignore cr/lf/tabs
  70. //complete it all in one pass
  71. for(int i=0;i<fsize;i++){
  72. c = fileStr[i];
  73. //use a rolling window of 100 bytes to detect elements, ignore control characters and spaces
  74. if(c != 127 && c > 32){
  75. for(int j=0;j<window_len-1;j++){
  76. window[j] = window[j+1];
  77. }
  78. window[window_len-1] = c;
  79. }
  80. //use a rolling window of 100 bytes to detect elements, but permit space, ignore control characters
  81. if(c != 127 && c > 31){
  82. for(int j=0;j<window_len-1;j++){
  83. windowWithSpaces[j] = windowWithSpaces[j+1];
  84. }
  85. windowWithSpaces[window_len-1] = c;
  86. }
  87. //Get Title
  88. if(titlefound == 2){
  89. if(titlesize < (title_len-2) && c != 127 && c > 31){
  90. title[titlesize]=c;
  91. titlesize++;
  92. if(c == 39){//check for single quotes and double them up for sql safety
  93. title[titlesize]=c;
  94. titlesize++;
  95. }
  96. if(c != 127 && c > 32){//some titles are just a bunch of spaces or garbage, need to check for that
  97. emptytitle = 0;
  98. }
  99. }
  100. if(locateInWindow(window,"</title>","</TITLE>",8)==1){
  101. titlefound = 3;
  102. //remove </title> from end of title by inserting null at location of <
  103. titlesize -= 8;
  104. title[titlesize] = 0;
  105. //printf("\n%s",title);
  106. }
  107. }
  108. if(titlefound == 1 && c=='>')//in case of this situation: <title some_nonsense>
  109. titlefound=2;
  110. if(titlefound == 0 && locateInWindow(window,"<title","<TITLE",6)==1){
  111. titlefound = 1;
  112. }
  113. //Get Charset
  114. if(charsetfound == 1){
  115. if(c == '>' || c == '/'){
  116. charsetfound = 2;
  117. //printf("\n%s",charset);
  118. }
  119. if(charsetfound == 1 && charsetsize < charset_len && c != '"' && c != '\''){
  120. charset[charsetsize]=c;
  121. charsetsize++;
  122. }
  123. }
  124. if(charsetfound == 0 && locateInWindow(window,"charset=","CHARSET=",8)==1){
  125. charsetfound = 1;
  126. }
  127. //Get Description
  128. if(descriptionfound == 1){
  129. if(c == '>' || c == '/'){
  130. descriptionfound = 2;
  131. //printf("\n%s",description);
  132. }
  133. if(descriptionfound == 1 && descriptionsize < (description_len-2) && c != '"'){
  134. description[descriptionsize]=c;
  135. descriptionsize++;
  136. if(c == 39){//check for single quotes and double them up for sql safety
  137. description[descriptionsize]=c;
  138. descriptionsize++;
  139. }
  140. }
  141. }
  142. if(descriptionfound == 0 && locateInWindow(window,"description\"content=","DESCRIPTION\"CONTENT=",20)==1){
  143. descriptionfound = 1;
  144. }
  145. //Get Keywords
  146. if(keywordsfound == 1){
  147. if(c == '>' || c == '/'){
  148. keywordsfound = 2;
  149. //printf("\n%s",keywords);
  150. }
  151. if(keywordsfound == 1 && keywordssize < (keywords_len-2) && c != '"'){
  152. keywords[keywordssize]=c;
  153. keywordssize++;
  154. if(c == 39){//check for single quotes and double them up for sql safety
  155. keywords[keywordssize]=c;
  156. keywordssize++;
  157. }
  158. }
  159. }
  160. if(keywordsfound == 0 && locateInWindow(window,"keywords\"content=","KEYWORDS\"CONTENT=",17)==1){
  161. keywordsfound = 1;
  162. }
  163. //Get Robots (nofollow, noindex)
  164. if(robotsfound == 1){
  165. if(c == '>' || c == '/'){
  166. robotsfound = 2;
  167. //printf("\n%s",robots);
  168. if(locateInWindow(window,"nofollow","NOFOLLOW",8)==1)
  169. nofollow=1;
  170. if(locateInWindow(window,"noindex","NOINDEX",7)==1 || locateInWindow(window,"none","NONE",4)==1)
  171. noindex=nofollow=1;
  172. }
  173. if(robotsfound == 1 && robotssize < robots_len && c != '"' && c != '\''){
  174. robots[robotssize]=c;
  175. robotssize++;
  176. }
  177. }
  178. if(robotsfound == 0 && locateInWindow(window,"robots\"content=","ROBOTS\"CONTENT=",15)==1){
  179. robotsfound = 1;
  180. }
  181. if(titlefound != 2){
  182. //Ignore between scripts, styles, and remove all tags, repeated spaces, tabs, cr, lf, null, add a space at end of every tag
  183. if(c=='<'){
  184. intag = 1;
  185. }else if(c=='>'){
  186. intag = 0;
  187. putspace = 1;
  188. }
  189. if(locateInWindow(window,"<!--","<!--",4)==1){
  190. incomment = 1;
  191. }else if(locateInWindow(window,"-->","-->",3)==1){
  192. incomment = 0;
  193. }
  194. if(locateInWindow(window,"<script","<SCRIPT",7)==1){
  195. inscript = 1;
  196. num_scripts++;
  197. }else if(locateInWindow(window,"</script>","</SCRIPT>",9)==1){
  198. inscript = 0;
  199. }
  200. if(locateInWindow(window,"<style","<STYLE",6)==1){
  201. instyle = 1;
  202. }else if(locateInWindow(window,"</style>","</STYLE>",8)==1){
  203. instyle = 0;
  204. }
  205. if(locateInWindow(window,"<link","<LINK",5)==1){
  206. inlink = 1;
  207. }else if(inlink==1 && locateInWindow(window,">",">",1)==1){
  208. inlink = 0;
  209. }
  210. if(inlink==1){
  211. if(locateInWindow(window,".css",".CSS",4)==1)
  212. num_stylesheets++;
  213. }
  214. //Get Body
  215. //exclude remaining tags, comments, scripts, styles, control characters, add a space after a '>' but only allow one
  216. if(intag == 0 && incomment == 0 && inscript == 0 && instyle == 0 && inlink == 0 && c != 127 && c > 31 && bodysize < (body_len-2)){
  217. if(putspace == 1){
  218. if(spacecount == 0){
  219. body[bodysize]=32;
  220. bodysize++;
  221. }
  222. spacecount++;
  223. putspace=0;
  224. }else{
  225. if(c==32)
  226. spacecount++;
  227. else spacecount = 0;
  228. if(spacecount < 2){
  229. body[bodysize]=c;
  230. bodysize++;
  231. if(c == 39){//check for single quotes and double them up for sql safety
  232. body[bodysize]=c;
  233. bodysize++;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. //Get URL's
  240. if(getURLs==1){
  241. if(urlFound == 1 && incomment==0 && instyle==0 && inscript==0 && inlink == 0){
  242. if(c=='"' || c=='\'')
  243. dqcount++;
  244. if((c == '#' && urlSize==0) || (dqcount == 2 && urlSize == 0) || (c == ' ' && urlSize == 0))
  245. urlFound=urlTagFound=dqcount=0;
  246. if((c == '>' || c == ' ') && urlFound == 1){
  247. if(canCrawl(urlSize)==0 || (urlSize+urlListSize) >= (urlList_len-1)){
  248. memset(strURL,0,strURL_len+1);
  249. }else{
  250. strcat(urlList,strURL);
  251. strcat(urlList,"\n");
  252. urlListSize+=urlSize+1;
  253. memset(strURL,0,strURL_len+1);
  254. numURL++;
  255. }
  256. urlFound = urlTagFound = urlSize = dqcount = 0;
  257. }
  258. if(urlFound == 1 && urlListSize < (urlList_len-2) && c != '"' && c != '\'' && urlSize < (strURL_len-2)){
  259. strURL[urlSize]=window[window_len-1];
  260. urlSize++;
  261. }
  262. if(urlSize==11){
  263. if(locateInWindow(window,"javascript:","JAVASCRIPT:",11)==1){
  264. urlFound=urlTagFound=urlSize=dqcount=0;
  265. memset(strURL,0,strURL_len+1);
  266. }
  267. }
  268. }
  269. if(urlFound == 0 && urlTagFound == 0 && incomment == 0 && instyle == 0 && inscript == 0 && inlink == 0 && locateInWindow(windowWithSpaces,"<a ","<A ",3)==1){//sometimes there is something between "<a" and "href"
  270. urlTagFound = 1;
  271. }
  272. if(urlFound == 0 && urlTagFound == 1 && incomment == 0 && instyle == 0 && inscript == 0 && inlink == 0 && locateInWindow(window,"href=","HREF=",5)==1){
  273. urlFound = 1;
  274. }
  275. }
  276. }
  277. //Convert charset to mysql equivalent
  278. charset2mysql();
  279. //print body to file
  280. /* bodyfile = fopen("body.txt","wb");
  281. fputs(body,bodyfile);
  282. fclose(bodyfile);
  283. //print title to file
  284. titlefile = fopen("title.txt","wb");
  285. fputs(title,titlefile);
  286. fclose(titlefile);
  287. //print keywords to file
  288. keywordsfile = fopen("keywords.txt","wb");
  289. fputs(keywords,keywordsfile);
  290. fclose(keywordsfile);
  291. //print description to file
  292. descriptionfile = fopen("description.txt","wb");
  293. fputs(description,descriptionfile);
  294. fclose(descriptionfile);
  295. //print charset to file
  296. charsetfile = fopen("charset.txt","wb");
  297. fputs(mysqlcharset,charsetfile);
  298. fclose(charsetfile);
  299. //print noindex to file
  300. noindexfile = fopen("noindex.txt","wb");
  301. if(noindex==1)
  302. fputs("noindex",noindexfile);
  303. fclose(noindexfile);
  304. //print nofollow to file
  305. nofollowfile = fopen("nofollow.txt","wb");
  306. if(nofollow==1)
  307. fputs("nofollow",nofollowfile);
  308. fclose(nofollowfile);*/
  309. if(getURLs==1){
  310. //shuffle order of collected URLs list
  311. shuffleURLs(10,urlListSize);
  312. //printf("\n%s",urlList);
  313. //print URLs to file
  314. /* urlfile = fopen("url.txt","wb");
  315. fputs(urlList,urlfile);
  316. fclose(urlfile);
  317. //print shuffled URLs to file
  318. shuffledurlfile = fopen("urlshuffled.txt","wb");
  319. fputs(urlListShuffled,shuffledurlfile);
  320. fclose(shuffledurlfile);*/
  321. }
  322. free(fileStr);
  323. printf("\nbody: %ld, title: %ld, charset: %ld, description: %ld, keywords: %ld, noindex: %d, nofollow: %d",bodysize,titlesize,charsetsize,descriptionsize,keywordssize,noindex,nofollow);
  324. }
  325. void shuffleURLs(int iterations, long urlListSize)
  326. {
  327. if(seeded==0){
  328. srand(time(NULL));
  329. seeded=1;
  330. }
  331. int r1,r2,r1to2;
  332. int urlCount,i,j,k,l;
  333. if(numURL>2){
  334. strcpy(urlListHoldShuffled,urlList);
  335. for(int loops=0;loops<iterations;loops++){
  336. r1 = r1to2 = (rand() % numURL) + 1;
  337. r2 = (rand() % numURL) + 1;
  338. if(r1>r2){
  339. r1=r2;
  340. r2=r1to2;
  341. }
  342. if(r1==r2){
  343. continue;
  344. }
  345. urlCount=i=j=k=l=0;
  346. //skip to url number r1
  347. while(urlCount < r1 /*&& i<urlList_len*/){
  348. if(urlListHoldShuffled[i]=='\n')
  349. urlCount++;
  350. i++;
  351. }
  352. j=i;
  353. //copy to urlListShuffled starting at j until reaching r2 location
  354. while(urlCount<r2 /*&& j<urlList_len*/){
  355. urlListShuffled[k]=urlListHoldShuffled[j];
  356. if(urlListHoldShuffled[j]=='\n')
  357. urlCount++;
  358. j++;
  359. k++;
  360. }
  361. //concat url's before i
  362. while(l<i /*&& k<urlList_len*/){
  363. urlListShuffled[k]=urlListHoldShuffled[l];
  364. l++;
  365. k++;
  366. }
  367. //concat url's after k
  368. while(k<urlListSize /*&& k<urlList_len*/){
  369. urlListShuffled[k]=urlListHoldShuffled[k];
  370. k++;
  371. }
  372. strcpy(urlListHoldShuffled,urlListShuffled);
  373. }
  374. }else{
  375. strcpy(urlListShuffled,urlList);
  376. }
  377. }
  378. void charset2mysql()
  379. {
  380. //if no charset specified, use utf8
  381. if(charsetsize == 0){
  382. strcpy(mysqlcharset,"SET CHARSET utf8;");
  383. printf("No Charset found. %s",mysqlcharset);
  384. }
  385. else{ //else, match charset with a proper mysql charset
  386. if(matchMySQLcharset(charsetsize,charset,5,"utf-8","UTF-8")==1){
  387. strcpy(mysqlcharset,"SET CHARSET utf8mb4;");
  388. printf("%s",mysqlcharset);
  389. }
  390. else if(matchMySQLcharset(charsetsize,charset,6,"latin1","LATIN1")==1){
  391. strcpy(mysqlcharset,"SET CHARSET latin1;");
  392. printf("%s",mysqlcharset);
  393. }
  394. else if(matchMySQLcharset(charsetsize,charset,9,"shift-jis","SHIFT-JIS")==1){
  395. strcpy(mysqlcharset,"SET CHARSET cp932;");
  396. printf("%s",mysqlcharset);
  397. }
  398. else if(matchMySQLcharset(charsetsize,charset,6,"x-sjis","X-SJIS")==1){
  399. strcpy(mysqlcharset,"SET CHARSET cp932;");
  400. printf("%s",mysqlcharset);
  401. }
  402. else if(matchMySQLcharset(charsetsize,charset,10,"iso-8859-1","ISO-8859-1")==1){
  403. strcpy(mysqlcharset,"SET CHARSET latin1;");
  404. printf("%s",mysqlcharset);
  405. }
  406. else if(matchMySQLcharset(charsetsize,charset,12,"windows-1252","WINDOWS-1252")==1){
  407. strcpy(mysqlcharset,"SET CHARSET latin1;");
  408. printf("%s",mysqlcharset);
  409. }
  410. else if(matchMySQLcharset(charsetsize,charset,12,"windows-1251","WINDOWS-1251")==1){
  411. strcpy(mysqlcharset,"SET CHARSET cp1251;");
  412. printf("%s",mysqlcharset);
  413. }
  414. else if(matchMySQLcharset(charsetsize,charset,6,"koi8-r","KOI8-R")==1){
  415. strcpy(mysqlcharset,"SET CHARSET cp1251;");
  416. printf("%s",mysqlcharset);
  417. }
  418. else if(matchMySQLcharset(charsetsize,charset,6,"euc-kr","EUC-KR")==1){
  419. strcpy(mysqlcharset,"SET CHARSET euckr;");
  420. printf("%s",mysqlcharset);
  421. }
  422. else if(matchMySQLcharset(charsetsize,charset,4,"big5","BIG5")==1){
  423. strcpy(mysqlcharset,"SET CHARSET big5;");
  424. printf("%s",mysqlcharset);
  425. }
  426. else{
  427. strcpy(mysqlcharset,"SET CHARSET utf8;");
  428. printf("Charset mismatch. %s",mysqlcharset);
  429. }
  430. }
  431. }
  432. int matchMySQLcharset(int html_charset_length, char *html_charset, int html_match_length, char *html_lowercase_match, char *html_uppercase_match)
  433. {
  434. int match = 0;
  435. int i=0;
  436. for(;i<html_match_length;i++){
  437. if(i > html_charset_length){
  438. return 0;
  439. }
  440. if(html_charset[i] != 95 && html_charset[i] != 45 && html_lowercase_match[i] != 95 && html_lowercase_match[i] != 45){ // _ or -
  441. if(html_lowercase_match[i] != html_charset[i] && html_uppercase_match[i] != html_charset[i]){
  442. return 0;
  443. }
  444. }
  445. match = 1;
  446. }
  447. return match;
  448. }
  449. int locateInWindow(char *window, char *birdLower, char *birdUpper, int length)
  450. {
  451. int start = window_len-length;
  452. for(int i=0;i<length;i++){
  453. if(window[start] != birdLower[i] && window[start] != birdUpper[i]){
  454. return 0;
  455. }
  456. start++;
  457. }
  458. return 1;
  459. }
  460. int locateInURL(char *url, char *birdLower, char *birdUpper, int length, int urlSize)
  461. {
  462. long start = urlSize-length;
  463. if(urlSize >= length){
  464. for(int i=0;i<length;i++){
  465. if(url[start] != birdLower[i] && window[start] != birdUpper[i]){
  466. return 0;
  467. }
  468. start++;
  469. }
  470. return 1;
  471. }else{
  472. return 0;
  473. }
  474. }
  475. //Check if url can be indexed (allow relative links for html and txt files. Removing this check will add to the queue everything listed including external links.
  476. int canCrawl(int urlSize){
  477. int numDots=0,numSlash=0;
  478. int slashpos=0,dotspos=0;
  479. int extfound=0,extlocation=0,prefixfound=0;
  480. for(int i=0;i<urlSize;i++){
  481. if(urlSize>5 && strURL[i]==':' && i>3){
  482. if((strURL[0]!='h' && strURL[0]!='H') || (strURL[1]!='t' && strURL[1]!='T') || (strURL[2]!='t' && strURL[2]!='T') || (strURL[3]!='p' && strURL[3]!='P') || (strURL[4]!='s' && strURL[4]!='S' && strURL[4]!=':') || (strURL[5]!=':' && strURL[5]!='/'))
  483. return 0;
  484. prefixfound=1;
  485. }
  486. if(strURL[i]=='?' || strURL[i]=='\\'){
  487. return 0;
  488. }
  489. if(strURL[i]=='.'){
  490. numDots++;
  491. }
  492. if(strURL[i]=='/'){
  493. numSlash++;
  494. }
  495. if(strURL[i]=='.' ){
  496. extfound=1;
  497. extlocation=i;
  498. }
  499. if(strURL[i]=='/' && extfound==1 && i>extlocation){
  500. extfound=0;
  501. }
  502. if(prefixfound==1 && numSlash-2<=0){
  503. extfound=0;
  504. }
  505. }
  506. if(numDots == 0){
  507. return 1;
  508. }
  509. //restrict file extensions to these
  510. if(extfound==1 && (locateInURL(strURL,".html",".HTML",5,urlSize)==1 || locateInURL(strURL,".htm",".HTM",4,urlSize)==1 || locateInURL(strURL,".txt",".TXT",4,urlSize)==1 || locateInURL(strURL,".php",".PHP",4,urlSize)==1 || locateInURL(strURL,".asp",".ASP",4,urlSize)==1)){
  511. return 1;
  512. }
  513. if(extfound==0 )
  514. return 1;
  515. return 0;
  516. }