WPILibC++ 2024.3.2
HttpServerConnection.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPINET_HTTPSERVERCONNECTION_H_
6#define WPINET_HTTPSERVERCONNECTION_H_
7
8#include <memory>
9#include <span>
10#include <string_view>
11
12#include "wpinet/HttpParser.h"
13#include "wpinet/uv/Stream.h"
14
15namespace wpi {
16
17class raw_ostream;
18
20 public:
21 explicit HttpServerConnection(std::shared_ptr<uv::Stream> stream);
22 virtual ~HttpServerConnection() = default;
23
24 protected:
25 /**
26 * Process an incoming HTTP request. This is called after the incoming
27 * message completes (e.g. from the HttpParser::messageComplete callback).
28 *
29 * The implementation should read request details from m_request and call the
30 * appropriate Send() functions to send a response back to the client.
31 */
32 virtual void ProcessRequest() = 0;
33
34 /**
35 * Build common response headers.
36 *
37 * Called by SendHeader() to send headers common to every response.
38 * Each line must be terminated with "\r\n".
39 *
40 * The default implementation sends the following:
41 * "Server: WebServer/1.0\r\n"
42 * "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, "
43 * "post-check=0, max-age=0\r\n"
44 * "Pragma: no-cache\r\n"
45 * "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"
46 *
47 * These parameters should ensure the browser does not cache the response.
48 * A browser should connect for each file and not serve files from its cache.
49 *
50 * @param os response stream
51 */
53
54 /**
55 * Build HTTP response header, along with other header information like
56 * mimetype. Calls BuildCommonHeaders().
57 *
58 * @param os response stream
59 * @param code HTTP response code (e.g. 200)
60 * @param codeText HTTP response code text (e.g. "OK")
61 * @param contentType MIME content type (e.g. "text/plain")
62 * @param contentLength Length of content. If 0 is provided, m_keepAlive will
63 * be set to false.
64 * @param extra Extra HTTP headers to send, including final "\r\n"
65 */
66 virtual void BuildHeader(raw_ostream& os, int code, std::string_view codeText,
67 std::string_view contentType, uint64_t contentLength,
68 std::string_view extra = {});
69
70 /**
71 * Send data to client.
72 *
73 * This is a convenience wrapper around m_stream.Write() to provide
74 * auto-close functionality.
75 *
76 * @param bufs Buffers to write. Deallocate() will be called on each
77 * buffer after the write completes. If different behavior
78 * is desired, call m_stream.Write() directly instead.
79 * @param closeAfter close the connection after the write completes
80 */
81 void SendData(std::span<const uv::Buffer> bufs, bool closeAfter = false);
82
83 /**
84 * Send HTTP response, along with other header information like mimetype.
85 * Calls BuildHeader().
86 *
87 * @param code HTTP response code (e.g. 200)
88 * @param codeText HTTP response code text (e.g. "OK")
89 * @param contentType MIME content type (e.g. "text/plain")
90 * @param content Response message content
91 * @param extraHeader Extra HTTP headers to send, including final "\r\n"
92 */
93 virtual void SendResponse(int code, std::string_view codeText,
94 std::string_view contentType,
95 std::string_view content,
96 std::string_view extraHeader = {});
97
98 /**
99 * Send HTTP response from static data, along with other header information
100 * like mimetype. Calls BuildHeader(). Supports gzip pre-compressed data
101 * (it will decompress if the client does not accept gzip encoded data).
102 * Unlike SendResponse(), content is not copied and its contents must remain
103 * valid for an unspecified lifetime.
104 *
105 * @param code HTTP response code (e.g. 200)
106 * @param codeText HTTP response code text (e.g. "OK")
107 * @param contentType MIME content type (e.g. "text/plain")
108 * @param content Response message content
109 * @param gzipped True if content is gzip compressed
110 * @param extraHeader Extra HTTP headers to send, including final "\r\n"
111 */
112 virtual void SendStaticResponse(int code, std::string_view codeText,
113 std::string_view contentType,
114 std::string_view content, bool gzipped,
115 std::string_view extraHeader = {});
116
117 /**
118 * Send error header and message.
119 * This provides standard code responses for 400, 401, 403, 404, 500, and 503.
120 * Other codes will be reported as 501. For arbitrary code handling, use
121 * SendResponse() instead.
122 *
123 * @param code HTTP error code (e.g. 404)
124 * @param message Additional message text
125 */
126 virtual void SendError(int code, std::string_view message = {});
127
128 /** The HTTP request. */
130
131 /** Whether the connection should be kept alive. */
132 bool m_keepAlive = false;
133
134 /** If gzip is an acceptable encoding for responses. */
135 bool m_acceptGzip = false;
136
137 /** The underlying stream for the connection. */
139
140 /** The header reader connection. */
142
143 /** The end stream connection. */
145
146 /** The message complete connection. */
148};
149
150} // namespace wpi
151
152#endif // WPINET_HTTPSERVERCONNECTION_H_
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source code
Definition: ThirdPartyNotices.txt:110
HTTP protocol parser.
Definition: HttpParser.h:24
@ kRequest
Definition: HttpParser.h:27
Definition: HttpServerConnection.h:19
HttpServerConnection(std::shared_ptr< uv::Stream > stream)
sig::ScopedConnection m_dataConn
The header reader connection.
Definition: HttpServerConnection.h:141
virtual void BuildCommonHeaders(raw_ostream &os)
Build common response headers.
virtual void SendStaticResponse(int code, std::string_view codeText, std::string_view contentType, std::string_view content, bool gzipped, std::string_view extraHeader={})
Send HTTP response from static data, along with other header information like mimetype.
virtual void SendError(int code, std::string_view message={})
Send error header and message.
virtual ~HttpServerConnection()=default
bool m_acceptGzip
If gzip is an acceptable encoding for responses.
Definition: HttpServerConnection.h:135
uv::Stream & m_stream
The underlying stream for the connection.
Definition: HttpServerConnection.h:138
HttpParser m_request
The HTTP request.
Definition: HttpServerConnection.h:129
void SendData(std::span< const uv::Buffer > bufs, bool closeAfter=false)
Send data to client.
sig::ScopedConnection m_endConn
The end stream connection.
Definition: HttpServerConnection.h:144
virtual void ProcessRequest()=0
Process an incoming HTTP request.
bool m_keepAlive
Whether the connection should be kept alive.
Definition: HttpServerConnection.h:132
virtual void BuildHeader(raw_ostream &os, int code, std::string_view codeText, std::string_view contentType, uint64_t contentLength, std::string_view extra={})
Build HTTP response header, along with other header information like mimetype.
virtual void SendResponse(int code, std::string_view codeText, std::string_view contentType, std::string_view content, std::string_view extraHeader={})
Send HTTP response, along with other header information like mimetype.
sig::Connection m_messageCompleteConn
The message complete connection.
Definition: HttpServerConnection.h:147
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:43
A Connection object allows interaction with an ongoing slot connection.
Definition: Signal.h:198
ScopedConnection is a RAII version of Connection It disconnects the slot from the signal upon destruc...
Definition: Signal.h:257
Stream handle.
Definition: Stream.h:68
basic_string_view< char > string_view
Definition: core.h:501
Definition: ntcore_cpp.h:26