92 void setTimeout(
int timeout_sec);
93 const std::vector<char> Get(
HttpRequest &nreq,
size_t maxTries=0);
94 const std::vector<char> Post(
HttpRequest &nreq,
const std::vector<char> &post,
size_t maxTries=0);
95 const std::vector<char> Put(
HttpRequest &nreq,
const std::vector<char> &put,
size_t maxTries=0);
96 const std::vector<char> Delete(
HttpRequest &nreq,
size_t maxTries=0);
104 size_t readBodyChunk(
char *buf,
size_t bufsize);
108 size_t readBodyChunkNonBlocking(
char *buf,
size_t bufsize);
111 bool isStreaming()
const;
116 bool waitReadable(
int timeout_ms);
120 static netplus::TlsSessionCache& tlsSessionCache();
122 void resetConnection();
123 void _ensureConnected();
124 bool tryHttp3First();
125 int readchunk(
const char *data,
int datasize,
int &pos);
128 size_t _recvBlocking(netplus::buffer &b,
int timeout_sec = 0);
130 size_t _recvNonBlocking(netplus::buffer &b);
131 void _sendAll(
const char *data,
size_t len);
132 void _sendAll(
const std::string &data);
135 std::vector<char> _h1ReadResponse(
const std::string &label);
139 bool _h2PrefaceSent =
false;
142 uint32_t _h2NextStreamId = 1;
143 std::unique_ptr<hpack::Decoder> _h2Decoder;
144 const std::vector<char> _h2Request(
const std::string &method,
146 const std::vector<char> *postBody =
nullptr);
147 const std::vector<char> _h3Request(
const std::string &method,
149 const std::vector<char> *postBody =
nullptr);
152 enum StreamMode { STREAM_NONE, STREAM_CONTENT_LENGTH, STREAM_CHUNKED, STREAM_EOF,
153 STREAM_H2, STREAM_H3 };
154 StreamMode _streamMode = STREAM_NONE;
155 size_t _streamRemaining = 0;
156 std::vector<char> _streamBuf;
157 size_t _streamBufPos = 0;
159 bool _streamChunkDone =
false;
160 size_t _streamChunkRemaining = 0;
163 uint32_t _streamH2Sid = 0;
164 bool _streamH2EndStream =
false;
165 std::vector<uint8_t> _streamH2Raw;
168 uint64_t _streamH3Sid = 0;
169 bool _streamH3EndStream =
false;
170 std::vector<uint8_t> _streamH3Raw;
171 std::vector<char> _streamH3Body;
172 size_t _streamH3BodyPos = 0;
173 bool _streamH3InDataFrame =
false;
174 uint64_t _streamH3DataRemaining = 0;
178 std::unique_ptr<netplus::socket> _cltsock;
179 netplus::socketwait _sw;
180 netplus::x509cert _cert;
181 int _recvTimeoutSec = 60;
182 int _sendTimeoutSec = 30;
270 void setState(
const std ::string &httpstate);
271 void setContentType(
const std ::string &type);
272 void setContentLength(
size_t len);
273 void setConnection(
const std ::string &type);
274 void setTransferEncoding(
const std ::string &enc);
277 const std ::string &getState()
const;
278 int getStatusCode()
const;
279 const std ::string &getContentType()
const;
280 size_t getContentLength()
const;
281 const std ::string &getConnection()
const;
282 const std ::string &getVersion()
const;
285 size_t printHeader(std::vector<char> &buffer);
288 void send(netplus::con &curconnection,
const std::string &data,
int datalen=0);
289 void send(netplus::con &curconnection,
const unsigned char *data,
int datalen);
290 void send(netplus::con &curconnection,
const std::vector<char> &data,
int datalen=0);
293 size_t parse(
const char *in,
size_t inlen);
296 bool _storeResponseInfo(netplus::con &curconnection,
int datalen);
298 std::string _State=HTTP200;
299 std::string _Version;
305 mutable std::string _ContentTypeCache;
322 size_t parseH2(
const std::vector<hpack::HeaderField> &headers, uint32_t stream_id = 0);
323 size_t parseH3(
const std::vector<qpack::HeaderField> &headers);
325 void printHeader(std::string &buffer);
326 int getRequestType();
327 const std::string &getRequestURL();
328 const std::string &getRequest();
329 size_t getRequestLength();
330 const std::string &getRequestVersion();
331 size_t getContentLength();
332 size_t getMaxUploadSize();
338 void setRequestType(
int req);
339 void setRequestURL(
const std::string &url);
340 void setRequestVersion(
const std::string &version);
342 void setRequestData(
const std::string &data,
size_t len);
343 void setMaxUploadSize(
size_t upsize);
345 void send(
const HttpUrl &dest,std::unique_ptr<netplus::socket> &sock);
354 static std::string extractPath(
const std::string &target);
356 int _RequestType = PARSEREQUEST;
357 size_t _MaxUploadSize = DEFAULT_UPLOADSIZE;
361 mutable std::string _cachedRequestURL;
362 mutable std::string _cachedRequest;
363 mutable std::string _cachedRequestVersion;
369 int _httpProtocol = 0;
371 struct H2PendingResponse {
379 struct H2StreamingResponse {
380 uint32_t streamId = 0;
381 size_t contentLength = 0;
382 size_t totalSent = 0;
383 std::string pendingData;
384 size_t pendingOffset = 0;
385 std::unique_ptr<HttpRequest> tempreq;
388 size_t emptyCount = 0;
389 unsigned int backoffMs = 1;
390 bool finished =
false;
393 struct H2PendingIncoming {
394 std::vector<hpack::HeaderField> headers;
396 std::vector<uint8_t> rawHpack;
397 bool headersComplete =
false;
398 bool endStreamOnHeaders =
false;
402 uint32_t streamId = 0;
403 bool headersSent =
false;
404 bool serverPrefaceSent =
false;
405 size_t expectedContentLength = 0;
406 size_t bodyBytesSent = 0;
407 std::deque<H2PendingResponse> pendingResponses;
408 std::map<uint32_t, H2PendingIncoming> pendingIncoming;
411 int32_t peerConnWindow = 65535;
412 int32_t peerInitialStreamWindow = 65535;
413 size_t peerMaxFrameSize = 16384;
414 std::map<uint32_t, int32_t> peerStreamWindows;
416 std::map<uint32_t, std::shared_ptr<H2StreamingResponse>> activeStreams;
420 std::unique_ptr<H2State> _h2;
424 if (!_h2) _h2 = std::make_unique<H2State>();
447 std::vector<Header> headers;
448 std::vector<Disposition> dispositions;
449 std::vector<char> value;
464 const std::string &getContentType()
const {
return _contentType; }
465 const std::string &getBoundary()
const {
return _boundary; }
466 const std::vector<MultipartEntry> &multipartData()
const {
return _multipartEntries; }
467 const std::vector<UrlEntry> &urlData()
const {
return _urlEntries; }
470 static void urlDecode(
const std::string &in, std::string &out);
471 static void urlEncode(
const std::string &in, std::string &out);
474 void _parseMultipart(
const char *data,
size_t len);
475 void _parseMultiSection(
const char *data,
size_t len,
size_t start,
size_t end);
476 void _parseUrlDecode(
const char *data,
size_t len);
478 std::string _boundary;
479 std::string _contentType;
480 std::vector<MultipartEntry> _multipartEntries;
481 std::vector<UrlEntry> _urlEntries;