pianod2
multisource multiuser scriptable networked music player
httpclient.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <string>
12 #include <sstream>
13 #include <exception>
14 #include <unordered_map>
15 #include <ostream>
16 #include <iostream>
17 
18 #include <curl/curl.h>
19 
20 class HttpClient {
21  CURL *session;
22  bool debug {false};
23 
24 public:
25  class Exception : public std::exception {
26  private:
27  std::string explanation;
28  public:
29  Exception (const char *reason = "HTTP error");
30  Exception (const char *function, CURLcode code);
31  Exception (const char *function, CURLcode code, const char *detail);
32  Exception (CURLcode code, const char *detail);
33  virtual const char *what() const noexcept override final {
34  return explanation.c_str();
35  }
36  };
37 
38  enum class RequestType {
39  Head,
40  Get,
41  Post
42  };
43 
44  using StringDict = std::unordered_map<std::string, std::string>;
45  struct Request {
46  mutable bool debug {false}; // If set, sends status information to stdlog.
47  long timeout {0}; // in seconds
49  std::string URL; // URL. May include query parameters, XOR they may be in `parameters`.
50  std::string proxy; // If set, a proxy server through which to request
51  StringDict parameters; // Query parameters to add to the URL
52  StringDict headers; // Headers to include with HTTP request
53  StringDict cookies; // Cookies to include in HTTP header
54  std::string user_agent; // If empty, a default (the package name) is used instead.
55  std::string body; // Body to include with POST requests.
56  void dump (std::ostream & = std::cerr) const;
57  };
58 
59  struct Response {
60  bool debug {false}; // Set to match the request debug flag.
61  bool awaiting_http_status_line {true}; // Private state variable
62  CURLcode curl_code; // Final libcurl status code before returning.
63  int http_status{0}; // The HTTP status included in the response.
64  std::string http_status_text; // The text portion of the HTTP status
65  bool header_corruption{false}; // If true, the header wasn't parsed correctly.
66  StringDict headers; // Headers included with the response.
67  StringDict cookies; // Cookies included with the response.
68  std::string body; // The body of the response.
69  void dump (std::ostream & = std::cerr) const;
70  };
71 
72  HttpClient (bool dbx = false);
73  ~HttpClient ();
74  HttpClient (const HttpClient &) = delete;
75  HttpClient (HttpClient &&) = delete;
76  HttpClient &operator= (const HttpClient &) = delete;
78  const Response performHttpRequest (const Request &request);
79 };
Definition: httpclient.h:25
std::string explanation
Definition: httpclient.h:27
virtual const char * what() const noexcept override final
Definition: httpclient.h:33
Exception(const char *reason="HTTP error")
Definition: httpclient.cpp:23
Definition: httpclient.h:20
CURL * session
Definition: httpclient.h:21
HttpClient & operator=(const HttpClient &)=delete
HttpClient(bool dbx=false)
Definition: httpclient.cpp:347
const Response performHttpRequest(const Request &request)
Definition: httpclient.cpp:285
~HttpClient()
Definition: httpclient.cpp:355
RequestType
Definition: httpclient.h:38
HttpClient(HttpClient &&)=delete
bool debug
Definition: httpclient.h:22
std::unordered_map< std::string, std::string > StringDict
Definition: httpclient.h:44
HttpClient(const HttpClient &)=delete
Definition: httpclient.h:45
StringDict parameters
Definition: httpclient.h:51
std::string user_agent
Definition: httpclient.h:54
bool debug
Definition: httpclient.h:46
std::string body
Definition: httpclient.h:55
RequestType type
Definition: httpclient.h:48
void dump(std::ostream &=std::cerr) const
Log request details to a file/stream.
Definition: httpclient.cpp:66
std::string URL
Definition: httpclient.h:49
StringDict headers
Definition: httpclient.h:52
StringDict cookies
Definition: httpclient.h:53
std::string proxy
Definition: httpclient.h:50
long timeout
Definition: httpclient.h:47
Definition: httpclient.h:59
std::string http_status_text
Definition: httpclient.h:64
StringDict headers
Definition: httpclient.h:66
bool header_corruption
Definition: httpclient.h:65
int http_status
Definition: httpclient.h:63
void dump(std::ostream &=std::cerr) const
Log response details to a file/stream.
Definition: httpclient.cpp:76
std::string body
Definition: httpclient.h:68
bool awaiting_http_status_line
Definition: httpclient.h:61
bool debug
Definition: httpclient.h:60
CURLcode curl_code
Definition: httpclient.h:62
StringDict cookies
Definition: httpclient.h:67