-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebList.cpp
150 lines (122 loc) · 3.13 KB
/
WebList.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
#pragma warning(disable : 4996)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <chrono>
#include <ctime>
#include "headers/WebList.h"
#include "headers/WebPage.h"
#define LOGNAME_DEFAULT "weblist.log"
using namespace std;
/// <summary>
/// Upload Web Pages from the file to the list
/// </summary>
/// <param name="filename">Name of the file</param>
/// <returns>
/// True if uploading successful
/// False if uploading failed
/// </returns>
bool WebList::uploadList(const std::string& filename)
{
ifstream inputFile(filename);
string buffer;
// Chech if the file is opened
if (!inputFile.is_open()) {
return false;
}
// Read the configuration line by line
while (getline(inputFile, buffer)) {
istringstream bufferInput(buffer);
WebPage page;
if (bufferInput >> page) {
webList.push_back(page);
}
}
return true;
}
void WebList::checkAll()
{
for (auto &page : webList) {
auto start = chrono::high_resolution_clock::now(); // Set the timestamp
bool reqResult = page.request(); // Make a request and save the result
auto stop = chrono::high_resolution_clock::now(); // Set the timestamp
bool conResult = page.checkCondition(); // Check the content and save the result
auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start); // Calculate the response time
// Update response time
page.setTime(duration.count());
// Update page status
if (reqResult && conResult) {
page.setStatus("OK");
}
else if (reqResult && !conResult) {
page.setStatus("CONTENT MISSING");
}
else {
page.setStatus("FAILED");
}
}
}
/// <summary>
/// Save page information to log file
/// </summary>
void WebList::saveLog() const
{
ofstream outputFile;
outputFile.open("weblist.log", std::ios_base::app); // Open file in append mode
for (auto page : webList) {
outputFile << getTime() << " " << page << endl;
}
outputFile << endl;
}
void WebList::updateHTML() const
{
ofstream outputFile;
outputFile.open("serverdir/index.html");
// Write to HTML file
outputFile << getHeadHTML();
for (auto page : webList) {
outputFile << "<pre p>" << getTime() << " " << page.getFormatHTML() << endl;
}
outputFile << getTailHTML();
}
/**
* Calculate current time
*/
string WebList::getTime() const
{
ostringstream timeStream;
auto currentTime = chrono::system_clock::now();
time_t time = chrono::system_clock::to_time_t(currentTime);
timeStream << put_time(gmtime(&time), "%F %X");
return timeStream.str();
}
/**
* Obtain HEAD of the HTML file
*/
string WebList::getHeadHTML() const
{
ostringstream temp;
temp << "<!DOCTYPE html>" << endl;
temp << "<html lang=\"en - US\">" << endl;
temp << "<head>" << endl;
temp << " <meta charset=\"utf - 8\">" << endl;
temp << " <title>WEB MONITOR</title>" << endl;
temp << "</head>" << endl;
temp << "<body>" << endl;
temp << "<main>" << endl;
return temp.str();
}
/**
* Obtain the rest of the HTML page
*/
string WebList::getTailHTML() const
{
ostringstream temp;
temp << "</main>" << endl;
temp << "<hr>" << endl;
temp << "<p> Web Monitor by Pavel Arefyev </p>" << endl;
temp << "</html>";
return temp.str();
}