netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
Documentation
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! The beanstalkd wire format: command parsing, reply rendering, and the check that a reply
//! answers the command it is sent for.
//!
//! Everything here is a pure function of its arguments, shared by the session loop, the action
//! executor and the tests. **The model never writes a byte of framing**: it supplies job ids,
//! job bodies, a status word from a fixed list and the key/value pairs of a stats report, and
//! this module decides the reply line, the byte count and the YAML. A job body that happens to
//! contain `\r\nINSERTED 9\r\n` is counted into `<bytes>` and read by the client as body, not as
//! a second reply.
//!
//! The protocol is upstream's `doc/protocol.txt` (beanstalkd 1.13): lines end in CRLF,
//! commands are lower-case and case-sensitive, tube names are at most 200 bytes of
//! `[A-Za-z0-9+/;.$_()-]` and do not begin with `-`, and a job body is `<bytes>` octets followed
//! by CRLF.

/// The longest command line a peer may send, **including** the trailing CRLF.
///
/// Upstream's `LINE_BUF_SIZE` is 224 (a 200-byte tube name plus the longest verb and its
/// numbers). Upstream answers an over-long line with `BAD_FORMAT`; NetGet does too, and then
/// closes, because there is no way to find the start of the next command in a stream that
/// ignored the limit.
pub const MAX_LINE_BYTES: usize = 224;

/// The largest job body `put` may declare: upstream's default `max-job-size` (`-z`, 65535).
///
/// Checked against the **declared** `<bytes>` before a single body byte is read, so a `put`
/// declaring four gigabytes is answered `JOB_TOO_BIG` at once and nothing is buffered for it.
/// The same limit applies to the bodies the model hands out in `RESERVED`/`FOUND`, because a
/// real beanstalkd never holds a job larger than it would have accepted.
pub const MAX_JOB_BYTES: usize = 65_535;

/// The longest tube name (upstream's `MAX_TUBE_NAME_LEN` - 1).
pub const MAX_TUBE_NAME: usize = 200;

/// How many bytes of an oversize body NetGet reads and discards after `JOB_TOO_BIG` to keep the
/// connection in step, as upstream does. A `put` declaring more than this is answered and then
/// closed instead: skipping a gigabyte to stay in sync is not worth holding the connection for.
pub const MAX_DISCARD_BYTES: u64 = 1024 * 1024;

/// The most entries a stats report or tube list the model writes may carry.
pub const MAX_YAML_ENTRIES: usize = 512;

/// The longest scalar value in a stats report.
pub const MAX_YAML_VALUE: usize = 200;

/// Is `name` a tube name upstream would accept?
pub fn valid_tube_name(name: &str) -> bool {
    !name.is_empty()
        && name.len() <= MAX_TUBE_NAME
        && !name.starts_with('-')
        && name
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b"-+/;.$_()".contains(&b))
}

/// One parsed command line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Command {
    Put {
        priority: u32,
        delay: u32,
        ttr: u32,
        /// The declared body length. `None` when the number does not fit a `u64`, which is
        /// larger than any bound and is refused the same way.
        bytes: Option<u64>,
    },
    Use(String),
    Reserve,
    ReserveWithTimeout(u32),
    ReserveJob(u64),
    Delete(u64),
    Release {
        id: u64,
        priority: u32,
        delay: u32,
    },
    Bury {
        id: u64,
        priority: u32,
    },
    Touch(u64),
    Watch(String),
    Ignore(String),
    Peek(u64),
    PeekReady,
    PeekDelayed,
    PeekBuried,
    Kick(u64),
    KickJob(u64),
    StatsJob(u64),
    StatsTube(String),
    Stats,
    ListTubes,
    ListTubeUsed,
    ListTubesWatched,
    PauseTube {
        tube: String,
        delay: u32,
    },
    Quit,
    /// A known command with missing, extra or malformed arguments.
    BadFormat,
    /// Not a beanstalkd command.
    Unknown,
}

fn num<T: std::str::FromStr>(s: &str) -> Option<T> {
    // Digits only: upstream's strtoul would also take a sign or leading space, but no client
    // sends either and accepting `-1` as a huge unsigned number is not a courtesy.
    if s.is_empty() || !s.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    s.parse().ok()
}

