25
25
#include < iostream>
26
26
#include < memory>
27
27
#include < string>
28
- #include " Searcher.h" "
28
+ #include " Searcher.h"
29
+ #include " IniParser.h"
29
30
30
31
namespace beast = boost::beast; // from <boost/beast.hpp>
31
32
namespace http = beast::http; // from <boost/beast/http.hpp>
@@ -140,44 +141,16 @@ class http_connection : public std::enable_shared_from_this<http_connection>
140
141
void
141
142
create_response ()
142
143
{
143
- if(request_.target() == " /count" )
144
- {
145
- response_.set(http::field::content_type, " text/html" );
146
- beast::ostream(response_.body())
147
- << " < html> \n"
148
- << " < head> < title> Request count< /title> < /head> \n"
149
- << " < body> \n"
150
- << " < h1> Request count< /h1> \n"
151
- << " < p> There have been "
152
- << my_program_state::request_count()
153
- << " requests so far.< /p> \n"
154
- << " < /body> \n"
155
- << " < /html> \n" ;
156
- }
157
- else if(request_.target() == " /time" )
158
- {
159
- response_.set(http::field::content_type, " text/html" );
160
- beast::ostream(response_.body())
161
- << " < html> \n"
162
- << " < head> < title> Current time< /title> < /head> \n"
163
- << " < body> \n"
164
- << " < h1> Current time< /h1> \n"
165
- << " < p> The current time is "
166
- << my_program_state::now()
167
- << " seconds since the epoch.< /p> \n"
168
- << " < /body> \n"
169
- << " < /html> \n" ;
170
- }
171
- else if(request_.target() == " /search" )
144
+ if (request_.target () == " /" )
172
145
{
173
146
response_.set (http::field::content_type, " text/html" );
174
147
beast::ostream (response_.body ())
175
148
<< " <!DOCTYPE html>\n "
176
149
<< " <html>\n "
177
150
<< " <body>\n "
178
- << " < form action=\"/search \" method=\"post\"> \n"
151
+ << " <form action=\" /\" method=\" post\" >\n "
179
152
<< " <label for=\" fname\" >Search request:</label><br>\n "
180
- << " < input type=\"text\" id=\"request\" name=\"request\" value=\"what? \"> < br> < br> \n"
153
+ << " <input type=\" text\" id=\" request\" name=\" request\" value=\" than \" ><br><br>\n "
181
154
<< " <input type=\" submit\" value=\" Submit\" >\n "
182
155
<< " </form>\n "
183
156
<< " </body>\n "
@@ -225,25 +198,27 @@ class http_connection : public std::enable_shared_from_this<http_connection>
225
198
std::cout << e.what () << std::endl;
226
199
}
227
200
228
- // преобразование списка ресурсов к HTML-формату
229
- std::string hrefsStr = getHrefListStringFromVector(urls);
230
-
231
- // формирование ответа
232
- response_.set(http::field::content_type, " text/html" );
233
- beast::ostream(response_.body())
234
- << " < !DOCTYPE html> \n"
235
- << " < html> \n"
236
- << " < body> \n"
237
- << " < form action=\"/search\" method=\"post\"> \n"
238
- << " < label for=\"fname\"> Search request:< /label> < br> \n"
239
- << " < input type=\"text\" id=\"request\" name=\"request\" value=\"what?\"> < br> < br> \n"
240
- << " < input type=\"submit\" value=\"Submit\"> \n"
241
- << " < /form> \n"
242
- << " < p> \n"
243
- << hrefsStr
244
- << " < /p> \n"
245
- << " < /body> \n"
246
- << " < /html> \n" ;
201
+ if (urls.size () > 0 ) {
202
+ // преобразование списка ресурсов к HTML-формату
203
+ std::string hrefsStr = getHrefListStringFromVector (urls);
204
+
205
+ // формирование ответа
206
+ response_.set (http::field::content_type, " text/html" );
207
+ beast::ostream (response_.body ())
208
+ << " <!DOCTYPE html>\n "
209
+ << " <html>\n "
210
+ << " <body>\n "
211
+ << " <form action=\" /search\" method=\" post\" >\n "
212
+ << " <label for=\" fname\" >Search request:</label><br>\n "
213
+ << " <input type=\" text\" id=\" request\" name=\" request\" value=\" than\" ><br><br>\n "
214
+ << " <input type=\" submit\" value=\" Submit\" >\n "
215
+ << " </form>\n "
216
+ << " <p>\n "
217
+ << hrefsStr
218
+ << " </p>\n "
219
+ << " </body>\n "
220
+ << " </html>\n " ;
221
+ }
247
222
}
248
223
249
224
// Asynchronously transmit the response message.
@@ -285,25 +260,32 @@ class http_connection : public std::enable_shared_from_this<http_connection>
285
260
{
286
261
// преобразование вектора ресурсов к их перечислению в формате HTML
287
262
288
- std::string line = " " ;
289
- auto it = urlsVector.begin();
290
- std::string val = *it;
291
- line += " < a href=\"> " ;
292
- line += (val);
293
- line += " \" >" ;
294
- line += (val);
295
- line += " </a>" ;
263
+ std::string line = " No results" ;
264
+ if (urlsVector.size () > 0 ) {
296
265
297
- while (it != urlsVector.end() - 1 )
298
- {
299
- ++it;
300
- line += " <br>" ;
266
+ std::string line = " " ;
267
+ auto it = urlsVector.begin ();
301
268
std::string val = *it;
302
- line += " <a href=\" >" ;
269
+ val = " https://" + val;
270
+ line += " <a href=\" " ;
303
271
line += (val);
304
272
line += " \" >" ;
305
273
line += (val);
306
274
line += " </a>" ;
275
+
276
+ while (it != urlsVector.end () - 1 )
277
+ {
278
+ ++it;
279
+ line += " <br>" ;
280
+ std::string val = *it;
281
+ val = " https://" + val;
282
+ line += " <a href=\" " ;
283
+ line += (val);
284
+ line += " \" >" ;
285
+ line += (val);
286
+ line += " </a>" ;
287
+ }
288
+ return line;
307
289
}
308
290
return line;
309
291
}
@@ -327,19 +309,10 @@ main(int argc, char* argv[])
327
309
{
328
310
try
329
311
{
330
- // Check command line arguments.
331
- if (argc != 3 )
332
- {
333
- std::cerr << " Usage: " << argv[0 ] << " <address> <port>\n " ;
334
- std::cerr << " For IPv4, try:\n " ;
335
- std::cerr << " receiver 0.0.0.0 80\n " ;
336
- std::cerr << " For IPv6, try:\n " ;
337
- std::cerr << " receiver 0::0 80\n " ;
338
- return EXIT_FAILURE;
339
- }
340
-
341
- auto const address = net::ip::make_address (argv[1 ]);
342
- unsigned short port = static_cast <unsigned short >(std::atoi (argv[2 ]));
312
+ IniParser parser (CONFIG_PATH);
313
+ std::string serverIp = parser.get_value <std::string>(" Searcher.serverIp" );
314
+ auto const address = net::ip::make_address (serverIp);
315
+ unsigned short port = parser.get_value <unsigned short >(" Searcher.serverPort" );
343
316
344
317
net::io_context ioc{1 };
345
318
0 commit comments