-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAction.h
77 lines (70 loc) · 2.69 KB
/
Action.h
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
#pragma once
#include <string>
#include <fstream>
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
#include "Exception.h"
#include <iostream>
#include <sstream>
struct HeadAction {
HeadAction(const std::string& path, bool is_verbose) : is_verbose {is_verbose} {
boost::filesystem::path boost_path(path);
boost::system::error_code ec;
create_directories(boost_path.parent_path(), ec);
file.exceptions(std::ios_base::failbit | std::ios_base::badbit);
file.open(path, std::ofstream::out | std::ofstream::app);
}
void process(int status_code, const std::string_view& candidate) {
if (status_code >= 200 && status_code < 300) { file << candidate << std::endl; }
if (is_verbose) std::cout << candidate << ": " << status_code << std::endl;
}
private:
const bool is_verbose;
std::ofstream file;
};
struct GetAction {
GetAction(const std::string& path_dir, const std::string& screen, bool is_verbose)
: re{ "[^a-zA-Z0-9.-]" }, is_verbose{ is_verbose }, path_dir{ path_dir }, screen{ screen } {
boost::filesystem::path boost_path(path_dir);
boost::system::error_code ec;
boost::filesystem::create_directories(path_dir, ec);
if (ec) throw AbradeException{"open path", ec};
}
template <typename Stream>
void process(int status_code, Stream contents, const std::string_view& candidate) {
if (is_verbose) {
std::stringstream ss;
ss << contents;
auto contents_string = ss.str();
std::cout << "[ ] Response from " << candidate << ":\n" << contents_string << std::endl;
write_out(contents_string, candidate);
}
else if (status_code >= 200 && status_code < 300) { write_out(contents, candidate); }
}
private:
template <typename Stream>
void write_out(Stream& contents, const std::string_view& candidate) {
auto path{path_dir};
path.append("/");
path.append(regex_replace(std::string{ candidate.begin(), candidate.end() }, re, "_"));
if (screen.empty()) {
std::ofstream file;
file.exceptions(std::ios_base::failbit | std::ios_base::badbit);
file.open(path, std::ofstream::out);
file << contents << std::endl;
} else {
std::stringstream ss;
ss << contents << std::endl;
const auto contents_str = ss.str();
const auto contains_screen = contents_str.find(screen) != std::string::npos;
if (contains_screen) return;
std::ofstream file;
file.exceptions(std::ios_base::failbit | std::ios_base::badbit);
file.open(path, std::ofstream::out);
file << contents_str << std::endl;
}
}
const boost::regex re;
const bool is_verbose;
const std::string path_dir, screen;
};