libhttppp ..
Loading...
Searching...
No Matches
httpd.h
1/*******************************************************************************
2 * Copyright (c) 2014, Jan Koester jan.koester@gmx.net
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *******************************************************************************/
27
28#include <netplus/socket.h>
29#include <netplus/eventapi.h>
30#include <atomic>
31#include <cstdint>
32#include <map>
33#include <memory>
34#include <mutex>
35#include <set>
36#include <string>
37#include <vector>
38
39#include "http.h"
40#include "qpack.h"
41#include "exception.h"
42
43#pragma once
44
45namespace cmdplus {
46 class CmdController;
47}
48
49namespace libhttppp {
50 class HttpEvent : public netplus::event {
51 public:
52 HttpEvent(std::vector<netplus::socket*> serversocket,int timeout = 1000);
53
54 virtual void RequestEvent(HttpRequest &curreq,const int tid,ULONG_PTR args);
55 virtual void ResponseEvent(HttpRequest &curreq,const int tid,ULONG_PTR args);
56 virtual void ConnectEvent(HttpRequest &curreq,const int tid,ULONG_PTR args);
57 virtual void DisconnectEvent(HttpRequest &curreq,const int tid,ULONG_PTR args);
58
59 virtual bool Http2RequestEvent(netplus::con &curcon,
60 const int tid,
61 ULONG_PTR args,
62 const std::string &alpn,
63 const netplus::ssl::FramingCallback &frame_cb);
64 virtual void Http3StreamEvent(netplus::socket *sock,
65 uint64_t stream_id,
66 const std::vector<uint8_t> &data,
67 bool fin);
68
69 // Streaming body callbacks for H2/H3.
70 // Called when headers are complete for a body-bearing stream.
71 // Return true to handle body data via onH2DataChunk/onH3DataChunk
72 // instead of buffering the full body.
73 virtual bool onH2StreamHeaders(HttpRequest &conn, uint32_t streamId,
74 const std::vector<hpack::HeaderField> &headers);
75 virtual bool onH3StreamHeaders(netplus::socket *sock, uint64_t streamId,
76 const std::vector<qpack::HeaderField> &headers);
77
78 // Called for each body data chunk when streaming is enabled.
79 // endStream/fin: true on the last chunk.
80 virtual void onH2DataChunk(HttpRequest &conn, uint32_t streamId,
81 const char *data, size_t len, bool endStream,
82 std::string &h2out, const int tid, ULONG_PTR args);
83 virtual void onH3DataChunk(netplus::socket *sock, uint64_t streamId,
84 const char *data, size_t len, bool fin);
85
86 protected:
87 // Helpers for sending a complete response on an H2/H3 stream.
88 void sendH2StreamResponse(std::string &h2out, uint32_t streamId,
89 uint16_t status, const std::string &contentType,
90 const std::string &body);
91 void sendH3StreamResponse(netplus::socket *sock, uint64_t streamId,
92 uint16_t status, const std::string &contentType,
93 const std::string &body);
94 virtual void CreateConnection(std::shared_ptr<netplus::con> &res);
95
96 virtual void RequestEvent(netplus::con &curcon, const int tid, ULONG_PTR args);
97 virtual void ResponseEvent(netplus::con &curcon,const int tid,ULONG_PTR args);
98 virtual void ConnectEvent(netplus::con &curcon,const int tid,ULONG_PTR args);
99 virtual void DisconnectEvent(netplus::con &curcon,const int tid,ULONG_PTR args);
100
101 std::string _altSvcH3; // Alt-Svc value for HTTP/3 advertisement
102
103 // TLS session cache — shared across all accepted SSL connections
104 // to enable abbreviated TLS 1.2 handshakes on client reconnection.
105 netplus::TlsSessionCache _tlsSessionCache;
106 private:
107 // Per-stream state for HTTP/3: accumulates data and supports
108 // incremental H3 frame parsing for streaming body callbacks.
109 struct H3StreamState {
110 std::vector<uint8_t> data;
111 bool headersParsed = false;
112 bool streaming = false;
113 size_t parseOffset = 0;
114 };
115 std::mutex _h3BufferMutex;
116 std::map<uint64_t, H3StreamState> _h3StreamStates;
117 std::atomic<int> _h3NextTid{0};
118 void _dispatchH2Stream(HttpRequest &cureq, std::string &out,
119 uint32_t sid,
120 const std::vector<hpack::HeaderField> &decoded,
121 const std::string &reqBody,
122 const int tid, ULONG_PTR args);
123 void _resumeH2Streams(HttpRequest &cureq, std::string &out,
124 const int tid, ULONG_PTR args);
125 };
126
127 class HttpD {
128 public:
129 HttpD(int argc, char** argv);
130 HttpD(const std::string &httpaddr, int port, int maxconnections, const std::string &sslcertpath, const std::string &sslkeypath, const std::string &sslpassword = "");
131 ~HttpD();
132 std::vector<netplus::socket*> getServerSockets();
133 protected:
134 void FileServer();
135 private:
136
137 bool _fileServer;
138 std::vector<std::unique_ptr<netplus::socket>> _ServerSockets;
139 std::map<std::string, netplus::ssl::CertificateBundle> _certBundle;
140 HTTPException _httpexception;
141 };
142};
Definition exception.h:42
Definition httpd.h:127
Definition httpd.h:50
Definition http.h:311