/// Parse one command line, without its CRLF. Commands are case-sensitive, as upstream's are.
pub fn parse_command(line: &str) -> Command {
    // Upstream separates arguments with single spaces; splitting on runs of spaces accepts the
    // same lines and a few more, and never splits a tube name (which cannot contain a space).
    let parts: Vec<&str> = line.split(' ').filter(|p| !p.is_empty()).collect();
    let Some((verb, args)) = parts.split_first() else {
        return Command::Unknown;
    };
    let tube = |s: &str| -> Option<String> { valid_tube_name(s).then(|| s.to_string()) };
    let parsed = match (*verb, args) {
        ("put", [p, d, t, b]) => (|| {
            Some(Command::Put {
                priority: num(p)?,
                delay: num(d)?,
                ttr: num(t)?,
                bytes: if b.is_empty() || !b.bytes().all(|c| c.is_ascii_digit()) {
                    return None;
                } else {
                    b.parse().ok()
                },
            })
        })(),
        ("use", [t]) => tube(t).map(Command::Use),
        ("reserve", []) => Some(Command::Reserve),
        ("reserve-with-timeout", [s]) => num(s).map(Command::ReserveWithTimeout),
        ("reserve-job", [id]) => num(id).map(Command::ReserveJob),
        ("delete", [id]) => num(id).map(Command::Delete),
        ("release", [id, p, d]) => (|| {
            Some(Command::Release {
                id: num(id)?,
                priority: num(p)?,
                delay: num(d)?,
            })
        })(),
        ("bury", [id, p]) => (|| {
            Some(Command::Bury {
                id: num(id)?,
                priority: num(p)?,
            })
        })(),
        ("touch", [id]) => num(id).map(Command::Touch),
        ("watch", [t]) => tube(t).map(Command::Watch),
        ("ignore", [t]) => tube(t).map(Command::Ignore),
        ("peek", [id]) => num(id).map(Command::Peek),
        ("peek-ready", []) => Some(Command::PeekReady),
        ("peek-delayed", []) => Some(Command::PeekDelayed),
        ("peek-buried", []) => Some(Command::PeekBuried),
        ("kick", [n]) => num(n).map(Command::Kick),
        ("kick-job", [id]) => num(id).map(Command::KickJob),
        ("stats-job", [id]) => num(id).map(Command::StatsJob),
        ("stats-tube", [t]) => tube(t).map(Command::StatsTube),
        ("stats", []) => Some(Command::Stats),
        ("list-tubes", []) => Some(Command::ListTubes),
        ("list-tube-used", []) => Some(Command::ListTubeUsed),
        ("list-tubes-watched", []) => Some(Command::ListTubesWatched),
        ("pause-tube", [t, d]) => (|| {
            Some(Command::PauseTube {
                tube: tube(t)?,
                delay: num(d)?,
            })
        })(),
        ("quit", []) => Some(Command::Quit),
        (
            "put"
            | "use"
            | "reserve"
            | "reserve-with-timeout"
            | "reserve-job"
            | "delete"
            | "release"
            | "bury"
            | "touch"
            | "watch"
            | "ignore"
            | "peek"
            | "peek-ready"
            | "peek-delayed"
            | "peek-buried"
            | "kick"
            | "kick-job"
            | "stats-job"
            | "stats-tube"
            | "stats"
            | "list-tubes"
            | "list-tube-used"
            | "list-tubes-watched"
            | "pause-tube"
            | "quit",
            _,
        ) => None,
        _ => return Command::Unknown,
    };
    parsed.unwrap_or(Command::BadFormat)
}

/// The status words `send_beanstalkd_status` may send. Everything else on the wire — `USING`,
/// `WATCHING`, `NOT_IGNORED`, `BAD_FORMAT`, `UNKNOWN_COMMAND`, `JOB_TOO_BIG`, `EXPECTED_CRLF`
/// — is NetGet's own answer about the connection, and `INSERTED`/`RESERVED`/`FOUND`/`OK` have
/// actions of their own because they carry data.
pub const STATUS_WORDS: &[&str] = &[
    "DELETED",
    "RELEASED",
    "BURIED",
    "TOUCHED",
    "KICKED",
    "PAUSED",
    "NOT_FOUND",
    "TIMED_OUT",
    "DEADLINE_SOON",
    "DRAINING",
    "OUT_OF_MEMORY",
    "INTERNAL_ERROR",
];

/// `STATUS` or `KICKED <count>`.
pub fn render_status(status: &str, count: Option<u64>) -> Result<String, String> {
    let word = status.trim().to_ascii_uppercase();
    if !STATUS_WORDS.contains(&word.as_str()) {
        return Err(format!(
            "status must be one of {}, got {status:?}",
            STATUS_WORDS.join(", ")
        ));
    }
    match (word.as_str(), count) {
        ("KICKED", Some(n)) => Ok(format!("KICKED {n}\r\n")),
        (_, Some(_)) => Err(format!(
            "'count' is only meaningful with KICKED, not {word}"
        )),
        (_, None) => Ok(format!("{word}\r\n")),
    }
}

