-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqtbrowser.cpp
942 lines (815 loc) · 21.5 KB
/
qtbrowser.cpp
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
#include "qtbrowser.h"
QtBrowser::QtBrowser(QObject *parent) :
QObject(parent)
{
this->cookieJar = new QNetworkCookieJar(this);
this->manager = new QNetworkAccessManager(this);
this->manager->setCookieJar(this->cookieJar);
this->prepareNetworkRequest(this->request);
}
QtBrowser::QtBrowser(QtBrowser &extBr)
{
this->clone(extBr);
}
/*
Clone current browser.
*/
void QtBrowser::clone(QtBrowser &extBr)
{
this->request = QNetworkRequest(extBr.getRequest());
this->manager = extBr.getManager();
this->page = extBr.getContent();
this->url = extBr.getURL();
// qDebug() << __FILE__ << __LINE__;
this->setUserAgent(extBr.getUserAgent());
}
QNetworkRequest QtBrowser::getRequest()
{
return this->request;
}
QNetworkAccessManager *QtBrowser::getManager()
{
return this->manager;
}
/*
Receive random integer number in range.
*/
int QtBrowser::randIt(int low, int high)
{
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
return qrand() % ((high + 1) - low) + low;
}
/*
Receive random integer number that is greater than 0.
*/
int QtBrowser::randIt(int high)
{
return this->randIt(0, high);
}
/*
Get cookie from host with key
*/
QByteArray QtBrowser::getCookie(const QNetworkCookieJar &cookieJar, const char *host, const QString &key)
{
QList<QNetworkCookie> cookies = cookieJar.cookiesForUrl(QUrl(host));
foreach(QNetworkCookie c, cookies)
{
if (QString(host).contains(c.domain()) && c.name() == key)
{
return c.value();
}
}
host = QString(host).replace(QUrl(QString(host)).scheme() + "://", ".").toUtf8().data();
foreach(QNetworkCookie c, cookies)
{
if (QString(host).contains(c.domain()) && c.name() == key)
{
return c.value();
}
}
return "";
}
QByteArray QtBrowser::getCookie(const char *host, const QString &key)
{
return this->getCookie(*this->manager->cookieJar(), host, key);
}
/*
Method for setting Cookie for the specified domain.
*/
bool QtBrowser::setCookie(QNetworkCookieJar *cookieJar, const QUrl &host, const QString &key, const QString &value)
{
QByteArray localKey;
localKey.append(key);
QByteArray localValue;
localValue.append(value);
QList<QNetworkCookie> cookies;
cookies.append(QNetworkCookie(localKey, localValue));
if (!cookieJar->setCookiesFromUrl(cookies, host))
{
return false;
}
else
{
return true;
}
}
/*
Reimpelemted method setCookie(QNetworkCookieJar &cookieJar, const QUrl &host, const QString &key, const QString &value)
*/
bool QtBrowser::setCookie(QNetworkCookieJar *cookieJar, const char *host, const QString &key, const QString &value)
{
return this->setCookie(cookieJar, QUrl(host), key, value);
}
bool QtBrowser::setCookie(const QUrl &host, const QString &key, const QString &value)
{
return this->setCookie(this->cookieJar, host, key, value);
}
bool QtBrowser::setCookie(const char *host, const QString &key, const QString &value)
{
return this->setCookie(this->cookieJar, QUrl(host), key, value);
}
/*
Methods for sync all cookies from source host with destination host
*/
bool QtBrowser::syncCookies(QNetworkCookieJar *cookieJar, const QString &src, const QString &dst)
{
QList<QNetworkCookie> cookies = cookieJar->cookiesForUrl(QUrl(src));
foreach(QNetworkCookie c, cookies)
{
if (!this->setCookie(cookieJar, dst, c.name(), c.value()))
{
return false;
}
}
return true;
}
bool QtBrowser::syncCookies(const QString &src, const QString &dst)
{
QString localSrc;
QString localDst;
if (!src.startsWith("http"))
{
localSrc.append(src).prepend("http://");
}
if (!dst.startsWith("http"))
{
localDst.append(dst).prepend("http://");
}
return this->syncCookies(this->cookieJar, localSrc, localDst);
}
void QtBrowser::clearCookies()
{
this->cookieJar->deleteLater();
this->cookieJar = new QNetworkCookieJar(this);
this->manager->setCookieJar(this->cookieJar);
}
QList<QNetworkCookie> QtBrowser::getCookies(QString src)
{
return cookieJar->cookiesForUrl(QUrl(src));
}
QNetworkCookieJar *QtBrowser::getCookieJar()
{
return this->cookieJar;
}
/*
Method for receiving text conform regExp.
*/
QString QtBrowser::regCap(const QString &where, const QString &what, bool setMin, int pos)
{
// qDebug() << "regCap" << what;
QRegExp r(what);
r.setMinimal(setMin);
r.setCaseSensitivity(Qt::CaseInsensitive);
if (r.indexIn(where) != -1)
{
// qDebug() << r.cap(pos);
return r.cap(pos);
}
else
{
return "";
}
}
QString QtBrowser::regCap(const QString &what, bool setMin, int pos)
{
return this->regCap(this->page, what, setMin, pos);
}
/*
Method for receiving all occurancies of regExp in text.
*/
QStringList QtBrowser::regCapAll(const QString &where, const QString ®exp, int column, bool setMin)
{
QRegExp rx(regexp, Qt::CaseInsensitive, QRegExp::RegExp2);
QStringList list;
int pos = 0;
rx.setMinimal(setMin);
QString cap;
while ((pos = rx.indexIn(where, pos)) != -1)
{
cap = rx.cap(column);
pos = rx.pos(column) + cap.size();
list << cap;
}
return list;
}
QStringList QtBrowser::regCapAll(const QString ®exp, int column)
{
return this->regCapAll(this->page, regexp, column);
}
/*
Поиск всех совпадений регулярных выражений в заданном тексте.
*/
QVector< QVector<QString> > QtBrowser::getMatches(const QString ®exp, QString where, bool setMin)
{
if (where.isEmpty())
{
where = this->getContent();
}
QVector< QVector<QString> > result;
// Количество выражений.
unsigned matchesCount = QString(regexp).replace("\\(", "(").count("(", Qt::CaseInsensitive);
// for (unsigned i = 1; i <= matchesCount; i++)
// {
// QRegExp rx(regexp, Qt::CaseInsensitive, QRegExp::RegExp2);
// QVector<QString> list;
// int pos = 0;
// rx.setMinimal(true);
// QString cap;
// while ((pos = rx.indexIn(where, pos)) != -1)
// {
// cap = rx.cap(i);
// pos = rx.pos(i) + cap.size();
// list << cap;
// }
// result.append(list);
// }
QRegExp rx(regexp, Qt::CaseInsensitive, QRegExp::RegExp2);
QVector<QString> list;
int pos = 0;
rx.setMinimal(setMin);
QString cap;
while ((pos = rx.indexIn(where, pos)) != -1)
{
list.clear();
for (unsigned i = 1; i <= matchesCount; i++)
{
cap = rx.cap(i);
pos = rx.pos(i) + cap.size();
list << cap;
}
result.append(list);
}
return result;
}
/*
Transfer content of current webpage.
*/
QString QtBrowser::getContent()
{
return this->page;
}
void QtBrowser::setContent(QString page)
{
this->page = page;
}
bool QtBrowser::contains(QString text, Qt::CaseSensitivity sensitivity)
{
return this->getContent().contains(text, sensitivity);
}
bool QtBrowser::contains(QRegExp regexp)
{
return this->getContent().contains(regexp);
}
QString QtBrowser::htmlDecode(QString text)
{
QMap<QString, QString> htmlSpecialEntities;
htmlSpecialEntities.insert(""", "\"");
htmlSpecialEntities.insert("&", "&");
htmlSpecialEntities.insert("<", "<");
htmlSpecialEntities.insert(">", ">");
htmlSpecialEntities.insert("Œ", "Œ");
htmlSpecialEntities.insert("–", "–");
htmlSpecialEntities.insert("—", "—");
htmlSpecialEntities.insert("‘", "‘");
htmlSpecialEntities.insert("’", "’");
htmlSpecialEntities.insert("‚", "‚");
htmlSpecialEntities.insert("“", "“");
htmlSpecialEntities.insert("”", "”");
foreach (QString entity, htmlSpecialEntities.keys())
{
text.replace(entity, htmlSpecialEntities.value(entity));
}
return text;
}
//QString QtBrowser::decodeXML(QString text)
//{
// if (text.trimmed().isDetached())
// {
// text = this->getContent();
// }
// QString pattern("(\\&#\\d{4};)");
// QStringList toDecode = this->regCapAll(text, pattern, 1, false);
// foreach(QString tc, toDecode)
// {
// uint num10 = this->regCap(tc, QString("(\\d{4})"), false).toInt();
// QString hex;
// hex.setNum(num10, 16);
// text.replace(tc, this->normalization(hex.prepend("\\u0")));
// }
// return text;
//}
QStringList QtBrowser::getHttpLinks(QString text, QString url)
{
if (text.trimmed().isEmpty())
{
text = this->getContent();
}
if (url.trimmed().isEmpty())
{
url = this->getURL();
}
text = text.trimmed();
if (text.isEmpty())
{
return QStringList();
}
text.replace("<", ">");
text.replace(">", "<");
text.replace("&", "&");
text.replace(""", "\"");
QRegExp re("<br.*>");
re.setMinimal(true);
text.replace(re, "\r\n");
text.remove("<wbr>");
text.remove(QRegExp("</?(i|b|u|s)>"));
QStringList links;
links.append(this->regCapAll(text, QString("href\\s*=\"(.*)\"")));
links.append(this->regCapAll(text, QString("<script type\\s*=\\s*\"text/javascript\" src=\"(.*)\">")));
links.append(this->regCapAll(text, QString("(https?://?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?)"), 1, false));
for (int i = 0; i < links.size(); i++)
{
// Если ссылка начинается с "/", значит она относительна корня, а значит стоит добавить домен и протокол впереди.
if (links.at(i).startsWith("/"))
{
QString link(links.at(i));
link.prepend(QUrl::fromUserInput(QUrl(url).host()).toString());
links.replace(i, link);
}
}
return links;
}
void QtBrowser::prepareNetworkRequest(QNetworkRequest &networkRequest)
{
networkRequest.setRawHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523 Ubuntu/9.04 (jaunty) Firefox/3.0.10");
networkRequest.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
networkRequest.setRawHeader("Accept-Language", "de, en-gb;q=0.9, en;q=0.8");
networkRequest.setRawHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
networkRequest.setRawHeader("Cache-Control", "no-cache");
networkRequest.setRawHeader("Connection", "close");
networkRequest.setRawHeader("Pragma", "no-cache");
}
/*
Method for synchronous receive data from Internet.
*/
QNetworkReply *QtBrowser::getItSync(const QUrl &url, const QByteArray params)
{
if (!this->visitedUrls.isEmpty())
{
this->request.setRawHeader("Referer", this->visitedUrls.last().toAscii().data());
}
this->visitedUrls.append(url.toString());
this->request.setUrl(url);
this->url = url.toString();
if (params.isEmpty())
{
this->reply = this->manager->get(this->request);
}
else
{
QNetworkRequest rtmp = this->request;
this->request.setRawHeader("Content-type", "application/x-www-form-urlencoded");
this->reply = this->manager->post(request, params);
this->request = rtmp;
}
QEventLoop loop;
connect(this->reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
this->redirectLocation = this->reply->rawHeader("Location");
if (this->followOnRedirect)
{
if (!this->redirectLocation.isEmpty())
{
if (!this->redirectLocation.startsWith("http"))
{
bool startedWithSlash = true;
if (!this->redirectLocation.startsWith("/"))
{
startedWithSlash = false;
}
QString toPrepend(this->regCap(this->getURL(), QString("(https?://(www\\.)?.*)/(.+)")));
if (!startedWithSlash)
{
toPrepend = this->getURL();
}
this->redirectLocation.prepend(toPrepend);
}
qDebug() << "Request redirected to:" << this->redirectLocation;
return this->getItSync(this->redirectLocation, params);
}
}
return this->reply;
}
/*
Method for receiving pair of values from JSON-string.
*/
QString QtBrowser::jsonVal(QString where, QString name)
{
return this->regCap(where, "'\\s?" + name + "\\s?':\\s?'(.*)'");
}
void QtBrowser::setURL(QString url)
{
this->url = url;
}
QString QtBrowser::getURL()
{
if (this->visitedUrls.size())
{
return this->visitedUrls.last();
}
else
{
return this->url;
}
}
/*
Method for receiving all occurancies of specified tag.
*/
QStringList QtBrowser::getSpecifiedTags(const QString &where, const QString &tag)
{
QStringList singleTagsList;
singleTagsList.append("input");
singleTagsList.append("a");
singleTagsList.append("p");
singleTagsList.append("image");
QStringList tags;
QString localWhere = where;
QString localTag;
int pos = 0;
QString singleTagClosed = "(<[\\s]*"+tag+".*/>)";
QString singleTagUnclosed = "(<[\\s]*"+tag+".*>)";
QString doubleTag = "(<[\\s]*"+tag+"[\\s]*.*>.*</"+tag+">)";
while (!this->regCap(localWhere, doubleTag).isEmpty() || !this->regCap(localWhere, singleTagClosed).isEmpty() || !this->regCap(localWhere, singleTagUnclosed).isEmpty())
{
if (singleTagsList.contains(tag, Qt::CaseInsensitive))
{
localTag = regCap(localWhere, singleTagUnclosed);
if (localTag.isEmpty())
{
localTag = regCap(localWhere, singleTagClosed);
}
}
else
{
localTag = regCap(localWhere, doubleTag);
}
pos = localWhere.indexOf(localTag) + localTag.size();
tags.append(localTag);
localWhere.remove(0, pos);
}
return tags;
}
/*
Check if tag is parameters
*/
bool QtBrowser::isTagParam(const QString &tag, const QString ¶m)
{
bool result = this->regCap(tag, "[\\s< ]?[^\"'](" + param + ")[^\"'][\\s]?").isEmpty();
return !result;
}
/*
Get value of the param.
*/
QString QtBrowser::getParamValue(const QString &where, const QString ¶mName)
{
QString result = this->regCap(where, paramName + "=\"(.*)\"[\\s>]").trimmed();
if (result.trimmed().isEmpty())
{
result = this->regCap(where, paramName + "='(.*)'[\\s>]").trimmed();
}
if (result.trimmed().isEmpty())
{
result = this->regCap(where, paramName + "=(.*)[\\s>]").trimmed();
}
if (result.trimmed().isEmpty())
{
result = this->regCap(where, paramName + "\\s*=\\s*['\"](.*)['\"]");
}
result.replace("\"\"", "");
result.replace("''", "");
return result;
}
/*
Method for parameter name and it's value, which will be used for form submitting.
*/
QString QtBrowser::getNameValue(QString where)
{
QString result;
QString name = this->getParamValue(where, "name");
QString value = this->getParamValue(where, "value");
result = name + "=" + value;
result.replace(" ", "+");
result.replace("\"\"", "");
result.replace("''", "");
return result;
}
QStringList QtBrowser::getForms()
{
return this->getSpecifiedTags(this->getContent(), "form");
}
/*
Find first form, that has specified properties.
*/
QString QtBrowser::getFormByProperty(const QString &where, const QString &prop, const QString &value)
{
foreach(QString form, this->getSpecifiedTags(where, "form"))
{
QString initTag = this->regCap(form, QString("(<.*>)"));
if (!this->regCap(initTag, "("+prop+"\\s*=\\s*\""+value+"\")").trimmed().isEmpty())
{
return form;
}
}
return "";
}
QString QtBrowser::getFormByProperty(const QString &property, const QString &value)
{
return this->getFormByProperty(this->page, property, value);
}
/*
Add value to the form.
*/
QString QtBrowser::formPutValue(QString form, const QString &name, const QString &value)
{
QString n = name;
QString v = value;
n.replace("\"", "");
v.replace("\"", "");
if (!form.contains(n, Qt::CaseInsensitive))
{
form.replace("</form>", "<input name=\"" + n + "\" value=\"" + v + "\" /></form>");
}
else
{
form.replace("name=\"" + n + "\"", "name=\"" + n + "\" value=\"" + v + "\" ");
}
return form;
}
/*
Methods for getting all form parameters.
*/
QByteArray QtBrowser::getFormParams(QString where)
{
QByteArray localParams;
QStringList tags;
tags.append("input");
tags.append("textarea");
foreach(QString t, tags)
{
foreach(QString tag, this->getSpecifiedTags(where, t))
{
if (!this->isTagParam(tag, "name"))
{
continue;
}
QString toAppend = this->getNameValue(tag) + "&";
localParams.append(toAppend);
}
}
localParams.remove(localParams.size()-1, 1);
return localParams;
}
/*
Submit form.
*/
QNetworkReply *QtBrowser::submitForm(QString form)
{
QString url = regCap(form, QString("action=\"(.*)\""));
url.replace("///", "//");
if (url.trimmed().isEmpty())
{
url = this->getURL();
}
if (!this->visitedUrls.isEmpty() && !url.contains(QUrl(this->visitedUrls.last()).host()) && QUrl(url).isRelative())
{
url = url.prepend(QUrl(this->visitedUrls.last()).host());
}
if (!url.contains("://"))
{
url = url.prepend("http://");
}
if (url.isEmpty())
{
return 0;
}
else
{
const QString method = this->getParamValue(form, "method").toLower();
if (method == QString("post").toLower())
{
this->request.setRawHeader("Content-type", "application/x-www-form-urlencoded");
QByteArray params = this->getFormParams(form);
return this->getItSync(url, params);
}
else if (method == QString("get").toLower())
{
this->request.setRawHeader("Content-type", 0);
return this->getItSync(url);
}
else
{
qDebug() << trUtf8("Unknow type of form submit.");
return 0;
}
}
}
void QtBrowser::submit(QString form)
{
this->setContent(this->submitForm(form)->readAll());
}
/**
* @brief QtBrowser::getFormByHTML - Поиск формы по содержимому.
* @param text - Текст, который искать.
* @return - нужная форма.
*/
QString QtBrowser::getFormByHTML(QString text)
{
QStringList forms = this->getSpecifiedTags(this->getContent(), "form");
foreach (QString form, forms)
{
if (form.contains(text))
{
return form;
}
}
return "";
}
/*
Generate new User-Agent header.
*/
void QtBrowser::generateUserAgent()
{
// qDebug() << __FILE__ << __LINE__;
UserAgenContainer container;
this->setUserAgent(container.getRandomUserAgent());
}
/*
Set User-Agent.
*/
void QtBrowser::setUserAgent(QByteArray userAgent)
{
// qDebug() << __FILE__ << __LINE__ << userAgent;
this->userAgent = userAgent;
this->request.setRawHeader("User-Agent", this->userAgent);
}
/*
Get User-Agent
*/
QByteArray QtBrowser::getUserAgent()
{
if (this->userAgent.trimmed().isEmpty())
{
this->userAgent = this->request.rawHeader("User-Agent");
}
// qDebug() << __FILE__ << __LINE__ << this->userAgent;
return this->userAgent;
}
QString QtBrowser::getRedirectLocation()
{
return this->redirectLocation;
}
bool QtBrowser::hasHeader(QByteArray headerName)
{
if (this->request.hasRawHeader(headerName))
{
return true;
}
else if (this->request.hasRawHeader(headerName))
{
return true;
}
return false;
}
QByteArray QtBrowser::getHeader(QByteArray header)
{
if (this->request.hasRawHeader(header))
{
return this->request.rawHeader(header);
}
else
{
if (this->hasHeader(header))
{
return this->reply->rawHeader(header);
}
}
return "";
}
void QtBrowser::setHeader(QByteArray name, QByteArray value)
{
this->request.setRawHeader(name, value);
}
QtBrowser QtBrowser::clearHeader(QByteArray name)
{
this->request.setRawHeader(name, 0);
return *this;
}
QList<QPair<QByteArray, QByteArray> > QtBrowser::headers()
{
return this->reply->rawHeaderPairs();
}
/*
Нормализация строки, содержащей юникод-символы.
https://ru.wikipedia.org/wiki/Юникод
*/
QString QtBrowser::normalization(QString str)
{
QStringList unicodeSymbols = this->regCapAll(str, "(\\\\u[0-9a-f]{4}|\\\\x[0-9a-f]{2})", 1, false);
for (int i = 0; i < unicodeSymbols.size(); i++)
{
// Удаляем первые 2 символа - это либо \u, либо \x.
QString curr = QString(unicodeSymbols.at(i)).remove(0, 2);
// Просто переменная, куда вернётся результат успешности преобразования строки.
bool ok;
// Заменяем юникод-символ на его ASCII-представление.
str.replace(unicodeSymbols.at(i), QString(char(curr.toInt(&ok, 16))));
}
return str;
}
/*
Декодирование строки.
*/
QString QtBrowser::decode(QString str)
{
QStringList hexSymbols = this->regCapAll(str, "(%[0-9a-f]{2})");
for (int i = 0; i < hexSymbols.size(); i++)
{
// Удаляем %
QString curr = QString(hexSymbols.at(i)).remove(0, 1);
// Просто переменная, куда вернётся результат успешности преобразования строки.
bool ok;
// Заменяем символ на его ASCII-представление.
str.replace(hexSymbols.at(i), QString(char(curr.toInt(&ok, 16))));
}
return str;
}
bool QtBrowser::save(const QString &fileName, QIODevice *data)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
qDebug() << "Couldn't open" << fileName << "for writing" << file.errorString();
return false;
}
file.write(data->readAll());
file.close();
return true;
}
bool QtBrowser::save(const QString &fileName, const QString &data)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Couldn't open" << fileName << "for writing" << file.errorString();
return false;
}
QByteArray localdata;
localdata.append(data);
file.write(localdata);
file.close();
return true;
}
/*
Receive webpage as text.
*/
QString QtBrowser::getPage(const QUrl url, const QByteArray params)
{
QUrl _url(url);
if (!this->visitedUrls.isEmpty() && !url.toString().contains(QUrl(this->visitedUrls.last()).host()) && url.isRelative())
{
_url.setUrl(QUrl::fromUserInput(url.toString().prepend(QUrl(this->visitedUrls.last()).host())).toString());
qDebug() << __FILE__ << __LINE__ << _url;
}
this->setURL(_url.toString());
this->page = this->getItSync(_url, params)->readAll();
return this->page;
}
QString QtBrowser::getPage(const char *url, const QByteArray params)
{
return this->getPage(QUrl(url), params);
}
/*
To redirect or not to redirect.
*/
void QtBrowser::setFollowRedirects(bool val)
{
this->followOnRedirect = val;
}
/*
HEAD-запрос.
*/
QNetworkReply *QtBrowser::head(const QUrl url)
{
QNetworkReply *reply;
QNetworkAccessManager *manager = new QNetworkAccessManager();
reply = manager->head(QNetworkRequest(url));
QEventLoop *loop = new QEventLoop(this);
connect(reply, SIGNAL(finished()), loop, SLOT(quit()));
loop->exec();
loop->deleteLater();
return reply;
}
int QtBrowser::getResponseCode()
{
return this->reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}