Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions include/cripts/Connections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,9 @@ class ConnBase

void virtual _initialize() { _initialized = true; }

cripts::Transaction *_state = nullptr;
struct sockaddr const *_socket = nullptr;
TSVConn _vc = nullptr;
char _str[INET6_ADDRSTRLEN + 1];
cripts::Transaction *_state = nullptr;
struct sockaddr const *_socket = nullptr;
TSVConn _vc = nullptr;
bool _initialized = false;

}; // End class ConnBase
Expand Down
22 changes: 8 additions & 14 deletions include/cripts/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <array>
#include <memory>
#include <variant>
#include "ts/ts.h"
#include "ts/remap.h"
Expand All @@ -28,7 +29,7 @@
#include "cripts/Connections.hpp"

// These are pretty arbitrary for now
constexpr int CONTEXT_DATA_SLOTS = 4;
constexpr int CONTEXT_DATA_SLOTS = 16;

namespace cripts
{
Expand Down Expand Up @@ -132,23 +133,16 @@ class Context
} _cache;

struct _UrlBlock {
cripts::Client::URL &request;
cripts::Pristine::URL pristine;
cripts::Parent::URL parent;
cripts::Client::URL &request;
std::unique_ptr<cripts::Pristine::URL> pristine;
std::unique_ptr<cripts::Parent::URL> parent;

struct {
cripts::Remap::From::URL from;
cripts::Remap::To::URL to;
std::unique_ptr<cripts::Remap::From::URL> from;
std::unique_ptr<cripts::Remap::To::URL> to;
} remap;

_UrlBlock(Context *ctx, cripts::Client::URL &alias) : request(alias)
{
request.set_context(ctx);
pristine.set_context(ctx);
parent.set_context(ctx);
remap.from.set_context(ctx);
remap.to.set_context(ctx);
}
_UrlBlock(Context *ctx, cripts::Client::URL &alias) : request(alias) { request.set_context(ctx); }

} _urls;
}; // End class Context
Expand Down
9 changes: 5 additions & 4 deletions include/cripts/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
#pragma once

#include <memory>
#include <utility>

#include "ts/ts.h"
Expand Down Expand Up @@ -132,10 +133,10 @@ class Error
void Execute(cripts::Context *context);

private:
Reason _reason;
Status _status;
bool _failed = false;
bool _redirect = false;
std::unique_ptr<Reason> _reason;
Status _status;
bool _failed = false;
bool _redirect = false;
};

} // namespace cripts
80 changes: 56 additions & 24 deletions include/cripts/Urls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <algorithm>
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
Expand Down Expand Up @@ -338,15 +339,17 @@ class Url

cripts::string_view GetSV() override;
cripts::string operator+=(cripts::string_view add);
self_type operator=(cripts::string_view path);
self_type &operator=(cripts::string_view path);
String operator[](Segments::size_type ix);

void
Erase(Segments::size_type ix)
{
auto p = operator[](ix);

Comment thread
zwoop marked this conversation as resolved.
_size -= p.size();
if (_state) {
_state->size -= p.size();
}
p.operator=("");
}

Expand All @@ -368,18 +371,31 @@ class Url
void
Flush()
{
if (_modified) {
if (_state && _state->modified) {
operator=(GetSV());
}
}

private:
void _parser();

bool _modified = false;
Segments _segments; // Lazy loading on this
cripts::string _storage; // Used when recombining the segments into a full path
cripts::string::size_type _size = 0; // Mostly a guestimate for managing _storage
struct State {
bool modified = false;
Segments segments; // Ordered list of path segments
cripts::string storage; // Used when recombining the segments into a full path
cripts::string::size_type size = 0; // Mostly a guestimate for managing storage
};

State &
_ensure_state()
{
if (!_state) {
_state = std::make_unique<State>();
}
return *_state;
}

std::unique_ptr<State> _state; // Lazily allocated when path is parsed or modified

}; // End class Url::Path

