-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
135 lines (112 loc) · 3.78 KB
/
Copy pathmain.cpp
File metadata and controls
135 lines (112 loc) · 3.78 KB
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
#include <iostream>
#include <vector>
#include <unordered_map>
#include <set>
#include <sstream>
#include <cstring>
#include <netinet/in.h>
#include <unistd.h>
class WebPage {
public:
std::string url;
std::string content;
WebPage(std::string u, std::string c) : url(u), content(c) {}
};
class InvertedIndex {
private:
std::unordered_map<std::string, std::set<int>> index;
public:
void addPage(int pageId, const std::vector<std::string>& tokens) {
for (const std::string& token : tokens) {
index[token].insert(pageId);
}
}
std::set<int> search(const std::string& token) const {
if (index.find(token) != index.end()) {
return index.at(token);
}
return std::set<int>();
}
};
class SearchEngine {
private:
std::vector<WebPage> pages;
InvertedIndex index;
std::vector<std::string> tokenize(const std::string& content) {
std::vector<std::string> tokens;
std::string token;
for (char ch : content) {
if (ch == ' ') {
if (!token.empty()) {
tokens.push_back(token);
token.clear();
}
} else {
token += ch;
}
}
if (!token.empty()) tokens.push_back(token);
return tokens;
}
public:
void addPage(const WebPage& page) {
pages.push_back(page);
int pageId = pages.size() - 1;
index.addPage(pageId, tokenize(page.content));
}
std::string search(const std::string& query) {
std::set<int> results = index.search(query);
if (results.empty()) {
return "No results found.";
}
std::stringstream resultStream;
for (int pageId : results) {
resultStream << "Found: " << pages[pageId].url << "<br>";
}
return resultStream.str();
}
};
void handleRequest(int clientSocket, SearchEngine& engine) {
char buffer[1024];
recv(clientSocket, buffer, sizeof(buffer), 0);
std::string request(buffer);
std::string response;
if (request.find("GET /search?query=") == 0) {
size_t queryStart = request.find("query=") + 6;
size_t queryEnd = request.find(" ", queryStart);
std::string query = request.substr(queryStart, queryEnd - queryStart);
response = engine.search(query);
} else if (request.find("GET / HTTP/1.1") == 0) {
response = "<html><body><h1>Welcome to the Search Engine</h1>"
"<form action='/search' method='get'>"
"Enter search query: <input type='text' name='query'>"
"<button type='submit'>Search</button>"
"</form></body></html>";
} else {
response = "HTTP/1.1 404 Not Found\n";
}
std::string httpResponse = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n" + response;
send(clientSocket, httpResponse.c_str(), httpResponse.length(), 0);
close(clientSocket);
}
void startServer(SearchEngine& engine) {
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080); // Use port 8080
serverAddr.sin_addr.s_addr = INADDR_ANY;
bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
listen(serverSocket, 5);
std::cout << "Server is running on port 8080...\n";
while (true) {
int clientSocket = accept(serverSocket, NULL, NULL);
handleRequest(clientSocket, engine);
}
}
int main() {
SearchEngine engine;
engine.addPage(WebPage("https://example.com", "Example content with data structures and algorithms"));
engine.addPage(WebPage("https://example2.com", "Another example with algorithms and more data"));
startServer(engine);
return 0;
}