Files
uWebSockets/src/ChunkedEncoding.h
T
2022-10-08 04:32:59 +02:00

195 lines
6.0 KiB
C++

/*
* Authored by Alex Hultman, 2018-2022.
* Intellectual property of third-party.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef UWS_CHUNKEDENCODING_H
#define UWS_CHUNKEDENCODING_H
/* Independent chunked encoding parser, used by HttpParser. */
#include <string>
#include <cstring>
#include <algorithm>
#include <string_view>
#include "MoveOnlyFunction.h"
#include <optional>
namespace uWS {
uint32_t STATE_HAS_SIZE = 0x80000000;
uint32_t STATE_IS_CHUNKED = 0x40000000;
uint32_t STATE_SIZE_MASK = 0x3FFFFFFF;
/* Reads hex number until CR or out of data to consume. Updates state. Returns bytes consumed. */
void consumeHexNumber(std::string_view &data, unsigned int &state) {
/* Consume everything higher than 32 */
while (data.length() && data.data()[0] > 32) {
unsigned char digit = data.data()[0];
if (digit >= 'a') {
digit -= ('a' - '9') - 1;
}
state = state * 16u + ((unsigned int) digit - (unsigned int) '0');
data.remove_prefix(1);
}
/* Consume everything not /n */
while (data.length() && data.data()[0] != '\n') {
data.remove_prefix(1);
}
/* Now we stand on \n so consume it and enable size */
if (data.length()) {
state += 2; // include the two last /r/n
state |= STATE_HAS_SIZE;
data.remove_prefix(1);
}
}
unsigned int chunkSize(unsigned int state) {
return state & STATE_SIZE_MASK;
}
void decChunkSize(unsigned int &state, unsigned int by) {
state = (state & ~STATE_SIZE_MASK) | (chunkSize(state) - by);
}
bool hasChunkSize(unsigned int state) {
return state & STATE_HAS_SIZE;
}
/* Returns next chunk (empty or not), or if all data was consumed, nullopt is returned. */
std::optional<std::string_view> getNextChunk(std::string_view &data, unsigned int &state) {
while (data.length()) {
// if in "drop trailer mode", just drop up to what we have as size
if ((state & STATE_IS_CHUNKED) && hasChunkSize(state) && chunkSize(state)) {
while(data.length() && chunkSize(state)) {
data.remove_prefix(1);
decChunkSize(state, 1);
if (chunkSize(state) == 0) {
state = 0;
}
}
continue;
}
if (!hasChunkSize(state)) {
consumeHexNumber(data, state);
if (hasChunkSize(state) && chunkSize(state) == 2) {
// set trailer state and increase size to 4
state = 4 | STATE_IS_CHUNKED | STATE_HAS_SIZE;
return std::string_view(nullptr, 0);
}
continue;
}
// do we have data to emit all?
if (data.length() >= chunkSize(state)) {
// emit all but 2 bytes then reset state to 0 and goto beginning
// not fin
std::string_view emitSoon;
bool shouldEmit = false;
if (chunkSize(state) > 2) {
emitSoon = std::string_view(data.data(), chunkSize(state) - 2);
shouldEmit = true;
}
data.remove_prefix(chunkSize(state));
state = 0;
if (shouldEmit) {
return emitSoon;
}
continue;
} else {
/* We will consume all our input data */
std::string_view emitSoon;
if (chunkSize(state) > 2) {
unsigned int maximalAppEmit = chunkSize(state) - 2;
if (data.length() > maximalAppEmit) {
emitSoon = data.substr(0, maximalAppEmit);
} else {
//cb(data);
emitSoon = data;
}
}
decChunkSize(state, data.length());
// new: decrease data by its size (bug)
data.remove_prefix(data.length()); // ny bug fix för getNextChunk
if (emitSoon.length()) {
return emitSoon;
} else {
return std::nullopt;
}
}
}
return std::nullopt;
}
/* This is really just a wrapper for convenience */
struct ChunkIterator {
std::string_view data;
std::optional<std::string_view> chunk;
unsigned int *state;
ChunkIterator(std::string_view data, unsigned int *state) : data(data), state(state) {
chunk = uWS::getNextChunk(this->data, *state);
if (!chunk && this->data.length()) {
std::abort();
}
}
ChunkIterator() {
}
ChunkIterator begin() {
return *this;
}
ChunkIterator end() {
return ChunkIterator();
}
std::string_view operator*() {
if (!chunk.has_value()) {
std::abort();
}
return chunk.value();
}
bool operator!=(const ChunkIterator &other) const {
return other.chunk.has_value() != chunk.has_value();
}
ChunkIterator &operator++() {
chunk = uWS::getNextChunk(data, *state);
if (!chunk && this->data.length()) {
std::abort();
}
return *this;
}
};
}
#endif // UWS_CHUNKEDENCODING_H