1 | #include <iostream> |
---|
2 | #include <string> |
---|
3 | #include "stopos.h" |
---|
4 | #include "wrequest.h" |
---|
5 | #include <stdlib.h> |
---|
6 | #include <curl/curl.h> |
---|
7 | #include <fstream> |
---|
8 | #include <sys/stat.h> |
---|
9 | #include <sys/types.h> |
---|
10 | #include <errno.h> |
---|
11 | |
---|
12 | //constructor |
---|
13 | wrequest::wrequest(void) |
---|
14 | { |
---|
15 | this->set_configfilename(); |
---|
16 | curl_global_init(CURL_GLOBAL_ALL); |
---|
17 | this->curl = curl_easy_init(); |
---|
18 | this->debugging = 0; |
---|
19 | } |
---|
20 | |
---|
21 | //destructor |
---|
22 | wrequest::~wrequest() |
---|
23 | { |
---|
24 | curl_easy_cleanup(curl); |
---|
25 | curl_global_cleanup(); |
---|
26 | } |
---|
27 | |
---|
28 | void wrequest::set_configfilename(void) |
---|
29 | { |
---|
30 | char *h = getenv("HOME"); |
---|
31 | std::string home; |
---|
32 | if (h == 0) |
---|
33 | home = "/"; |
---|
34 | else |
---|
35 | home = h; |
---|
36 | this->configdirname = home + "/.stopos"; |
---|
37 | this->configfilename = this->configdirname + "/id"; |
---|
38 | } |
---|
39 | |
---|
40 | int wrequest::read_config(void) |
---|
41 | { |
---|
42 | int rc = get_file_contents(this->config,this->configfilename); |
---|
43 | if (rc == 0) |
---|
44 | { |
---|
45 | size_t p= this->config.find_first_of('\n'); |
---|
46 | if (p != this->config.npos) |
---|
47 | this->config.resize(p); |
---|
48 | return 0; |
---|
49 | } |
---|
50 | else |
---|
51 | return 1; |
---|
52 | } |
---|
53 | |
---|
54 | int wrequest::write_config(void) |
---|
55 | { |
---|
56 | std::ofstream f; |
---|
57 | int rc; |
---|
58 | |
---|
59 | std::cerr << "WTD:" << __LINE__ <<this->configdirname<<std::endl; |
---|
60 | rc = mkdir(this->configdirname.c_str(),0700); |
---|
61 | if (rc < 0) |
---|
62 | if (errno != EEXIST) |
---|
63 | return 1; |
---|
64 | f.open(this->configfilename.c_str(),std::ios::out); |
---|
65 | if (f.fail()) |
---|
66 | return 1; |
---|
67 | this->config = gen_random(this->config_size)+'\n'; |
---|
68 | f << this->config; |
---|
69 | f.close(); |
---|
70 | rc = chmod(this->configfilename.c_str(),0600); |
---|
71 | if (rc < 0) |
---|
72 | return 1; |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | size_t write_data(char *p, size_t size, size_t nmemb, std::string *userdata) |
---|
78 | { |
---|
79 | size_t l = size*nmemb; |
---|
80 | for (size_t i=0; i<l; i++) |
---|
81 | (*userdata).push_back(p[i]); |
---|
82 | return l; |
---|
83 | } |
---|
84 | |
---|
85 | int wrequest::set_serverurl(const std::string &u) |
---|
86 | { |
---|
87 | this->serverurl = u; |
---|
88 | if (this->serverurl[this->serverurl.size()-1] != '/') |
---|
89 | this->serverurl += '/'; |
---|
90 | curl_easy_setopt(this->curl,CURLOPT_HEADER,0L); |
---|
91 | curl_easy_setopt(this->curl,CURLOPT_NOPROGRESS, 1L); |
---|
92 | curl_easy_setopt(this->curl,CURLOPT_WRITEFUNCTION, write_data); |
---|
93 | |
---|
94 | return 0; |
---|
95 | } |
---|
96 | |
---|
97 | void wrequest::compose_message(std::string &m) |
---|
98 | { |
---|
99 | m=""; |
---|
100 | m += zstrtohex("stopos") + httpsep; |
---|
101 | m += zstrtohex(this->message.get_protocol() ) + httpsep; |
---|
102 | m += zstrtohex(this->message.get_whoami()+"."+this->config) + httpsep; |
---|
103 | m += zstrtohex(this->message.get_pool() ) + httpsep; |
---|
104 | m += zstrtohex(this->message.get_command() ) + httpsep; |
---|
105 | m += zstrtohex(this->message.get_multi() ) + httpsep; |
---|
106 | m += zstrtohex(this->message.get_value() ); |
---|
107 | } |
---|
108 | |
---|
109 | int wrequest::send_message(std::string &body, std::string &header) |
---|
110 | { |
---|
111 | |
---|
112 | body.clear(); |
---|
113 | header.clear(); |
---|
114 | std::string m; |
---|
115 | this->compose_message(m); |
---|
116 | std::string x; |
---|
117 | x = this->serverurl+m; |
---|
118 | std::cerr << "WTD:" << __LINE__ <<":"<<x<<std::endl; |
---|
119 | curl_easy_setopt(this->curl,CURLOPT_URL,x.c_str()); |
---|
120 | curl_easy_setopt(this->curl,CURLOPT_WRITEDATA, &body); |
---|
121 | curl_easy_setopt(this->curl,CURLOPT_WRITEHEADER, &header); |
---|
122 | int rc; |
---|
123 | rc = curl_easy_perform(this->curl); |
---|
124 | |
---|
125 | return rc; |
---|
126 | } |
---|
127 | |
---|