CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
wsresponse.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 WSRESPONSE_H
23#define WSRESPONSE_H
24
25#include "local_config.h"
26#include "wsstatic.h"
27#include "wsheader.h"
28#include "wsrequest.h"
29
30#include <cstddef> // for size_t
31#include <string>
32#include <map>
33
34namespace NSROOT
35{
36
37 class NetSocket;
38 class TcpSocket;
39 class Decompressor;
40
41 class WSResponse
42 {
43 public:
44 typedef std::map<std::string, WSHeader> VARS;
45
46 WSResponse(const WSRequest& request)
47 { init(request, 1, true, false); }
48 WSResponse(const WSRequest &request, int maxRedirs, bool trustedLocation, bool followAny)
49 { init(request, maxRedirs, trustedLocation, followAny); }
50 ~WSResponse();
51
52 bool IsSuccessful() const { return p->IsSuccessful(); }
53 bool IsChunkedTransfer() const { return p->IsChunkedTransfer(); }
54 size_t GetContentLength() const { return p->GetContentLength(); }
55 int ReadContent(char *buf, int buflen) { return p->ReadContent(buf, buflen); }
56 size_t GetConsumed() const { return p->GetConsumed(); }
57 int GetStatusCode() const { return p->GetStatusCode(); }
58 const std::string& Redirection() const { return p->Redirection(); }
59
60 bool GetHeaderValue(const std::string& header, std::string& value)
61 {
62 return p->GetHeaderValue(header, value);
63 }
64 const std::string& GetHeaderValue(const std::string& header) const
65 {
66 return p->GetHeaderValue(header);
67 }
68 const VARS& GetRequestHeaders() const
69 {
70 return p->GetRequestHeaders();
71 }
72
73 // helpers
74 static bool ReadHeaderLine(NetSocket *socket, const char *eol, std::string& line, size_t *len);
75
76 private:
77 void init(const WSRequest &request, int maxRedirs, bool trustedLocation, bool followAny);
78
79 // prevent copy
80 WSResponse(const WSResponse&);
81 WSResponse& operator=(const WSResponse&);
82
83 class _response : private WSRequestStreamSink
84 {
85 public:
86 _response(const WSRequest& request);
87 virtual ~_response();
88
89 bool IsSuccessful() const { return m_successful; }
90 bool IsChunkedTransfer() const { return m_contentChunked; }
91 size_t GetContentLength() const { return m_contentLength; }
92 int ReadContent(char *buf, size_t buflen);
93 size_t GetConsumed() const { return m_consumed; }
94 int GetStatusCode() const { return m_statusCode; }
95 const std::string& Redirection() const { return m_location; }
96
97 bool GetHeaderValue(const std::string& header, std::string& value);
98 const std::string& GetHeaderValue(const std::string& header) const;
99 const VARS& GetRequestHeaders() const { return m_headers; }
100
101 private:
102 TcpSocket *m_socket;
103 bool m_successful;
104 int m_statusCode;
105 std::string m_serverInfo;
106 std::string m_etag;
107 std::string m_location;
108 WS_CENCODING m_contentEncoding;
109 bool m_hasContent;
110 bool m_contentEmpty;
111 bool m_contentChunked;
112 bool m_chunkNext;
113 size_t m_contentLength;
114 size_t m_consumed;
119 Decompressor *m_decoder;
120
121 VARS m_headers;
122
123 // prevent copy
124 _response(const _response&);
125 _response& operator=(const _response&);
126
127 bool WriteRequestStream(const char * data, unsigned len) override;
128 bool FlushRequestStream() override;
129
130 bool ReadResponse();
131 int ReadChunk(void *buf, size_t buflen);
132 static int SocketStreamReader(void *hdl, void *buf, int sz);
133 static int ChunkStreamReader(void *hdl, void *buf, int sz);
134 };
135
136 _response * p;
137 };
138
139}
140
141#endif /* WSRESPONSE_H */
char * m_chunkEOR
The end of received data in the chunk.
Definition wsresponse.h:117
char * m_chunkPtr
The next position to read data from the chunk.
Definition wsresponse.h:116
char * m_chunkEnd
The end of the chunk buffer.
Definition wsresponse.h:118
char * m_chunkBuffer
The chunk data buffer.
Definition wsresponse.h:115