/// `INSERTED <id>`, or `BURIED <id>` when the job went straight to the buried list.
pub fn render_inserted(id: u64, buried: bool) -> Result<String, String> {
    check_id(id)?;
    Ok(format!(
        "{} {id}\r\n",
        if buried { "BURIED" } else { "INSERTED" }
    ))
}

/// Which job-carrying reply to write.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JobReply {
    Reserved,
    Found,
}

/// `RESERVED <id> <bytes>\r\n<body>\r\n` or `FOUND …`. The count is the body's byte length,
/// computed here, so whatever the body contains is read as body.
pub fn render_job(kind: JobReply, id: u64, body: &str) -> Result<String, String> {
    check_id(id)?;
    if body.len() > MAX_JOB_BYTES {
        return Err(format!(
            "job body is {} bytes; beanstalkd's max-job-size is {MAX_JOB_BYTES}",
            body.len()
        ));
    }
    let word = match kind {
        JobReply::Reserved => "RESERVED",
        JobReply::Found => "FOUND",
    };
    Ok(format!("{word} {id} {}\r\n{body}\r\n", body.len()))
}

fn check_id(id: u64) -> Result<(), String> {
    if id == 0 {
        return Err("job ids start at 1".to_string());
    }
    Ok(())
}

/// A scalar in a stats report, rendered the way beanstalkd renders it.
fn yaml_scalar(key: &str, value: &serde_json::Value) -> Result<String, String> {
    use serde_json::Value;
    let text = match value {
        Value::Number(n) => n.to_string(),
        Value::Bool(b) => b.to_string(),
        Value::String(s) => s.clone(),
        Value::Null => return Err(format!("stat '{key}' is null; give a number or a string")),
        _ => {
            return Err(format!(
                "stat '{key}' is a list or an object; a beanstalkd stats report is flat \
                 key: value lines"
            ))
        }
    };
    // Clients decode the report as ASCII (greenstalk calls `.decode("ascii")`), and a newline
    // would start a line of its own. Refused rather than rewritten: the model wrote the value
    // and can be told, and a rewritten value would disagree with the decision the log records.
    if text.is_empty()
        || text.len() > MAX_YAML_VALUE
        || !text.bytes().all(|b| (0x20..0x7f).contains(&b))
    {
        return Err(format!(
            "stat '{key}' must be 1..={MAX_YAML_VALUE} printable ASCII characters, got {text:?}"
        ));
    }
    Ok(text)
}

fn valid_stat_key(key: &str) -> bool {
    !key.is_empty()
        && key.len() <= 64
        && key
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
}

/// `OK <bytes>\r\n---\nkey: value\n…\r\n` — the YAML dictionary `stats`, `stats-tube` and
/// `stats-job` answer with.
pub fn render_stats(stats: &serde_json::Map<String, serde_json::Value>) -> Result<String, String> {
    if stats.len() > MAX_YAML_ENTRIES {
        return Err(format!("at most {MAX_YAML_ENTRIES} stats"));
    }
    let mut yaml = String::from("---\n");
    for (key, value) in stats {
        if !valid_stat_key(key) {
            return Err(format!(
                "stat name {key:?} must be letters, digits, '-' or '_' (like current-jobs-ready)"
            ));
        }
        yaml.push_str(&format!("{key}: {}\n", yaml_scalar(key, value)?));
    }
    Ok(ok_block(&yaml))
}

/// `OK <bytes>\r\n---\n- name\n…\r\n` — the YAML list `list-tubes` and `list-tubes-watched`
/// answer with. Every entry must be a tube name, so no entry can break the list.
pub fn render_tube_list<S: AsRef<str>>(tubes: &[S]) -> Result<String, String> {
    if tubes.len() > MAX_YAML_ENTRIES {
        return Err(format!("at most {MAX_YAML_ENTRIES} tubes"));
    }
    let mut yaml = String::from("---\n");
    for tube in tubes {
        let tube = tube.as_ref();
        if !valid_tube_name(tube) {
            return Err(format!(
                "{tube:?} is not a tube name: 1-{MAX_TUBE_NAME} of letters, digits and \
                 -+/;.$_() not starting with '-'"
            ));
        }
        yaml.push_str(&format!("- {tube}\n"));
    }
    Ok(ok_block(&yaml))
}

