-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_util.cc
81 lines (66 loc) · 1.56 KB
/
http_util.cc
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
#include "http_session.h"
#include <gflags/gflags.h>
#include <iostream>
using std::cout;
using std::cerr;
#include <memory>
using std::unique_ptr;
#include <string>
using std::string;
#include <vector>
using std::vector;
DEFINE_string(url, "", "URL to GET/POST");
DEFINE_string(post_data, "", "data for POST fields");
namespace reddit {
void PrintUsage(const string& exec) {
cerr << "Usage: " << exec << " <get|post>\n";
}
int get_main(HttpSession* http);
int post_main(HttpSession* http);
int get_main(HttpSession* http) {
if (!http) {
return 1;
}
if (FLAGS_url.empty()) {
cerr << "Need --url\n";
return 1;
}
string hdrs, body;
http->Get(FLAGS_url, &hdrs, &body);
cout << hdrs << "\n" << body << "\n";
return 0;
}
int post_main(HttpSession* http) {
if (!http) {
return 1;
}
if (FLAGS_url.empty()) {
cerr << "Need --url\n";
return 1;
}
string hdrs, body;
if (!http->Post(FLAGS_url, FLAGS_post_data, &hdrs, &body)) {
cerr << "Post failed.\n";
return 2;
}
cout << hdrs << "\n" << body << "\n";
return 0;
}
}; // namespace reddit
using namespace reddit;
int main(int argc, char** argv) {
google::ParseCommandLineFlags(&argc, &argv, true);
if (argc != 2) {
PrintUsage(argv[0]);
return 1;
}
unique_ptr<HttpSession> http(HttpSession::ConstructNew(""));
if (argv[1] == string("get")) {
return get_main(http.get());
} else if (argv[1] == string("post")) {
return post_main(http.get());
} else {
PrintUsage(argv[0]);
return 1;
}
}