Bridges-C++  3.2.0
Bridges(C++API)
ServerComm.h
Go to the documentation of this file.
1 #ifndef SERVER_COMM_H
2 #define SERVER_COMM_H
3 
4 #include <string>
5 #include <vector>
6 using namespace std;
7 #include <curl/curl.h> //curl
9 #include "./data_src/Game.h"
10 #include "./data_src/Shakespeare.h"
14 #include "./data_src/Song.h"
15 
16 namespace bridges {
22  class ServerComm {
23  //Used to access to this class private functions
24  friend class Bridges;
25  friend class DataSource;
26 
27  ServerComm() = delete; //Prevents instantiation
28 
32  static size_t curlWriteFunction(void *contents, size_t size,
33  size_t nmemb, void *results) {
34  size_t handled = size * nmemb;
35  if (results) {
36  ((string*)results)->append((char*)contents, handled);
37  }
38  return handled;
39  }
48  static string makeRequest(const string& url, const vector<string>&
49  headers, const string& data = "") {
50  string results;
51  // first load curl enviornment (only need be called
52  // once in entire session tho)
53  curl_global_init(CURL_GLOBAL_ALL);
54  CURL* curl = curl_easy_init(); // get a curl handle
55  if (curl) {
56  char error_buffer[CURL_ERROR_SIZE];
57  CURLcode res;
58  //setting verbose
59  if (0) {
60  res = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
61  if (res != CURLE_OK)
62  throw "curl_easy_setopt failed";
63  }
64  // setting error buffer
65  res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
66  if (res != CURLE_OK)
67  throw "curl_easy_setopt failed";
68 
69  // set the URL to GET from
70  res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
71  if (res != CURLE_OK)
72  throw "curl_easy_setopt failed";
73  //pass pointer to callback function
74  res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &results);
75  if (res != CURLE_OK)
76  throw "curl_easy_setopt failed";
77  //sends all data to this function
78  res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunction);
79  if (res != CURLE_OK)
80  throw "curl_easy_setopt failed";
81  // need this to catch http errors
82  res = curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
83  if (res != CURLE_OK)
84  throw "curl_easy_setopt failed";
85  if (data.length() > 0) {
86  // Now specify the POST data
87  res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
88  if (res != CURLE_OK)
89  throw "curl_easy_setopt failed";
90  // Now specify the POST data size
91  res = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.length());
92  if (res != CURLE_OK)
93  throw "curl_easy_setopt failed";
94  // a post request
95  res = curl_easy_setopt(curl, CURLOPT_POST, 1L);
96  if (res != CURLE_OK)
97  throw "curl_easy_setopt failed";
98  }
99 
100  struct curl_slist* curlHeaders = nullptr;
101  for (const string& header : headers) {
102  curlHeaders = curl_slist_append(curlHeaders, header.c_str());
103  }
104  res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curlHeaders);
105  if (res != CURLE_OK)
106  throw "curl_easy_setopt failed";
107 
108  // Perform the request, res will get the return code
109  res = curl_easy_perform(curl);
110 
111  if (res != CURLE_OK) {
112  throw "curl_easy_perform() failed.\nCurl Error Code "
113  + to_string(res) + "\n" + curl_easy_strerror(res) +
114  "\n"
115  + error_buffer + "\nPossibly Bad BRIDGES Credentials\n";
116  }
117  curl_slist_free_all(curlHeaders);
118  curl_easy_cleanup(curl);
119  }
120  else {
121  throw "curl_easy_init() failed!\nNothing retrieved from server.\n";
122  }
123 
124  curl_global_cleanup();
125  return results;
126  }
127 
128  static std::string encodeURLPart (const std::string& s) {
129  std::string returnstr;
130 
131  curl_global_init(CURL_GLOBAL_ALL);
132  CURL* curl = curl_easy_init(); // get a curl handle
133 
134  char* encodedstr = curl_easy_escape (curl, s.c_str(), 0);
135  returnstr = encodedstr;
136 
137  curl_free(encodedstr);
138 
139  curl_easy_cleanup(curl);
140  curl_global_cleanup();
141 
142  return returnstr;
143  }
144  }; //server comm
145 
146 
147 }// namespace bridges
148 #endif
This is a class for handling calls to the BRIDGES server to transmit JSON to the server and subsequen...
Definition: ServerComm.h:22
STL namespace.
This class contains methods to connect and transmit a user&#39;s data structure representation to the Bri...
Definition: Bridges.h:39
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
This class provides an API to various data sources used in BRIDGES.
Definition: DataSource.h:59