[7] | 1 | |
---|
| 2 | /* |
---|
| 3 | Copyright 2013 Willem Vermin, SURFsara |
---|
| 4 | |
---|
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 6 | you may not use this file except in compliance with the License. |
---|
| 7 | You may obtain a copy of the License at |
---|
| 8 | |
---|
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | |
---|
| 11 | Unless required by applicable law or agreed to in writing, software |
---|
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | See the License for the specific language governing permissions and |
---|
| 15 | limitations under the License. |
---|
| 16 | */ |
---|
| 17 | #include <iostream> |
---|
| 18 | #include <string> |
---|
| 19 | #include "stopos.h" |
---|
| 20 | #include "wrequest.h" |
---|
| 21 | #include <stdlib.h> |
---|
| 22 | #include <curl/curl.h> |
---|
| 23 | #include <fstream> |
---|
| 24 | #include <sys/stat.h> |
---|
| 25 | #include <sys/types.h> |
---|
| 26 | #include <errno.h> |
---|
| 27 | |
---|
| 28 | //constructor |
---|
| 29 | wrequest::wrequest(void) |
---|
| 30 | { |
---|
| 31 | this->set_configfilename(); |
---|
| 32 | curl_global_init(CURL_GLOBAL_ALL); |
---|
| 33 | this->curl = curl_easy_init(); |
---|
| 34 | this->debugging = 0; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | //destructor |
---|
| 38 | wrequest::~wrequest() |
---|
| 39 | { |
---|
| 40 | curl_easy_cleanup(curl); |
---|
| 41 | curl_global_cleanup(); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void wrequest::set_configfilename(void) |
---|
| 45 | { |
---|
| 46 | char *h = getenv("HOME"); |
---|
| 47 | std::string home; |
---|
| 48 | if (h == 0) |
---|
| 49 | home = "/"; |
---|
| 50 | else |
---|
| 51 | home = h; |
---|
| 52 | this->configdirname = home + "/.stopos"; |
---|
| 53 | this->configfilename = this->configdirname + "/id"; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | int wrequest::read_config(void) |
---|
| 57 | { |
---|
| 58 | int rc = get_file_contents(this->config,this->configfilename); |
---|
| 59 | if (rc == 0) |
---|
| 60 | { |
---|
| 61 | size_t p= this->config.find_first_of('\n'); |
---|
| 62 | if (p != this->config.npos) |
---|
| 63 | this->config.resize(p); |
---|
| 64 | return 0; |
---|
| 65 | } |
---|
| 66 | else |
---|
| 67 | return 1; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | int wrequest::write_config(void) |
---|
| 71 | { |
---|
| 72 | std::ofstream f; |
---|
| 73 | int rc; |
---|
| 74 | |
---|
| 75 | rc = mkdir(this->configdirname.c_str(),0700); |
---|
| 76 | if (rc < 0) |
---|
| 77 | if (errno != EEXIST) |
---|
| 78 | return 1; |
---|
| 79 | f.open(this->configfilename.c_str(),std::ios::out); |
---|
| 80 | if (f.fail()) |
---|
| 81 | return 1; |
---|
| 82 | this->config = gen_random(this->config_size)+'\n'; |
---|
| 83 | f << this->config; |
---|
| 84 | f.close(); |
---|
| 85 | rc = chmod(this->configfilename.c_str(),0600); |
---|
| 86 | if (rc < 0) |
---|
| 87 | return 1; |
---|
| 88 | return 0; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | size_t write_data(char *p, size_t size, size_t nmemb, std::string *userdata) |
---|
| 93 | { |
---|
| 94 | size_t l = size*nmemb; |
---|
| 95 | for (size_t i=0; i<l; i++) |
---|
| 96 | (*userdata).push_back(p[i]); |
---|
| 97 | return l; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | int wrequest::set_serverurl(const std::string &u) |
---|
| 101 | { |
---|
| 102 | this->serverurl = u + '?'; |
---|
| 103 | curl_easy_setopt(this->curl,CURLOPT_HEADER,0L); |
---|
| 104 | curl_easy_setopt(this->curl,CURLOPT_NOPROGRESS, 1L); |
---|
| 105 | curl_easy_setopt(this->curl,CURLOPT_WRITEFUNCTION, write_data); |
---|
| 106 | |
---|
| 107 | return 0; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void wrequest::compose_message(std::string &m) |
---|
| 111 | { |
---|
| 112 | m=""; |
---|
| 113 | m += zstrtohex("stopos") + httpsep; |
---|
| 114 | m += zstrtohex(this->message.get_protocol() ) + httpsep; |
---|
| 115 | m += zstrtohex(this->message.get_whoami()+"."+this->config) + httpsep; |
---|
| 116 | m += zstrtohex(this->message.get_pool() ) + httpsep; |
---|
| 117 | m += zstrtohex(this->message.get_command() ) + httpsep; |
---|
| 118 | m += zstrtohex(this->message.get_multi() ) + httpsep; |
---|
| 119 | m += zstrtohex(this->message.get_value() ); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | int wrequest::send_message(std::string &body, std::string &header) |
---|
| 123 | { |
---|
| 124 | |
---|
| 125 | body.clear(); |
---|
| 126 | header.clear(); |
---|
| 127 | std::string m; |
---|
| 128 | this->compose_message(m); |
---|
| 129 | std::string x; |
---|
| 130 | x = this->serverurl+m; |
---|
| 131 | curl_easy_setopt(this->curl,CURLOPT_URL,x.c_str()); |
---|
| 132 | curl_easy_setopt(this->curl,CURLOPT_WRITEDATA, &body); |
---|
| 133 | curl_easy_setopt(this->curl,CURLOPT_WRITEHEADER, &header); |
---|
| 134 | int rc; |
---|
| 135 | rc = curl_easy_perform(this->curl); |
---|
| 136 | |
---|
| 137 | return rc; |
---|
| 138 | } |
---|
| 139 | |
---|