99namespace influxdb ::transports
1010{
1111
12- HTTP::HTTP (const std::string& url)
12+ HTTP::HTTP (const std::string & url)
1313{
1414 initCurl (url);
1515 initCurlRead (url);
1616 obtainInfluxServiceUrl (url);
1717 obtainDatabaseName (url);
1818}
1919
20- void HTTP::initCurl (const std::string& url)
20+ void HTTP::initCurl (const std::string & url)
2121{
2222 CURLcode globalInitResult = curl_global_init (CURL_GLOBAL_ALL);
23- if (globalInitResult != CURLE_OK) {
24- throw InfluxDBException (" HTTP::initCurl" , curl_easy_strerror (globalInitResult));
23+ if (globalInitResult != CURLE_OK)
24+ {
25+ throw InfluxDBException (__PRETTY_FUNCTION__, curl_easy_strerror (globalInitResult));
2526 }
2627
2728 std::string writeUrl = url;
2829 auto position = writeUrl.find (" ?" );
29- if (position == std::string::npos) {
30- throw InfluxDBException (" HTTP::initCurl" , " Database not specified" );
30+ if (position == std::string::npos)
31+ {
32+ throw InfluxDBException (__PRETTY_FUNCTION__, " Database not specified" );
3133 }
3234 if (writeUrl.at (position - 1 ) != ' /' )
3335 {
@@ -38,7 +40,7 @@ void HTTP::initCurl(const std::string& url)
3840 writeUrl.insert (position, " write" );
3941 }
4042 writeHandle = curl_easy_init ();
41- curl_easy_setopt (writeHandle, CURLOPT_URL, writeUrl.c_str ());
43+ curl_easy_setopt (writeHandle, CURLOPT_URL, writeUrl.c_str ());
4244 curl_easy_setopt (writeHandle, CURLOPT_SSL_VERIFYPEER, 0 );
4345 curl_easy_setopt (writeHandle, CURLOPT_CONNECTTIMEOUT, 10 );
4446 curl_easy_setopt (writeHandle, CURLOPT_TIMEOUT, 10 );
@@ -51,44 +53,39 @@ void HTTP::initCurl(const std::string& url)
5153
5254static size_t WriteCallback (void *contents, size_t size, size_t nmemb, void *userp)
5355{
54- ((std::string*) userp)->append ((char *) contents, size * nmemb);
55- return size * nmemb;
56+ ((std::string *) userp)->append ((char *) contents, size * nmemb);
57+ return size * nmemb;
5658}
5759
58- void HTTP::initCurlRead (const std::string& url)
60+ void HTTP::initCurlRead (const std::string & url)
5961{
6062 mReadUrl = url + " &q=" ;
6163 mReadUrl .insert (mReadUrl .find (" ?" ), " /query" );
6264 readHandle = curl_easy_init ();
63- curl_easy_setopt (readHandle, CURLOPT_SSL_VERIFYPEER, 0 );
65+ curl_easy_setopt (readHandle, CURLOPT_SSL_VERIFYPEER, 0 );
6466 curl_easy_setopt (readHandle, CURLOPT_CONNECTTIMEOUT, 10 );
6567 curl_easy_setopt (readHandle, CURLOPT_TIMEOUT, 10 );
6668 curl_easy_setopt (readHandle, CURLOPT_TCP_KEEPIDLE, 120L );
6769 curl_easy_setopt (readHandle, CURLOPT_TCP_KEEPINTVL, 60L );
6870 curl_easy_setopt (readHandle, CURLOPT_WRITEFUNCTION, WriteCallback);
6971}
7072
71- std::string HTTP::query (const std::string& query)
73+ std::string HTTP::query (const std::string & query)
7274{
7375 CURLcode response;
7476 long responseCode;
7577 std::string buffer;
76- char * encodedQuery = curl_easy_escape (readHandle, query.c_str (), query.size ());
78+ char * encodedQuery = curl_easy_escape (readHandle, query.c_str (), query.size ());
7779 auto fullUrl = mReadUrl + std::string (encodedQuery);
7880 curl_easy_setopt (readHandle, CURLOPT_URL, fullUrl.c_str ());
7981 curl_easy_setopt (readHandle, CURLOPT_WRITEDATA, &buffer);
8082 response = curl_easy_perform (readHandle);
8183 curl_easy_getinfo (readHandle, CURLINFO_RESPONSE_CODE, &responseCode);
82- if (response != CURLE_OK) {
83- throw InfluxDBException (" HTTP::query" , curl_easy_strerror (response));
84- }
85- if (responseCode != 200 ) {
86- throw InfluxDBException (" HTTP::query" , " Status code: " + std::to_string (responseCode));
87- }
84+ treatCurlResponse (response, responseCode);
8885 return buffer;
8986}
9087
91- void HTTP::enableBasicAuth (const std::string& auth)
88+ void HTTP::enableBasicAuth (const std::string & auth)
9289{
9390 curl_easy_setopt (writeHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
9491 curl_easy_setopt (writeHandle, CURLOPT_USERPWD, auth.c_str ());
@@ -109,19 +106,38 @@ HTTP::~HTTP()
109106 curl_global_cleanup ();
110107}
111108
112- void HTTP::send (std::string&& post )
109+ void HTTP::send (std::string &&lineprotocol )
113110{
114111 CURLcode response;
115112 long responseCode;
116- curl_easy_setopt (writeHandle, CURLOPT_POSTFIELDS, post .c_str ());
117- curl_easy_setopt (writeHandle, CURLOPT_POSTFIELDSIZE, (long ) post .length ());
113+ curl_easy_setopt (writeHandle, CURLOPT_POSTFIELDS, lineprotocol .c_str ());
114+ curl_easy_setopt (writeHandle, CURLOPT_POSTFIELDSIZE, (long ) lineprotocol .length ());
118115 response = curl_easy_perform (writeHandle);
119116 curl_easy_getinfo (writeHandle, CURLINFO_RESPONSE_CODE, &responseCode);
120- if (response != CURLE_OK) {
121- throw InfluxDBException (" HTTP::send" , curl_easy_strerror (response));
117+ treatCurlResponse (response, responseCode);
118+ }
119+
120+ void HTTP::treatCurlResponse (const CURLcode &response, long responseCode) const
121+ {
122+ if (response != CURLE_OK)
123+ {
124+ throw ConnectionError (__PRETTY_FUNCTION__, curl_easy_strerror (response));
122125 }
123- if (responseCode < 200 || responseCode > 206 ) {
124- throw InfluxDBException (" HTTP::send" , " Response code: " + std::to_string (responseCode));
126+ //
127+ // Influx API response codes:
128+ // https://docs.influxdata.com/influxdb/v1.7/tools/api/#status-codes-and-responses-2
129+ //
130+ if (responseCode == 404 )
131+ {
132+ throw NonExistentDatabase (__PRETTY_FUNCTION__, " Nonexistent database: " + std::to_string (responseCode));
133+ }
134+ else if ((responseCode >= 400 ) && (responseCode < 500 ))
135+ {
136+ throw BadRequest (__PRETTY_FUNCTION__, " Bad request: " + std::to_string (responseCode));
137+ }
138+ else if (responseCode > 500 )
139+ {
140+ throw ServerError (__PRETTY_FUNCTION__, " Influx server error:" + std::to_string (responseCode));
125141 }
126142}
127143
@@ -152,8 +168,8 @@ void HTTP::createDatabase()
152168 std::string createUrl = mInfluxDbServiceUrl + " /query" ;
153169 std::string postFields = " q=CREATE DATABASE " + mDatabaseName ;
154170
155- CURL* createHandle = curl_easy_init ();
156- curl_easy_setopt (createHandle, CURLOPT_URL, createUrl.c_str ());
171+ CURL * createHandle = curl_easy_init ();
172+ curl_easy_setopt (createHandle, CURLOPT_URL, createUrl.c_str ());
157173 curl_easy_setopt (createHandle, CURLOPT_SSL_VERIFYPEER, 0 );
158174 curl_easy_setopt (createHandle, CURLOPT_CONNECTTIMEOUT, 10 );
159175 curl_easy_setopt (createHandle, CURLOPT_TIMEOUT, 10 );
@@ -169,14 +185,7 @@ void HTTP::createDatabase()
169185 CURLcode response = curl_easy_perform (createHandle);
170186 long responseCode;
171187 curl_easy_getinfo (createHandle, CURLINFO_RESPONSE_CODE, &responseCode);
172- if (response != CURLE_OK)
173- {
174- throw InfluxDBException (" HTTP::createDatabase" , curl_easy_strerror (response));
175- }
176- if (responseCode < 200 || responseCode > 206 )
177- {
178- throw InfluxDBException (" HTTP::createDatabase" , " Response code: " + std::to_string (responseCode));
179- }
188+ treatCurlResponse (response,responseCode);
180189}
181190
182191} // namespace influxdb
0 commit comments