Expand Down Expand Up @@ -461,18 +477,18 @@ class Url

using Component::Component;

Query(cripts::string_view load)
Query(cripts::string_view load) : _state(std::make_unique<State>())
{
_data = load;
_size = load.size();
_loaded = true;
_standalone = true;
_data = load;
_state->size = load.size();
_loaded = true;
_state->standalone = true;
}

void Reset() override;

cripts::string_view GetSV() override;
self_type operator=(cripts::string_view query);
self_type &operator=(cripts::string_view query);
cripts::string operator+=(cripts::string_view add);
Parameter operator[](cripts::string_view param);
void Erase(cripts::string_view param);
Expand All @@ -482,7 +498,9 @@ class Url
Erase()
{
operator=("");
_size = 0;
if (_state) {
_state->size = 0;
}
}

void
Expand All @@ -503,34 +521,48 @@ class Url
// Make sure the hash and vector are populated
_parser();

std::ranges::sort(_ordered);
_modified = true;
std::ranges::sort(_state->ordered);
_state->modified = true;
}

void
Flush()
{
if (_modified) {
if (_state && _state->modified) {
operator=(GetSV());
}
}

private:
void _parser();

bool _modified = false;
bool _standalone = false; // This component is used outside of a URL owner, not common
OrderedParams _ordered; // Ordered vector of all parameters, can be sorted etc.
HashParams _hashed; // Unordered map to go from "name" to the query parameter
cripts::string _storage; // Used when recombining the query params into a
// full query string
cripts::string::size_type _size = 0; // Mostly a guesttimate
struct State {
bool modified = false;
bool standalone = false; // This component is used outside of a URL owner, not common
OrderedParams ordered; // Ordered vector of all parameters, can be sorted etc.
HashParams hashed; // Unordered map to go from "name" to the query parameter
cripts::string storage; // Used when recombining the query params into a full query string
cripts::string::size_type size = 0; // Mostly a guesttimate
};

State &
_ensure_state()
{
if (!_state) {
_state = std::make_unique<State>();
}
return *_state;
}

std::unique_ptr<State> _state; // Lazily allocated when query is parsed or modified

}; // End class Url::Query

public:
Url() : scheme(this), host(this), port(this), path(this), query(this) {}

virtual ~Url() = default;

// Clear anything "cached" in the Url, this is rather draconian, but it's safe...
virtual void
Reset()
Expand Down
13 changes: 6 additions & 7 deletions src/cripts/Context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ Context::reset()
_server.request.Reset();
}

// Clear the initialized URLs before calling next hook
if (_urls.pristine.Initialized()) {
_urls.pristine.Reset();
}
if (_urls.parent.Initialized()) {
_urls.parent.Reset();
}
// Release lazy URLs entirely — they get recreated on demand for the next txn.
_urls.pristine.reset();
_urls.parent.reset();
_urls.remap.from.reset();
_urls.remap.to.reset();

if (_cache.url.Initialized()) {
_cache.url.Reset();
}
Expand Down
10 changes: 8 additions & 2 deletions src/cripts/Error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ void
Error::Reason::_set(cripts::Context *context, const cripts::string_view msg)
{
context->state.error.Fail();
context->state.error._reason._setter(msg);
if (!context->state.error._reason) {
context->state.error._reason = std::make_unique<Reason>();
}
context->state.error._reason->_setter(msg);
}

// For convenience, an optional Reason message can also be specified with the status
Expand All @@ -52,7 +55,10 @@ Error::Status::_set(cripts::Context *context, TSHttpStatus status, const cripts:
context->state.error._status._setter(status);

if (msg.size() > 0) {
context->state.error._reason._setter(msg);
if (!context->state.error._reason) {
context->state.error._reason = std::make_unique<Reason>();
}
context->state.error._reason->_setter(msg);
}

if (context->state.error.Redirected() || status == TS_HTTP_STATUS_MOVED_PERMANENTLY ||
Expand Down
Loading