[9] | 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 | rc = mkdir(this->configdirname.c_str(),0700); |
---|
| 60 | if (rc < 0) |
---|
| 61 | if (errno != EEXIST) |
---|
| 62 | return 1; |
---|
| 63 | f.open(this->configfilename.c_str(),std::ios::out); |
---|
| 64 | if (f.fail()) |
---|
| 65 | return 1; |
---|
| 66 | this->config = gen_random(this->config_size)+'\n'; |
---|
| 67 | f << this->config; |
---|
| 68 | f.close(); |
---|
| 69 | rc = chmod(this->configfilename.c_str(),0600); |
---|
| 70 | if (rc < 0) |
---|
| 71 | return 1; |
---|
| 72 | return 0; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | size_t write_data(char *p, size_t size, size_t nmemb, std::string *userdata) |
---|
| 77 | { |
---|
| 78 | size_t l = size*nmemb; |
---|
| 79 | for (size_t i=0; i<l; i++) |
---|
| 80 | (*userdata).push_back(p[i]); |
---|
| 81 | return l; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | int wrequest::set_serverurl(const std::string &u) |
---|
| 85 | { |
---|
| 86 | this->serverurl = u; |
---|
| 87 | if (this->serverurl[this->serverurl.size()-1] != '/') |
---|
| 88 | this->serverurl += '/'; |
---|
| 89 | curl_easy_setopt(this->curl,CURLOPT_HEADER,0L); |
---|
| 90 | curl_easy_setopt(this->curl,CURLOPT_NOPROGRESS, 1L); |
---|
| 91 | curl_easy_setopt(this->curl,CURLOPT_WRITEFUNCTION, write_data); |
---|
| 92 | |
---|
| 93 | return 0; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | void wrequest::compose_message(std::string &m) |
---|
| 97 | { |
---|
| 98 | m=""; |
---|
| 99 | m += zstrtohex("stopos") + httpsep; |
---|
| 100 | m += zstrtohex(this->message.get_protocol() ) + httpsep; |
---|
| 101 | m += zstrtohex(this->message.get_whoami()+"."+this->config) + httpsep; |
---|
| 102 | m += zstrtohex(this->message.get_pool() ) + httpsep; |
---|
| 103 | m += zstrtohex(this->message.get_command() ) + httpsep; |
---|
| 104 | m += zstrtohex(this->message.get_multi() ) + httpsep; |
---|
| 105 | m += zstrtohex(this->message.get_value() ); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | int wrequest::send_message(std::string &body, std::string &header) |
---|
| 109 | { |
---|
| 110 | |
---|
| 111 | body.clear(); |
---|
| 112 | header.clear(); |
---|
| 113 | std::string m; |
---|
| 114 | this->compose_message(m); |
---|
| 115 | std::string x; |
---|
| 116 | x = this->serverurl+m; |
---|
| 117 | curl_easy_setopt(this->curl,CURLOPT_URL,x.c_str()); |
---|
| 118 | curl_easy_setopt(this->curl,CURLOPT_WRITEDATA, &body); |
---|
| 119 | curl_easy_setopt(this->curl,CURLOPT_WRITEHEADER, &header); |
---|
| 120 | int rc; |
---|
| 121 | rc = curl_easy_perform(this->curl); |
---|
| 122 | |
---|
| 123 | return rc; |
---|
| 124 | } |
---|
| 125 | |
---|