CPPMyth
Library to interoperate with MythTV server
wsrequest.h
1 /*
2  * Copyright (C) 2014-2015 Jean-Luc Barriere
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 3, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; see the file COPYING. If not, write to
16  * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21 
22 #ifndef WSREQUEST_H
23 #define WSREQUEST_H
24 
25 #include <cppmyth_config.h>
26 #include "wscontent.h"
27 #include "uriparser.h"
28 
29 #include <cstddef> // for size_t
30 #include <string>
31 #include <map>
32 
33 #define REQUEST_PROTOCOL "HTTP/1.1"
34 #define REQUEST_USER_AGENT "libcppmyth/2.0"
35 #define REQUEST_CONNECTION "close" // "keep-alive"
36 #define REQUEST_STD_CHARSET "utf-8"
37 
38 namespace NSROOT
39 {
40 
41  typedef enum
42  {
43  HRM_GET,
44  HRM_POST,
45  HRM_HEAD,
46  HRM_SUBSCRIBE,
47  HRM_UNSUBSCRIBE,
48  HRM_NOTIFY,
49  } HRM_t;
50 
51  class WSRequest
52  {
53  public:
54  WSRequest(const std::string& server, unsigned port);
55  WSRequest(const std::string& server, unsigned port, bool secureURI);
56  WSRequest(const URIParser& uri, HRM_t method = HRM_GET);
57  ~WSRequest();
58 
59  void RequestService(const std::string& url, HRM_t method = HRM_GET);
60  void RequestAccept(CT_t contentType);
61  void RequestAcceptEncoding(bool yesno);
62  void SetUserAgent(const std::string& value);
63  void SetContentParam(const std::string& param, const std::string& value);
64  void SetContentCustom(CT_t contentType, const char *content);
65  void SetHeader(const std::string& field, const std::string& value);
66  const std::string& GetContent() const { return m_contentData; }
67  void ClearContent();
68 
69  void MakeMessage(std::string& msg) const;
70 
71  const std::string& GetServer() const { return m_server; }
72  unsigned GetPort() const { return m_port; }
73  bool IsSecureURI() const { return m_secure_uri; }
74 
75  private:
76  std::string m_server;
77  unsigned m_port;
78  bool m_secure_uri;
79  std::string m_service_url;
80  HRM_t m_service_method;
81  std::string m_charset;
82  CT_t m_accept;
83  CT_t m_contentType;
84  std::string m_contentData;
85  std::map<std::string, std::string> m_headers;
86  std::string m_userAgent;
87 
88  void MakeMessageGET(std::string& msg, const char* method = "GET") const;
89  void MakeMessagePOST(std::string& msg, const char* method = "POST") const;
90  void MakeMessageHEAD(std::string& msg, const char* method = "HEAD") const;
91  };
92 
93 }
94 
95 #endif /* WSREQUEST_H */