Skip to content

Commit a1b642a

Browse files
authored
[wpinet] Add simple web server (#7527)
Also add EscapeHTML to HttpUtil.
1 parent f9b3efb commit a1b642a

File tree

7 files changed

+535
-0
lines changed

7 files changed

+535
-0
lines changed

wpinet/src/main/java/edu/wpi/first/net/WPINetJNI.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ public static synchronized void forceLoad() throws IOException {
8080
*/
8181
public static native void removePortForwarder(int port);
8282

83+
/**
84+
* Create a web server at the given port. Note that local ports less than 1024 won't work as a
85+
* normal user.
86+
*
87+
* @param port local port number
88+
* @param path local path to document root
89+
*/
90+
public static native void startWebServer(int port, String path);
91+
92+
/**
93+
* Stop web server running at the given port.
94+
*
95+
* @param port local port number
96+
*/
97+
public static native void stopWebServer(int port);
98+
8399
/**
84100
* Creates a MulticastServiceAnnouncer.
85101
*
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
package edu.wpi.first.net;
6+
7+
/** A web server using the HTTP protocol. */
8+
public final class WebServer {
9+
private WebServer() {
10+
throw new UnsupportedOperationException("This is a utility class!");
11+
}
12+
13+
/**
14+
* Create a web server at the given port. Note that local ports less than 1024 won't work as a
15+
* normal user. Also, many ports are blocked by the FRC robot radio; check the game manual for
16+
* what is allowed through the radio firewall.
17+
*
18+
* @param port local port number
19+
* @param path local path to document root
20+
*/
21+
public static void start(int port, String path) {
22+
WPINetJNI.startWebServer(port, path);
23+
}
24+
25+
/**
26+
* Stop web server running at the given port.
27+
*
28+
* @param port local port number
29+
*/
30+
public static void stop(int port) {
31+
WPINetJNI.stopWebServer(port);
32+
}
33+
}

wpinet/src/main/native/cpp/HttpUtil.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ std::string_view EscapeURI(std::string_view str, SmallVectorImpl<char>& buf,
8181
return {buf.data(), buf.size()};
8282
}
8383

84+
std::string_view EscapeHTML(std::string_view str, SmallVectorImpl<char>& buf) {
85+
buf.clear();
86+
for (auto i = str.begin(), end = str.end(); i != end; ++i) {
87+
if (*i == '&') {
88+
buf.append({'&', 'a', 'm', 'p', ';'});
89+
} else if (*i == '<') {
90+
buf.append({'&', 'l', 't', ';'});
91+
} else if (*i == '>') {
92+
buf.append({'&', 'g', 't', ';'});
93+
} else {
94+
buf.push_back(*i);
95+
}
96+
}
97+
return {buf.data(), buf.size()};
98+
}
99+
84100
HttpQueryMap::HttpQueryMap(std::string_view query) {
85101
SmallVector<std::string_view, 16> queryElems;
86102
split(query, queryElems, '&', 100, false);

0 commit comments

Comments
 (0)