-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWsCommand.cpp
More file actions
237 lines (198 loc) · 5.44 KB
/
Copy pathWsCommand.cpp
File metadata and controls
237 lines (198 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
*
* @file WsCommand.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/commands/WsCommand.hpp>
#include <vix/cli/commands/ws/WsChecker.hpp>
#include <vix/cli/commands/ws/WsConfig.hpp>
#include <vix/cli/commands/ws/WsOutput.hpp>
#include <vix/cli/commands/ws/WsTypes.hpp>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
namespace vix::commands
{
namespace
{
bool consume_flag(
std::vector<std::string> &args,
const std::string &flag)
{
for (auto it = args.begin(); it != args.end(); ++it)
{
if (*it == flag)
{
args.erase(it);
return true;
}
}
return false;
}
bool consume_value(
std::vector<std::string> &args,
const std::string &flag,
std::string &out)
{
for (auto it = args.begin(); it != args.end(); ++it)
{
if (*it != flag)
continue;
auto valueIt = it + 1;
if (valueIt == args.end())
return false;
out = *valueIt;
args.erase(it, valueIt + 1);
return true;
}
return true;
}
bool consume_u64_value(
std::vector<std::string> &args,
const std::string &flag,
std::uint64_t &out)
{
std::string value;
if (!consume_value(args, flag, value))
return false;
if (value.empty())
return true;
try
{
const unsigned long long parsed = std::stoull(value);
if (parsed == 0)
return false;
out = static_cast<std::uint64_t>(parsed);
return true;
}
catch (...)
{
return false;
}
}
ws::WsOptions parse_options(
std::vector<std::string> &args,
bool &ok,
std::string &errorMessage)
{
ws::WsOptions options;
ok = true;
options.verbose = consume_flag(args, "--verbose") ||
consume_flag(args, "-v");
options.ping = !consume_flag(args, "--no-ping");
if (!consume_u64_value(args, "--timeout", options.timeoutMs))
{
ok = false;
errorMessage = "invalid value for --timeout";
return options;
}
return options;
}
bool consume_check_target(
std::vector<std::string> &args,
ws::WsOptions &options,
std::string &errorMessage)
{
if (args.empty())
{
options.target = ws::WsTarget::Check;
return true;
}
const std::string action = args[0];
if (action != "check")
{
errorMessage = "unknown ws command: " + action;
return false;
}
options.target = ws::WsTarget::Check;
args.erase(args.begin());
if (!args.empty() &&
args[0].rfind("-", 0) != 0)
{
options.url = args[0];
args.erase(args.begin());
}
return true;
}
}
int WsCommand::run(const std::vector<std::string> &argsIn)
{
std::vector<std::string> args = argsIn;
if (!args.empty() &&
(args[0] == "-h" || args[0] == "--help"))
{
return help();
}
bool ok = true;
std::string errorMessage;
ws::WsOptions options =
parse_options(args, ok, errorMessage);
if (!ok)
{
ws::output::error(std::cerr, errorMessage);
ws::output::fix(std::cerr, "vix ws --help");
return 1;
}
if (!consume_check_target(args, options, errorMessage))
{
ws::output::error(std::cerr, errorMessage);
ws::output::fix(std::cerr, "vix ws --help");
return 1;
}
if (!args.empty())
{
ws::output::error(
std::cerr,
"unknown ws argument: " + args[0]);
ws::output::fix(
std::cerr,
"vix ws --help");
return 1;
}
ws::WsConfig cfg =
ws::apply_ws_options(
ws::load_ws_config(),
options);
return ws::checker::check(cfg, options);
}
int WsCommand::help()
{
std::cout
<< "Usage:\n"
<< " vix ws check [url] [options]\n\n"
<< "Commands:\n"
<< " check Check a WebSocket endpoint\n\n"
<< "Options:\n"
<< " --timeout Connection timeout in milliseconds\n"
<< " --no-ping Do not send a ping frame after handshake\n"
<< " --verbose Print additional diagnostics\n"
<< " -v Alias for --verbose\n"
<< " -h, --help Show this help message\n\n"
<< "Examples:\n"
<< " vix ws check ws://127.0.0.1:9090/ws\n"
<< " vix ws check ws://127.0.0.1:9090/ws --timeout 5000\n"
<< " vix ws check ws://127.0.0.1:9090/ws --no-ping\n\n"
<< "Notes:\n"
<< " wss:// checks are not supported yet by the native checker.\n"
<< " Use ws:// for local checks until TLS WebSocket support is added.\n\n"
<< "Config:\n"
<< " production.websocket.host\n"
<< " production.websocket.port\n"
<< " production.websocket.path\n"
<< " production.websocket.local_url\n"
<< " production.websocket.public_url\n"
<< " production.websocket.timeout_ms\n"
<< " production.websocket.heartbeat\n"
<< " production.proxy.websocket.path\n"
<< " production.proxy.websocket.port\n";
return 0;
}
}