WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
HttpServerConnection.hpp
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#pragma once
6
7#include <memory>
8#include <span>
9#include <string_view>
10
12#include "wpi/net/uv/Stream.hpp"
13
14namespace wpi::util {
15class raw_ostream;
16}
17
18namespace wpi::net {
19
21 public:
22 explicit HttpServerConnection(std::shared_ptr<uv::Stream> stream);
23 virtual ~HttpServerConnection() = default;
24
25 protected:
26 /**
27 * Process an incoming HTTP request. This is called after the incoming
28 * message completes (e.g. from the HttpParser::messageComplete callback).
29 *
30 * The implementation should read request details from m_request and call the
31 * appropriate Send() functions to send a response back to the client.
32 */
33 virtual void ProcessRequest() = 0;
34
35 /**
36 * Build common response headers.
37 *
38 * Called by SendHeader() to send headers common to every response.
39 * Each line must be terminated with "\r\n".
40 *
41 * The default implementation sends the following:
42 * "Server: WebServer/1.0\r\n"
43 * "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, "
44 * "post-check=0, max-age=0\r\n"
45 * "Pragma: no-cache\r\n"
46 * "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"
47 *
48 * These parameters should ensure the browser does not cache the response.
49 * A browser should connect for each file and not serve files from its cache.
50 *
51 * @param os response stream
52 */
53 virtual void BuildCommonHeaders(wpi::util::raw_ostream& os);
54
55 /**
56 * Build HTTP response header, along with other header information like
57 * mimetype. Calls BuildCommonHeaders().
58 *
59 * @param os response stream
60 * @param code HTTP response code (e.g. 200)
61 * @param codeText HTTP response code text (e.g. "OK")
62 * @param contentType MIME content type (e.g. "text/plain")
63 * @param contentLength Length of content. If 0 is provided, m_keepAlive will
64 * be set to false.
65 * @param extra Extra HTTP headers to send, including final "\r\n"
66 */
67 virtual void BuildHeader(wpi::util::raw_ostream& os, int code,
68 std::string_view codeText,
69 std::string_view contentType, uint64_t contentLength,
70 std::string_view extra = {});
71
72 /**
73 * Send data to client.
74 *
75 * This is a convenience wrapper around m_stream.Write() to provide
76 * auto-close functionality.
77 *
78 * @param bufs Buffers to write. Deallocate() will be called on each
79 * buffer after the write completes. If different behavior
80 * is desired, call m_stream.Write() directly instead.
81 * @param closeAfter close the connection after the write completes
82 */
83 void SendData(std::span<const uv::Buffer> bufs, bool closeAfter = false);
84
85 /**
86 * Send HTTP response, along with other header information like mimetype.
87 * Calls BuildHeader().
88 *
89 * @param code HTTP response code (e.g. 200)
90 * @param codeText HTTP response code text (e.g. "OK")
91 * @param contentType MIME content type (e.g. "text/plain")
92 * @param content Response message content
93 * @param extraHeader Extra HTTP headers to send, including final "\r\n"
94 */
95 virtual void SendResponse(int code, std::string_view codeText,
96 std::string_view contentType,
97 std::string_view content,
98 std::string_view extraHeader = {});
99
100 /**
101 * Send HTTP response from static data, along with other header information
102 * like mimetype. Calls BuildHeader(). Supports gzip pre-compressed data
103 * (it will decompress if the client does not accept gzip encoded data).
104 * Unlike SendResponse(), content is not copied and its contents must remain
105 * valid for an unspecified lifetime.
106 *
107 * @param code HTTP response code (e.g. 200)
108 * @param codeText HTTP response code text (e.g. "OK")
109 * @param contentType MIME content type (e.g. "text/plain")
110 * @param content Response message content
111 * @param gzipped True if content is gzip compressed
112 * @param extraHeader Extra HTTP headers to send, including final "\r\n"
113 */
114 virtual void SendStaticResponse(int code, std::string_view codeText,
115 std::string_view contentType,
116 std::string_view content, bool gzipped,
117 std::string_view extraHeader = {});
118
119 /**
120 * Send error header and message.
121 * This provides standard code responses for 400, 401, 403, 404, 500, and 503.
122 * Other codes will be reported as 501. For arbitrary code handling, use
123 * SendResponse() instead.
124 *
125 * @param code HTTP error code (e.g. 404)
126 * @param message Additional message text
127 */
128 virtual void SendError(int code, std::string_view message = {});
129
130 /** The HTTP request. */
132
133 /** Whether the connection should be kept alive. */
134 bool m_keepAlive = false;
135
136 /** If gzip is an acceptable encoding for responses. */
137 bool m_acceptGzip = false;
138
139 /** The underlying stream for the connection. */
141
142 /** The header reader connection. */
144
145 /** The end stream connection. */
147
148 /** The message complete connection. */
150};
151
152} // namespace wpi::net
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 glfw and nanopb were modified for use in 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:122
HTTP protocol parser.
Definition HttpParser.hpp:22
@ kRequest
Definition HttpParser.hpp:25
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.
bool m_acceptGzip
If gzip is an acceptable encoding for responses.
Definition HttpServerConnection.hpp:137
virtual void ProcessRequest()=0
Process an incoming HTTP request.
uv::Stream & m_stream
The underlying stream for the connection.
Definition HttpServerConnection.hpp:140
virtual void SendError(int code, std::string_view message={})
Send error header and message.
wpi::util::sig::ScopedConnection m_endConn
The end stream connection.
Definition HttpServerConnection.hpp:146
bool m_keepAlive
Whether the connection should be kept alive.
Definition HttpServerConnection.hpp:134
wpi::util::sig::ScopedConnection m_dataConn
The header reader connection.
Definition HttpServerConnection.hpp:143
virtual void BuildHeader(wpi::util::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.
HttpParser m_request
The HTTP request.
Definition HttpServerConnection.hpp:131
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.
void SendData(std::span< const uv::Buffer > bufs, bool closeAfter=false)
Send data to client.
virtual void BuildCommonHeaders(wpi::util::raw_ostream &os)
Build common response headers.
HttpServerConnection(std::shared_ptr< uv::Stream > stream)
virtual ~HttpServerConnection()=default
wpi::util::sig::Connection m_messageCompleteConn
The message complete connection.
Definition HttpServerConnection.hpp:149
Stream handle.
Definition Stream.hpp:66
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
Definition raw_socket_ostream.hpp:9
Definition raw_os_ostream.hpp:19