1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 07:15:08 +00:00
serenity/Userland/Services/TelnetServer/Parser.cpp
Aatos Majava 85da8cbb07 TelnetServer: Ignore null and \n when parsing
This fixes issues with carriage return sequences.

Before, using <CR><NUL> as the return sequence wouldn't work at all,
and when using <CR><LF> there was an extra newline after every newline.

After this patch, the behaviour should be closer to the Telnet RFC.
2022-04-14 16:12:16 +02:00

72 lines
1.7 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <AK/Types.h>
#include "Parser.h"
void Parser::write(StringView data)
{
for (size_t i = 0; i < data.length(); i++) {
u8 ch = data[i];
switch (m_state) {
case State::Free:
switch (ch) {
case IAC:
m_state = State::ReadCommand;
break;
case '\r':
if (on_data)
on_data("\n");
break;
case '\0':
case '\n':
// Ignore.
break;
default:
if (on_data)
on_data(StringView(&ch, 1));
break;
}
break;
case State::ReadCommand:
switch (ch) {
case IAC: {
m_state = State::Free;
if (on_data)
on_data("\xff");
break;
}
case CMD_WILL:
case CMD_WONT:
case CMD_DO:
case CMD_DONT:
m_command = ch;
m_state = State::ReadSubcommand;
break;
default:
m_state = State::Error;
if (on_error)
on_error();
break;
}
break;
case State::ReadSubcommand: {
auto command = m_command;
m_command = 0;
m_state = State::Free;
if (on_command)
on_command({ command, ch });
break;
}
case State::Error:
// ignore everything
break;
}
}
}