mirror of
https://github.com/OpenVPN/openvpn3.git
synced 2024-09-20 20:13:05 +02:00
075143f23f
Signed-off-by: James Yonan <james@openvpn.net>
178 lines
3.1 KiB
C++
178 lines
3.1 KiB
C++
//
|
|
// chunked.hpp
|
|
// OpenVPN
|
|
//
|
|
// Copyright (C) 2012-2017 OpenVPN Technologies, Inc.
|
|
// All rights reserved.
|
|
//
|
|
|
|
#ifndef OPENVPN_WS_CHUNKED_H
|
|
#define OPENVPN_WS_CHUNKED_H
|
|
|
|
#include <openvpn/common/size.hpp>
|
|
#include <openvpn/common/hexstr.hpp>
|
|
#include <openvpn/buffer/buffer.hpp>
|
|
#include <openvpn/frame/frame.hpp>
|
|
|
|
namespace openvpn {
|
|
namespace WS {
|
|
class ChunkedHelper
|
|
{
|
|
enum State {
|
|
hex,
|
|
post_hex,
|
|
post_hex_lf,
|
|
post_chunk_cr,
|
|
post_chunk_lf,
|
|
post_content_cr,
|
|
post_content_lf,
|
|
done,
|
|
chunk,
|
|
};
|
|
|
|
public:
|
|
ChunkedHelper()
|
|
: state(hex),
|
|
size(0)
|
|
{
|
|
}
|
|
|
|
template <typename PARENT>
|
|
bool receive(PARENT& callback, BufferAllocated& buf)
|
|
{
|
|
while (buf.defined())
|
|
{
|
|
if (state == chunk)
|
|
{
|
|
if (size)
|
|
{
|
|
if (buf.size() <= size)
|
|
{
|
|
size -= buf.size();
|
|
callback.chunked_content_in(buf);
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
BufferAllocated content(buf.read_alloc(size), size, 0);
|
|
size = 0;
|
|
callback.chunked_content_in(content);
|
|
}
|
|
}
|
|
else
|
|
state = post_chunk_cr;
|
|
}
|
|
else if (state == done)
|
|
break;
|
|
else
|
|
{
|
|
const char c = char(buf.pop_front());
|
|
reprocess:
|
|
switch (state)
|
|
{
|
|
case hex:
|
|
{
|
|
const int v = parse_hex_char(c);
|
|
if (v >= 0)
|
|
size = (size << 4) + v;
|
|
else
|
|
{
|
|
state = post_hex;
|
|
goto reprocess;
|
|
}
|
|
}
|
|
break;
|
|
case post_hex:
|
|
if (c == '\r')
|
|
state = post_hex_lf;
|
|
break;
|
|
case post_hex_lf:
|
|
if (c == '\n')
|
|
{
|
|
if (size)
|
|
state = chunk;
|
|
else
|
|
state = post_content_cr;
|
|
}
|
|
else
|
|
{
|
|
state = post_hex;
|
|
goto reprocess;
|
|
}
|
|
break;
|
|
case post_chunk_cr:
|
|
if (c == '\r')
|
|
state = post_chunk_lf;
|
|
break;
|
|
case post_chunk_lf:
|
|
if (c == '\n')
|
|
state = hex;
|
|
else
|
|
{
|
|
state = post_chunk_cr;
|
|
goto reprocess;
|
|
}
|
|
break;
|
|
case post_content_cr:
|
|
if (c == '\r')
|
|
state = post_content_lf;
|
|
break;
|
|
case post_content_lf:
|
|
if (c == '\n')
|
|
state = done;
|
|
else
|
|
{
|
|
state = post_content_cr;
|
|
goto reprocess;
|
|
}
|
|
break;
|
|
default: // should never be reached
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return state == done;
|
|
}
|
|
|
|
static BufferPtr transmit(BufferPtr buf)
|
|
{
|
|
const size_t headroom = 24;
|
|
const size_t tailroom = 16;
|
|
static const char crlf[] = "\r\n";
|
|
|
|
if (!buf || buf->offset() < headroom || buf->remaining() < tailroom)
|
|
{
|
|
// insufficient headroom/tailroom, must realloc
|
|
Frame::Context fc(headroom, 0, tailroom, 0, sizeof(size_t), 0);
|
|
buf = fc.copy(buf);
|
|
}
|
|
|
|
size_t size = buf->size();
|
|
buf->prepend((unsigned char *)crlf, 2);
|
|
if (size)
|
|
{
|
|
while (size)
|
|
{
|
|
char *pc = (char *)buf->prepend_alloc(1);
|
|
*pc = render_hex_char(size & 0xF);
|
|
size >>= 4;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
char *pc = (char *)buf->prepend_alloc(1);
|
|
*pc = '0';
|
|
}
|
|
buf->write((unsigned char *)crlf, 2);
|
|
return buf;
|
|
}
|
|
|
|
private:
|
|
State state;
|
|
size_t size;
|
|
};
|
|
}
|
|
}
|
|
|
|
#endif
|