WPILibC++ 2024.3.2
UrlParser.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_URLPARSER_H_
6#define WPINET_URLPARSER_H_
7
8#include <string_view>
9
10#include <wpi/StringExtras.h>
11
12#include "wpinet/http_parser.h"
13
14namespace wpi {
15
16/**
17 * Parses a URL into its constiuent components.
18 * `schema://userinfo@host:port/the/path?query#fragment`
19 */
20class UrlParser {
21 public:
22 /**
23 * Parse a URL.
24 * @param in input
25 * @param isConnect
26 */
27 UrlParser(std::string_view in, bool isConnect) {
28 m_data = in;
30 m_error = http_parser_parse_url(in.data(), in.size(), isConnect, &m_url);
31 }
32
33 /**
34 * Determine if the URL is valid (e.g. the parse was successful).
35 */
36 bool IsValid() const { return !m_error; }
37
38 bool HasSchema() const { return (m_url.field_set & (1 << UF_SCHEMA)) != 0; }
39
40 bool HasHost() const { return (m_url.field_set & (1 << UF_HOST)) != 0; }
41
42 bool HasPort() const { return (m_url.field_set & (1 << UF_PORT)) != 0; }
43
44 bool HasPath() const { return (m_url.field_set & (1 << UF_PATH)) != 0; }
45
46 bool HasQuery() const { return (m_url.field_set & (1 << UF_QUERY)) != 0; }
47
48 bool HasFragment() const {
49 return (m_url.field_set & (1 << UF_FRAGMENT)) != 0;
50 }
51
52 bool HasUserInfo() const {
53 return (m_url.field_set & (1 << UF_USERINFO)) != 0;
54 }
55
57 return wpi::substr(m_data, m_url.field_data[UF_SCHEMA].off,
58 m_url.field_data[UF_SCHEMA].len);
59 }
60
62 return wpi::substr(m_data, m_url.field_data[UF_HOST].off,
63 m_url.field_data[UF_HOST].len);
64 }
65
66 unsigned int GetPort() const { return m_url.port; }
67
69 return wpi::substr(m_data, m_url.field_data[UF_PATH].off,
70 m_url.field_data[UF_PATH].len);
71 }
72
74 return wpi::substr(m_data, m_url.field_data[UF_QUERY].off,
75 m_url.field_data[UF_QUERY].len);
76 }
77
79 return wpi::substr(m_data, m_url.field_data[UF_FRAGMENT].off,
81 }
82
84 return wpi::substr(m_data, m_url.field_data[UF_USERINFO].off,
86 }
87
88 private:
89 bool m_error;
90 std::string_view m_data;
91 http_parser_url m_url;
92};
93
94} // namespace wpi
95
96#endif // WPINET_URLPARSER_H_
Parses a URL into its constiuent components.
Definition: UrlParser.h:20
std::string_view GetQuery() const
Definition: UrlParser.h:73
bool IsValid() const
Determine if the URL is valid (e.g.
Definition: UrlParser.h:36
std::string_view GetFragment() const
Definition: UrlParser.h:78
bool HasQuery() const
Definition: UrlParser.h:46
bool HasSchema() const
Definition: UrlParser.h:38
UrlParser(std::string_view in, bool isConnect)
Parse a URL.
Definition: UrlParser.h:27
std::string_view GetUserInfo() const
Definition: UrlParser.h:83
bool HasFragment() const
Definition: UrlParser.h:48
bool HasHost() const
Definition: UrlParser.h:40
bool HasUserInfo() const
Definition: UrlParser.h:52
std::string_view GetHost() const
Definition: UrlParser.h:61
std::string_view GetPath() const
Definition: UrlParser.h:68
bool HasPort() const
Definition: UrlParser.h:42
std::string_view GetSchema() const
Definition: UrlParser.h:56
bool HasPath() const
Definition: UrlParser.h:44
unsigned int GetPort() const
Definition: UrlParser.h:66
basic_string_view< char > string_view
Definition: core.h:501
constexpr auto in(type t, int set) -> bool
Definition: core.h:611
Definition: ntcore_cpp.h:26
void http_parser_url_init(struct http_parser_url *u)
constexpr std::string_view substr(std::string_view str, std::string_view::size_type start, std::string_view::size_type n=std::string_view::npos) noexcept
Returns the substring of str from [start, start + n).
Definition: StringExtras.h:224
int http_parser_parse_url(const char *buf, size_t buflen, int is_connect, struct http_parser_url *u)
Definition: http_parser.h:346
struct wpi::http_parser_url::@17 field_data[UF_MAX]
uint16_t off
Definition: http_parser.h:351
uint16_t field_set
Definition: http_parser.h:347
uint16_t port
Definition: http_parser.h:348
uint16_t len
Definition: http_parser.h:352
@ UF_QUERY
Definition: http_parser.h:332
@ UF_SCHEMA
Definition: http_parser.h:328
@ UF_PATH
Definition: http_parser.h:331
@ UF_FRAGMENT
Definition: http_parser.h:333
@ UF_USERINFO
Definition: http_parser.h:334
@ UF_HOST
Definition: http_parser.h:329
@ UF_PORT
Definition: http_parser.h:330