fn ok_block(yaml: &str) -> String {
    format!("OK {}\r\n{yaml}\r\n", yaml.len())
}

/// The first line of a rendered reply, split into its word and arguments.
pub fn reply_head(reply: &[u8]) -> (String, Vec<String>) {
    let end = reply
        .windows(2)
        .position(|w| w == b"\r\n")
        .unwrap_or(reply.len());
    let line = String::from_utf8_lossy(&reply[..end]);
    let mut parts = line.split(' ').map(str::to_string);
    let word = parts.next().unwrap_or_default();
    (word, parts.collect())
}

/// Does `reply` — something the executor rendered — answer `command`?
///
/// The executor is stateless and cannot know which command it is answering, so a model can
/// produce a well-formed reply to the wrong command: `RESERVED` to a `delete`, a stats
/// dictionary to `list-tubes`, `KICKED` without a count to `kick`. Each would be read by the
/// client as something else. `OUT_OF_MEMORY` and `INTERNAL_ERROR` answer anything.
pub fn reply_fits(command: &Command, reply: &[u8]) -> bool {
    let (word, args) = reply_head(reply);
    let n = args.len();
    if n == 0 && (word == "OUT_OF_MEMORY" || word == "INTERNAL_ERROR") {
        return true;
    }
    // For `OK <bytes>`, the YAML payload: a list has only `- ` lines, a dictionary none.
    let yaml_lines = || -> Option<Vec<String>> {
        let size: usize = args.first()?.parse().ok()?;
        let start = reply.windows(2).position(|w| w == b"\r\n")? + 2;
        let payload = reply.get(start..start.checked_add(size)?)?;
        let text = std::str::from_utf8(payload).ok()?;
        let body = text.strip_prefix("---\n")?;
        Some(body.lines().map(str::to_string).collect())
    };
    let is_list = || yaml_lines().is_some_and(|l| l.iter().all(|l| l.starts_with("- ")));
    let is_dict = || yaml_lines().is_some_and(|l| l.iter().all(|l| !l.starts_with("- ")));
    use Command::*;
    match command {
        Put { .. } => matches!(
            (word.as_str(), n),
            ("INSERTED", 1) | ("BURIED", 1) | ("DRAINING", 0)
        ),
        Reserve => matches!((word.as_str(), n), ("RESERVED", 2) | ("DEADLINE_SOON", 0)),
        ReserveWithTimeout(_) => matches!(
            (word.as_str(), n),
            ("RESERVED", 2) | ("DEADLINE_SOON", 0) | ("TIMED_OUT", 0)
        ),
        ReserveJob(_) => matches!((word.as_str(), n), ("RESERVED", 2) | ("NOT_FOUND", 0)),
        Delete(_) => matches!((word.as_str(), n), ("DELETED", 0) | ("NOT_FOUND", 0)),
        Release { .. } => matches!(
            (word.as_str(), n),
            ("RELEASED", 0) | ("BURIED", 0) | ("NOT_FOUND", 0)
        ),
        Bury { .. } => matches!((word.as_str(), n), ("BURIED", 0) | ("NOT_FOUND", 0)),
        Touch(_) => matches!((word.as_str(), n), ("TOUCHED", 0) | ("NOT_FOUND", 0)),
        Peek(_) | PeekReady | PeekDelayed | PeekBuried => {
            matches!((word.as_str(), n), ("FOUND", 2) | ("NOT_FOUND", 0))
        }
        Kick(_) => word == "KICKED" && n == 1,
        KickJob(_) => matches!((word.as_str(), n), ("KICKED", 0) | ("NOT_FOUND", 0)),
        Stats => word == "OK" && n == 1 && is_dict(),
        StatsJob(_) | StatsTube(_) => {
            (word == "OK" && n == 1 && is_dict()) || (word == "NOT_FOUND" && n == 0)
        }
        ListTubes => word == "OK" && n == 1 && is_list(),
        PauseTube { .. } => matches!((word.as_str(), n), ("PAUSED", 0) | ("NOT_FOUND", 0)),
        _ => false,
    }
}

/// Is this reply a refusal (the model saying no) rather than an answer?
pub fn is_refusal(reply: &[u8]) -> bool {
    let (word, _) = reply_head(reply);
    matches!(
        word.as_str(),
        "NOT_FOUND"
            | "TIMED_OUT"
            | "DEADLINE_SOON"
            | "DRAINING"
            | "OUT_OF_MEMORY"
            | "INTERNAL_ERROR"
    )
}