CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
wsrequest.h
1/*
2 * Copyright (C) 2014-2026 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 "local_config.h"
26#include "wsstatic.h"
27#include "uriparser.h"
28
29#include <string>
30#include <map>
31
32#define REQUEST_PROTOCOL "HTTP/1.1"
33#define REQUEST_USER_AGENT LIBTAG "/" LIBVERSION
34#define REQUEST_CONNECTION "close"
35#define REQUEST_STD_CHARSET "utf-8"
36
37namespace NSROOT
38{
39
41 {
42 public:
43 virtual bool WriteRequestStream(const char * data, unsigned len) = 0;
44 virtual bool FlushRequestStream() = 0;
45 };
46
47 class WSRequest
48 {
49 public:
50 WSRequest(const std::string& server, unsigned port);
51 WSRequest(const std::string& server, unsigned port, bool secureURI);
52 WSRequest(const URIParser& uri, WS_METHOD method = WS_METHOD_Get);
53 ~WSRequest();
54
55 // Clone for redirection: see RFC-9110 section 10.2.2 Location
56 WSRequest(const WSRequest& o, const URIParser& redirection);
57
58 void RequestService(const std::string& url, WS_METHOD method = WS_METHOD_Get);
59 void RequestAccept(const std::string& contentType);
60 void RequestAcceptEncoding(bool yesno);
61 void SetUserAgent(const std::string& value);
62
63 void SetHeader(const std::string& field, const std::string& value);
64 void ClearHeader(const std::string& field);
65
66 void SetContentParam(const std::string& param, const std::string& value);
67 void SetContentCustom(const std::string& contentType, const char *content);
68 const std::string& GetContent() const { return m_contentData; }
69 void ClearContent();
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 WS_METHOD GetMethod() const { return m_service_method; }
75 const std::string& GetService() const { return m_service_url; }
76
77 bool WriteMessage(WSRequestStreamSink& sink) const;
78
79 private:
80 std::string m_server;
81 unsigned m_port;
82 bool m_secure_uri;
83 std::string m_service_url;
84 WS_METHOD m_service_method;
85 std::string m_charset;
86 std::string m_accept;
87 WS_CTYPE m_contentType;
88 std::string m_contentTypeStr;
89 std::string m_contentData;
90 typedef std::pair<std::string, std::string> header_t;
91 std::map<std::string, header_t> m_headers;
92 std::string m_userAgent;
93
94 bool WriteCommonHeading(WSRequestStreamSink& sink) const;
95 bool WriteMessageGET(WSRequestStreamSink& sink, const char* method) const;
96 bool WriteMessagePOST(WSRequestStreamSink& sink, const char* method) const;
97
98 };
99
100}
101
102#endif /* WSREQUEST_H */